Skip to content

Commit

Permalink
Add class coverage scripting
Browse files Browse the repository at this point in the history
  • Loading branch information
Struan Judd committed Jan 4, 2024
1 parent 72f6c9e commit 10a9470
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 2 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -487,4 +487,6 @@ _site/
_exported_templates/

test/**/*.received.*
test/**/activities.yml
activities.yml
per-class
runs-*.yml
42 changes: 42 additions & 0 deletions class-combine.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
Install-Module -Name powershell-yaml

$runs = @{}

$files = Get-ChildItem .\per-class -Filter "*.yml"

$count = 0
$total = $files.Count

$files | ForEach-Object {
$run = $_.BaseName

$count += 1
$perc = $count * 100 / $total
Write-Progress "Combine" $run -PercentComplete $perc

$_ | Get-Content | ConvertFrom-Yaml | `
ForEach-Object {
$_ | ForEach-Object {
if ($_.filename -match "\\test\\") {
return
}
$class = $_.name
if ($runs.Keys -notcontains $class ) {
$runs[$class] = @{filename=$_.filename;allLines=$_.allLines;runs=@{}}
}
$runs[$class].runs[$run] = $_.lines.ToString()
}
}
}

Write-Progress "Combine" -Completed

$runs | ConvertTo-Yaml -Options DisableAliases | Set-Content "runs-all.yml"

$singles = $runs.Keys | Where-Object { $runs[$_].runs.Count -lt 2 }

foreach ($class in $singles) {
$runs.Remove($class)
}

$runs | ConvertTo-Yaml -Options DisableAliases | Set-Content "runs-many.yml"
61 changes: 61 additions & 0 deletions class-coverage.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
Install-Module -Name powershell-yaml

dotnet tool restore
dotnet build --disable-build-servers

New-Item per-class -ItemType Directory -ErrorAction Ignore | Out-Null

Push-Location .\test\GqlPlus.Verifier.ClassTests

$base = "-s","coverage.runsettings","-f","cobertura","-o"
$dotnet = "--","dotnet","test","--no-build","--verbosity","quiet","--disable-build-servers","--filter"

try {
$classes = dotnet test --list-tests --no-build | `
Select-String " GqlPlus.Verifier" -Raw | `
ForEach-Object {
($_ -split '\.')[-2]
} | `
Select-Object -Unique

$allLinesField = @{L="allLines";E={ ($_.lines.line |
Select-Object -ExpandProperty number
) -join " "
}}

$linesField = @{L="lines";E={ ($_.lines.line |
Where-Object hits -ne 0 |
Select-Object -ExpandProperty number
) -join " "
}}

$count = 0
$total = $classes.Count

foreach ($run in $classes) {
$count += 1
$perc = $count * 100 / $total
Write-Progress "Test run" $run -PercentComplete $perc

$xmlFile = "../../per-class/$run.xml"
$params = $base + @($xmlFile,"-id",$run) + $dotnet + @(".$run.")
dotnet coverage collect @params | Out-Null

$runField = @{L="run";E={$run}}

[xml]$xml = Get-Content $xmlFile
$xmlClasses = $xml.coverage.packages | `
ForEach-Object { $_.package.classes } | `
Select-Object -ExpandProperty class | `
Where-Object line-rate -ne 0 | `
Select-Object $runField,name,filename,line-rate,$linesField,$allLinesField

$ymlFile = "../../per-class/$run.yml"
$xmlClasses | ConvertTo-Yaml -Options DisableAliases | Set-Content $ymlFile
}
}
finally {
Pop-Location
}

Write-Progress "Test run" -Completed
49 changes: 49 additions & 0 deletions class.runsettings
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?xml version="1.0" encoding="utf-8" ?>
<RunSettings>
<DataCollectionRunSettings>
<DataCollectors>
<DataCollector friendlyName="Code Coverage"
uri="datacollector://Microsoft/CodeCoverage/2.0"
assemblyQualifiedName="Microsoft.VisualStudio.Coverage.DynamicCoverageDataCollector, Microsoft.VisualStudio.TraceCollector, Version=11.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<Configuration>
<CodeCoverage>
<ModulePaths>
<Include>
<ModulePath>.*\\GqlPlus\.Verifier\.dll$</ModulePath>
</Include>
</ModulePaths>

<!-- Match attributes on any code element: -->
<Attributes>
<Exclude>
<!-- Don't forget "Attribute" at the end of the name -->
<Attribute>^System\.Diagnostics\.DebuggerHiddenAttribute$</Attribute>
<Attribute>^System\.Diagnostics\.DebuggerNonUserCodeAttribute$</Attribute>
<Attribute>^System\.CodeDom\.Compiler\.GeneratedCodeAttribute$</Attribute>
<Attribute>^System\.Diagnostics\.CodeAnalysis\.ExcludeFromCodeCoverageAttribute$</Attribute>
</Exclude>
</Attributes>

<!-- We recommend you do not change the following values: -->

<!-- Set this to True to collect coverage information for functions marked with the "SecuritySafeCritical" attribute. Instead of writing directly into a memory location from such functions, code coverage inserts a probe that redirects to another function, which in turns writes into memory. -->
<UseVerifiableInstrumentation>True</UseVerifiableInstrumentation>
<!-- When set to True, collects coverage information from child processes that are launched with low-level ACLs, for example, UWP apps. -->
<AllowLowIntegrityProcesses>True</AllowLowIntegrityProcesses>
<!-- When set to True, collects coverage information from child processes that are launched by test or production code. -->
<CollectFromChildProcesses>True</CollectFromChildProcesses>
<!-- When set to True, restarts the IIS process and collects coverage information from it. -->
<CollectAspDotNet>False</CollectAspDotNet>
<!-- When set to True, static native instrumentation will be enabled. -->
<EnableStaticNativeInstrumentation>True</EnableStaticNativeInstrumentation>
<!-- When set to True, dynamic native instrumentation will be enabled. -->
<EnableDynamicNativeInstrumentation>True</EnableDynamicNativeInstrumentation>
<!-- When set to True, instrumented binaries on disk are removed and original files are restored. -->
<EnableStaticNativeInstrumentationRestore>True</EnableStaticNativeInstrumentationRestore>

</CodeCoverage>
</Configuration>
</DataCollector>
</DataCollectors>
</DataCollectionRunSettings>
</RunSettings>
2 changes: 1 addition & 1 deletion coverage.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ if ($Section) {
prettier -w .
dotnet tool restore
dotnet build
dotnet coverage collect --settings coverage.runsettings --output-format cobertura dotnet @params
dotnet coverage collect --settings coverage.runsettings -- dotnet @params
dotnet reportgenerator -reports:output.cobertura.xml -targetdir:.\coverage riskHotspotsAnalysisThresholds:metricThresholdForCyclomaticComplexity=20
dotnet livereloadserver coverage
1 change: 1 addition & 0 deletions coverage.runsettings
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
uri="datacollector://Microsoft/CodeCoverage/2.0"
assemblyQualifiedName="Microsoft.VisualStudio.Coverage.DynamicCoverageDataCollector, Microsoft.VisualStudio.TraceCollector, Version=11.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<Configuration>
<Format>cobertura</Format>
<CodeCoverage>
<ModulePaths>
<Include>
Expand Down

0 comments on commit 10a9470

Please sign in to comment.