首页 文章

Sonar MSBuild:获取有关Linux的报道

提问于
浏览
0

我已经设法在 debian:stretch docker容器上使用SonarQube Scanner for MSBuild.NET 核心项目执行静态代码分析 .

我现在正在尝试制作一份报道报道 .

除非我错了,相关guidelines提示 you cannot just use an existing report ,而是遵循一个动态的过程

  • begin 通过指向报告(待完成)路径进行分析

  • 通过以下工具之一运行实际的覆盖范围:

a)Visual Studio代码覆盖率

b)dotCover

c)OpenCover

  • 导入报告

  • endmsbuild 发起的分析

我的问题是,是否可以在Linux上运行上述过程(还没有设法这样做或找到任何资源)

2 回答

  • 1

    我已经设法从封面上获得了报道(https://github.com/tonerdo/coverlet),但到目前为止还无法让msbuild的声纳扫描仪完成 . 一些配置问题,希望我很快就会解决 .

    我使用的脚本是:

    #!/bin/bash -e
    
    api_key=$API_KEY
    
    # Get nuget packages and then build DEBUG
    dotnet restore --packages packages --configfile nuget.config /nowarn:NU1701,NU1603,NU1605 MyApp.sln
    
    # set the version on all the projects
    echo Set SDK Versions to [$apiversion]
    find sdk -name "*.csproj" -print | xargs -I £ sed -ie "s/<Version>.*<\/Version>/<Version>${apiversion}<\/Version>/g" £
    
    echo Set Implementation Versions to [$version]
    find implementation -name "*.csproj" -print | xargs -I £ sed -ie "s/<Version>.*<\/Version>/<Version>${version}<\/Version>/g" £
    
    echo Starting SONAR...
    export token=$SONAR_TOKEN
    
    dotnet /bin/SonarScanner.MSBuild.dll begin \
        /d:sonar.login=${token}  \
        /d:sonar.host.url=https://my-sonar-server.com \
        /v:$version \
        /k:$key \
        /d:sonar.analysis.mode=preview \
        /d:sonar.cs.opencover.reportsPaths="$(find . -name coverage.xml | tr '\n' ',')" \
        /d:sonar.coverage.exclusions=tests/** \
        /d:sonar.cs.vstest.reportsPaths="$(pwd)/.output/*.trx" \
        /d:sonar.verbose=true
    
    echo Build solution...
    dotnet build --no-restore /nowarn:NU1701,NU1603,NU1605 Core.sln /clp:PerformanceSummary
    
    # Run the tests
    for testproj in $(ls tests/*.Tests -d); do 
        echo $testproj; 
        set +e
        time \
            dotnet test \
                --no-build \
                --no-restore \
                --filter "TestCategory!=Integration" \
                --logger:trx \
                -r .output/ \
                $testproj \
                /nowarn:NU1701,NU1603,NU1605 \
                /p:CollectCoverage=true \
                /p:CoverletOutputFormat=opencover
        set -e
    done
    
    dotnet /bin/SonarScanner.MSBuild.dll end /d:sonar.login=$token
    

    您需要在每个测试组件中引用coverlet .

  • 1

    覆盖率报告也可以 SonarQube integration ,而不是minicover .

    Minicover现在使用mini-OpenCover生成(并上传到SQ服务器)SQ兼容的覆盖报告;应该遵循的程序或多或少,脚本化:

    (假设 tools 是执行minicover的块安装的文件夹)

    echo "Starting sonarqube integration"
    mono /home/pathTo/SonarQube.Scanner.MSBuild.exe begin /k:"MyProject" /d:sonar.host.url="http://localhost:9000" /d:sonar.login="myLoginId" /d:sonar.cs.opencover.reportsPaths="coverage.xml"
    
    
    dotnet restore
    dotnet build
    cd tools
    
    export "MiniCover=dotnet minicover"
    
    # Instrument assemblies inside 'MyTestFolder' folder to detect hits for source files inside 'MySrcFolder' folder
    $MiniCover instrument --workdir ../ --assemblies MyTestFolder/**/bin/**/*.dll --sources MySrcFolder/**/*.cs 
    
    # Reset hits count in case minicover was run for this project  
    $MiniCover reset
    
    cd ..    
    
    dotnet test --no-build ./MyTestFolder/MyTest.csproj
    
    cd tools    
    # Uninstrument assemblies, it's important if you're going to publish or deploy build outputs
    $MiniCover uninstrument --workdir ../
    
    # Create html reports inside folder coverage-html
    $MiniCover opencoverreport --workdir ../ --threshold 90
    
    # Print opencover report
    $MiniCover opencoverreport --workdir ../ --threshold 90
    
    cd ..
    echo "Ending sonarqube integration..."
    mono /home/pathTo/SonarQube.Scanner.MSBuild.exe end /d:sonar.login="myLoginId"
    

    可以在thisthis个主题上找到更广泛的讨论/详细信息 .

相关问题