-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathci.ps1
More file actions
227 lines (191 loc) · 8.34 KB
/
ci.ps1
File metadata and controls
227 lines (191 loc) · 8.34 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#!/usr/bin/env pwsh
<#
.SYNOPSIS
CI script for the notes project — runs linting and tests.
#>
Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'
$script:exitCode = 0
$script:results = @()
function Write-Step($msg) { Write-Host "`n▶ $msg" -ForegroundColor Cyan }
function Write-Pass($msg) { Write-Host " ✅ $msg" -ForegroundColor Green }
function Write-Fail($msg) { Write-Host " ❌ $msg" -ForegroundColor Red; $script:exitCode = 1 }
function Get-PythonCommand {
foreach ($candidate in @('python', 'python3')) {
if (Get-Command $candidate -ErrorAction SilentlyContinue) {
return $candidate
}
}
throw "Python 3 is required to run the GUI tests."
}
function Get-VenvPythonPath {
param([string]$VenvPath)
if ($IsWindows -or $env:OS -eq 'Windows_NT') {
return Join-Path $VenvPath 'Scripts\python.exe'
}
return Join-Path $VenvPath 'bin/python'
}
function Get-PlatformSuffix {
if ($IsWindows -or $env:OS -eq 'Windows_NT') {
return 'windows'
}
if ($IsMacOS) {
return 'macos'
}
return 'linux'
}
function Test-PythonImport {
param(
[string]$PythonCommand,
[string]$ModuleName
)
& $PythonCommand -c "import $ModuleName" 2>$null
return $LASTEXITCODE -eq 0
}
function Test-PythonImportFromPath {
param(
[string]$PythonCommand,
[string]$ModuleName,
[string]$AdditionalPath
)
& $PythonCommand -c "import sys; sys.path.insert(0, r'$AdditionalPath'); import $ModuleName" 2>$null
return $LASTEXITCODE -eq 0
}
# ── 1. Install modules ──────────────────────────────────────────────
Write-Step "Installing required modules"
foreach ($mod in @('Pester', 'PSScriptAnalyzer')) {
if (-not (Get-Module -ListAvailable -Name $mod)) {
Write-Host " Installing $mod ..."
Install-Module -Name $mod -Force -Scope CurrentUser -SkipPublisherCheck
} else {
Write-Host " $mod already installed"
}
}
Import-Module Pester -MinimumVersion 5.0 -Force
Import-Module PSScriptAnalyzer -Force
# ── 2. PSScriptAnalyzer ─────────────────────────────────────────────
Write-Step "Running PSScriptAnalyzer on notes.ps1"
$excludeRules = @(
'PSUseShouldProcessForStateChangingFunctions'
'PSAvoidUsingWriteHost'
'PSAvoidUsingInvokeExpression'
'PSAvoidUsingConvertToSecureStringWithPlainText'
)
$analyzerResults = Invoke-ScriptAnalyzer -Path ./notes.ps1 -ExcludeRule $excludeRules -Severity Error, Warning
if ($analyzerResults) {
$errors = @($analyzerResults | Where-Object Severity -eq 'Error')
$warnings = @($analyzerResults | Where-Object Severity -eq 'Warning')
$analyzerResults | Format-Table -AutoSize RuleName, Severity, Line, Message
if ($errors.Count -gt 0) {
Write-Fail "PSScriptAnalyzer: $($errors.Count) error(s), $($warnings.Count) warning(s)"
$script:results += "Lint: FAIL ($($errors.Count) errors, $($warnings.Count) warnings)"
} else {
Write-Pass "PSScriptAnalyzer: 0 errors, $($warnings.Count) warning(s) (warnings are non-blocking)"
$script:results += "Lint: PASS (0 errors, $($warnings.Count) warnings)"
}
} else {
Write-Pass "PSScriptAnalyzer: clean"
$script:results += "Lint: PASS (clean)"
}
# ── 3. Pester tests ─────────────────────────────────────────────────
Write-Step "Running Pester tests"
$pesterConfig = New-PesterConfiguration
$pesterConfig.Run.Path = './notes.tests.ps1'
$pesterConfig.Run.Exit = $false
$pesterConfig.Run.PassThru = $true
$pesterConfig.Output.Verbosity = 'Detailed'
$pesterConfig.TestResult.Enabled = $true
$pesterConfig.TestResult.OutputPath = './testResults.xml'
$pesterConfig.TestResult.OutputFormat = 'NUnitXml'
$savedPref = $ErrorActionPreference
$ErrorActionPreference = 'Continue'
$pesterResult = Invoke-Pester -Configuration $pesterConfig
$ErrorActionPreference = $savedPref
if ($pesterResult.FailedCount -gt 0) {
Write-Fail "Pester: $($pesterResult.FailedCount) failed out of $($pesterResult.TotalCount) tests"
$script:results += "Tests: FAIL ($($pesterResult.FailedCount)/$($pesterResult.TotalCount) failed)"
} else {
Write-Pass "Pester: all $($pesterResult.TotalCount) tests passed"
$script:results += "Tests: PASS ($($pesterResult.TotalCount) tests)"
}
# ── 4. Python GUI tests ────────────────────────────────────────────────
if (Test-Path ./requirements-gui.txt) {
Write-Step "Installing GUI Python dependencies"
$python = Get-PythonCommand
$platformSuffix = Get-PlatformSuffix
$venvPath = Join-Path (Get-Location) ".venv-gui-ci-$platformSuffix"
$venvPython = Get-VenvPythonPath -VenvPath $venvPath
$packagePath = Join-Path (Get-Location) ".python-packages-gui-ci-$platformSuffix"
$guiPython = $venvPython
$guiPythonPath = $null
$venvUsable = (Test-Path -LiteralPath $venvPython) -and (Test-PythonImport -PythonCommand $venvPython -ModuleName 'pip')
if (-not $venvUsable) {
& $python -m venv $venvPath
$venvUsable = $LASTEXITCODE -eq 0 -and (Test-Path -LiteralPath $venvPython) -and (Test-PythonImport -PythonCommand $venvPython -ModuleName 'pip')
if (-not $venvUsable) {
Write-Host " Falling back to a repo-local Python package directory because venv creation failed." -ForegroundColor Yellow
$guiPython = $python
$guiPythonPath = $packagePath
}
}
if ($script:exitCode -eq 0) {
$dependenciesInstalled = if ($null -eq $guiPythonPath) {
Test-PythonImport -PythonCommand $guiPython -ModuleName 'PySide6'
} else {
Test-PythonImportFromPath -PythonCommand $guiPython -ModuleName 'PySide6' -AdditionalPath $guiPythonPath
}
if (-not $dependenciesInstalled) {
if ($null -eq $guiPythonPath) {
& $guiPython -m pip install --disable-pip-version-check -r ./requirements-gui.txt
} else {
New-Item -ItemType Directory -Path $guiPythonPath -Force | Out-Null
& $guiPython -m pip install --disable-pip-version-check --target $guiPythonPath -r ./requirements-gui.txt
}
if ($LASTEXITCODE -ne 0) {
Write-Fail "GUI dependencies install failed"
$script:results += "GUI: FAIL (dependency install failed)"
} else {
Write-Pass "GUI dependencies installed"
}
} else {
Write-Pass "GUI dependencies already installed"
}
}
if ($script:exitCode -eq 0) {
Write-Step "Running Python GUI tests"
$savedQtPlatform = $env:QT_QPA_PLATFORM
$savedPythonPath = $env:PYTHONPATH
$env:QT_QPA_PLATFORM = 'offscreen'
if ($null -ne $guiPythonPath) {
if ([string]::IsNullOrEmpty($savedPythonPath)) {
$env:PYTHONPATH = $guiPythonPath
} else {
$env:PYTHONPATH = $guiPythonPath + [System.IO.Path]::PathSeparator + $savedPythonPath
}
}
& $guiPython -m unittest discover -s ./gui_tests -v
$guiExitCode = $LASTEXITCODE
if ($null -eq $savedQtPlatform) {
Remove-Item Env:QT_QPA_PLATFORM -ErrorAction SilentlyContinue
} else {
$env:QT_QPA_PLATFORM = $savedQtPlatform
}
if ($null -eq $savedPythonPath) {
Remove-Item Env:PYTHONPATH -ErrorAction SilentlyContinue
} else {
$env:PYTHONPATH = $savedPythonPath
}
if ($guiExitCode -ne 0) {
Write-Fail "GUI tests failed"
$script:results += "GUI: FAIL (Python tests)"
} else {
Write-Pass "GUI tests passed"
$script:results += "GUI: PASS (Python tests)"
}
}
}
# ── Summary ──────────────────────────────────────────────────────────
Write-Host "`n━━━ Summary ━━━" -ForegroundColor Yellow
$script:results | ForEach-Object { Write-Host " $_" }
Write-Host ""
exit $script:exitCode