Skip to content

Commit

Permalink
Capture full coverage of cadl ranch in ci (#4054)
Browse files Browse the repository at this point in the history
Fixes #4036
  • Loading branch information
m-nash committed Jul 31, 2024
1 parent 1d43455 commit 12a8ddb
Show file tree
Hide file tree
Showing 6 changed files with 207 additions and 96 deletions.
45 changes: 45 additions & 0 deletions packages/http-client-csharp/eng/scripts/CadlRanch-Helper.psm1
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
$repoRoot = Resolve-Path (Join-Path $PSScriptRoot '..')

function IsGenerated {
param (
[string]$dir
)

if (-not ($dir.EndsWith("Generated"))) {
return $false
}

$csFiles = Get-ChildItem -Path $dir -Filter *.cs -File
return $csFiles.Count -gt 0
}

function Capitalize-FirstLetter {
param (
[string]$inputString
)

if ([string]::IsNullOrEmpty($inputString)) {
return $inputString
}

$firstChar = $inputString[0].ToString().ToUpper()
$restOfString = $inputString.Substring(1)

return $firstChar + $restOfString
}

function Get-Namespace {
param (
[string]$dir
)

$words = $dir.Split('-')
$namespace = ""
foreach ($word in $words) {
$namespace += Capitalize-FirstLetter $word
}
return $namespace
}

Export-ModuleMember -Function "IsGenerated"
Export-ModuleMember -Function "Get-Namespace"
112 changes: 61 additions & 51 deletions packages/http-client-csharp/eng/scripts/Generate.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#Requires -Version 7.0
param($filter)

Import-Module "$PSScriptRoot\Generation.psm1" -DisableNameChecking -Force;

Expand All @@ -7,24 +8,26 @@ $solutionDir = Join-Path $packageRoot 'generator'

Refresh-Build

Write-Host "Generating UnbrandedTypeSpec" -ForegroundColor Cyan
$testProjectsLocalDir = Join-Path $packageRoot 'generator' 'TestProjects' 'Local'
if ($null -eq $filter -or $filter -eq "Unbranded-TypeSpec") {
Write-Host "Generating UnbrandedTypeSpec" -ForegroundColor Cyan
$testProjectsLocalDir = Join-Path $packageRoot 'generator' 'TestProjects' 'Local'

$unbrandedTypespecTestProject = Join-Path $testProjectsLocalDir "Unbranded-TypeSpec"
$unbrandedTypespecTestProject = Join-Path $testProjectsLocalDir "Unbranded-TypeSpec"

Invoke (Get-TspCommand "$unbrandedTypespecTestProject/Unbranded-TypeSpec.tsp" $unbrandedTypespecTestProject)
Invoke (Get-TspCommand "$unbrandedTypespecTestProject/Unbranded-TypeSpec.tsp" $unbrandedTypespecTestProject)

# exit if the generation failed
if ($LASTEXITCODE -ne 0) {
exit $LASTEXITCODE
}
# exit if the generation failed
if ($LASTEXITCODE -ne 0) {
exit $LASTEXITCODE
}

Write-Host "Building UnbrandedTypeSpec" -ForegroundColor Cyan
Invoke "dotnet build $packageRoot/generator/TestProjects/Local/Unbranded-TypeSpec/src/UnbrandedTypeSpec.csproj"
Write-Host "Building UnbrandedTypeSpec" -ForegroundColor Cyan
Invoke "dotnet build $packageRoot/generator/TestProjects/Local/Unbranded-TypeSpec/src/UnbrandedTypeSpec.csproj"

# exit if the generation failed
if ($LASTEXITCODE -ne 0) {
exit $LASTEXITCODE
# exit if the generation failed
if ($LASTEXITCODE -ne 0) {
exit $LASTEXITCODE
}
}

$specsDirectory = "$packageRoot/node_modules/@azure-tools/cadl-ranch-specs"
Expand Down Expand Up @@ -100,6 +103,10 @@ foreach ($directory in $directories) {
$subPath = $directory.FullName.Substring($specsDirectory.Length + 1)
$folders = $subPath.Split([System.IO.Path]::DirectorySeparatorChar)

if (-not (Compare-Paths $subPath $filter)) {
continue
}

if ($folders.Contains("azure")) {
continue
}
Expand Down Expand Up @@ -135,45 +142,48 @@ foreach ($directory in $directories) {
# TODO need to build but depends on https://github.com/Azure/autorest.csharp/issues/4463
}

Write-Host "Writing new launch settings" -ForegroundColor Cyan
$mgcExe = "`$(SolutionDir)/../dist/generator/Microsoft.Generator.CSharp.exe"
$sampleExe = "`$(SolutionDir)/../generator/artifacts/bin/SamplePlugin/Debug/net8.0/Microsoft.Generator.CSharp.exe"
$unbrandedSpec = "TestProjects/Local/Unbranded-TypeSpec"

$launchSettings = @{}
$launchSettings.Add("profiles", @{})
$launchSettings["profiles"].Add("Unbranded-TypeSpec", @{})
$launchSettings["profiles"]["Unbranded-TypeSpec"].Add("commandLineArgs", "`$(SolutionDir)/$unbrandedSpec -p ClientModelPlugin")
$launchSettings["profiles"]["Unbranded-TypeSpec"].Add("commandName", "Executable")
$launchSettings["profiles"]["Unbranded-TypeSpec"].Add("executablePath", $mgcExe)
$launchSettings["profiles"].Add("Debug-Plugin-Test-TypeSpec", @{})
$launchSettings["profiles"]["Debug-Plugin-Test-TypeSpec"].Add("commandLineArgs", "`$(SolutionDir)/$unbrandedSpec -p SampleCodeModelPlugin")
$launchSettings["profiles"]["Debug-Plugin-Test-TypeSpec"].Add("commandName", "Executable")
$launchSettings["profiles"]["Debug-Plugin-Test-TypeSpec"].Add("executablePath", $sampleExe)

foreach ($kvp in $cadlRanchLaunchProjects.GetEnumerator()) {
$launchSettings["profiles"].Add($kvp.Key, @{})
$launchSettings["profiles"][$kvp.Key].Add("commandLineArgs", "`$(SolutionDir)/$($kvp.Value) -p StubLibraryPlugin")
$launchSettings["profiles"][$kvp.Key].Add("commandName", "Executable")
$launchSettings["profiles"][$kvp.Key].Add("executablePath", $mgcExe)
}
# only write new launch settings if no filter was passed in
if ($null -eq $filter) {
Write-Host "Writing new launch settings" -ForegroundColor Cyan
$mgcExe = "`$(SolutionDir)/../dist/generator/Microsoft.Generator.CSharp.exe"
$sampleExe = "`$(SolutionDir)/../generator/artifacts/bin/SamplePlugin/Debug/net8.0/Microsoft.Generator.CSharp.exe"
$unbrandedSpec = "TestProjects/Local/Unbranded-TypeSpec"

$launchSettings = @{}
$launchSettings.Add("profiles", @{})
$launchSettings["profiles"].Add("Unbranded-TypeSpec", @{})
$launchSettings["profiles"]["Unbranded-TypeSpec"].Add("commandLineArgs", "`$(SolutionDir)/$unbrandedSpec -p ClientModelPlugin")
$launchSettings["profiles"]["Unbranded-TypeSpec"].Add("commandName", "Executable")
$launchSettings["profiles"]["Unbranded-TypeSpec"].Add("executablePath", $mgcExe)
$launchSettings["profiles"].Add("Debug-Plugin-Test-TypeSpec", @{})
$launchSettings["profiles"]["Debug-Plugin-Test-TypeSpec"].Add("commandLineArgs", "`$(SolutionDir)/$unbrandedSpec -p SampleCodeModelPlugin")
$launchSettings["profiles"]["Debug-Plugin-Test-TypeSpec"].Add("commandName", "Executable")
$launchSettings["profiles"]["Debug-Plugin-Test-TypeSpec"].Add("executablePath", $sampleExe)

foreach ($kvp in $cadlRanchLaunchProjects.GetEnumerator()) {
$launchSettings["profiles"].Add($kvp.Key, @{})
$launchSettings["profiles"][$kvp.Key].Add("commandLineArgs", "`$(SolutionDir)/$($kvp.Value) -p StubLibraryPlugin")
$launchSettings["profiles"][$kvp.Key].Add("commandName", "Executable")
$launchSettings["profiles"][$kvp.Key].Add("executablePath", $mgcExe)
}

$sortedLaunchSettings = @{}
$sortedLaunchSettings.Add("profiles", [ordered]@{})
$launchSettings["profiles"].Keys | Sort-Object | ForEach-Object {
$profileKey = $_
$originalProfile = $launchSettings["profiles"][$profileKey]

$sortedLaunchSettings = @{}
$sortedLaunchSettings.Add("profiles", [ordered]@{})
$launchSettings["profiles"].Keys | Sort-Object | ForEach-Object {
$profileKey = $_
$originalProfile = $launchSettings["profiles"][$profileKey]

# Sort the keys inside each profile
# This is needed due to non deterministic ordering of json elements in powershell
$sortedProfile = [ordered]@{}
$originalProfile.GetEnumerator() | Sort-Object Key | ForEach-Object {
$sortedProfile[$_.Key] = $_.Value
# Sort the keys inside each profile
# This is needed due to non deterministic ordering of json elements in powershell
$sortedProfile = [ordered]@{}
$originalProfile.GetEnumerator() | Sort-Object Key | ForEach-Object {
$sortedProfile[$_.Key] = $_.Value
}

$sortedLaunchSettings["profiles"][$profileKey] = $sortedProfile
}

$sortedLaunchSettings["profiles"][$profileKey] = $sortedProfile
# Write the launch settings to the launchSettings.json file
$launchSettingsPath = Join-Path $solutionDir "Microsoft.Generator.CSharp" "src" "Properties" "launchSettings.json"
$sortedLaunchSettings | ConvertTo-Json | Set-Content $launchSettingsPath
}

# Write the launch settings to the launchSettings.json file
$launchSettingsPath = Join-Path $solutionDir "Microsoft.Generator.CSharp" "src" "Properties" "launchSettings.json"
$sortedLaunchSettings | ConvertTo-Json | Set-Content $launchSettingsPath
15 changes: 15 additions & 0 deletions packages/http-client-csharp/eng/scripts/Generation.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,21 @@ function Refresh-Build {
}
}

function Compare-Paths {
param (
[string]$path1,
[string]$path2
)

# Normalize the directory separators
$normalizedPath1 = $path1 -replace '/', '\'
$normalizedPath2 = $path2 -replace '/', '\'

# Compare the normalized paths
return $normalizedPath1.Contains($normalizedPath2)
}

Export-ModuleMember -Function "Invoke"
Export-ModuleMember -Function "Get-TspCommand"
Export-ModuleMember -Function "Refresh-Build"
Export-ModuleMember -Function "Compare-Paths"
76 changes: 76 additions & 0 deletions packages/http-client-csharp/eng/scripts/Get-CadlRanch-Coverage.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# cspell:ignore cadlranch

#Requires -Version 7.0

Import-Module "$PSScriptRoot\Generation.psm1" -DisableNameChecking -Force;
Import-Module "$PSScriptRoot\CadlRanch-Helper.psm1" -DisableNameChecking -Force;

$packageRoot = Resolve-Path (Join-Path $PSScriptRoot '..' '..')

Refresh-Build

$specsDirectory = Join-Path $packageRoot 'node_modules' '@azure-tools' 'cadl-ranch-specs'
$cadlRanchRoot = Join-Path $packageRoot 'generator' 'TestProjects' 'CadlRanch'
$directories = Get-ChildItem -Path "$cadlRanchRoot" -Directory -Recurse
$cadlRanchCsproj = Join-Path $packageRoot 'generator' 'TestProjects' 'CadlRanch.Tests' 'TestProjects.CadlRanch.Tests.csproj'
$runSettings = Join-Path $packageRoot 'eng' 'test-configurations' 'cadlranch.runsettings'

$coverageDir = Join-Path $packageRoot 'generator' 'artifacts' 'coverage'

if (-not (Test-Path $coverageDir)) {
New-Item -ItemType Directory -Path $coverageDir | Out-Null
}

# generate all
foreach ($directory in $directories) {
if (-not (IsGenerated $directory.FullName)) {
continue
}

$outputDir = $directory.FullName.Substring(0, $directory.FullName.IndexOf("src") - 1)
$subPath = $outputDir.Substring($cadlRanchRoot.Length + 1)

Write-Host "Regenerating $subPath" -ForegroundColor Cyan

$specFile = Join-Path $specsDirectory $subPath "main.tsp"

$command = Get-TspCommand $specFile $outputDir
Invoke $command
# exit if the generation failed
if ($LASTEXITCODE -ne 0) {
exit $LASTEXITCODE
}
}

# test all
Write-Host "Testing $subPath" -ForegroundColor Cyan
$command = "dotnet test $cadlRanchCsproj --settings $runSettings"
Invoke $command
# exit if the testing failed
if ($LASTEXITCODE -ne 0) {
exit $LASTEXITCODE
}

# restore all
foreach ($directory in $directories) {
if (-not (IsGenerated $directory.FullName)) {
continue
}

$outputDir = $directory.FullName.Substring(0, $directory.FullName.IndexOf("src") - 1)
$subPath = $outputDir.Substring($cadlRanchRoot.Length + 1)

Write-Host "Restoring $subPath" -ForegroundColor Cyan
$command = "git clean -xfd $outputDir"
Invoke $command
# exit if the restore failed
if ($LASTEXITCODE -ne 0) {
exit $LASTEXITCODE
}
$command = "git restore $outputDir"
Invoke $command
# exit if the restore failed
if ($LASTEXITCODE -ne 0) {
exit $LASTEXITCODE
}
}
53 changes: 9 additions & 44 deletions packages/http-client-csharp/eng/scripts/Test-CadlRanch.ps1
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# cspell:ignore cadlranch

#Requires -Version 7.0
param($filter)

Import-Module "$PSScriptRoot\Generation.psm1" -DisableNameChecking -Force;
Import-Module "$PSScriptRoot\CadlRanch-Helper.psm1" -DisableNameChecking -Force;

$packageRoot = Resolve-Path (Join-Path $PSScriptRoot '..' '..')

Expand All @@ -20,47 +22,6 @@ if (-not (Test-Path $coverageDir)) {
New-Item -ItemType Directory -Path $coverageDir | Out-Null
}

function IsGenerated {
param (
[string]$dir
)

if (-not ($dir.EndsWith("Generated"))) {
return $false
}

$csFiles = Get-ChildItem -Path $directory -Filter *.cs -File
return $csFiles.Count -gt 0
}

function Capitalize-FirstLetter {
param (
[string]$inputString
)

if ([string]::IsNullOrEmpty($inputString)) {
return $inputString
}

$firstChar = $inputString[0].ToString().ToUpper()
$restOfString = $inputString.Substring(1)

return $firstChar + $restOfString
}

function Get-Namespace {
param (
[string]$dir
)

$words = $dir.Split('-')
$namespace = ""
foreach ($word in $words) {
$namespace += Capitalize-FirstLetter $word
}
return $namespace
}

foreach ($directory in $directories) {
if (-not (IsGenerated $directory.FullName)) {
continue
Expand All @@ -70,6 +31,10 @@ foreach ($directory in $directories) {
$subPath = $outputDir.Substring($cadlRanchRoot.Length + 1)
$folders = $subPath.Split([System.IO.Path]::DirectorySeparatorChar)

if (-not (Compare-Paths $subPath $filter)) {
continue
}

$testFilter = "TestProjects.CadlRanch.Tests"
foreach ($folder in $folders) {
$testFilter += ".$(Get-Namespace $folder)"
Expand All @@ -89,21 +54,21 @@ foreach ($directory in $directories) {
Write-Host "Testing $subPath" -ForegroundColor Cyan
$command = "dotnet test $cadlRanchCsproj --filter `"FullyQualifiedName~$testFilter`" --settings $runSettings"
Invoke $command
# exit if the generation failed
# exit if the testing failed
if ($LASTEXITCODE -ne 0) {
exit $LASTEXITCODE
}

Write-Host "Restoring $subPath" -ForegroundColor Cyan
$command = "git clean -xfd $outputDir"
Invoke $command
# exit if the generation failed
# exit if the restore failed
if ($LASTEXITCODE -ne 0) {
exit $LASTEXITCODE
}
$command = "git restore $outputDir"
Invoke $command
# exit if the generation failed
# exit if the restore failed
if ($LASTEXITCODE -ne 0) {
exit $LASTEXITCODE
}
Expand Down
2 changes: 1 addition & 1 deletion packages/http-client-csharp/eng/scripts/Test-Packages.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ try {
# test the generator
Invoke-LoggedCommand "dotnet test ./generator" -GroupOutput

Invoke-LoggedCommand "./eng/scripts/Test-CadlRanch.ps1" -GroupOutput
Invoke-LoggedCommand "./eng/scripts/Get-CadlRanch-Coverage.ps1" -GroupOutput
}
finally {
Pop-Location
Expand Down

0 comments on commit 12a8ddb

Please sign in to comment.