-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathtest_dotnet_framework.ps1
More file actions
375 lines (320 loc) · 13.2 KB
/
test_dotnet_framework.ps1
File metadata and controls
375 lines (320 loc) · 13.2 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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
param(
[string]$Solution = "src\Imageflow.dnfull.sln",
[string]$Platforms = "AnyCPU,x86,x64",
[switch]$RestoreOnly,
[switch]$BuildOnly,
[switch]$TestOnly,
[switch]$SkipClean,
[switch]$SkipPack,
[string]$Configuration = "Release",
[switch]$Help
)
<#
.SYNOPSIS
Unified test script for .NET Framework testing of Imageflow.Net
.DESCRIPTION
This script consolidates all .NET Framework testing approaches into a single, flexible script.
It supports testing both packages.config and PackageReference projects on multiple platforms.
.PARAMETER Solution
Path to the solution file to test. Default: "src\Imageflow.both.sln"
Options:
- src\Imageflow.both.sln (includes all projects)
- src\Imageflow.dnfull.sln (.NET Framework only)
.PARAMETER Platforms
Array of platforms to test. Default: @("AnyCPU", "x86", "x64")
Options: "AnyCPU", "x86", "x64"
.PARAMETER RestoreOnly
Only restore packages, don't build or test
.PARAMETER BuildOnly
Only build the solution, don't run tests
.PARAMETER TestOnly
Only run tests (assumes solution is already built)
.PARAMETER SkipClean
Skip cleaning previous builds
.PARAMETER SkipPack
Skip packing NuGet packages
.PARAMETER Configuration
Build configuration. Default: "Release"
.PARAMETER Help
Show this help message
.EXAMPLE
.\test_dotnet_framework.ps1
Run full test suite with default settings
.EXAMPLE
.\test_dotnet_framework.ps1 -Platforms @("x86", "x64") -TestOnly
Only test x86 and x64 platforms (assumes already built)
.EXAMPLE
.\test_dotnet_framework.ps1 -Solution "src\Imageflow.dnfull.sln" -RestoreOnly
Only restore packages for .NET Framework solution
.EXAMPLE
.\test_dotnet_framework.ps1 -SkipClean -SkipPack
Run tests without cleaning or packing
#>
if ($Help) {
Get-Help $MyInvocation.MyCommand.Path -Full
exit 0
}
$ErrorActionPreference = "Stop"
# Script starts from repository root regardless of where it's called from
$scriptPath = Split-Path -Parent $MyInvocation.MyCommand.Definition
Set-Location $scriptPath
Write-Host "Working directory: $(Get-Location)"
function Get-MSBuildPath {
try {
# Use vswhere to find the latest Visual Studio installation
$vswherePath = "${env:ProgramFiles(x86)}\Microsoft Visual Studio\Installer\vswhere.exe"
if (Test-Path $vswherePath) {
$vsInstallPath = & $vswherePath -latest -property installationPath
if ($vsInstallPath) {
$msbuildPath = Join-Path $vsInstallPath "MSBuild\Current\Bin\MSBuild.exe"
if (Test-Path $msbuildPath) {
return $msbuildPath
}
}
}
}
catch {
Write-Warning "vswhere.exe failed. Trying fallback paths."
}
# Fallback paths for different Visual Studio editions
$fallbackPaths = @(
"C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\MSBuild\Current\Bin\MSBuild.exe",
"C:\Program Files\Microsoft Visual Studio\2022\Enterprise\MSBuild\Current\Bin\MSBuild.exe",
"C:\Program Files\Microsoft Visual Studio\2022\Professional\MSBuild\Current\Bin\MSBuild.exe",
"C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Current\Bin\MSBuild.exe",
"C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\MSBuild\Current\Bin\MSBuild.exe",
"C:\Program Files\Microsoft Visual Studio\2019\Enterprise\MSBuild\Current\Bin\MSBuild.exe"
)
foreach ($path in $fallbackPaths) {
if (Test-Path $path) {
return $path
}
}
return $null
}
function Test-AndAddToPath {
param(
[string]$ProgramName,
[string[]]$PathList,
[bool]$InstallWithWinget = $false,
[string]$WingetPackageName = ""
)
# Check if program is already in PATH
$programPresent = Get-Command $ProgramName -ErrorAction SilentlyContinue
if ($programPresent -ne $null) {
Write-Host "$ProgramName found in PATH at: $($programPresent.Path)"
return $true
}
# Search in provided paths
foreach ($path in $PathList) {
$fullPath = Join-Path -Path $path -ChildPath $ProgramName
if (Test-Path $fullPath) {
Write-Host "Adding $ProgramName to PATH from: $fullPath"
$env:Path = $env:Path + ";" + $path
return $true
}
# Search recursively if path is a directory
if ((Test-Path $path) -and (Get-ChildItem $path -Recurse -Filter $ProgramName -ErrorAction SilentlyContinue | Select-Object -First 1 -ExpandProperty FullName)) {
Write-Host "Adding $ProgramName to PATH (found via search): $path"
$env:Path = $env:Path + ";" + $path
return $true
}
}
Write-Error "$ProgramName not found in any of the specified paths"
if ($InstallWithWinget) {
Write-Host "Install $WingetPackageName with winget: winget install $WingetPackageName"
}
return $false
}
function Invoke-Step {
param(
[string]$StepName,
[scriptblock]$StepCode,
[switch]$ContinueOnError = $false
)
Write-Host "=== $StepName ===" -ForegroundColor Cyan
try {
$result = & $StepCode
if ($LASTEXITCODE -ne 0 -and $LASTEXITCODE -ne $null) {
throw "Step failed with exit code $LASTEXITCODE"
}
Write-Host "✓ $StepName completed successfully" -ForegroundColor Green
return $true
}
catch {
Write-Host "✗ $StepName failed:" -ForegroundColor Red
Write-Host " $_" -ForegroundColor Red
if (-not $ContinueOnError) {
throw
}
return $false
}
}
# Validate solution exists
if (-not (Test-Path $Solution)) {
Write-Error "Solution file not found: $Solution"
exit 1
}
# Parse platforms from comma-separated string
$platformArray = $Platforms -split ',' | ForEach-Object { $_.Trim() }
Write-Host "Configuration:" -ForegroundColor Yellow
foreach ($line in @(
" Solution: $Solution",
" Configuration: $Configuration",
" Platforms: $($platformArray -join ', ')",
" Restore Only: $RestoreOnly",
" Build Only: $BuildOnly",
" Test Only: $TestOnly",
" Skip Clean: $SkipClean",
" Skip Pack: $SkipPack"
)) {
Write-Host $line
}
$finalExitCode = 0
# Step 0: Setup tools and clean (if not skipped)
if (-not $TestOnly) {
if (-not $SkipClean) {
Invoke-Step "Cleaning previous builds" {
# Check if DLLs exist before cleaning
$existingDlls = @(
"tests\Imageflow.TestDotNetFull\bin\$Configuration\Imageflow.TestDotNetFull.dll",
"tests\Imageflow.TestDotNetFullPackageReference\bin\$Configuration\Imageflow.TestDotNetFullPackageReference.dll"
) | Where-Object { Test-Path $_ }
if ($existingDlls.Count -gt 0) {
Write-Host "Found existing DLLs that should be cleaned: $($existingDlls.Count)"
}
dotnet clean $Solution --configuration $Configuration /v:minimal
if (Test-Path "src\packages") {
Remove-Item -Recurse -Force "src\packages"
}
# Verify DLLs were actually removed
$remainingDlls = @(
"tests\Imageflow.TestDotNetFull\bin\$Configuration\Imageflow.TestDotNetFull.dll",
"tests\Imageflow.TestDotNetFullPackageReference\bin\$Configuration\Imageflow.TestDotNetFullPackageReference.dll"
) | Where-Object { Test-Path $_ }
if ($remainingDlls.Count -gt 0) {
Write-Host "✗ Clean failed - DLLs still exist after cleaning:" -ForegroundColor Red
$remainingDlls | ForEach-Object { Write-Host " $_" -ForegroundColor Red }
throw "Clean operation failed to remove all DLLs"
}
Write-Host "Cleanup completed successfully - all DLLs removed"
} -ContinueOnError
}
# Setup required tools
Invoke-Step "Setting up required tools" {
$nugetLocations = @(
"C:\Program Files (x86)\Microsoft Visual Studio\2022\Community\Common7\IDE\CommonExtensions\Microsoft\NuGet",
"C:\Program Files (x86)\NuGet",
"C:\Program Files (x86)\Microsoft Visual Studio\2022\Professional\Common7\IDE\CommonExtensions\Microsoft\NuGet",
"C:\Program Files (x86)\Microsoft Visual Studio\2022\Enterprise\Common7\IDE\CommonExtensions\Microsoft\NuGet"
)
if (-not (Test-AndAddToPath "nuget.exe" $nugetLocations $true "Microsoft.NuGet")) {
throw "NuGet.exe is required but not found"
}
}
# Detect MSBuild path in main scope
$msbuild = Get-MSBuildPath
if (-not $msbuild) {
throw "MSBuild.exe could not be found. Please ensure Visual Studio 2022 is installed."
}
Write-Host "Found MSBuild at: $msbuild"
# Step 1: Restore packages
Invoke-Step "Restoring NuGet packages (dotnet restore)" {
dotnet restore $Solution --force-evaluate /v:minimal
if ($LASTEXITCODE -ne 0) { throw "dotnet restore failed" }
}
Invoke-Step "Restoring NuGet packages (nuget.exe for packages.config)" {
$output = nuget restore $Solution -SolutionDirectory src 2>&1
if ($LASTEXITCODE -ne 0) {
throw "nuget restore failed with exit code $LASTEXITCODE"
}
# Check for SDK resolution errors ONLY in .NET Framework test projects
$frameworkErrors = $output | Where-Object {
$_ -match "Could not resolve SDK" -and
($_ -match "TestDotNetFull" -or $_ -match "net462")
}
if ($frameworkErrors) {
Write-Host "Critical SDK resolution errors detected in .NET Framework projects:" -ForegroundColor Red
$frameworkErrors | ForEach-Object { Write-Host $_ -ForegroundColor Red }
throw "nuget restore failed due to .NET Framework SDK resolution errors"
}
# Show warnings about modern .NET SDK issues but don't fail
$modernErrors = $output | Where-Object {
$_ -match "Could not resolve SDK" -and
-not ($_ -match "TestDotNetFull" -or $_ -match "net462")
}
if ($modernErrors) {
Write-Host "Note: Modern .NET projects have SDK issues (will be ignored for .NET Framework testing):" -ForegroundColor Yellow
$modernErrors | ForEach-Object { Write-Host $_ -ForegroundColor Yellow }
}
}
if ($RestoreOnly) {
Write-Host "Restore completed. Exiting..." -ForegroundColor Green
exit 0
}
# Step 2: Build solution
Invoke-Step "Building solution with MSBuild" {
& "$msbuild" $Solution /p:Configuration=$Configuration /p:Platform="Any CPU" /v:minimal
if ($LASTEXITCODE -ne 0) { throw "MSBuild failed" }
}
if ($BuildOnly) {
Write-Host "Build completed. Exiting..." -ForegroundColor Green
exit 0
}
# Step 3: Pack NuGet packages (if not skipped)
if (-not $SkipPack) {
Invoke-Step "Packing NuGet packages" {
dotnet pack ./src/Imageflow/Imageflow.Net.csproj -c $Configuration --include-source -o NuGetPackages/Release/
if ($LASTEXITCODE -ne 0) { throw "dotnet pack failed for Imageflow.Net" }
dotnet pack ./src/Imageflow.AllPlatforms/Imageflow.AllPlatforms.csproj -c $Configuration --include-source -o NuGetPackages/Release/
if ($LASTEXITCODE -ne 0) { throw "dotnet pack failed for Imageflow.AllPlatforms" }
}
}
}
# Step 4: Run tests
$testProjects = @(
"tests\Imageflow.TestDotNetFull\bin\$Configuration\Imageflow.TestDotNetFull.dll",
"tests\Imageflow.TestDotNetFullPackageReference\bin\$Configuration\Imageflow.TestDotNetFullPackageReference.dll"
)
# Validate that test DLLs exist before running tests
Invoke-Step "Validating test DLLs exist" {
$missingDlls = $testProjects | Where-Object { -not (Test-Path $_) }
if ($missingDlls.Count -gt 0) {
Write-Host "✗ Missing test DLLs - cannot run tests:" -ForegroundColor Red
$missingDlls | ForEach-Object { Write-Host " $_" -ForegroundColor Red }
throw "Test DLLs not found - build may have failed"
}
Write-Host "All test DLLs found: $($testProjects.Count)"
}
foreach ($testDll in $testProjects) {
if (-not (Test-Path $testDll)) {
Write-Error "Test DLL not found: $testDll"
$finalExitCode = 1
continue
}
foreach ($platform in $platformArray) {
$platformArg = ""
if ($platform -eq "x86") {
$platformArg = "--platform:x86"
} elseif ($platform -eq "x64") {
$platformArg = "--platform:x64"
}
$stepName = "Running tests for $testDll on $platform"
Invoke-Step $stepName {
if ($platformArg) {
dotnet test $testDll $platformArg
} else {
dotnet test $testDll
}
if ($LASTEXITCODE -ne 0) { throw "dotnet test failed for $testDll on $platform" }
} -ContinueOnError
}
}
if ($finalExitCode -eq 0) {
Write-Host ""
Write-Host "🎉 All tests completed successfully!" -ForegroundColor Green
} else {
Write-Host ""
Write-Host "❌ Some tests failed. Check the output above for details." -ForegroundColor Red
}
exit $finalExitCode