1+ # Stop on first error
2+ $ErrorActionPreference = " Stop"
3+
4+ # Get the absolute path of the script's directory
5+ $SCRIPT_DIR = Split-Path - Parent $MyInvocation.MyCommand.Path
6+ $CLI_PATH = Join-Path (Get-Location ) " cli-v2.exe"
7+
8+ Write-Host " Script directory: $SCRIPT_DIR "
9+ Write-Host " Current working directory: $ ( Get-Location ) "
10+
11+ # Check if API token is provided for token-based test
12+ if (-not $env: CODACY_API_TOKEN ) {
13+ Write-Host " Warning: CODACY_API_TOKEN environment variable is not set. Token-based test will be skipped."
14+ }
15+
16+ # Function to normalize and sort configuration values
17+ function Normalize-Config {
18+ param (
19+ [string ]$file
20+ )
21+
22+ $ext = [System.IO.Path ]::GetExtension($file ).TrimStart(' .' )
23+
24+ switch ($ext ) {
25+ { $_ -in @ (' yaml' , ' yml' ) } {
26+ # For YAML files, use yq to sort
27+ # Note: Requires yq to be installed on Windows
28+ yq e ' .' $file | Sort-Object
29+ }
30+ { $_ -in @ (' rc' , ' conf' , ' ini' , ' xml' ) } {
31+ # For other config files, sort values after '=' and keep other lines
32+ Get-Content $file | ForEach-Object {
33+ if ($_ -match ' ^[^#].*=.*,' ) {
34+ $parts = $_ -split ' ='
35+ $values = $parts [1 ] -split ' ,' | Sort-Object
36+ " $ ( $parts [0 ]) =$ ( $values -join ' ,' ) "
37+ } else {
38+ $_
39+ }
40+ } | Sort-Object
41+ }
42+ default {
43+ # For other files, just sort
44+ Get-Content $file | Sort-Object
45+ }
46+ }
47+ }
48+
49+ function Compare-Files {
50+ param (
51+ [string ]$expectedDir ,
52+ [string ]$actualDir ,
53+ [string ]$label
54+ )
55+
56+ # Compare files in current directory
57+ Get-ChildItem - Path $expectedDir - File | ForEach-Object {
58+ $filename = $_.Name
59+ $actualFile = Join-Path $actualDir $filename
60+
61+ if (-not (Test-Path $actualFile )) {
62+ Write-Host " ❌ $label /$filename does not exist in actual output"
63+ Write-Host " Expected: $ ( $_.FullName ) "
64+ Write-Host " Actual should be: $actualFile "
65+ exit 1
66+ }
67+
68+ $expectedContent = Normalize- Config $_.FullName
69+ $actualContent = Normalize- Config $actualFile
70+
71+ if (Compare-Object $expectedContent $actualContent ) {
72+ Write-Host " ❌ $label /$filename does not match expected"
73+ Write-Host " === Expected (normalized) ==="
74+ $expectedContent
75+ Write-Host " === Actual (normalized) ==="
76+ $actualContent
77+ Write-Host " === Diff ==="
78+ Compare-Object $expectedContent $actualContent
79+ Write-Host " ==================="
80+ exit 1
81+ } else {
82+ Write-Host " ✅ $label /$filename matches expected"
83+ }
84+ }
85+
86+ # Compare subdirectories
87+ Get-ChildItem - Path $expectedDir - Directory | ForEach-Object {
88+ $dirname = $_.Name
89+ if ($dirname -eq " logs" ) { return }
90+
91+ $actualSubDir = Join-Path $actualDir $dirname
92+ if (-not (Test-Path $actualSubDir )) {
93+ Write-Host " ❌ Directory $label /$dirname does not exist in actual output"
94+ Write-Host " Expected: $ ( $_.FullName ) "
95+ Write-Host " Actual should be: $actualSubDir "
96+ exit 1
97+ }
98+ Compare-Files $_.FullName $actualSubDir " $label /$dirname "
99+ }
100+ }
101+
102+ function Run-InitTest {
103+ param (
104+ [string ]$testDir ,
105+ [string ]$testName ,
106+ [bool ]$useToken
107+ )
108+
109+ Write-Host " Running test: $testName "
110+ if (-not (Test-Path $testDir )) {
111+ Write-Host " ❌ Test directory does not exist: $testDir "
112+ exit 1
113+ }
114+
115+ Set-Location $testDir
116+ if (Test-Path " .codacy" ) {
117+ Remove-Item - Recurse - Force " .codacy"
118+ }
119+
120+ if ($useToken ) {
121+ if (-not $env: CODACY_API_TOKEN ) {
122+ Write-Host " ❌ Skipping token-based test: CODACY_API_TOKEN not set"
123+ return
124+ }
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
128+ }
129+
130+ Compare-Files " expected" " .codacy" " Test $testName "
131+ Write-Host " ✅ Test $testName completed successfully"
132+ Write-Host " ----------------------------------------"
133+ }
134+
135+ # Run both tests
136+ Write-Host " Starting integration tests..."
137+ Write-Host " ----------------------------------------"
138+
139+ # Test 1: Init without token
140+ Run- InitTest (Join-Path $SCRIPT_DIR " init-without-token" ) " init-without-token" $false
141+
142+ # Test 2: Init with token
143+ Run- InitTest (Join-Path $SCRIPT_DIR " init-with-token" ) " init-with-token" $true
144+
145+ Write-Host " All tests completed successfully! 🎉"
0 commit comments