diff --git a/dependency-updates/README.md b/dependency-updates/README.md index 1137da8..136c778 100644 --- a/dependency-updates/README.md +++ b/dependency-updates/README.md @@ -5,6 +5,8 @@ Target repositories contain dependency declarations and consistency tests, but n The scheduled workflow reads `targets.json`, checks out each repository, and invokes the update engine published by `AtlassianPS.Standards`. +The controller synchronizes workflow pins and the expected SHA in the existing +`Tests/Tools/StandardsVersionConsistency.Unit.Tests.ps1` assertion together. It creates or refreshes `automation/update-powershell-dependencies` when files change. The controller and workflow contract are validated by the repository's `CI` workflow. diff --git a/dependency-updates/Tests/Update-RepositoryDependencies.Tests.ps1 b/dependency-updates/Tests/Update-RepositoryDependencies.Tests.ps1 index cee89b1..0a322e0 100644 --- a/dependency-updates/Tests/Update-RepositoryDependencies.Tests.ps1 +++ b/dependency-updates/Tests/Update-RepositoryDependencies.Tests.ps1 @@ -109,6 +109,38 @@ Describe 'Update-RepositoryDependencies' { [System.IO.File]::ReadAllBytes($plainPath)[0] | Should -Not -Be 0xEF } + It 'synchronizes the existing consistency assertion without renaming it' -ForEach @( + @{ Variable = '$expectedStandardsSha'; HasBom = $true; NewLine = "`r`n" } + @{ Variable = '$script:standardsActionSha'; HasBom = $false; NewLine = "`n" } + ) { + $repositoryPath = Join-Path $TestDrive 'consistency-test' + New-TestRepository -Path $repositoryPath + $testsPath = Join-Path $repositoryPath 'Tests/Tools' + $null = New-Item -ItemType Directory -Path $testsPath -Force + $testPath = Join-Path $testsPath 'StandardsVersionConsistency.Unit.Tests.ps1' + $oldSha = '0' * 40 + $newSha = 'c' * 40 + $originalText = "$Variable = '$oldSha'" + $NewLine + '$actualSha | Should -Be @(' + $Variable + ')' + $NewLine + $encoding = [System.Text.UTF8Encoding]::new($HasBom) + [System.IO.File]::WriteAllText($testPath, $originalText, $encoding) + Mock Invoke-RestMethod { [PSCustomObject]@{ sha = $newSha } } + Mock Get-PSRepository { [PSCustomObject]@{ InstallationPolicy = 'Trusted' } } + Mock Get-Module { [PSCustomObject]@{ Version = [Version]'0.3.1' } } + Mock Import-Module + Mock Invoke-StandardsDependencyUpdate { [PSCustomObject]@{ Changed = $false } } + + Invoke-RepositoryDependencyUpdate -RepositoryPath $repositoryPath -ModuleName TestModule -StandardsVersion '0.3.1' + + $expectedText = $originalText.Replace($oldSha, $newSha) + [System.IO.File]::ReadAllText($testPath) | Should -BeExactly $expectedText + $expectedBytes = @($encoding.GetPreamble()) + @($encoding.GetBytes($expectedText)) + [System.IO.File]::ReadAllBytes($testPath) | Should -Be $expectedBytes + + Invoke-RepositoryDependencyUpdate -RepositoryPath $repositoryPath -ModuleName TestModule -StandardsVersion '0.3.1' + + [System.IO.File]::ReadAllBytes($testPath) | Should -Be $expectedBytes + } + It 'fails when GitHub does not return a commit SHA' { $repositoryPath = Join-Path $TestDrive 'invalid-sha' New-TestRepository -Path $repositoryPath diff --git a/dependency-updates/Update-RepositoryDependencies.ps1 b/dependency-updates/Update-RepositoryDependencies.ps1 index 32a3ce9..af1cbd6 100644 --- a/dependency-updates/Update-RepositoryDependencies.ps1 +++ b/dependency-updates/Update-RepositoryDependencies.ps1 @@ -101,6 +101,20 @@ function Sync-StandardsWorkflowReference { Set-FileTextState -State $state -Text $updatedText } } + + $consistencyTestPath = Join-Path $ProjectRoot 'Tests/Tools/StandardsVersionConsistency.Unit.Tests.ps1' + if (Test-Path -LiteralPath $consistencyTestPath -PathType Leaf) { + $state = Get-FileTextState -Path $consistencyTestPath + $updatedText = [regex]::Replace( + $state.Text, + '(?\$(?:expectedStandardsSha|script:standardsActionSha)\s*=\s*[''"])[0-9a-f]{40}(?[''"])', + ('${prefix}' + $commit + '${suffix}') + ) + + if ($updatedText -ne $state.Text) { + Set-FileTextState -State $state -Text $updatedText + } + } } function Invoke-StandardsDependencyUpdate {