How to make azure devops build fail when R linting issues occur

676 Views Asked by At

I am using lintr library in R to find linting issues in the code. I put them into an xml format like this:

<lintsuites>
  <lintissue filename="/home/.../blah.R" line_number="36" column_number="1" type="style" message="Trailing blank lines are superfluous."/>
  <lintissue filename="/home/.../blahblah.R" line_number="1" column_number="8" type="style" message="Only use double-quotes."/>
</lintsuites>

Now, I would like to fail the Azure devops build when issues like this occur.

I was able to get my tests in a JUnit format like this:

  <testsuite name="MB Unit Tests" timestamp="2020-01-22 22:34:07" hostname="0000" tests="29" skipped="0" failures="0" errors="0" time="0.05">
    <testcase time="0.01" classname="1_Unit_Tests" name="1_calculates_correctly"/>
    <testcase time="0.01" classname="1_Unit_Tests" name="2_absorbed_correctly"/>
...
</testsuite>

And when i do this step in the azure pipeline, my build fails if any tests in the test suite fail:

  - task: PublishTestResults@2
    displayName: 'Publish Test Results'
    inputs:
      testResultsFiles: '**/*.xml'
      searchFolder: '$(System.DefaultWorkingDirectory)/fe'
      mergeTestResults: true
      failTaskOnFailedTests: true

I would like something similar for failing the build when there are linting issues. I would also like the users to see what those linting issues are in the build output. Thanks

1

There are 1 best solutions below

5
Levi Lu-MSFT On

This is not possible to achieve a similar result for lintr xml with plishTestResults@2.

The workaround you can try is to use a powershell task to check for the content of your lintr xml file. If the content isnot empty, then fail the pipeline in the powershell task.

Below powershell task will check the content of lintr.xml(<car></car>) and will echo the content to the task logs and exit 1 to fail the task if the content is null.

 - powershell: |
        [xml]$XmlDocument = Get-Content -Path "$(system.defaultworkingdirectory)/lintr.xml"

        if($XmlDocument.OuterXml){
           echo $XmlDocument.OuterXml
          }else{exit 1}

   displayName: lintr result.

enter image description here

You can aslo use below statement in a powershell task to upload lintr xml file to the build summary page where you can download

echo "##vso[task.uploadsummary]$(system.defaultworkingdirectory)/lintr.xml"

You can check here for more logging commands.

Update:

A workaround to show the lintr results in a nice way is to create a custom extension to display html results in azure devops pipeline.

You can try creating a custom extension, and produce a html lint results. Please refer to the answer to this thread an example custom extension to display html

Other developers already submit requests to Microsoft for implementing this feature. Please vote it up here or create a new one.