-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest-package.ps1
More file actions
197 lines (166 loc) · 5.6 KB
/
test-package.ps1
File metadata and controls
197 lines (166 loc) · 5.6 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
# Comprehensive test suite for the opencode Chocolatey package
param(
[switch]$SkipUninstall,
[switch]$Verbose
)
$ErrorActionPreference = 'Stop'
$VerbosePreference = if ($Verbose) { 'Continue' } else { 'SilentlyContinue' }
function Test-Command {
param([string]$Command)
$null = Get-Command $Command -ErrorAction SilentlyContinue
return $?
}
function Write-TestResult {
param(
[string]$Test,
[bool]$Passed,
[string]$Details = ""
)
$symbol = if ($Passed) { "[PASS]" } else { "[FAIL]" }
$color = if ($Passed) { "Green" } else { "Red" }
Write-Host "$symbol $Test" -ForegroundColor $color
if ($Details -and (-not $Passed -or $Verbose)) {
Write-Host " $Details" -ForegroundColor Gray
}
}
Write-Host "`nopencode Chocolatey Package Test Suite" -ForegroundColor Cyan
Write-Host "======================================" -ForegroundColor Cyan
# Test 1: Package file exists
$nuspecPath = Join-Path $PSScriptRoot 'opencode.nuspec'
$packageExists = Test-Path $nuspecPath
Write-TestResult "Package specification exists" $packageExists
if (-not $packageExists) {
Write-Host "`nCritical error: opencode.nuspec not found" -ForegroundColor Red
exit 1
}
# Test 2: Valid nuspec
try {
$nuspec = [xml](Get-Content $nuspecPath)
$version = $nuspec.package.metadata.version
$validNuspec = $true
Write-TestResult "Valid nuspec file" $validNuspec "Version: $version"
}
catch {
Write-TestResult "Valid nuspec file" $false $_.Exception.Message
exit 1
}
# Test 3: Checksum is not placeholder
$installScript = Get-Content (Join-Path $PSScriptRoot 'tools\chocolateyinstall.ps1') -Raw
$hasPlaceholder = $installScript -match 'PLACEHOLDER_CHECKSUM'
Write-TestResult "Checksum is set (not placeholder)" (-not $hasPlaceholder)
if ($hasPlaceholder) {
Write-Host "`nRun .\update.ps1 to set the checksum" -ForegroundColor Yellow
}
# Test 4: Required files exist
$requiredFiles = @(
'tools\chocolateyinstall.ps1',
'tools\chocolateyuninstall.ps1'
)
$allFilesExist = $true
foreach ($file in $requiredFiles) {
$filePath = Join-Path $PSScriptRoot $file
$exists = Test-Path $filePath
if (-not $exists) {
$allFilesExist = $false
Write-Verbose "Missing: $file"
}
}
Write-TestResult "All required files exist" $allFilesExist
# Test 5: Pack the package
Write-Host "`nPacking package..." -ForegroundColor Yellow
try {
$output = choco pack 2>&1
$packSuccess = $LASTEXITCODE -eq 0
$packageFile = Get-ChildItem -Path $PSScriptRoot -Filter "opencode.$version.nupkg" | Select-Object -First 1
Write-TestResult "Package creation" $packSuccess
if ($packageFile) {
Write-Verbose "Created: $($packageFile.Name) ($('{0:N2} MB' -f ($packageFile.Length / 1MB)))"
}
}
catch {
Write-TestResult "Package creation" $false $_.Exception.Message
exit 1
}
# Test 6: Install the package
Write-Host "`nInstalling package..." -ForegroundColor Yellow
try {
# First uninstall if already installed
if (Test-Command 'opencode.exe') {
Write-Verbose "Uninstalling existing installation..."
choco uninstall opencode -y --force 2>&1 | Out-Null
}
$installOutput = choco install opencode -dvy -s . --force 2>&1
$installSuccess = $LASTEXITCODE -eq 0
Write-TestResult "Package installation" $installSuccess
if (-not $installSuccess) {
$errors = $installOutput | Where-Object { $_ -match 'ERROR|FATAL' }
foreach ($err in $errors) {
Write-Verbose " $err"
}
}
}
catch {
Write-TestResult "Package installation" $false $_.Exception.Message
}
# Test 7: Verify installation
$commandExists = Test-Command 'opencode.exe'
Write-TestResult "opencode command available" $commandExists
# Test 8: Version check
if ($commandExists) {
try {
$installedVersion = & opencode --version 2>&1
$versionMatches = $installedVersion -match $version
Write-TestResult "Version verification" $versionMatches "Expected: $version, Got: $installedVersion"
}
catch {
Write-TestResult "Version verification" $false $_.Exception.Message
}
}
# Test 9: PATH verification
$inPath = $env:PATH -split ';' | Where-Object { $_ -match 'chocolatey\\bin' }
Write-TestResult "Chocolatey bin in PATH" ($null -ne $inPath)
# Test 10: Uninstall test
if (-not $SkipUninstall -and $commandExists) {
Write-Host "`nTesting uninstallation..." -ForegroundColor Yellow
try {
choco uninstall opencode -y 2>&1
$uninstallSuccess = $LASTEXITCODE -eq 0
# Verify removal
$stillExists = Test-Command 'opencode.exe'
$cleanUninstall = $uninstallSuccess -and (-not $stillExists)
Write-TestResult "Package uninstallation" $cleanUninstall
}
catch {
Write-TestResult "Package uninstallation" $false $_.Exception.Message
}
}
# Test 10: Update script exists and is executable
$updateScriptPath = Join-Path $PSScriptRoot 'update.ps1'
$updateScriptExists = Test-Path $updateScriptPath
Write-TestResult "Update script exists" $updateScriptExists
# Summary
Write-Host "`nTest Summary" -ForegroundColor Cyan
Write-Host "============" -ForegroundColor Cyan
$totalTests = 10
$passedTests = @(
$packageExists,
$validNuspec,
(-not $hasPlaceholder),
$allFilesExist,
$packSuccess,
$installSuccess,
$commandExists,
$versionMatches,
$cleanUninstall,
$updateScriptExists
) | Where-Object { $_ -eq $true } | Measure-Object | Select-Object -ExpandProperty Count
$allPassed = $passedTests -eq $totalTests
Write-Host "Passed: $passedTests/$totalTests" -ForegroundColor $(if ($allPassed) { "Green" } else { "Yellow" })
if (-not $allPassed) {
Write-Host "`nSome tests failed. Review the output above for details." -ForegroundColor Yellow
exit 1
}
else {
Write-Host "`nAll tests passed!" -ForegroundColor Green
exit 0
}