Skip to content

Commit 03fbc3b

Browse files
Cleanup even when tests fail
1 parent 395a7e2 commit 03fbc3b

File tree

1 file changed

+67
-67
lines changed

1 file changed

+67
-67
lines changed

test/GitInfoTest.ps1

Lines changed: 67 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -20,94 +20,94 @@ $tempPath = [System.IO.Path]::GetTempPath()
2020
$testRepoPath = Join-Path $tempPath $name
2121
New-Item -ItemType Directory -Path $testRepoPath | Out-Null
2222

23-
# Create a react app using create-react-app
24-
Set-Location $testRepoPath
25-
npx create-react-app .
26-
if (-not $?) {
27-
throw 'Unable to create react app'
28-
}
29-
30-
# Install the package
31-
Write-Output "Installing local package from $repoPath..."
32-
yarn add $repoPath
33-
if (-not $?) {
34-
throw 'Local package installation failed.'
35-
}
36-
37-
38-
Write-Output '------------------- Test 1 -------------------'
23+
try {
24+
# Create a react app using create-react-app
25+
Set-Location $testRepoPath
26+
npx create-react-app .
27+
if (-not $?) {
28+
throw 'Unable to create react app'
29+
}
3930

40-
$testFile = '01.test.js'
41-
$testScript = Join-Path $testRepoPath "src/$testFile"
42-
Copy-Item (Join-Path $scriptsPath $testFile) $testScript
31+
# Install the package
32+
Write-Output "Installing local package from $repoPath..."
33+
yarn add $repoPath
34+
if (-not $?) {
35+
throw 'Local package installation failed.'
36+
}
4337

44-
$commandError = $false
4538

46-
git tag 'hello-world'
47-
$commandError = $commandError -or -not $?
48-
git tag 'hello-world-again'
49-
$commandError = $commandError -or -not $?
39+
Write-Output '------------------- Test 1 -------------------'
5040

51-
if ($commandError) {
52-
throw 'Unable to run git commands.'
53-
}
41+
$testFile = '01.test.js'
42+
$testScript = Join-Path $testRepoPath "src/$testFile"
43+
Copy-Item (Join-Path $scriptsPath $testFile) $testScript
5444

55-
yarn test
56-
if (-not $?) {
57-
throw "One or more tests failed. Exit code = $LASTEXITCODE"
58-
}
45+
$commandError = $false
5946

60-
Remove-Item -Force $testScript
47+
git tag 'hello-world'
48+
$commandError = $commandError -or -not $?
49+
git tag 'hello-world-again'
50+
$commandError = $commandError -or -not $?
6151

52+
if ($commandError) {
53+
throw 'Unable to run git commands.'
54+
}
6255

63-
Write-Output '------------------- Test 2 -------------------'
56+
yarn test
57+
if (-not $?) {
58+
throw "One or more tests failed. Exit code = $LASTEXITCODE"
59+
}
6460

65-
$testFile = '02.test.js'
66-
$testScript = Join-Path $testRepoPath "src/$testFile"
67-
Copy-Item (Join-Path $scriptsPath $testFile) $testScript
61+
Remove-Item -Force $testScript
6862

69-
git commit --allow-empty -m 'Git commit message'
70-
$commandError = $commandError -or -not $?
7163

72-
if ($commandError) {
73-
throw 'Unable to run git commands.'
74-
}
64+
Write-Output '------------------- Test 2 -------------------'
7565

76-
yarn test
77-
if (-not $?) {
78-
throw "One or more tests failed. Exit code = $LASTEXITCODE"
79-
}
66+
$testFile = '02.test.js'
67+
$testScript = Join-Path $testRepoPath "src/$testFile"
68+
Copy-Item (Join-Path $scriptsPath $testFile) $testScript
8069

81-
Remove-Item -Force $testScript
70+
git commit --allow-empty -m 'Git commit message'
71+
$commandError = $commandError -or -not $?
8272

73+
if ($commandError) {
74+
throw 'Unable to run git commands.'
75+
}
8376

84-
Write-Output '------------------- Test 3 -------------------'
77+
yarn test
78+
if (-not $?) {
79+
throw "One or more tests failed. Exit code = $LASTEXITCODE"
80+
}
8581

86-
# tests for detached HEAD
82+
Remove-Item -Force $testScript
8783

88-
$testFile = '03.test.js'
89-
$testScript = Join-Path $testRepoPath "src/$testFile"
90-
Copy-Item (Join-Path $scriptsPath $testFile) $testScript
9184

92-
$firstCommit = (git rev-list --max-parents=0 HEAD | Out-String).Trim()
93-
$commandError = $commandError -or -not $?
94-
git checkout $firstCommit
95-
$commandError = $commandError -or -not $?
85+
Write-Output '------------------- Test 3 -------------------'
9686

97-
if ($commandError) {
98-
throw 'Unable to run git commands.'
99-
}
87+
# tests for detached HEAD
10088

101-
yarn test
102-
if (-not $?) {
103-
throw "One or more tests failed. Exit code = $LASTEXITCODE"
104-
}
89+
$testFile = '03.test.js'
90+
$testScript = Join-Path $testRepoPath "src/$testFile"
91+
Copy-Item (Join-Path $scriptsPath $testFile) $testScript
10592

106-
Remove-Item -Force $testScript
93+
$firstCommit = (git rev-list --max-parents=0 HEAD | Out-String).Trim()
94+
$commandError = $commandError -or -not $?
95+
git checkout $firstCommit
96+
$commandError = $commandError -or -not $?
10797

98+
if ($commandError) {
99+
throw 'Unable to run git commands.'
100+
}
108101

109-
Write-Output '------------------- Cleanup -------------------'
102+
yarn test
103+
if (-not $?) {
104+
throw "One or more tests failed. Exit code = $LASTEXITCODE"
105+
}
110106

111-
Pop-Location
112-
$env:CI = $oldEnvCI
113-
Remove-Item -Recurse -Force $testRepoPath
107+
Remove-Item -Force $testScript
108+
} finally {
109+
Write-Output 'Cleaning up...'
110+
Pop-Location
111+
$env:CI = $oldEnvCI
112+
Remove-Item -Recurse -Force $testRepoPath
113+
}

0 commit comments

Comments
 (0)