-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathrun.ps1
More file actions
169 lines (145 loc) · 5.69 KB
/
run.ps1
File metadata and controls
169 lines (145 loc) · 5.69 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
# Stop on first error
$ErrorActionPreference = "Stop"
# Get the absolute path of the script's directory and CLI path
$SCRIPT_DIR = Split-Path -Parent $MyInvocation.MyCommand.Path
$CLI_PATH = Join-Path (Get-Location) "cli-v2.exe"
Write-Host "Script directory: $SCRIPT_DIR"
Write-Host "Current working directory: $(Get-Location)"
# Check if API token is provided for token-based test
if (-not $env:CODACY_API_TOKEN) {
Write-Host "Warning: CODACY_API_TOKEN environment variable is not set. Token-based test will be skipped."
}
# Function to normalize and sort configuration values
function Normalize-Config {
param ([string]$file)
$ext = [System.IO.Path]::GetExtension($file).TrimStart('.')
switch ($ext) {
{ $_ -in @('yaml', 'yml') } {
# For YAML files, preserve structure and sort within sections
$content = Get-Content $file
$output = @()
$currentSection = ""
$sectionContent = @()
foreach ($line in $content) {
$line = $line.Trim()
if ($line -match '^(\w+):$') {
# If we have a previous section, sort and add its content
if ($currentSection -and $sectionContent.Count -gt 0) {
$output += $currentSection
$output += ($sectionContent | Sort-Object)
$sectionContent = @()
}
$currentSection = $line
}
elseif ($line -match '^\s*-\s*') {
$sectionContent += $line
}
elseif ($line -match '\S') {
$output += $line
}
}
# Add the last section
if ($currentSection -and $sectionContent.Count -gt 0) {
$output += $currentSection
$output += ($sectionContent | Sort-Object)
}
# Add empty line at the end if the original had one
if ($content[-1] -match '^\s*$') {
$output += ""
}
$output
}
{ $_ -in @('rc', 'conf', 'ini', 'xml') } {
Get-Content $file | ForEach-Object {
if ($_ -match '^[^#].*=.*,') {
$parts = $_ -split '='
$values = $parts[1] -split ',' | Sort-Object
"$($parts[0])=$($values -join ',')"
} else { $_ }
} | Sort-Object
}
default { Get-Content $file | Sort-Object }
}
}
function Compare-Files {
param (
[string]$expectedDir,
[string]$actualDir,
[string]$label
)
# Compare files
Get-ChildItem -Path $expectedDir -File | ForEach-Object {
$actualFile = Join-Path $actualDir $_.Name
if (-not (Test-Path $actualFile)) {
Write-Host "❌ $label/$($_.Name) does not exist in actual output"
Write-Host "Expected: $($_.FullName)"
Write-Host "Actual should be: $actualFile"
exit 1
}
$expectedContent = Normalize-Config $_.FullName
$actualContent = Normalize-Config $actualFile
# Compare line by line
$diff = Compare-Object $expectedContent $actualContent -PassThru
if ($diff) {
Write-Host "❌ $label/$($_.Name) does not match expected"
Write-Host "=== Expected (normalized) ==="
$expectedContent
Write-Host "=== Actual (normalized) ==="
$actualContent
Write-Host "=== Diff ==="
$diff
Write-Host "==================="
exit 1
}
Write-Host "✅ $label/$($_.Name) matches expected"
}
# Compare subdirectories
Get-ChildItem -Path $expectedDir -Directory | Where-Object { $_.Name -ne "logs" } | ForEach-Object {
$actualSubDir = Join-Path $actualDir $_.Name
if (-not (Test-Path $actualSubDir)) {
Write-Host "❌ Directory $label/$($_.Name) does not exist in actual output"
Write-Host "Expected: $($_.FullName)"
Write-Host "Actual should be: $actualSubDir"
exit 1
}
Compare-Files $_.FullName $actualSubDir "$label/$($_.Name)"
}
}
function Run-InitTest {
param (
[string]$testDir,
[string]$testName,
[bool]$useToken
)
Write-Host "Running test: $testName"
if (-not (Test-Path $testDir)) {
Write-Host "❌ Test directory does not exist: $testDir"
exit 1
}
$originalLocation = Get-Location
try {
Set-Location $testDir
if (Test-Path ".codacy") { Remove-Item -Recurse -Force ".codacy" }
if ($useToken) {
if (-not $env:CODACY_API_TOKEN) {
Write-Host "❌ Skipping token-based test: CODACY_API_TOKEN not set"
return
}
& $CLI_PATH init --api-token $env:CODACY_API_TOKEN --organization troubleshoot-codacy-dev --provider gh --repository codacy-cli-test
} else {
& $CLI_PATH init
}
Compare-Files "expected" ".codacy" "Test $testName"
Write-Host "✅ Test $testName completed successfully"
Write-Host "----------------------------------------"
}
finally {
Set-Location $originalLocation
}
}
# Run tests
Write-Host "Starting integration tests..."
Write-Host "----------------------------------------"
Run-InitTest (Join-Path $SCRIPT_DIR "init-without-token") "init-without-token" $false
Run-InitTest (Join-Path $SCRIPT_DIR "init-with-token") "init-with-token" $true
Write-Host "All tests completed successfully! 🎉"