@@ -112,24 +112,37 @@ function Run-InitTest {
112112 exit 1
113113 }
114114
115- Set-Location $testDir
116- if (Test-Path " .codacy" ) {
117- Remove-Item - Recurse - Force " .codacy"
118- }
115+ # Store the original location
116+ $originalLocation = Get-Location
119117
120- if ($useToken ) {
121- if (-not $env: CODACY_API_TOKEN ) {
122- Write-Host " ❌ Skipping token-based test: CODACY_API_TOKEN not set"
123- return
118+ try {
119+ # Change to the test directory
120+ Set-Location $testDir
121+
122+ # Remove existing .codacy directory if it exists
123+ if (Test-Path " .codacy" ) {
124+ Remove-Item - Recurse - Force " .codacy"
125+ }
126+
127+ if ($useToken ) {
128+ if (-not $env: CODACY_API_TOKEN ) {
129+ Write-Host " ❌ Skipping token-based test: CODACY_API_TOKEN not set"
130+ return
131+ }
132+ & $CLI_PATH init -- api- token $env: CODACY_API_TOKEN -- organization troubleshoot- codacy- dev -- provider gh -- repository codacy- cli- test
133+ } else {
134+ & $CLI_PATH init
124135 }
125- & $CLI_PATH init -- api- token $env: CODACY_API_TOKEN -- organization troubleshoot- codacy- dev -- provider gh -- repository codacy- cli- test
126- } else {
127- & $CLI_PATH init
136+
137+ # Compare files using relative paths
138+ Compare-Files " expected" " .codacy" " Test $testName "
139+ Write-Host " ✅ Test $testName completed successfully"
140+ Write-Host " ----------------------------------------"
141+ }
142+ finally {
143+ # Always return to the original location
144+ Set-Location $originalLocation
128145 }
129-
130- Compare-Files " expected" " .codacy" " Test $testName "
131- Write-Host " ✅ Test $testName completed successfully"
132- Write-Host " ----------------------------------------"
133146}
134147
135148# Run both tests
0 commit comments