-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibs_and_tests.ps1
More file actions
442 lines (305 loc) · 16.4 KB
/
libs_and_tests.ps1
File metadata and controls
442 lines (305 loc) · 16.4 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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
Import-Module Psake
# Include additional tasks in the build script
if(Test-Path "$PSScriptRoot\psake.tasks.ps1") {
Include -fileNamePathToInclude "$PSScriptRoot\psake.tasks.ps1"
}
#region Validate Environment
Task doctor -description "Test dev environment" {
Import-Module Pester
Describe ".Net core framework installation" {
# dotnet.exe is need for buildung and test execution.
# Without having dotnet.exe in path nothing will work
It "dotnet cli is in path" {
Get-Command dotnet.exe | Should Not Be $null
}
# Common problem is: Nuget.org is not accessible for package download.
# Nuget.Org provides a dummy package to llookup or download for test purposes
$lookupUri = "https://www.nuget.org/api/v2/Packages(Id='NuGet.GalleryUptime',Version='1.0.0')"
if($Env:HTTP_PROXY) {
It "Nuget.Org is accessible for lookup through proxy http://$Env:HTTP_PROXY" {
Invoke-RestMethod -Proxy "http://$Env:HTTP_PROXY" -Uri $lookupUri | Should Not Be $null
}
} else {
It "Nuget.Org is accessible for lookup through windows inet config" {
Invoke-RestMethod -Uri $lookupUri | Should Not Be $null
}
}
}
Describe "Global project configuration" {
It "Project root contains global.json" {
$script:globalJsonExists = Test-Path $PSScriptRoot\global.json
$script:globalJsonExists | Should Be $true
}
$globalJsonContent = Get-Content $PSScriptRoot\global.json | ConvertFrom-Json
It "global.json contains a sdk spec" {
$globalJsonContent.sdk.version | Should Not Be $null
$script:sdkVersion = $globalJsonContent.sdk.version
}
It "Subprojects from global.json $([string]::Join(",", $globalJsonContent.projects)) do exist" {
$globalJsonContent.projects | ForEach-Object { Join-Path $PSScriptRoot $_ } | Test-Path | Should Be $true
}
It "Required Framework is installed" {
(Get-Command dotnet).Path | Split-Path -Parent | Join-Path -ChildPath "sdk\$script:sdkVersion" | Test-Path | Should Be $true
}
}
}
#endregion
$script:dotnet = (Get-Command dotnet.exe).Path
$script:nuget = (Get-Command nuget.exe).Path
#region Define file sets
Task query_workspace -description "Collect infomation about the workspace structure which is useful for other build tasks" {
$script:projectItems = @{}
$script:projectItems.all = @()
if(Test-Path $PSScriptRoot\global.json) {
# A global.json specfies which project belongs to the solution
$globalJsonContent = Get-Content -Path $PSScriptRoot\global.json -Raw | ConvertFrom-Json
$globalJsonContent.projects | ForEach-Object {
# separate projects by its group name in global.json
$projectSubDir = $_
$script:projectItems.Add($projectSubDir,(Get-ChildItem -Path (Join-Path $PSScriptRoot $projectSubDir) -Include "project.json" -File -Recurse))
$script:projectItems[$projectSubDir] | Select-Object -ExpandProperty FullName | ForEach-Object { Write-Host "found projects in $projectSubDir : $_" }
$script:projectItems.all += $script:projectItems[$projectSubDir]
}
} else {
# without the global.json we guess (Visual Studio doesn't create a global.json and doesn't seperate the projects in 'src' and 'test')
# Best guess is: test projects end with '.Test', all other projects are source projects.
$is_src_project = { !($_.Directory.Name.ToLowerInvariant().EndsWith("test")) }
$is_test_project = { $_.Directory.Name.ToLowerInvariant().EndsWith("test") }
$script:projectItems.all = (Get-ChildItem -Path (Join-Path $PSScriptRoot $projectSubDir) -Include "project.json" -File -Recurse)
$script:projectItems.src = $script:projectItems.all | Where-Object -FilterScript $is_src_project
$script:projectItems.src | Select-Object -ExpandProperty FullName | ForEach-Object { Write-Host "found projects in "src" : $_" }
$script:projectItems.test = $script:projectItems.all | Where-Object -FilterScript $is_test_project
$script:projectItems.test | Select-Object -ExpandProperty FullName | ForEach-Object { Write-Host "found projects in "test" : $_" }
}
}
#endregion
#region Tasks for NuGet dependencies
Task clean_dependencies -description "Remove nuget package cache from current users home" {
# dotnet cli utility uses the users package cache only.
# a project local nuget cache can be enforced but is not necessary by default.
# This differs from nuget.exe's behavior which uses by default project local packages directories
# see also: https://docs.microsoft.com/de-de/dotnet/articles/core/tools/dotnet-restore
Remove-Item (Join-Path $HOME .nuget\packages) -Force -Recurse -ErrorAction SilentlyContinue
}
Task build_dependencies -description "Restore nuget dependencies for all projects" {
Push-Location $PSScriptRoot
try {
# Calling dot net restore in root directory should be enough.
& $script:dotnet restore
} finally {
Pop-Location
}
}
Task query_dependencies -description "Print a list of all nuget dependencies. This is useful for OSS licence clearing" {
# For Mainline clearing a complete set of nuget packages has to be retrieved.
# These are taken from the 'dependencies' section of all src project.jsons
# !! framework specific dependencies are not queried !!
$nugetDependencies = $script:projectItems.src | Get-Content -Raw | ConvertFrom-Json | ForEach-Object {
$_.dependencies.PSObject.Properties | ForEach-Object {
if($_.Value -is [string]) {
[pscustomobject]@{
Id = $_.Name
Version = $_.Value
}
} else {
[pscustomobject]@{
Id = $_.Name
Version = $_.Value.Version
}
}
}
}
$nugetDependencies | Group-Object Id | Select-Object Name,Group
} -depends query_workspace
#endregion
#region Tasks for .Net Assemblies
Task init_assemblies {
# test results are stored in a separate directory
if(Test-Path $PSScriptRoot\.testresults) {
$script:testResultsDirectory = Get-Item -Path $PSScriptRoot\.testresults
} else {
$script:testResultsDirectory = New-Item -Path $PSScriptRoot\.testresults -ItemType Directory
}
}
Task clean_assemblies -description "Removes the assembles built by 'build_assembly' task" {
$script:projectItems.all | ForEach-Object {
# just remove the usual oputput directories instead of spefific files or file extensions.
# This includes automatically assemblies, config files or other artefacts which are copied
# to the build directory
Remove-Item -Path (Join-Path $_.Directory bin) -Recurse -ErrorAction SilentlyContinue
Remove-Item -Path (Join-Path $_.Directory obj) -Recurse -ErrorAction SilentlyContinue
}
} -depends query_workspace
Task build_assemblies -description "Compile all projects into .Net assemblies" {
Push-Location $PSScriptRoot
try {
& $script:dotnet build "**\project.json"
} finally {
Pop-Location
}
}
Task test_assemblies -description "Run the unit test under 'test'. Output is written to .testresults directory" {
$script:test_assemblies_results = [System.Collections.ArrayList]::new()
$script:projectItems.test | ForEach-Object {
Push-Location $_.Directory
try {
$testResultFileName = Join-Path -Path $script:testResultsDirectory -ChildPath "$($_.Directory.BaseName).xml"
# Check if nunit or xunit is used as a test runner. They use diffrent parameters
# for test result file path specification
$testProjectJsonContent = Get-Content -Path $_.FullName -Raw | ConvertFrom-Json
if($testProjectJsonContent.testRunner -eq "xunit") {
# the projects directory name is taken as the name of the test result file.
& $script:dotnet test -xml $testResultFileName
} elseif($testProjectJsonContent.testRunner -eq "nunit") {
# the projects directory name is taken as the name of the test result file.
& $script:dotnet test -result:$testResultFileName
}
} finally {
Pop-Location
}
}
} -depends query_workspace,init_assemblies
Task report_test_assemblies -description "Retrieves a report of failed tests" {
Get-ChildItem -Path $script:testResultsDirectory -Filter *.xml | ForEach-Object {
$testResultContent = [xml](Get-Content $_.FullName)
if((Select-Xml -Xml $testResultContent -XPath "//assembly")) {
# must be xunit
$failed = (Select-Xml -Xml $testResultContent -XPath "//test[@result != 'Pass']").Node | Select-Object Name,Result
if(!$failed) {
"No test failed: $($_.FullName)" | Write-Host -ForegroundColor Green
} else {
"Some tests failed: $($_.FullName)" | Write-Host -ForegroundColor Red
$failed | Format-Table
}
} else {
# must be nunit
$failed = (Select-Xml -Path $_.FullName -XPath "//test-case[@result != 'Passed']").Node | Select-Object Name,Result
if(!$failed) {
"No test failed: $($_.FullName)" | Write-Host -ForegroundColor Green
} else {
"Some tests failed: $($_.FullName)" | Write-Host -ForegroundColor Red
$failed | Format-Table
}
}
}
} -depends query_workspace,init_assemblies
Task measure_assemblies -description "Run the benchmark projects under measure. Output is written to the .benchmark directory" {
$script:projectItems.measure | ForEach-Object {
Push-Location $_.Directory
try {
& $script:dotnet run
} finally {
Pop-Location
}
}
} -depends query_workspace
#endregion
#region Tasks for Nuget packages
Task init_packages {
# nuget packages are stored in .packages.
# The directory is created if necessary
if(Test-Path $PSScriptRoot\.packages) {
$script:packageBuildDirectory = Get-Item -Path $PSScriptRoot\.packages
} else {
$script:packageBuildDirectory = New-Item -Path $PSScriptRoot\.packages -ItemType Directory
}
}
Task clean_packages -description "Removes nuget packages build directory" {
Remove-Item $PSScriptRoot\.packages -Recurse -Force -ErrorAction SilentlyContinue
}
Task build_packages -description "Create nuget packages from all projects having packOptions defined" {
$script:projectItems.src | ForEach-Object {
Push-Location $_.Directory
try {
$projectJsonContent = Get-Content -Path $_.FullName -Raw | ConvertFrom-Json
if($projectJsonContent.packOptions -ne $null) {
& $script:dotnet pack -c "Release" -o $script:packageBuildDirectory
} else {
"Skipping project $($_.Fullname). No packOptions defined." | Write-Host -ForegroundColor DarkYellow
}
} finally {
Pop-Location
}
}
} -depends query_workspace,init_packages,clean_packages
Task publish_packages -description "Makes the packages known to the used package source" {
# Publishing a package requires the nuget.exe.
# Credentials/api key of the package feed ist url are taken from the efective Nuget.Config
Push-Location $script:packageBuildDirectory
try {
Get-ChildItem *.nupkg -Exclude *.symbols.nupkg | ForEach-Object {
"Publishing: $($_.FullName)" | Write-Host
& $script:nuget push $_.FullName
}
} finally {
Pop-Location
}
} -depends query_workspace,init_packages
Task report_nugetConfig -description "Extracts some config values from the effective Nuget config" {
"Nuget.Config Path: $env:APPDATA\nuget\NuGet.config" | Write-Host
"Default NuGet Push Source: $(& $script:nuget config defaultPushSource)" | Write-Host
"Known Api Keys:" | Write-Host
$nugetConfigContentXml = [xml](Get-Content -Path $env:APPDATA\nuget\NuGet.config)
$nugetConfigContentXml.configuration.apikeys.add | Format-Table -AutoSize
}
#endregion
#region Tasks for coverage reporting
Task init_coverage {
# coverage information is stoired i n .coverage.
# he directoyr is created if necessary
if(Test-Path $PSScriptRoot\.coverage) {
$script:coverageResultsDirectory = Get-Item -Path $PSScriptRoot\.coverage
} else {
$script:coverageResultsDirectory = New-Item -Path $PSScriptRoot\.coverage -ItemType Directory
}
# Get Opencover
& $script:nuget install OpenCover -Output $script:coverageResultsDirectory
# Get hold on the OpenCover executable
$script:openCoverConsole = Get-ChildItem -Path $script:coverageResultsDirectory -Filter OpenCover.Console.Exe -Recurse | Select-Object -ExpandProperty FullName
# Get ReportGenerator
& $script:nuget install ReportGenerator -Output $script:coverageResultsDirectory
# Get Hold on ReportGerenator executable
$script:reportGenerator = Get-ChildItem -Path $script:coverageResultsDirectory -Filter ReportGenerator.Exe -Recurse | Select-Object -ExpandProperty FullName
}
Task clean_coverage {
Remove-Item -Path $PSScriptRoot\.coverage\* -Include @("*.xml","*.htm","*.png","*.css","*.js") -ErrorAction SilentlyContinue
}
Task build_coverage -description "Run the unit test under 'test' to measure the tests coverage. Output is written to .coverage directory" {
# Run test projects with openCover and collect result separted by project in an XML file.
$filterAssemblies = "-[xunit.*]*"
$script:projectItems.test | ForEach-Object {
$projectDirectory = $_.Directory
Get-ChildItem "$($_.Directory.FullName)\bin\Debug\netcoreapp1.*\$($_.Directory.BaseName).Dll" -Recurse | ForEach-Object {
"test assembly: $_" | Write-Host
& $script:openCoverConsole -oldStyle -target:$script:dotnet -targetdir:$($projectDirectory.FullName) -targetargs:test -register:user -output:$script:coverageResultsDirectory\$($_.BaseName).xml
# -filter:$filterAssemblies
}
}
} -depends query_workspace,init_coverage,clean_coverage
Task report_coverage -description "Creates a report of the collected coverage data" {
# Generate a report for avery coverage file found
& $script:reportGenerator -targetdir:$script:coverageResultsDirectory -reports:([string]::Join(";",(Get-ChildItem -Path $script:coverageResultsDirectory -Filter *.xml | Select-Object -ExpandProperty FullName)))
# Start b rowser woth coverage result
ii $script:coverageResultsDirectory\index.htm
} -depends query_workspace,init_coverage
#endregion
Task update_self {
# Update this script directly from github
Invoke-RestMethod https://raw.githubusercontent.com/wgross/PSake.NetCore/master/libs_and_tests.ps1 -OutFile $PSScriptRoot\default.ps1
}
#
# The default build pipeline
#
Task clean -description "The project tree is clean: all artifacts created by the development tool chain are removed" -depends clean_assemblies,clean_packages,clean_coverage
Task restore -description "External dependencies are restored.The project is ready to be built." -depends build_dependencies
Task build -description "The project is built: all artifacts created by the development tool chain are created" -depends restore,build_assemblies
Task test -description "The project is tested: all automated tests of the project are run" -depends build,test_assemblies,report_test_assemblies
Task pack -description "All nuget packages are built" -depends build_packages
Task default -depends clean,restore,build,test,pack
#
# Additional build targets
#
Task publish -description "All artefacts are published to their destinations" -depends publish_packages
Task report -description "Calls all reports" -depends report_test_assemblies
Task coverage -description "Starts a coverage analysys of the workspace" -depends build_coverage,report_coverage
Task measure -description "The project is measured: all benchmarks are running" -depends build_assemblies,measure_assemblies