-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathtest.Tests.ps1
More file actions
256 lines (222 loc) · 10.9 KB
/
Copy pathtest.Tests.ps1
File metadata and controls
256 lines (222 loc) · 10.9 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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
<#
.SYNOPSIS
Pester 5.x tests for the Electron sample and guide workflow.
.DESCRIPTION
Phase 1: Follows the Electron guide from scratch — scaffolds an Electron app,
installs winapp, initializes workspace, creates and builds C#/C++ addons,
packages the app, and creates a signed MSIX package.
Phase 2: Quick install of the existing sample to verify it is not stale.
.PARAMETER WinappPath
Path to the winapp npm package (.tgz or directory) to install.
.PARAMETER SkipCleanup
Keep generated artifacts after test completes.
#>
param(
[string]$WinappPath,
[switch]$SkipCleanup
)
BeforeDiscovery {
$script:skip = $null -eq (Get-Command node -ErrorAction SilentlyContinue) -or $null -eq (Get-Command npm -ErrorAction SilentlyContinue)
}
Describe "Electron Sample" {
BeforeAll {
Import-Module "$PSScriptRoot\..\SampleTestHelpers.psm1" -Force
$script:skip = $null -eq (Get-Command node -ErrorAction SilentlyContinue) -or $null -eq (Get-Command npm -ErrorAction SilentlyContinue)
$script:sampleDir = $PSScriptRoot
$script:tempDir = $null
$script:appDir = $null
$script:resolvedPkg = $null
if (-not $script:skip) {
$script:resolvedPkg = Resolve-WinappCliPath -WinappPath $WinappPath
}
}
AfterAll {
Set-Location $script:sampleDir
if (-not $SkipCleanup) {
if ($script:tempDir) { Remove-TempTestDirectory -Path $script:tempDir }
Remove-Item -Path (Join-Path $script:sampleDir 'node_modules') -Recurse -Force -ErrorAction SilentlyContinue
}
}
Context "Phase 1: Electron Guide Workflow (from scratch)" {
BeforeAll {
if (-not $script:skip) {
$script:tempDir = New-TempTestDirectory -Prefix "electron-guide"
# Use a dedicated npm cache to avoid ECOMPROMISED errors in CI
$npmCacheDir = Join-Path $script:tempDir ".npm-cache"
$null = New-Item -ItemType Directory -Path $npmCacheDir -Force
$env:npm_config_cache = $npmCacheDir
}
}
It "Should create a new Electron app" -Skip:$script:skip {
Push-Location $script:tempDir
try {
$maxRetries = 3
$created = $false
for ($i = 1; $i -le $maxRetries; $i++) {
if ($i -gt 1) {
Remove-Item -Path (Join-Path $script:tempDir "electron-app") -Recurse -Force -ErrorAction SilentlyContinue
Invoke-Expression "npm cache clean --force" 2>$null
Start-Sleep -Seconds 2
}
Invoke-Expression "npx -y create-electron-app@latest electron-app"
if ($LASTEXITCODE -eq 0) { $created = $true; break }
}
$created | Should -Be $true -Because "Electron app creation should succeed within $maxRetries attempts"
$script:appDir = Join-Path $script:tempDir "electron-app"
$script:appDir | Should -Exist
# Electron 42+ no longer auto-downloads its binary during npm install.
# Use `npx install-electron` (the supported mechanism) to fetch it.
$electronExe = Join-Path $script:appDir "node_modules\electron\dist\electron.exe"
if (-not (Test-Path $electronExe)) {
Write-Host "Electron binary not found after scaffold — running npx install-electron..."
Push-Location $script:appDir
try {
& npx --yes install-electron 2>&1 | ForEach-Object { Write-Host $_ }
$LASTEXITCODE | Should -Be 0 -Because "install-electron must exit cleanly"
$electronExe | Should -Exist -Because "Electron binary should be present after install-electron"
} finally { Pop-Location }
}
} finally { Pop-Location }
}
It "Should configure package.json for MSIX" -Skip:$script:skip {
$pkgPath = Join-Path $script:appDir "package.json"
$pkg = Get-Content $pkgPath | ConvertFrom-Json
$pkg | Add-Member -MemberType NoteProperty -Name "displayName" -Value "WinApp Electron Test" -Force
$pkg | Add-Member -MemberType NoteProperty -Name "description" -Value "Test app for winapp CLI" -Force
if ([string]::IsNullOrEmpty($pkg.version)) { $pkg.version = "1.0.0" }
$pkg | ConvertTo-Json -Depth 10 | Set-Content $pkgPath
$pkgPath | Should -Exist
}
It "Should install winapp as a local devDependency" -Skip:$script:skip {
Push-Location $script:appDir
try {
Install-WinappNpmPackage -PackagePath $script:resolvedPkg
Join-Path $script:appDir "node_modules\.bin\winapp.cmd" | Should -Exist
} finally { Pop-Location }
}
It "Should initialize winapp workspace" -Skip:$script:skip {
Push-Location $script:appDir
try {
Invoke-WinappCommand -Arguments "init . --use-defaults --setup-sdks=stable"
} finally { Pop-Location }
}
It "Should create workspace files" -Skip:$script:skip {
Join-Path $script:appDir ".winapp" | Should -Exist
Join-Path $script:appDir "winapp.yaml" | Should -Exist
Join-Path $script:appDir "Package.appxmanifest" | Should -Exist
}
It "Should create a C++ native addon" -Skip:$script:skip {
Push-Location $script:appDir
try {
Invoke-WinappCommand -Arguments "node create-addon --template cpp --name testCppAddon"
Join-Path $script:appDir "testCppAddon" | Should -Exist
Join-Path $script:appDir "testCppAddon\binding.gyp" | Should -Exist
} finally { Pop-Location }
}
It "Should create a C# native addon" -Skip:$script:skip {
Push-Location $script:appDir
try {
Invoke-WinappCommand -Arguments "node create-addon --template cs --name testCsAddon"
Join-Path $script:appDir "testCsAddon" | Should -Exist
Join-Path $script:appDir "testCsAddon\testCsAddon.csproj" | Should -Exist
} finally { Pop-Location }
}
It "Should build the C++ addon" -Skip:$script:skip {
Push-Location $script:appDir
try {
$output = Invoke-Expression "npx node-gyp clean configure build --directory=testCppAddon --verbose 2>&1"
$output | ForEach-Object { Write-Host $_ }
$LASTEXITCODE | Should -Be 0
} finally { Pop-Location }
}
It "Should build the C# addon" -Skip:$script:skip {
Push-Location $script:appDir
try {
Invoke-Expression "npm run build-testCsAddon"
$LASTEXITCODE | Should -Be 0
} finally { Pop-Location }
}
It "Should download the Electron binary" -Skip:$script:skip {
# Electron 42+ no longer downloads its binary during `npm install`.
# Use `npx --yes install-electron` to fetch it reliably in CI.
$exe = Join-Path $script:appDir "node_modules\electron\dist\electron.exe"
if (Test-Path $exe) {
Write-Host "Electron binary already present — skipping download."
} else {
Push-Location $script:appDir
try {
Write-Host "Running npx install-electron to fetch Electron binary..."
& npx --yes install-electron 2>&1 | ForEach-Object { Write-Host $_ }
$LASTEXITCODE | Should -Be 0 -Because "install-electron must succeed"
} finally { Pop-Location }
}
$exe | Should -Exist -Because "Electron binary is required for add-electron-debug-identity"
}
It "Should add Electron debug identity" -Skip:$script:skip {
Push-Location $script:appDir
try {
Invoke-WinappCommand -Arguments "node add-electron-debug-identity --no-install"
} finally { Pop-Location }
}
It "Should package the Electron app" -Skip:$script:skip {
Push-Location $script:appDir
try {
Invoke-Expression "npm run package"
$LASTEXITCODE | Should -Be 0
$script:outDir = Join-Path $script:appDir "out"
$script:outDir | Should -Exist
$script:appPackageDir = (Get-ChildItem -Path $script:outDir -Directory | Select-Object -First 1).FullName
$script:appPackageDir | Should -Not -BeNullOrEmpty
} finally { Pop-Location }
}
It "Should register app with winapp run --no-launch" -Skip:$script:skip {
Push-Location $script:appDir
try {
Invoke-WinappCommand -Arguments "run `"$($script:appPackageDir)`" --no-launch"
} finally { Pop-Location }
}
It "Should generate a development certificate" -Skip:$script:skip {
Push-Location $script:appDir
try {
Invoke-WinappCommand -Arguments "cert generate"
Join-Path $script:appDir "devcert.pfx" | Should -Exist
} finally { Pop-Location }
}
It "Should package as MSIX" -Skip:$script:skip {
Push-Location $script:appDir
try {
$certPath = Join-Path $script:appDir "devcert.pfx"
Invoke-WinappCommand -Arguments "pack `"$($script:appPackageDir)`" --cert `"$certPath`""
Get-ChildItem -Path $script:appDir -Filter "*.msix" | Should -Not -BeNullOrEmpty
} finally { Pop-Location }
}
}
Context "Phase 2: Sample Build Check" {
It "Should install sample dependencies" -Skip:$script:skip {
Push-Location $script:sampleDir
try {
Invoke-Expression "npm install --ignore-scripts"
$LASTEXITCODE | Should -Be 0
} finally { Pop-Location }
}
It "Should have node_modules" -Skip:$script:skip {
Join-Path $script:sampleDir 'node_modules' | Should -Exist
}
It "Should have package.json" -Skip:$script:skip {
Join-Path $script:sampleDir 'package.json' | Should -Exist
}
It "Should have forge.config.js" -Skip:$script:skip {
Join-Path $script:sampleDir 'forge.config.js' | Should -Exist
}
It "Should have appxmanifest.xml" -Skip:$script:skip {
Join-Path $script:sampleDir 'appxmanifest.xml' | Should -Exist
}
It "Should build the C# addon" -Skip:$script:skip {
Push-Location $script:sampleDir
try {
Invoke-Expression "npm run build-csAddon"
$LASTEXITCODE | Should -Be 0
} finally { Pop-Location }
}
}
}