11[CmdletBinding ()]
22param (
33 [switch ]$SelfTest ,
4+ [switch ]$PrepareArtifacts ,
45 [string ]$Repository = (Split-Path $PSScriptRoot - Parent),
56 [ValidateSet (" Debug" , " Release" )]
67 [string ]$Configuration = " Release" ,
@@ -11,6 +12,99 @@ param(
1112$ErrorActionPreference = " Stop"
1213. (Join-Path $PSScriptRoot " collection-test-modules.ps1" )
1314
15+ function Prepare-CTSComparisonArtifacts {
16+ param ([string ]$ResultsDirectory )
17+
18+ if ([string ]::IsNullOrWhiteSpace($ResultsDirectory )) {
19+ throw " CTS artifact preparation requires a results directory."
20+ }
21+ if (-not (Test-Path - LiteralPath $ResultsDirectory - PathType Container)) {
22+ Write-Host " No CTS results directory to prepare: $ResultsDirectory "
23+ return
24+ }
25+
26+ foreach ($run in Get-ChildItem - LiteralPath $ResultsDirectory - Directory) {
27+ $maps = Join-Path $run.FullName " maps"
28+ if (-not (Test-Path - LiteralPath $maps - PathType Container)) {
29+ continue
30+ }
31+ $archive = Join-Path $run.FullName " maps.zip"
32+ if (Test-Path - LiteralPath $archive ) {
33+ throw " CTS map archive already exists: $archive "
34+ }
35+ $temporaryArchive = Join-Path $run.FullName (" maps-" + [guid ]::NewGuid().ToString(" N" ) + " .tmp" )
36+ try {
37+ [System.IO.Compression.ZipFile ]::CreateFromDirectory($maps , $temporaryArchive )
38+ Move-Item - LiteralPath $temporaryArchive - Destination $archive
39+ Remove-Item - LiteralPath $maps - Recurse - Force
40+ Write-Host " Archived CTS maps for publication: $archive "
41+ } finally {
42+ if (Test-Path - LiteralPath $temporaryArchive ) {
43+ Remove-Item - LiteralPath $temporaryArchive - Force
44+ }
45+ }
46+ }
47+ }
48+
49+ function Invoke-CTSArchiveSelfTest {
50+ $fixture = Join-Path ([System.IO.Path ]::GetTempPath()) (" testfx-cts-archive-" + [guid ]::NewGuid().ToString(" N" ))
51+ $run = Join-Path $fixture " run"
52+ $maps = Join-Path $run " maps"
53+ $nested = Join-Path $maps ((" long-directory-" + (" x" * 60 ) + " \" ) * 6 )
54+ New-Item - ItemType Directory - Path $nested - Force | Out-Null
55+ try {
56+ Set-Content - LiteralPath (Join-Path $maps " committed" ) - Value " first" - NoNewline
57+ Set-Content - LiteralPath (Join-Path $nested " .hidden-map" ) - Value " second" - NoNewline
58+ Set-Content - LiteralPath (Join-Path $run " comparison.json" ) - Value " comparison"
59+ Prepare- CTSComparisonArtifacts - ResultsDirectory $fixture
60+ $archivePath = Join-Path $run " maps.zip"
61+ if (-not (Test-Path - LiteralPath $archivePath - PathType Leaf) -or (Test-Path - LiteralPath $maps ) -or
62+ -not (Test-Path - LiteralPath (Join-Path $run " comparison.json" ) - PathType Leaf)) {
63+ throw " CTSMapsAreArchivedForPublication: the archive must replace only the maps tree."
64+ }
65+
66+ $archive = [System.IO.Compression.ZipFile ]::OpenRead($archivePath )
67+ try {
68+ $entries = @ ($archive.Entries | Where-Object { -not $_.FullName.EndsWith (" /" ) })
69+ $committed = @ ($entries | Where-Object FullName -eq " committed" )
70+ $hidden = @ ($entries | Where-Object { $_.FullName.EndsWith (" /.hidden-map" ) })
71+ if ($entries.Count -ne 2 -or $committed.Count -ne 1 -or $hidden.Count -ne 1 -or
72+ $hidden [0 ].FullName.Length -le 399 ) {
73+ throw " CTSMapsAreArchivedForPublication: long and hidden map paths must be retained."
74+ }
75+ foreach ($case in @ (@ { Entry = $committed [0 ]; Expected = " first" }, @ { Entry = $hidden [0 ]; Expected = " second" })) {
76+ $reader = [System.IO.StreamReader ]::new($case.Entry.Open ())
77+ try {
78+ if ($reader.ReadToEnd () -cne $case.Expected ) {
79+ throw " CTSMapsAreArchivedForPublication: archive contents changed."
80+ }
81+ } finally {
82+ $reader.Dispose ()
83+ }
84+ }
85+ } finally {
86+ $archive.Dispose ()
87+ }
88+ Write-Output " PASS CTSMapsAreArchivedForPublication"
89+
90+ New-Item - ItemType Directory - Path $maps | Out-Null
91+ Set-Content - LiteralPath (Join-Path $maps " committed" ) - Value " preserve"
92+ $rejected = $false
93+ try {
94+ Prepare- CTSComparisonArtifacts - ResultsDirectory $fixture
95+ } catch {
96+ $rejected = $_.Exception.Message.StartsWith (" CTS map archive already exists:" )
97+ }
98+ if (-not $rejected -or -not (Test-Path - LiteralPath (Join-Path $maps " committed" )) -or
99+ (Get-Content - LiteralPath (Join-Path $maps " committed" ) - Raw).Trim() -ne " preserve" ) {
100+ throw " CTSArchiveCollisionPreservesMaps: an existing archive must not overwrite or delete maps."
101+ }
102+ Write-Output " PASS CTSArchiveCollisionPreservesMaps"
103+ } finally {
104+ Remove-Item - LiteralPath $fixture - Recurse - Force
105+ }
106+ }
107+
14108function Invoke-CTSModuleSelectionSelfTest {
15109 $fixture = Join-Path - Path ([System.IO.Path ]::GetTempPath()) - ChildPath (" testfx-cts-selection-" + [guid ]::NewGuid().ToString(" N" ))
16110 New-Item - ItemType Directory - Path $fixture | Out-Null
@@ -316,6 +410,12 @@ function Invoke-CTSArtifactSelfTest {
316410if ($SelfTest ) {
317411 Invoke-CTSModuleSelectionSelfTest
318412 Invoke-CTSArtifactSelfTest
413+ Invoke-CTSArchiveSelfTest
414+ exit 0
415+ }
416+
417+ if ($PrepareArtifacts ) {
418+ Prepare- CTSComparisonArtifacts - ResultsDirectory $ResultsDirectory
319419 exit 0
320420}
321421
0 commit comments