-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathupdate-dependency.Tests.ps1
More file actions
429 lines (370 loc) · 12.7 KB
/
update-dependency.Tests.ps1
File metadata and controls
429 lines (370 loc) · 12.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
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 using the same logic as update-dependency.ps1
. "$PSScriptRoot/../scripts/common.ps1"
[string[]]$tags = $(git ls-remote --refs --tags $repoUrl)
$tags = $tags | ForEach-Object { ($_ -split '\s+')[1] -replace '^refs/tags/', '' }
$tags = $tags -match '^v?([0-9.]+)$'
$tags = & "$PSScriptRoot/../scripts/sort-versions.ps1" $tags
$currentVersion = $tags[-1]
}
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
}
}
}
Context 'cmake-fetchcontent' {
BeforeAll {
$cmakeTestDir = "$testDir/cmake"
if (-not (Test-Path $cmakeTestDir)) {
New-Item $cmakeTestDir -ItemType Directory
}
}
It 'updates CMake file with explicit dependency name' {
$testFile = "$cmakeTestDir/sentry-explicit.cmake"
@'
include(FetchContent)
FetchContent_Declare(
sentry-native
GIT_REPOSITORY https://github.com/getsentry/sentry-native
GIT_TAG v0.9.1
GIT_SHALLOW FALSE
)
FetchContent_MakeAvailable(sentry-native)
'@ | Out-File $testFile
UpdateDependency "$testFile#sentry-native"
$content = Get-Content $testFile -Raw
$content | Should -Not -Match 'v0.9.1'
$content | Should -Match 'GIT_TAG \d+\.\d+\.\d+'
$content | Should -Match 'GIT_REPOSITORY https://github.com/getsentry/sentry-native'
}
It 'auto-detects single FetchContent dependency' {
$testFile = "$cmakeTestDir/sentry-auto.cmake"
@'
include(FetchContent)
FetchContent_Declare(
sentry-native
GIT_REPOSITORY https://github.com/getsentry/sentry-native
GIT_TAG v0.9.0
GIT_SHALLOW FALSE
)
FetchContent_MakeAvailable(sentry-native)
'@ | Out-File $testFile
UpdateDependency $testFile
$content = Get-Content $testFile -Raw
$content | Should -Not -Match 'v0.9.0'
$content | Should -Match 'GIT_TAG \d+\.\d+\.\d+'
}
It 'updates from hash to newer tag preserving hash format' {
$testFile = "$cmakeTestDir/sentry-hash.cmake"
@'
include(FetchContent)
FetchContent_Declare(
sentry-native
GIT_REPOSITORY https://github.com/getsentry/sentry-native
GIT_TAG a64d5bd8ee130f2cda196b6fa7d9b65bfa6d32e2 # 0.9.1
GIT_SHALLOW FALSE
)
FetchContent_MakeAvailable(sentry-native)
'@ | Out-File $testFile
UpdateDependency $testFile
$content = Get-Content $testFile -Raw
# Should update to a new hash with tag comment
$content | Should -Match 'GIT_TAG [a-f0-9]{40} # \d+\.\d+\.\d+'
$content | Should -Not -Match 'a64d5bd8ee130f2cda196b6fa7d9b65bfa6d32e2'
}
It 'handles multiple dependencies with explicit selection' {
$testFile = "$cmakeTestDir/multiple-deps.cmake"
@'
include(FetchContent)
FetchContent_Declare(
sentry-native
GIT_REPOSITORY https://github.com/getsentry/sentry-native
GIT_TAG v0.9.1
)
FetchContent_Declare(
googletest
GIT_REPOSITORY https://github.com/google/googletest
GIT_TAG v1.14.0
)
FetchContent_MakeAvailable(sentry-native googletest)
'@ | Out-File $testFile
UpdateDependency "$testFile#googletest"
$content = Get-Content $testFile -Raw
# sentry-native should remain unchanged
$content | Should -Match 'sentry-native[\s\S]*GIT_TAG v0\.9\.1'
# googletest should be updated
$content | Should -Match 'googletest[\s\S]*GIT_TAG v1\.\d+\.\d+'
$content | Should -Not -Match 'googletest[\s\S]*GIT_TAG v1\.14\.0'
}
It 'outputs correct GitHub Actions variables' {
$testFile = "$cmakeTestDir/output-test.cmake"
@'
include(FetchContent)
FetchContent_Declare(
sentry-native
GIT_REPOSITORY https://github.com/getsentry/sentry-native
GIT_TAG v0.9.0
)
'@ | Out-File $testFile
$output = UpdateDependency $testFile
# Join output lines for easier searching
$outputText = $output -join "`n"
$outputText | Should -Match 'originalTag=v0\.9\.0'
$outputText | Should -Match 'latestTag=\d+\.\d+\.\d+'
$outputText | Should -Match 'url=https://github.com/getsentry/sentry-native'
$outputText | Should -Match 'mainBranch=master'
}
It 'respects version patterns' {
$testFile = "$cmakeTestDir/pattern-test.cmake"
@'
include(FetchContent)
FetchContent_Declare(
sentry-native
GIT_REPOSITORY https://github.com/getsentry/sentry-native
GIT_TAG v0.8.0
)
'@ | Out-File $testFile
# Limit to 0.9.x versions
UpdateDependency $testFile '^v?0\.9\.'
$content = Get-Content $testFile -Raw
$content | Should -Match 'GIT_TAG 0\.9\.\d+'
$content | Should -Not -Match 'v0\.8\.0'
}
It 'fails on multiple dependencies without explicit name' {
$testFile = "$cmakeTestDir/multi-fail.cmake"
@'
include(FetchContent)
FetchContent_Declare(sentry-native GIT_REPOSITORY https://github.com/getsentry/sentry-native GIT_TAG v0.9.1)
FetchContent_Declare(googletest GIT_REPOSITORY https://github.com/google/googletest GIT_TAG v1.14.0)
'@ | Out-File $testFile
{ UpdateDependency $testFile } | Should -Throw '*Multiple FetchContent declarations found*'
}
It 'fails on missing dependency' {
$testFile = "$cmakeTestDir/missing-dep.cmake"
@'
include(FetchContent)
FetchContent_Declare(
sentry-native
GIT_REPOSITORY https://github.com/getsentry/sentry-native
GIT_TAG v0.9.1
)
'@ | Out-File $testFile
{ UpdateDependency "$testFile#nonexistent" } | Should -Throw "*FetchContent_Declare for 'nonexistent' not found*"
}
}
}