Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions .github/workflows/dotnet-private.yml
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,73 @@ jobs:
"should_release=false" >> $env:GITHUB_OUTPUT
}

# A pull request in a private repository packs as well, for the same reason as in dotnet.yml:
# packing runs package validation (ApiCompat), and `ktsubuild ci` only packs inside a release,
# which a pull request never reaches, so a change that breaks `dotnet pack` would otherwise go
# green, be auto-merged by Dependabot, and leave the repository unreleasable
# (ktsu-dev/.github#62, following #41). Nothing is pushed and nothing is tagged.
#
# A held repository (KTSU_RELEASE_HOLD) packs too. Validating a package publishes nothing, and
# `ktsubuild build` builds in Release as `ci` does, so the --no-build pack finds its output.
#
# The script is a copy of the Validate Package step in dotnet.yml, which explains the project
# selection. scripts/tests/dotnet-pr-pack.tests.ps1 runs both copies and fails if they differ,
# so a change to one has to be made to the other.
- name: Validate Package
id: validate_pack
if: github.event_name == 'pull_request' && steps.pipeline.outputs.build_skipped != 'true'
shell: pwsh
run: |
$workspace = $env:GITHUB_WORKSPACE.TrimEnd('/', '\')
$output = Join-Path $env:RUNNER_TEMP 'pr-pack'
$supportingDirectories = @('test', 'tests', 'example', 'examples', 'sample', 'samples', 'benchmark', 'benchmarks', 'demo', 'demos')

$solutionArgs = @("-p:SolutionDir=$workspace/")
$solution = @(Get-ChildItem -LiteralPath $workspace -File -Filter '*.slnx') + @(Get-ChildItem -LiteralPath $workspace -File -Filter '*.sln') | Select-Object -First 1
if ($solution) { $solutionArgs += "-p:SolutionName=$($solution.BaseName)" }

$projects = @()
foreach ($project in Get-ChildItem -LiteralPath $workspace -Recurse -File -Filter '*.csproj') {
$relative = [System.IO.Path]::GetRelativePath($workspace, $project.FullName)
$segments = $relative -split '[\\/]'
$content = Get-Content -LiteralPath $project.FullName -Raw

$isTest = $content -match '(?i)Sdk(?:\s+Name)?\s*=\s*"(?:[^"]*;)?\s*MSTest\.Sdk(?:/[^";]*)?\s*[;"]' `
-or $content -match '(?i)<PackageReference\b[^>]*\bInclude\s*=\s*"\s*Microsoft\.NET\.Test\.Sdk\s*"' `
-or $content -match '(?i)<IsTestProject>\s*true\s*</IsTestProject>'
$isSupporting = $relative -match '(?i)(Benchmark|Test|Sample|Example|Demo)s?\.csproj$' `
-or @($segments | Select-Object -SkipLast 1 | Where-Object { $supportingDirectories -contains $_ }).Count -gt 0
$frameworks = @()
if ($content -match '(?i)<TargetFrameworks?>\s*([^<]+?)\s*</TargetFrameworks?>') {
$frameworks = @($Matches[1] -split ';' | ForEach-Object { $_.Trim() } | Where-Object { $_ })
}
$iosOnly = $frameworks.Count -gt 0 -and @($frameworks | Where-Object { $_ -notmatch '(?i)-ios' }).Count -eq 0

if ($isTest -or $isSupporting) { continue }
if ($iosOnly) {
Write-Host "Skipping $($project.BaseName): an iOS project is not built on this host, so the release does not pack it either"
continue
}
$projects += $project
}

if ($projects.Count -eq 0) {
Write-Host "No .NET library projects found to package"
exit 0
}

$failures = @()
foreach ($project in $projects) {
Write-Host "Packing $($project.BaseName)..."
& dotnet pack $project.FullName --configuration Release --no-build --output $output @solutionArgs
if ($LASTEXITCODE -ne 0) { $failures += "$($project.BaseName) (exit code $LASTEXITCODE)" }
}

if ($failures.Count -gt 0) {
Write-Host "::error title=Pack failed::Pack failed for $($failures.Count) project(s): $($failures -join ', '). The release would fail the same way once this is merged."
exit 1
}

- name: End SonarQube
if: ${{ env.SONAR_TOKEN != '' && steps.sonar.outputs.available == 'true' }}
env:
Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,9 @@ jobs:
# less test projects, benchmarks, samples, examples and demos (SupportingProjects), and less
# projects this Windows host cannot build, which are iOS-only ones. A project that is not
# packable (IsPackable=false, as ktsu.Sdk sets for applications) is a no-op that exits 0.
#
# dotnet-private.yml runs a copy of this script for private repositories (#62).
# scripts/tests/dotnet-pr-pack.tests.ps1 fails if the two differ, so change both.
- name: Validate Package
id: validate_pack
if: github.event_name == 'pull_request' && steps.pipeline.outputs.build_skipped != 'true'
Expand Down
3 changes: 3 additions & 0 deletions CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,9 @@ This script:
- A repository holds its releases by setting the variable `KTSU_RELEASE_HOLD` to `true`, which runs
`ktsubuild build` and reports `should_release=false`. See `docs/shared-ci.md`
- `scripts/tests/dotnet-private-hold.tests.ps1` runs the pipeline step against a stub `ktsubuild`
- On pull requests, held or not, **Validate Package** packs what a release would, so a change that
breaks package validation fails its PR (ktsu-dev/.github#62). Its script is a copy of `dotnet.yml`'s;
`scripts/tests/dotnet-pr-pack.tests.ps1` runs both and fails if they differ

### Script Organization

Expand Down
11 changes: 10 additions & 1 deletion docs/shared-ci.md
Original file line number Diff line number Diff line change
Expand Up @@ -233,9 +233,18 @@ pushed or tagged. Package validation (ApiCompat) runs during `dotnet pack`. Befo
that check ran only in the post-merge Release step, so a Polyfill bump that broke it was
auto-merged into about 19 repositories ([#41]). Its selection of projects copies KtsuBuild's
`PackAsync`, so a change to the projects KtsuBuild packs has to be made in both places.
`scripts/tests/dotnet-pr-pack.tests.ps1` runs the step against a stub `dotnet`.

`dotnet-private.yml` runs the same step on its pull requests, after **Run KtsuBuild CI Pipeline**.
Without it, `ktsubuild ci` on a private pull request never packed, because KtsuBuild packs only
inside a release, so the gap [#41] describes stayed open for every private repository ([#62]). A
held repository (`KTSU_RELEASE_HOLD=true`) packs on pull requests too: validating a package
publishes nothing, and `ktsubuild build` builds in Release, as `ci` does. The private step's script
is a copy of the public one, because a reusable workflow runs in the caller's checkout and cannot
read a script from this repository. `scripts/tests/dotnet-pr-pack.tests.ps1` runs both copies
against a stub `dotnet` and fails if they differ.

[#41]: https://github.com/ktsu-dev/.github/issues/41
[#62]: https://github.com/ktsu-dev/.github/issues/62

## Interaction with the profile README

Expand Down
122 changes: 78 additions & 44 deletions scripts/tests/dotnet-pr-pack.tests.ps1
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
<#
.SYNOPSIS
Runs the Validate Package step of dotnet.yml against a stub dotnet and checks what it packs.
Runs the Validate Package step of dotnet.yml and dotnet-private.yml against a stub dotnet and
checks what it packs.

.DESCRIPTION
Packing, and with it package validation, used to run only in the Release step, which a pull
request never reaches. A Polyfill bump that broke `dotnet pack` was green on its PR, was
auto-merged, and left about 19 repositories unreleasable. See ktsu-dev/.github#41.
auto-merged, and left about 19 repositories unreleasable. See ktsu-dev/.github#41. The private
pipeline had no such step, so the same could still happen to every private repository
(ktsu-dev/.github#62).

The step's pwsh is taken from the workflow as written and run in a fixture workspace with a
Each pipeline's step is checked the same way, and the two copies must be identical. The step's
pwsh is taken from the workflow as written and run in a fixture workspace with a
`dotnet` on PATH that records each call and fails the pack of whichever project the case
names. The workflow itself is also checked for where the step sits and what gates it.

Expand All @@ -19,7 +23,14 @@ param()
Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'

$script:Workflow = Join-Path $PSScriptRoot '..' '..' '.github' 'workflows' 'dotnet.yml' | Resolve-Path
$script:WorkflowDir = Join-Path $PSScriptRoot '..' '..' '.github' 'workflows' | Resolve-Path
# Each pipeline, with the step that builds what Validate Package packs and, where the pipeline has
# one, the step that releases afterwards.
$script:Pipelines = @(
[pscustomobject]@{ File = 'dotnet.yml'; Build = 'Run KtsuBuild Pipeline'; Release = 'Release' }
[pscustomobject]@{ File = 'dotnet-private.yml'; Build = 'Run KtsuBuild CI Pipeline'; Release = $null }
)
$script:Workflow = $null
$script:StepName = 'Validate Package'
$script:Failures = 0
$script:Count = 0
Expand Down Expand Up @@ -131,6 +142,7 @@ function Test-Case {
param([string]$Name, [scriptblock]$Assertion)

$script:Count++
$Name = "$([System.IO.Path]::GetFileName($script:Workflow)): $Name"
$problem = & $Assertion
if ($problem) {
$script:Failures++
Expand All @@ -143,53 +155,75 @@ function Test-Case {

$script:OriginalPath = $env:PATH

Test-Case -Name 'a pull request run packs, after the build and before Release' -Assertion {
$lines = Get-Content -LiteralPath $script:Workflow
$pipeline = [array]::FindIndex($lines, [Predicate[string]] { param($l) $l -match '^\s*- name: Run KtsuBuild Pipeline\s*$' })
$pack = [array]::FindIndex($lines, [Predicate[string]] { param($l) $l -match "^\s*- name: $script:StepName\s*$" })
$release = [array]::FindIndex($lines, [Predicate[string]] { param($l) $l -match '^\s*- name: Release\s*$' })
if ($pack -lt 0) { return "dotnet.yml has no '$script:StepName' step, so a pull request never runs dotnet pack." }
if (-not ($pipeline -lt $pack -and $pack -lt $release)) { return "the step must sit after 'Run KtsuBuild Pipeline', which builds what it packs with --no-build, and before 'Release'." }

$step = (Get-StepLines -Name $script:StepName) -join "`n"
if ($step -notmatch "if:.*github\.event_name == 'pull_request'") { return "the step is not gated on pull_request, so it would pack a second time on main." }
if ($step -notmatch "if:.*steps\.pipeline\.outputs\.build_skipped != 'true'") { return "the step would pack a build the pipeline skipped." }
if ($step -match 'secrets\.|nuget push|ktsubuild release|git tag') { return "the step can publish or tag; a pull request run must do neither." }
}
$copies = @{}
foreach ($pipelineSpec in $script:Pipelines) {
$script:Workflow = Join-Path $script:WorkflowDir $pipelineSpec.File

Test-Case -Name 'a pull request run packs, after the build and before any release' -Assertion {
$lines = Get-Content -LiteralPath $script:Workflow
$pipeline = [array]::FindIndex($lines, [Predicate[string]] { param($l) $l -match "^\s*- name: $([regex]::Escape($pipelineSpec.Build))\s*$" })
$pack = [array]::FindIndex($lines, [Predicate[string]] { param($l) $l -match "^\s*- name: $script:StepName\s*$" })
if ($pack -lt 0) { return "there is no '$script:StepName' step, so a pull request never runs dotnet pack." }
if ($pipeline -lt 0 -or $pipeline -gt $pack) { return "the step must sit after '$($pipelineSpec.Build)', which builds what it packs with --no-build." }
if ($pipelineSpec.Release) {
$release = [array]::FindIndex($lines, [Predicate[string]] { param($l) $l -match "^\s*- name: $([regex]::Escape($pipelineSpec.Release))\s*$" })
if ($pack -gt $release) { return "the step must sit before '$($pipelineSpec.Release)'." }
}
$between = $lines[($pipeline + 1)..($pack - 1)] | Where-Object { $_ -match '^\S|^ \S' }
if ($between) { return "the step is not in the same job as '$($pipelineSpec.Build)', so it has no build to pack." }

$run = Invoke-PackStep

Test-Case -Name 'packs every deliverable with --no-build in Release, into a folder outside the workspace' -Assertion {
if ($run.Code -ne 0) { return "a clean pack failed the step (exit $($run.Code)): $($run.Output.Trim())" }
$packs = @($run.Calls | Where-Object { $_ -like 'pack *' })
$packed = @($packs | ForEach-Object { [System.IO.Path]::GetFileNameWithoutExtension(($_ -split ' ')[1]) } | Sort-Object)
if (($packed -join ',') -ne 'Widget,Widget.Core') { return "expected Widget and Widget.Core to be packed, got: $($packed -join ', ')" }
foreach ($call in $packs) {
if ($call -notmatch '--configuration Release') { return "not packed in Release, as the release packs: $call" }
if ($call -notmatch '--no-build') { return "packed without --no-build, so it is not the build the pipeline validated: $call" }
if ($call -notmatch "--output $([regex]::Escape((Join-Path $run.Temp 'pr-pack')))") { return "not packed into RUNNER_TEMP: $call" }
if ($call -notmatch "-p:SolutionDir=$([regex]::Escape($run.Workspace))/") { return "packed without SolutionDir, which ktsu.Sdk reads package metadata from: $call" }
if ($call -notmatch '-p:SolutionName=Widget') { return "packed without SolutionName: $call" }
$step = (Get-StepLines -Name $script:StepName) -join "`n"
if ($step -notmatch "if:.*github\.event_name == 'pull_request'") { return "the step is not gated on pull_request, so it would pack a second time on main." }
if ($step -notmatch "if:.*steps\.pipeline\.outputs\.build_skipped != 'true'") { return "the step would pack a build the pipeline skipped." }
if ($step -match 'secrets\.|nuget push|ktsubuild release|git tag') { return "the step can publish or tag; a pull request run must do neither." }
}
}

Test-Case -Name 'skips tests, demos, examples and iOS-only projects, as the release does' -Assertion {
foreach ($skipped in 'Widget.Test', 'Checks', 'Widget.Demo', 'Gallery', 'Widget.Mobile') {
if (@($run.Calls | Where-Object { $_ -like "*$skipped.csproj*" }).Count -gt 0) { return "$skipped was packed, but ktsubuild release does not pack it." }
# Without the step there is nothing to run; the case above has already failed for it.
if (-not (Get-StepLines -Name $script:StepName)) { continue }
$copies[$pipelineSpec.File] = Get-StepScript

$run = Invoke-PackStep

Test-Case -Name 'packs every deliverable with --no-build in Release, into a folder outside the workspace' -Assertion {
if ($run.Code -ne 0) { return "a clean pack failed the step (exit $($run.Code)): $($run.Output.Trim())" }
$packs = @($run.Calls | Where-Object { $_ -like 'pack *' })
$packed = @($packs | ForEach-Object { [System.IO.Path]::GetFileNameWithoutExtension(($_ -split ' ')[1]) } | Sort-Object)
if (($packed -join ',') -ne 'Widget,Widget.Core') { return "expected Widget and Widget.Core to be packed, got: $($packed -join ', ')" }
foreach ($call in $packs) {
if ($call -notmatch '--configuration Release') { return "not packed in Release, as the release packs: $call" }
if ($call -notmatch '--no-build') { return "packed without --no-build, so it is not the build the pipeline validated: $call" }
if ($call -notmatch "--output $([regex]::Escape((Join-Path $run.Temp 'pr-pack')))") { return "not packed into RUNNER_TEMP: $call" }
if ($call -notmatch "-p:SolutionDir=$([regex]::Escape($run.Workspace))/") { return "packed without SolutionDir, which ktsu.Sdk reads package metadata from: $call" }
if ($call -notmatch '-p:SolutionName=Widget') { return "packed without SolutionName: $call" }
}
}
}

Test-Case -Name 'a pack that fails package validation fails the step and names the project' -Assertion {
$r = Invoke-PackStep -Fail 'Widget.Core'
if ($r.Code -eq 0) { return "a failing pack passed the step, so the pull request stays green." }
if ($r.Output -notmatch 'Widget\.Core') { return "the failure does not name the project: $($r.Output.Trim())" }
if (@($r.Calls | Where-Object { $_ -like 'pack *Widget.csproj*' }).Count -ne 1) { return "a failure stopped the other projects from being packed, so one run would not report them all." }
Test-Case -Name 'skips tests, demos, examples and iOS-only projects, as the release does' -Assertion {
foreach ($skipped in 'Widget.Test', 'Checks', 'Widget.Demo', 'Gallery', 'Widget.Mobile') {
if (@($run.Calls | Where-Object { $_ -like "*$skipped.csproj*" }).Count -gt 0) { return "$skipped was packed, but ktsubuild release does not pack it." }
}
}

Test-Case -Name 'a pack that fails package validation fails the step and names the project' -Assertion {
$r = Invoke-PackStep -Fail 'Widget.Core'
if ($r.Code -eq 0) { return "a failing pack passed the step, so the pull request stays green." }
if ($r.Output -notmatch 'Widget\.Core') { return "the failure does not name the project: $($r.Output.Trim())" }
if (@($r.Calls | Where-Object { $_ -like 'pack *Widget.csproj*' }).Count -ne 1) { return "a failure stopped the other projects from being packed, so one run would not report them all." }
}

Test-Case -Name 'a repository with nothing to pack passes' -Assertion {
$r = Invoke-PackStep -Files @{ 'App.Demo/App.Demo.csproj' = '<Project Sdk="ktsu.Sdk.App"></Project>' }
if ($r.Code -ne 0) { return "nothing to pack failed the step: $($r.Output.Trim())" }
if ($r.Calls.Count -ne 0) { return "dotnet was called with nothing to pack: $($r.Calls -join '; ')" }
}
}

Test-Case -Name 'a repository with nothing to pack passes' -Assertion {
$r = Invoke-PackStep -Files @{ 'App.Demo/App.Demo.csproj' = '<Project Sdk="ktsu.Sdk.App"></Project>' }
if ($r.Code -ne 0) { return "nothing to pack failed the step: $($r.Output.Trim())" }
if ($r.Calls.Count -ne 0) { return "dotnet was called with nothing to pack: $($r.Calls -join '; ')" }
$script:Workflow = Join-Path $script:WorkflowDir 'dotnet-private.yml'
Test-Case -Name "runs the same script as dotnet.yml's step" -Assertion {
if ($copies.Count -lt 2) { return "there is no step in both pipelines to compare." }
if ($copies['dotnet-private.yml'] -ne $copies['dotnet.yml']) {
return "the private pipeline's copy has drifted from dotnet.yml's, so the two pipelines would pack different projects or report failures differently. Make the same change to both."
}
}

Write-Host ''
Expand Down
Loading