diff --git a/.github/workflows/script-tests.yml b/.github/workflows/script-tests.yml index 589c686..3a6f16e 100644 --- a/.github/workflows/script-tests.yml +++ b/.github/workflows/script-tests.yml @@ -20,11 +20,6 @@ jobs: - uses: actions/checkout@v4 - - name: Install make - if: ${{ matrix.host == 'macos' }} - run: | - brew install make - echo "$(brew --prefix)/opt/make/libexec/gnubin" >> $GITHUB_PATH - - - run: make test + - run: Invoke-Pester working-directory: updater + shell: pwsh diff --git a/updater/Makefile b/updater/Makefile deleted file mode 100644 index 23da328..0000000 --- a/updater/Makefile +++ /dev/null @@ -1,10 +0,0 @@ -.PHONY: test -.SILENT: test -.ONESHELL: - -test: tests/*.ps1 - @set -e - for file in $^ ; do - echo "Running " $${file} ; - pwsh $${file} ; - done diff --git a/updater/tests/common/test-utils.ps1 b/updater/tests/common/test-utils.ps1 deleted file mode 100644 index a9bf444..0000000 --- a/updater/tests/common/test-utils.ps1 +++ /dev/null @@ -1,70 +0,0 @@ -Set-StrictMode -Version latest -$ErrorActionPreference = "Stop" - -function RunTest ([string] $name, [ScriptBlock] $code, [string] $skipReason = "") -{ - if ($skipReason -ne "") - { - Write-Warning "Test $name - skipped $skipReason" - return - } - try - { - Write-Host "Test $name - starting" -ForegroundColor Yellow - $code.Invoke(); - Write-Host "Test $name - PASS" -ForegroundColor Green - } - catch - { - Write-Host "Test $name - FAILED" -ForegroundColor Red - throw - } -} - -function AssertEqual([string] $expected, [string] $actual) -{ - $diff = Compare-Object $expected $actual - if ($null -ne $diff -and $diff.Count -ne 0) - { - Write-Host "Given strings are not equal:" -ForegroundColor Red - Write-Host "========================================" - Write-Host "Expected:" - Write-Host "----------------------------------------" - Write-Host $expected - Write-Host "----------------------------------------" - Write-Host "Actual:" - Write-Host "----------------------------------------" - Write-Host $actual - Write-Host "========================================" - throw "AssertEqual failed" - } -} - -function AssertContains([string[]] $list, [string] $value) -{ - if (-not ($list -contains $value)) - { - Write-Host "Expected list to contain '$value':" -ForegroundColor Red - Write-Host "========================================" - Write-Host $list - Write-Host "========================================" - throw "AssertContains failed" - } -} - -function AssertFailsWith([string] $substring, [scriptblock] $block) -{ - $e = $null - try - { - $block.Invoke() - } - catch - { - $e = $_ - } - if (-not "$e".Contains($substring)) - { - throw "AssertFailsWith failed - expected to find '$substring' in the error '$e'" - } -} diff --git a/updater/tests/get-changelog.Tests.ps1 b/updater/tests/get-changelog.Tests.ps1 new file mode 100644 index 0000000..98cccc2 --- /dev/null +++ b/updater/tests/get-changelog.Tests.ps1 @@ -0,0 +1,106 @@ + +Describe 'get-changelog' { + It 'with existing versions' { + $actual = & "$PSScriptRoot/../scripts/get-changelog.ps1" ` + -RepoUrl 'https://github.com/getsentry/github-workflows' -OldTag '1.0.0' -NewTag '2.1.0' + $expected = @' +## Changelog +### 2.1.0 + +#### Features + +- New reusable workflow, `danger.yml`, to check Pull Requests with predefined rules ([#34](https://github-redirect.dependabot.com/getsentry/github-workflows/pull/34)) + +### 2.0.0 + +#### Changes + +- Rename `api_token` secret to `api-token` ([#21](https://github-redirect.dependabot.com/getsentry/github-workflows/pull/21)) +- Change changelog target section header from "Features" to "Dependencies" ([#19](https://github-redirect.dependabot.com/getsentry/github-workflows/pull/19)) + +#### Features + +- Add `pr-strategy` switch to choose between creating new PRs or updating an existing one ([#22](https://github-redirect.dependabot.com/getsentry/github-workflows/pull/22)) +- Add `changelog-section` input setting to specify target changelog section header ([#19](https://github-redirect.dependabot.com/getsentry/github-workflows/pull/19)) + +#### Fixes + +- Preserve changelog bullet-point format ([#20](https://github-redirect.dependabot.com/getsentry/github-workflows/pull/20)) +- Changelog section parsing when an entry text contains the section name in the text ([#25](https://github-redirect.dependabot.com/getsentry/github-workflows/pull/25)) +'@ + + $actual | Should -Be $expected + } + + It 'with missing versions' { + $actual = & "$PSScriptRoot/../scripts/get-changelog.ps1" ` + -RepoUrl 'https://github.com/getsentry/sentry-javascript' -OldTag 'XXXXXXX' -NewTag 'YYYYYYYYY' + $actual | Should -BeNullOrEmpty + } + + It 'with missing repo' { + $actual = & "$PSScriptRoot/../scripts/get-changelog.ps1" ` + -RepoUrl 'https://github.com/getsentry/foo-bar' -OldTag 'XXXXXXX' -NewTag 'YYYYYYYYY' + # May print a warning but still returns (an empty string) + $actual | Should -BeNullOrEmpty + } + + It 'with unsupported repo' { + $actual = & "$PSScriptRoot/../scripts/get-changelog.ps1" ` + -RepoUrl 'https://dart.googlesource.com/args' -OldTag 'XXXXXXX' -NewTag 'YYYYYYYYY' + # May print a warning but still returns (an empty string) + $actual | Should -BeNullOrEmpty + } + + It 'removes at-mentions' { + $actual = & "$PSScriptRoot/../scripts/get-changelog.ps1" ` + -RepoUrl 'https://github.com/getsentry/sentry-cli' -OldTag '2.1.0' -NewTag '2.2.0' + $expected = @' +## Changelog +### 2.2.0 + +#### Various fixes & improvements + +- feat: Compute and upload il2cpp line mappings ([#1248](https://github-redirect.dependabot.com/getsentry/sentry-cli/issues/1248)) by loewenheim +- ref: Skip protected zip files when uploading debug files ([#1245](https://github-redirect.dependabot.com/getsentry/sentry-cli/issues/1245)) by kamilogorek +'@ + + $actual | Should -Be $expected + } + + It "get-changelog removes doesn't duplicate PR links" { + $actual = & "$PSScriptRoot/../scripts/get-changelog.ps1" ` + -RepoUrl 'https://github.com/getsentry/sentry-native' -OldTag '0.4.16' -NewTag '0.4.17' + $expected = @' +## Changelog +### 0.4.17 + +**Fixes**: + +- sentry-native now successfully builds when examples aren't included. ([#702](https://github-redirect.dependabot.com/getsentry/sentry-native/pull/702)) + +**Thank you**: + +Features, fixes and improvements in this release have been contributed by: + +- [AenBleidd](https://github-redirect.dependabot.com/AenBleidd) +'@ + + $actual | Should -Be $expected + } + + It 'truncates too long text' { + $actual = & "$PSScriptRoot/../scripts/get-changelog.ps1" ` + -RepoUrl 'https://github.com/getsentry/sentry-cli' -OldTag '1.0.0' -NewTag '2.4.0' + if ($actual.Length -gt 61000) + { + throw "Expected the content to be truncated to less-than 61k characters, but got: $($actual.Length)" + } + $msg = "Changelog content truncated by [0-9]+ characters because it was over the limit \(60000\) and wouldn't fit into PR description." + if ("$actual" -notmatch $msg) + { + Write-Host $actual + throw "Expected changelog to contain message '$msg'" + } + } +} diff --git a/updater/tests/get-changelog.ps1 b/updater/tests/get-changelog.ps1 deleted file mode 100644 index ccb7cb7..0000000 --- a/updater/tests/get-changelog.ps1 +++ /dev/null @@ -1,105 +0,0 @@ -. "$PSScriptRoot/common/test-utils.ps1" - -RunTest 'get-changelog with existing versions' { - $actual = & "$PSScriptRoot/../scripts/get-changelog.ps1" ` - -RepoUrl 'https://github.com/getsentry/github-workflows' -OldTag '1.0.0' -NewTag '2.1.0' - $expected = @' -## Changelog -### 2.1.0 - -#### Features - -- New reusable workflow, `danger.yml`, to check Pull Requests with predefined rules ([#34](https://github-redirect.dependabot.com/getsentry/github-workflows/pull/34)) - -### 2.0.0 - -#### Changes - -- Rename `api_token` secret to `api-token` ([#21](https://github-redirect.dependabot.com/getsentry/github-workflows/pull/21)) -- Change changelog target section header from "Features" to "Dependencies" ([#19](https://github-redirect.dependabot.com/getsentry/github-workflows/pull/19)) - -#### Features - -- Add `pr-strategy` switch to choose between creating new PRs or updating an existing one ([#22](https://github-redirect.dependabot.com/getsentry/github-workflows/pull/22)) -- Add `changelog-section` input setting to specify target changelog section header ([#19](https://github-redirect.dependabot.com/getsentry/github-workflows/pull/19)) - -#### Fixes - -- Preserve changelog bullet-point format ([#20](https://github-redirect.dependabot.com/getsentry/github-workflows/pull/20)) -- Changelog section parsing when an entry text contains the section name in the text ([#25](https://github-redirect.dependabot.com/getsentry/github-workflows/pull/25)) -'@ - - AssertEqual $expected $actual -} - -RunTest 'get-changelog with missing versions' { - $actual = & "$PSScriptRoot/../scripts/get-changelog.ps1" ` - -RepoUrl 'https://github.com/getsentry/sentry-javascript' -OldTag 'XXXXXXX' -NewTag 'YYYYYYYYY' - AssertEqual '' $actual -} - -RunTest 'get-changelog with missing repo' { - $actual = & "$PSScriptRoot/../scripts/get-changelog.ps1" ` - -RepoUrl 'https://github.com/getsentry/foo-bar' -OldTag 'XXXXXXX' -NewTag 'YYYYYYYYY' - # May print a warning but still returns (an empty string) - AssertEqual '' $actual -} - -RunTest 'get-changelog with unsupported repo' { - $actual = & "$PSScriptRoot/../scripts/get-changelog.ps1" ` - -RepoUrl 'https://dart.googlesource.com/args' -OldTag 'XXXXXXX' -NewTag 'YYYYYYYYY' - # May print a warning but still returns (an empty string) - AssertEqual '' $actual -} - -RunTest 'get-changelog removes at-mentions' { - $actual = & "$PSScriptRoot/../scripts/get-changelog.ps1" ` - -RepoUrl 'https://github.com/getsentry/sentry-cli' -OldTag '2.1.0' -NewTag '2.2.0' - $expected = @' -## Changelog -### 2.2.0 - -#### Various fixes & improvements - -- feat: Compute and upload il2cpp line mappings ([#1248](https://github-redirect.dependabot.com/getsentry/sentry-cli/issues/1248)) by loewenheim -- ref: Skip protected zip files when uploading debug files ([#1245](https://github-redirect.dependabot.com/getsentry/sentry-cli/issues/1245)) by kamilogorek -'@ - - AssertEqual $expected $actual -} - -RunTest "get-changelog removes doesn't duplicate PR links" { - $actual = & "$PSScriptRoot/../scripts/get-changelog.ps1" ` - -RepoUrl 'https://github.com/getsentry/sentry-native' -OldTag '0.4.16' -NewTag '0.4.17' - $expected = @' -## Changelog -### 0.4.17 - -**Fixes**: - -- sentry-native now successfully builds when examples aren't included. ([#702](https://github-redirect.dependabot.com/getsentry/sentry-native/pull/702)) - -**Thank you**: - -Features, fixes and improvements in this release have been contributed by: - -- [AenBleidd](https://github-redirect.dependabot.com/AenBleidd) -'@ - - AssertEqual $expected $actual -} - -RunTest 'get-changelog truncates too long text' { - $actual = & "$PSScriptRoot/../scripts/get-changelog.ps1" ` - -RepoUrl 'https://github.com/getsentry/sentry-cli' -OldTag '1.0.0' -NewTag '2.4.0' - if ($actual.Length -gt 61000) - { - throw "Expected the content to be truncated to less-than 61k characters, but got: $($actual.Length)" - } - $msg = "Changelog content truncated by [0-9]+ characters because it was over the limit \(60000\) and wouldn't fit into PR description." - if ("$actual" -notmatch $msg) - { - Write-Host $actual - throw "Expected changelog to contain message '$msg'" - } -} diff --git a/updater/tests/nonbot-commits.Tests.ps1 b/updater/tests/nonbot-commits.Tests.ps1 new file mode 100644 index 0000000..c67f1c0 --- /dev/null +++ b/updater/tests/nonbot-commits.Tests.ps1 @@ -0,0 +1,37 @@ +Set-StrictMode -Version latest + +Describe 'nonbot-commits' { + Context 'Repo <_>' -ForEach @('https://github.com/getsentry/github-workflows', 'git@github.com:getsentry/github-workflows.git') { + BeforeEach { + $repoUrl = $_ + function NonBotCommits([Parameter(Mandatory = $true)][string] $branch) + { + $result = & "$PSScriptRoot/../scripts/nonbot-commits.ps1" -RepoUrl $repoUrl -MainBranch 'main' -PrBranch $branch + if (-not $?) + { + throw $result + } + elseif ($LASTEXITCODE -ne 0) + { + throw "Script finished with exit code $LASTEXITCODE" + } + $result + } + } + + It 'empty-if-all-commits-by-bot' { + $commits = NonBotCommits 'deps/updater/tests/sentry-cli.properties' + $commits | Should -BeNullOrEmpty + } + + It 'empty-if-branch-doesnt-exist' { + $commits = NonBotCommits 'non-existent-branch' + $commits | Should -BeNullOrEmpty + } + + It 'non-empty-if-changed' { + $commits = NonBotCommits 'test/nonbot-commits' + $commits | Should -Be '0b7d9cc test: keep this branch' + } + } +} diff --git a/updater/tests/nonbot-commits.ps1 b/updater/tests/nonbot-commits.ps1 deleted file mode 100644 index 0ab37ae..0000000 --- a/updater/tests/nonbot-commits.ps1 +++ /dev/null @@ -1,35 +0,0 @@ -Set-StrictMode -Version latest - -. "$PSScriptRoot/common/test-utils.ps1" - -foreach ($repoUrl in @('https://github.com/getsentry/github-workflows', 'git@github.com:getsentry/github-workflows.git')) -{ - function NonBotCommits([Parameter(Mandatory = $true)][string] $branch) - { - $result = & "$PSScriptRoot/../scripts/nonbot-commits.ps1" -RepoUrl $repoUrl -MainBranch 'main' -PrBranch $branch - if (-not $?) - { - throw $result - } - elseif ($LASTEXITCODE -ne 0) - { - throw "Script finished with exit code $LASTEXITCODE" - } - $result - } - - RunTest 'empty-if-all-commits-by-bot' { - $commits = NonBotCommits 'deps/updater/tests/sentry-cli.properties' - AssertEqual '' "$commits" - } - - RunTest 'empty-if-branch-doesnt-exist' { - $commits = NonBotCommits 'non-existent-branch' - AssertEqual '' "$commits" - } - - RunTest 'non-empty-if-changed' { - $commits = NonBotCommits 'test/nonbot-commits' - AssertEqual '0b7d9cc test: keep this branch' "$commits" - } -} \ No newline at end of file diff --git a/updater/tests/sort-versions.Tests.ps1 b/updater/tests/sort-versions.Tests.ps1 new file mode 100644 index 0000000..aec246e --- /dev/null +++ b/updater/tests/sort-versions.Tests.ps1 @@ -0,0 +1,30 @@ + +BeforeAll { + function SortVersions([Parameter(Mandatory = $true)][string[]] $List) + { + $result = & "$PSScriptRoot/../scripts/sort-versions.ps1" $List + if (-not $?) + { + throw $result + } + $result + } +} + +Describe 'sort-versions' { + It 'standard versions' { + $sorted = SortVersions @('3.0.0', '5.4.11', 'v1.2.3', '5.4.1') + $sorted | Should -Be @('v1.2.3', '3.0.0', '5.4.1', '5.4.11') + } + + It 'standard versions v2' { + $sorted = SortVersions @('3.0.0', 'v6.0', '5.4.11', '5.5', 'v1.2.3', '5.4.1') + $sorted | Should -Be @('v1.2.3', '3.0.0', '5.4.1', '5.4.11', '5.5', 'v6.0') + } + + # https://semver.org/#spec-item-11 + It 'pre-releases' { + $sorted = SortVersions @('1.0.0-rc.1', '1.0.0', '1.0.0-beta.11', '1.0.0-alpha.1', '1.0.0-beta', '1.0.0-alpha.beta', '1.0.0-alpha', '1.0.0-beta.2') + $sorted | Should -Be @('1.0.0-alpha', '1.0.0-alpha.1', '1.0.0-alpha.beta', '1.0.0-beta', '1.0.0-beta.2', '1.0.0-beta.11', '1.0.0-rc.1', '1.0.0') + } +} diff --git a/updater/tests/sort-versions.ps1 b/updater/tests/sort-versions.ps1 deleted file mode 100644 index b7cf5e6..0000000 --- a/updater/tests/sort-versions.ps1 +++ /dev/null @@ -1,27 +0,0 @@ -. "$PSScriptRoot/common/test-utils.ps1" - -function SortVersions([Parameter(Mandatory = $true)][string[]] $List) -{ - $result = & "$PSScriptRoot/../scripts/sort-versions.ps1" $List - if (-not $?) - { - throw $result - } - $result -} - -RunTest "sort standard versions" { - $sorted = SortVersions @('3.0.0', '5.4.11', 'v1.2.3', '5.4.1') - AssertEqual @('v1.2.3', '3.0.0', '5.4.1', '5.4.11') $sorted -} - -RunTest "sort standard versions v2" { - $sorted = SortVersions @('3.0.0', 'v6.0', '5.4.11', '5.5', 'v1.2.3', '5.4.1') - AssertEqual @('v1.2.3', '3.0.0', '5.4.1', '5.4.11', '5.5', 'v6.0') $sorted -} - -# https://semver.org/#spec-item-11 -RunTest "sort with pre-releases" { - $sorted = SortVersions @('1.0.0-rc.1', '1.0.0', '1.0.0-beta.11', '1.0.0-alpha.1', '1.0.0-beta', '1.0.0-alpha.beta', '1.0.0-alpha', '1.0.0-beta.2') - AssertEqual @('1.0.0-alpha', '1.0.0-alpha.1', '1.0.0-alpha.beta', '1.0.0-beta', '1.0.0-beta.2', '1.0.0-beta.11', '1.0.0-rc.1', '1.0.0') $sorted -} diff --git a/updater/tests/update-changelog.ps1 b/updater/tests/update-changelog.Tests.ps1 similarity index 64% rename from updater/tests/update-changelog.ps1 rename to updater/tests/update-changelog.Tests.ps1 index 90d4896..aa6d3a2 100644 --- a/updater/tests/update-changelog.ps1 +++ b/updater/tests/update-changelog.Tests.ps1 @@ -1,10 +1,9 @@ -. "$PSScriptRoot/common/test-utils.ps1" -$testCases = Get-ChildItem "$PSScriptRoot/testdata/changelog/" +$testCases = -foreach ($testCase in $testCases) -{ - RunTest $testCase { +Describe 'update-changelog' { + It '<_>' -ForEach @(Get-ChildItem "$PSScriptRoot/testdata/changelog/") { + $testCase = $_ Copy-Item "$testCase/CHANGELOG.md.original" "$testCase/CHANGELOG.md" pwsh -WorkingDirectory $testCase -File "$PSScriptRoot/../scripts/update-changelog.ps1" ` @@ -16,6 +15,6 @@ foreach ($testCase in $testCases) -NewTag '7.17.0' ` -Section 'Dependencies' - AssertEqual (Get-Content "$testCase/CHANGELOG.md.expected") (Get-Content "$testCase/CHANGELOG.md") + Get-Content "$testCase/CHANGELOG.md" | Should -Be (Get-Content "$testCase/CHANGELOG.md.expected") } } diff --git a/updater/tests/update-dependency.Tests.ps1 b/updater/tests/update-dependency.Tests.ps1 new file mode 100644 index 0000000..3d1c9fe --- /dev/null +++ b/updater/tests/update-dependency.Tests.ps1 @@ -0,0 +1,250 @@ +BeforeAll { + function UpdateDependency([Parameter(Mandatory = $true)][string] $path, [string] $pattern = $null) + { + $result = & "$PSScriptRoot/../scripts/update-dependency.ps1" -Path $path -Pattern $pattern + if (-not $?) + { + throw $result + } + $result + } + + $testDir = "$PSScriptRoot/testdata/dependencies" + if (-not (Test-Path $testDir)) + { + New-Item $testDir -ItemType Directory + } + $repoUrl = 'https://github.com/getsentry/github-workflows' + + # Find the latest latest version in this repo. We're intentionally using different code than `update-dependency.ps1` + # script uses to be able to catch issues, if any. + $currentVersion = (git -c 'versionsort.suffix=-' ls-remote --tags --sort='v:refname' $repoUrl ` + | Select-Object -Last 1 | Select-String -Pattern 'refs/tags/(.*)$').Matches.Groups[1].Value +} + +Describe ('update-dependency') { + Context ('properties-file') { + It 'works' { + $testFile = "$testDir/test.properties" + @("repo=$repoUrl", 'version = none') | Out-File $testFile + UpdateDependency $testFile + Get-Content $testFile | Should -Be @("repo=$repoUrl", "version = $currentVersion") + } + + It 'version pattern match' { + $testFile = "$testDir/test.properties" + $repo = 'https://github.com/getsentry/sentry-cli' + @("repo=$repo", 'version=0') | Out-File $testFile + UpdateDependency $testFile '^0\.' + Get-Content $testFile | Should -Be @("repo=$repo", 'version=0.28.0') + } + + # Note: without custom sorting, this would have yielded 'v1.7.31_gradle_plugin' + It 'version sorting must work properly' { + $testFile = "$testDir/test.properties" + $repo = 'https://github.com/getsentry/sentry-java' + @("repo=$repo", 'version=0') | Out-File $testFile + UpdateDependency $testFile '^v?[123].*$' + Get-Content $testFile | Should -Be @("repo=$repo", 'version=3.2.1') + } + + It 'will not update from a later release to an earlier release' { + $testFile = "$testDir/test.properties" + $repo = 'https://github.com/getsentry/sentry-java' + @("repo=$repo", 'version=999.0.0-beta.1') | Out-File $testFile + UpdateDependency $testFile + Get-Content $testFile | Should -Be @("repo=$repo", 'version=999.0.0-beta.1') + } + } + + Context 'bash-script' -Skip:$IsWindows { + It 'works' { + $testFile = "$testDir/test.version" + '' | Out-File $testFile + $testScript = "$testDir/test.sh" + @' +#!/usr/bin/env bash +set -euo pipefail +cd $(dirname "$0") +case $1 in +get-version) + cat test.version + ;; +get-repo) + echo +'@ + ' "' + $repoUrl + '"' + @' + ;; +set-version) + echo $2 > test.version + ;; +*) + echo "Unknown argument $1" + exit 1 + ;; +esac +'@ | Out-File $testScript + UpdateDependency $testScript + (Get-Content $testFile) | Should -Be $currentVersion + } + + It 'fails in get-version' { + $testScript = "$testDir/test.sh" + @' +#!/usr/bin/env bash +echo "Failure" +exit 1 +'@ | Out-File $testScript + + { UpdateDependency $testScript } | Should -Throw '*get-version | output: Failure*' + } + + It 'fails in get-repo' { + $testScript = "$testDir/test.sh" + @' +#!/usr/bin/env bash +set -euo pipefail +case $1 in +get-version) + ;; +get-repo) + echo "Failure" + exit 1 +;; +esac +'@ | Out-File $testScript + + { UpdateDependency $testScript } | Should -Throw '*get-repo | output: Failure*' + } + + It 'fails in set-version' { + $testScript = "$testDir/test.sh" + @' +#!/usr/bin/env bash +set -euo pipefail +cd $(dirname "$0") +case $1 in +get-version) + echo "" + ;; +get-repo) + echo +'@ + ' "' + $repoUrl + '"' + @' + ;; +set-version) + echo "Failure" + exit 1 + ;; +esac +'@ | Out-File $testScript + + { UpdateDependency $testScript } | Should -Throw "*set-version $currentVersion | output: Failure*" + } + } + + Context 'powershell-script' { + It 'works' { + $testFile = "$testDir/test.version" + '' | Out-File $testFile + $testScript = "$testDir/test.ps1" + @' +param([string] $action, [string] $value) +$file = "$PSScriptRoot/test.version" +switch ($action) +{ + "get-version" { Get-Content $file } + "get-repo" { +'@ + '"' + $repoUrl + '"' + @' + } + "set-version" { $value | Out-File $file } + Default { throw "Unknown action $action" } +} +'@ | Out-File $testScript + UpdateDependency $testScript + Get-Content $testFile | Should -Be $currentVersion + } + + It 'fails in get-version' { + $testScript = "$testDir/test.ps1" + @' +throw "Failure" +'@ | Out-File $testScript + + { UpdateDependency $testScript } | Should -Throw '*get-version | output: Failure*' + } + + It 'fails in get-repo' { + $testScript = "$testDir/test.ps1" + @' +param([string] $action, [string] $value) +if ($action -eq "get-repo") +{ + throw "Failure" +} +'@ | Out-File $testScript + + { UpdateDependency $testScript } | Should -Throw '*get-repo | output: Failure*' + } + + It 'fails in set-version' { + $testScript = "$testDir/test.ps1" + @' +param([string] $action, [string] $value) +switch ($action) +{ + "get-version" { '' } + "get-repo" { +'@ + '"' + $repoUrl + '"' + @' + } + "set-version" { throw "Failure" } +} +'@ | Out-File $testScript + + { UpdateDependency $testScript } | Should -Throw "*set-version $currentVersion | output: Failure*" + } + } + + Context ('output') { + BeforeAll { + function _testOutput([string[]] $output) + { + $output | Should -Contain 'originalTag=0' + $output | Should -Contain 'originalTag=0' + $output | Should -Contain 'latestTag=0.28.0' + $output | Should -Contain 'latestTagNice=v0.28.0' + $output | Should -Contain 'url=https://github.com/getsentry/sentry-cli' + $output | Should -Contain 'mainBranch=master' + } + } + + It 'writes output' { + $testFile = "$testDir/test.properties" + $repo = 'https://github.com/getsentry/sentry-cli' + @("repo=$repo", 'version=0') | Out-File $testFile + $stdout = UpdateDependency $testFile '^0\.' + _testOutput $stdout + } + + It 'writes to env:GITHUB_OUTPUT' { + $testFile = "$testDir/test.properties" + $repo = 'https://github.com/getsentry/sentry-cli' + @("repo=$repo", 'version=0') | Out-File $testFile + $outFile = "$testDir/outfile" + New-Item $outFile -ItemType File | Out-Null + try + { + $env:GITHUB_OUTPUT = $outFile + $stdout = UpdateDependency $testFile '^0\.' + Write-Host 'Testing standard output' + _testOutput $stdout + Write-Host 'Testing env:GITHUB_OUTPUT' + _testOutput (Get-Content $outFile) + } + finally + { + # Delete the file and unser the env variable + Remove-Item $outFile | Out-Null + Remove-Item env:GITHUB_OUTPUT | Out-Null + } + } + } +} diff --git a/updater/tests/update-dependency.ps1 b/updater/tests/update-dependency.ps1 deleted file mode 100644 index 4fed6af..0000000 --- a/updater/tests/update-dependency.ps1 +++ /dev/null @@ -1,239 +0,0 @@ -. "$PSScriptRoot/common/test-utils.ps1" - -function UpdateDependency([Parameter(Mandatory = $true)][string] $path, [string] $pattern = $null) -{ - $result = & "$PSScriptRoot/../scripts/update-dependency.ps1" -Path $path -Pattern $pattern - if (-not $?) - { - throw $result - } - $result -} - -$testDir = "$PSScriptRoot/testdata/dependencies" -if (-not (Test-Path $testDir)) -{ - New-Item $testDir -ItemType Directory -} - -$repoUrl = 'https://github.com/getsentry/github-workflows' - -# Find the latest latest version in this repo. We're intentionally using different code than `update-dependency.ps1` -# script uses to be able to catch issues, if any. -$currentVersion = (git -c 'versionsort.suffix=-' ls-remote --tags --sort='v:refname' $repoUrl ` - | Select-Object -Last 1 | Select-String -Pattern 'refs/tags/(.*)$').Matches.Groups[1].Value - -RunTest 'properties-file' { - $testFile = "$testDir/test.properties" - @("repo=$repoUrl", 'version = none') | Out-File $testFile - UpdateDependency $testFile - AssertEqual @("repo=$repoUrl", "version = $currentVersion") (Get-Content $testFile) -} - -RunTest 'version pattern match' { - $testFile = "$testDir/test.properties" - $repo = 'https://github.com/getsentry/sentry-cli' - @("repo=$repo", 'version=0') | Out-File $testFile - UpdateDependency $testFile '^0\.' - AssertEqual @("repo=$repo", 'version=0.28.0') (Get-Content $testFile) -} - -function _testOutput([string[]] $output) -{ - AssertContains $output 'originalTag=0' - AssertContains $output 'originalTag=0' - AssertContains $output 'latestTag=0.28.0' - AssertContains $output 'latestTagNice=v0.28.0' - AssertContains $output 'url=https://github.com/getsentry/sentry-cli' - AssertContains $output 'mainBranch=master' -} - -RunTest 'writes output' { - $testFile = "$testDir/test.properties" - $repo = 'https://github.com/getsentry/sentry-cli' - @("repo=$repo", 'version=0') | Out-File $testFile - $stdout = UpdateDependency $testFile '^0\.' - _testOutput $stdout -} - -RunTest 'writes to env:GITHUB_OUTPUT' { - $testFile = "$testDir/test.properties" - $repo = 'https://github.com/getsentry/sentry-cli' - @("repo=$repo", 'version=0') | Out-File $testFile - $outFile = "$testDir/outfile" - New-Item $outFile -ItemType File | Out-Null - try - { - $env:GITHUB_OUTPUT = $outFile - $stdout = UpdateDependency $testFile '^0\.' - Write-Host 'Testing standard output' - _testOutput $stdout - Write-Host 'Testing env:GITHUB_OUTPUT' - _testOutput (Get-Content $outFile) - } - finally - { - # Delete the file and unser the env variable - Remove-Item $outFile | Out-Null - Remove-Item env:GITHUB_OUTPUT | Out-Null - } -} - -# Note: without custom sorting, this would have yielded 'v1.7.31_gradle_plugin' -RunTest 'version sorting must work properly' { - $testFile = "$testDir/test.properties" - $repo = 'https://github.com/getsentry/sentry-java' - @("repo=$repo", 'version=0') | Out-File $testFile - UpdateDependency $testFile '^v?[123].*$' - AssertEqual @("repo=$repo", 'version=3.2.1') (Get-Content $testFile) -} - -RunTest 'will not update from a later release to an earlier release' { - $testFile = "$testDir/test.properties" - $repo = 'https://github.com/getsentry/sentry-java' - @("repo=$repo", 'version=999.0.0-beta.1') | Out-File $testFile - UpdateDependency $testFile - AssertEqual @("repo=$repo", 'version=999.0.0-beta.1') (Get-Content $testFile) -} - -RunTest 'powershell-script' { - $testFile = "$testDir/test.version" - '' | Out-File $testFile - $testScript = "$testDir/test.ps1" - @' -param([string] $action, [string] $value) -$file = "$PSScriptRoot/test.version" -switch ($action) -{ - "get-version" { Get-Content $file } - "get-repo" { -'@ + '"' + $repoUrl + '"' + @' - } - "set-version" { $value | Out-File $file } - Default { throw "Unknown action $action" } -} -'@ | Out-File $testScript - UpdateDependency $testScript - AssertEqual $currentVersion (Get-Content $testFile) -} - -RunTest 'bash-script' { - $testFile = "$testDir/test.version" - '' | Out-File $testFile - $testScript = "$testDir/test.sh" - @' -#!/usr/bin/env bash -set -euo pipefail -cd $(dirname "$0") -case $1 in -get-version) - cat test.version - ;; -get-repo) - echo -'@ + ' "' + $repoUrl + '"' + @' - ;; -set-version) - echo $2 > test.version - ;; -*) - echo "Unknown argument $1" - exit 1 - ;; -esac -'@ | Out-File $testScript - UpdateDependency $testScript - AssertEqual $currentVersion (Get-Content $testFile) -} -skipReason ($IsWindows ? 'on Windows' : '') - -RunTest 'powershell-script fails in get-version' { - $testScript = "$testDir/test.ps1" - @' -throw "Failure" -'@ | Out-File $testScript - - AssertFailsWith 'get-version | output: Failure' { UpdateDependency $testScript } -} - -RunTest 'bash-script fails in get-version' { - $testScript = "$testDir/test.sh" - @' -#!/usr/bin/env bash -echo "Failure" -exit 1 -'@ | Out-File $testScript - - AssertFailsWith 'get-version | output: Failure' { UpdateDependency $testScript } -} -skipReason ($IsWindows ? 'on Windows' : '') - -RunTest 'powershell-script fails in get-repo' { - $testScript = "$testDir/test.ps1" - @' -param([string] $action, [string] $value) -if ($action -eq "get-repo") -{ - throw "Failure" -} -'@ | Out-File $testScript - - AssertFailsWith 'get-repo | output: Failure' { UpdateDependency $testScript } -} - -RunTest 'bash-script fails in get-repo' { - $testScript = "$testDir/test.sh" - @' -#!/usr/bin/env bash -set -euo pipefail -case $1 in -get-version) - ;; -get-repo) - echo "Failure" - exit 1 -;; -esac -'@ | Out-File $testScript - - AssertFailsWith 'get-repo | output: Failure' { UpdateDependency $testScript } -} -skipReason ($IsWindows ? 'on Windows' : '') - -RunTest 'powershell-script fails in set-version' { - $testScript = "$testDir/test.ps1" - @' -param([string] $action, [string] $value) -switch ($action) -{ - "get-version" { '' } - "get-repo" { -'@ + '"' + $repoUrl + '"' + @' - } - "set-version" { throw "Failure" } -} -'@ | Out-File $testScript - - AssertFailsWith "set-version $currentVersion | output: Failure" { UpdateDependency $testScript } -} - -RunTest 'bash-script fails in set-version' { - $testScript = "$testDir/test.sh" - @' -#!/usr/bin/env bash -set -euo pipefail -cd $(dirname "$0") -case $1 in -get-version) - echo "" - ;; -get-repo) - echo -'@ + ' "' + $repoUrl + '"' + @' - ;; -set-version) - echo "Failure" - exit 1 - ;; -esac -'@ | Out-File $testScript - - AssertFailsWith "set-version $currentVersion | output: Failure" { UpdateDependency $testScript } -} -skipReason ($IsWindows ? 'on Windows' : '')