Skip to content

Commit

Permalink
Fix code coverage with new collector (#845)
Browse files Browse the repository at this point in the history
  • Loading branch information
atifaziz authored Oct 16, 2022
1 parent e700980 commit 9ecd1dc
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 20 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
*.opencover.xml
**/TestResults/

### VisualStudio ###
## Ignore Visual Studio temporary files, build results, and
Expand Down
5 changes: 3 additions & 2 deletions MoreLinq.Test/MoreLinq.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="coverlet.msbuild" Version="3.0.0">
<PackageReference Include="coverlet.collector" Version="1.3.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand All @@ -47,9 +47,10 @@
</ItemGroup>

<ItemGroup>
<None Remove="TestResult.xml" />
<None Remove="TestResults\**" />
<Compile Include="*Test.cs" />
<Compile Include="Test*.cs" />
<None Remove="TestResult.xml" />
<Compile Include="..\MoreLinq\Disposable.cs" Link="Disposable.cs" />
<Compile Include="..\MoreLinq\Reactive\Subject.cs" Link="Subject.cs" />
<Compile Include="AssertThrowsArgument.cs" />
Expand Down
12 changes: 12 additions & 0 deletions MoreLinq.Test/coverlet.runsettings
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<RunSettings>
<DataCollectionRunSettings>
<DataCollectors>
<DataCollector friendlyName="XPlat Code Coverage">
<Configuration>
<Format>opencover</Format>
<Exclude>[NUnit*]*,[MoreLinq]MoreLinq.Extensions.*,[MoreLinq]MoreLinq.Experimental.*</Exclude>
</Configuration>
</DataCollector>
</DataCollectors>
</DataCollectionRunSettings>
</RunSettings>
6 changes: 3 additions & 3 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,13 @@ build_script:
test_script:
- cmd: test.cmd
- sh: ./test.sh
- ps: dotnet reportgenerator -reports:MoreLinq.Test/coverage.net6.0.opencover.xml -targetdir:tmp/cover -tag:(git show -q --pretty=%H)
- ps: dotnet reportgenerator -reports:MoreLinq.Test/TestResults/coverage-*.opencover.xml -targetdir:tmp/cover -tag:(git show -q --pretty=%H)
- ps: |
cd tmp/cover
tar -cz -f "../../coverage-report-${IMAGE_NAME}.tar.gz" *
tar -cz -f "../../coverage-report-${env:IMAGE_NAME}.tar.gz" *
- sh: curl -sSL https://codecov.io/bash > codecov
- sh: chmod +x codecov
- sh: if [ "$CI_LINUX" = "true" ]; then ./codecov -f ./MoreLinq.Test/coverage.net6.0.opencover.xml; fi
- sh: if [ "$CI_LINUX" = "true" ]; then ./codecov; fi
artifacts:
- path: dist\*.nupkg
- path: coverage-report-*
38 changes: 31 additions & 7 deletions test.cmd
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,47 @@ goto :EOF
:main
setlocal
call build ^
&& call :clean ^
&& call :test net6.0 Debug ^
&& call :test net6.0 Release ^
&& call :test netcoreapp3.1 Debug ^
&& call :test netcoreapp3.1 Release ^
&& call :test net451 Debug ^
&& call :test net451 Release
&& call :test net451 Release ^
&& call :report-cover
goto :EOF

:clean
setlocal
cd MoreLinq.Test
if exist TestResults rd /s /q TestResults || exit /b 1
if exist TestResult.xml del TestResult.xml || exit /b 1
goto :EOF

:test
setlocal
cd MoreLinq.Test
echo Testing %1 (%2)...
if %2==Debug set COVERAGE_ARGS=-p:CollectCoverage=true ^
-p:CoverletOutputFormat=opencover ^
-p:Exclude=\"[NUnit*]*,[MoreLinq]MoreLinq.Extensions.*,[MoreLinq]MoreLinq.Experimental.*\"
if %1==net451 (
MoreLinq.Test\bin\%2\net451\MoreLinq.Test.exe
) else (
dotnet test --no-build MoreLinq.Test -f %1 -c %2 %COVERAGE_ARGS%
bin\%2\net451\MoreLinq.Test.exe
exit /b %ERRORLEVEL%
)
dotnet test --no-build -f %1 -c %2 --settings coverlet.runsettings || exit /b 1
cd TestResults
set TEST_RESULTS_DIR=
for /f %%d in ('dir /b /od /ad') do if not defined TEST_RESULTS_DIR set TEST_RESULTS_DIR=%%~d
if not defined TEST_RESULTS_DIR (
echo>&2 Test coverage XML not found!
exit /b 1
)
copy "%TEST_RESULTS_DIR%\coverage.opencover.xml" coverage-%1-%2.opencover.xml > nul
goto :EOF

:report-cover
setlocal
cd MoreLinq.Test\TestResults
dotnet reportgenerator -reports:coverage-*.opencover.xml ^
-reporttypes:Html;TextSummary ^
-targetdir:reports ^
&& type reports\Summary.txt
goto :EOF
18 changes: 10 additions & 8 deletions test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,25 @@
set -e
cd "$(dirname "$0")"
./build.sh $c
if [[ -d "MoreLinq.Test/TestResults" ]]; then
rm -rf MoreLinq.Test/TestResults
fi
if [[ -z "$1" ]]; then
configs="Debug Release"
else
configs="$1"
fi
for f in netcoreapp3.1 net6.0; do
for c in $configs; do
if [[ "$c" == "Debug" ]]; then
coverage_args="-p:CollectCoverage=true
-p:CoverletOutputFormat=opencover
-p:Exclude=\"[NUnit*]*,[MoreLinq]MoreLinq.Extensions.*,[MoreLinq]MoreLinq.Experimental.*\""
else
unset coverage_args
fi
dotnet test --no-build -c $c -f $f MoreLinq.Test $coverage_args
dotnet test --no-build -c $c -f $f --settings MoreLinq.Test/coverlet.runsettings MoreLinq.Test
TEST_RESULTS_DIR="$(ls -dc MoreLinq.Test/TestResults/* | head -1)"
cp "$TEST_RESULTS_DIR/coverage.opencover.xml" "MoreLinq.Test/TestResults/coverage-$f-$c.opencover.xml"
done
done
dotnet reportgenerator -reports:MoreLinq.Test/TestResults/coverage-*.opencover.xml \
-reporttypes:Html\;TextSummary \
-targetdir:MoreLinq.Test/TestResults/reports
cat MoreLinq.Test/TestResults/reports/Summary.txt
if [[ -z `which mono 2>/dev/null` ]]; then
echo>&2 NOTE! Mono does not appear to be installed so unit tests
echo>&2 against the Mono runtime will be skipped.
Expand Down

0 comments on commit 9ecd1dc

Please sign in to comment.