forked from PowerShell/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.psm1
3034 lines (2592 loc) · 104 KB
/
build.psm1
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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Use the .NET Core APIs to determine the current platform; if a runtime
# exception is thrown, we are on FullCLR, not .NET Core.
try {
$Runtime = [System.Runtime.InteropServices.RuntimeInformation]
$OSPlatform = [System.Runtime.InteropServices.OSPlatform]
$IsCoreCLR = $true
$IsLinux = $Runtime::IsOSPlatform($OSPlatform::Linux)
$IsOSX = $Runtime::IsOSPlatform($OSPlatform::OSX)
$IsWindows = $Runtime::IsOSPlatform($OSPlatform::Windows)
} catch {
# If these are already set, then they're read-only and we're done
try {
$IsCoreCLR = $false
$IsLinux = $false
$IsOSX = $false
$IsWindows = $true
}
catch { }
}
if ($IsWindows)
{
$IsAdmin = (New-Object Security.Principal.WindowsPrincipal ([Security.Principal.WindowsIdentity]::GetCurrent())).IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
# Can't use $env:HOME - not available on older systems (e.g. in AppVeyor)
$nugetPackagesRoot = "${env:HOMEDRIVE}${env:HOMEPATH}\.nuget\packages"
}
else
{
$nugetPackagesRoot = "${env:HOME}/.nuget/packages"
}
if ($IsLinux) {
$LinuxInfo = Get-Content /etc/os-release | ConvertFrom-StringData
$IsUbuntu = $LinuxInfo.ID -match 'ubuntu'
$IsUbuntu14 = $IsUbuntu -and $LinuxInfo.VERSION_ID -match '14.04'
$IsUbuntu16 = $IsUbuntu -and $LinuxInfo.VERSION_ID -match '16.04'
$IsCentOS = $LinuxInfo.ID -match 'centos' -and $LinuxInfo.VERSION_ID -match '7'
$IsFedora = $LinuxInfo.ID -match 'fedora' -and $LinuxInfo.VERSION_ID -ge 24
$IsRedHatFamily = $IsCentOS -or $IsFedora
# Workaround for temporary LD_LIBRARY_PATH hack for Fedora 24
# https://github.com/PowerShell/PowerShell/issues/2511
if ($IsFedora -and (Test-Path ENV:\LD_LIBRARY_PATH)) {
Remove-Item -Force ENV:\LD_LIBRARY_PATH
Get-ChildItem ENV:
}
}
#
# At the moment, we just support x64 builds. When we support x86 builds, this
# check may need to verify the SDK for the specified architecture.
#
function Get-Win10SDKBinDir {
return "${env:ProgramFiles(x86)}\Windows Kits\10\bin\x64"
}
function Test-Win10SDK {
# The Windows 10 SDK is installed to "${env:ProgramFiles(x86)}\Windows Kits\10\bin\x64",
# but the directory may exist even if the SDK has not been installed.
#
# A slightly more robust check is for the mc.exe binary within that directory.
# It is only present if the SDK is installed.
return (Test-Path "${env:ProgramFiles(x86)}\Windows Kits\10\bin\x64\mc.exe")
}
function Start-PSBuild {
[CmdletBinding(DefaultParameterSetName='CoreCLR')]
param(
# When specified this switch will stops running dev powershell
# to help avoid compilation error, because file are in use.
[switch]$StopDevPowerShell,
[switch]$NoPath,
[switch]$Restore,
[string]$Output,
[switch]$ResGen,
[switch]$TypeGen,
[switch]$Clean,
[switch]$PSModuleRestore,
# this switch will re-build only System.Management.Automation.dll
# it's useful for development, to do a quick changes in the engine
[switch]$SMAOnly,
# These runtimes must match those in project.json
# We do not use ValidateScript since we want tab completion
[ValidateSet("ubuntu.14.04-x64",
"ubuntu.16.04-x64",
"debian.8-x64",
"centos.7-x64",
"fedora.24-x64",
"win7-x64",
"win7-x86",
"win81-x64",
"win10-x64",
"osx.10.11-x64",
"osx.10.12-x64")]
[Parameter(ParameterSetName='CoreCLR')]
[string]$Runtime,
[Parameter(ParameterSetName='FullCLR', Mandatory=$true)]
[switch]$FullCLR,
[Parameter(ParameterSetName='FullCLR')]
[switch]$XamlGen,
[ValidateSet('Linux', 'Debug', 'Release', 'CodeCoverage', '')] # We might need "Checked" as well
[string]$Configuration,
[Parameter(ParameterSetName='CoreCLR')]
[switch]$Publish,
[Parameter(ParameterSetName='CoreCLR')]
[switch]$CrossGen
)
function Stop-DevPowerShell {
Get-Process powershell* |
Where-Object {
$_.Modules |
Where-Object {
$_.FileName -eq (Resolve-Path $script:Options.Output).Path
}
} |
Stop-Process -Verbose
}
if ($CrossGen -and !$Publish) {
# By specifying -CrossGen, we implicitly set -Publish to $true, if not already specified.
$Publish = $true
}
if ($Clean) {
log "Cleaning your working directory. You can also do it with 'git clean -fdX'"
Push-Location $PSScriptRoot
try {
git clean -fdX
# Extra cleaning is required to delete the CMake temporary files.
# These are not cleaned when using "X" and cause CMake to retain state, leading to
# mis-configured environment issues when switching between x86 and x64 compilation
# environments.
git clean -fdx .\src\powershell-native
} finally {
Pop-Location
}
}
# save Git description to file for PowerShell to include in PSVersionTable
git --git-dir="$PSScriptRoot/.git" describe --dirty --abbrev=60 > "$psscriptroot/powershell.version"
# simplify ParameterSetNames
if ($PSCmdlet.ParameterSetName -eq 'FullCLR') {
$FullCLR = $true
}
# Add .NET CLI tools to PATH
Find-Dotnet
# verify we have all tools in place to do the build
$precheck = precheck 'dotnet' "Build dependency 'dotnet' not found in PATH. Run Start-PSBootstrap. Also see: https://dotnet.github.io/getting-started/"
if ($IsWindows) {
# cmake is needed to build powershell.exe
$precheck = $precheck -and (precheck 'cmake' 'cmake not found. Run Start-PSBootstrap. You can also install it from https://chocolatey.org/packages/cmake')
Use-MSBuild
#mc.exe is Message Compiler for native resources
if (-Not (Test-Win10SDK)) {
throw 'Win 10 SDK not found. Run Start-PSBootstrap or install Microsoft Windows 10 SDK from https://developer.microsoft.com/en-US/windows/downloads/windows-10-sdk'
}
$vcVarsPath = (Get-Item(Join-Path -Path "$env:VS140COMNTOOLS" -ChildPath '../../vc')).FullName
if ((Test-Path -Path $vcVarsPath\vcvarsall.bat) -eq $false) {
throw "Could not find Visual Studio vcvarsall.bat at $vcVarsPath. Please ensure the optional feature 'Common Tools for Visual C++' is installed."
}
# setup msbuild configuration
if ($Configuration -eq 'Debug' -or $Configuration -eq 'Release') {
$msbuildConfiguration = $Configuration
} else {
$msbuildConfiguration = 'Release'
}
} elseif ($IsLinux -or $IsOSX) {
foreach ($Dependency in 'cmake', 'make', 'g++') {
$precheck = $precheck -and (precheck $Dependency "Build dependency '$Dependency' not found. Run Start-PSBootstrap.")
}
}
# Abort if any precheck failed
if (-not $precheck) {
return
}
# set output options
$OptionsArguments = @{
Publish=$Publish
CrossGen=$CrossGen
Output=$Output
FullCLR=$FullCLR
Runtime=$Runtime
Configuration=$Configuration
Verbose=$true
SMAOnly=[bool]$SMAOnly
}
$script:Options = New-PSOptions @OptionsArguments
if ($StopDevPowerShell) {
Stop-DevPowerShell
}
# setup arguments
$Arguments = @()
if ($Publish -or $FullCLR) {
$Arguments += "publish"
} else {
$Arguments += "build"
}
if ($Output) {
$Arguments += "--output", $Output
}
elseif ($SMAOnly) {
$Arguments += "--output", (Split-Path $script:Options.Output)
}
$Arguments += "--configuration", $Options.Configuration
$Arguments += "--framework", $Options.Framework
$Arguments += "--runtime", $Options.Runtime
# handle Restore
if ($Restore -or -not (Test-Path "$($Options.Top)/project.lock.json")) {
log "Run dotnet restore"
$RestoreArguments = @("--verbosity")
if ($PSCmdlet.MyInvocation.BoundParameters["Verbose"].IsPresent) {
$RestoreArguments += "Info"
} else {
$RestoreArguments += "Warning"
}
$RestoreArguments += "$PSScriptRoot"
Start-NativeExecution { dotnet restore $RestoreArguments }
# .NET Core's crypto library needs brew's OpenSSL libraries added to its rpath
if ($IsOSX) {
# This is the restored library used to build
# This is allowed to fail since the user may have already restored
Write-Warning ".NET Core links the incorrect OpenSSL, correcting NuGet package libraries..."
find $env:HOME/.nuget -name System.Security.Cryptography.Native.OpenSsl.dylib | % { sudo install_name_tool -add_rpath /usr/local/opt/openssl/lib $_ }
find $env:HOME/.nuget -name System.Net.Http.Native.dylib | % { sudo install_name_tool -change /usr/lib/libcurl.4.dylib /usr/local/opt/curl/lib/libcurl.4.dylib $_ }
}
}
# handle ResGen
# Heuristic to run ResGen on the fresh machine
if ($ResGen -or -not (Test-Path "$PSScriptRoot/src/Microsoft.PowerShell.ConsoleHost/gen")) {
log "Run ResGen (generating C# bindings for resx files)"
Start-ResGen
}
# handle xaml files
# Heuristic to resolve xaml on the fresh machine
if ($FullCLR -and ($XamlGen -or -not (Test-Path "$PSScriptRoot/src/Microsoft.PowerShell.Activities/gen/*.g.cs"))) {
log "Run XamlGen (generating .g.cs and .resources for .xaml files)"
Start-XamlGen -MSBuildConfiguration $msbuildConfiguration
}
# Build native components
if (($IsLinux -or $IsOSX) -and -not $SMAOnly) {
$Ext = if ($IsLinux) {
"so"
} elseif ($IsOSX) {
"dylib"
}
$Native = "$PSScriptRoot/src/libpsl-native"
$Lib = "$($Options.Top)/libpsl-native.$Ext"
log "Start building $Lib"
try {
Push-Location $Native
Start-NativeExecution { cmake -DCMAKE_BUILD_TYPE=Debug . }
Start-NativeExecution { make -j }
Start-NativeExecution { ctest --verbose }
} finally {
Pop-Location
}
if (-not (Test-Path $Lib)) {
throw "Compilation of $Lib failed"
}
} elseif ($IsWindows -and (-not $SMAOnly)) {
log "Start building native Windows binaries"
try {
Push-Location "$PSScriptRoot\src\powershell-native"
$NativeHostArch = "x64"
if ($script:Options.Runtime -match "-x86")
{
$NativeHostArch = "x86"
}
# setup cmakeGenerator
if ($NativeHostArch -eq 'x86') {
$cmakeGenerator = 'Visual Studio 14 2015'
} else {
$cmakeGenerator = 'Visual Studio 14 2015 Win64'
}
# Compile native resources
$currentLocation = Get-Location
@("nativemsh/pwrshplugin") | % {
$nativeResourcesFolder = $_
Get-ChildItem $nativeResourcesFolder -Filter "*.mc" | % {
$command = @"
cmd.exe /C cd /d "$currentLocation" "&" "$($vcVarsPath)\vcvarsall.bat" "$NativeHostArch" "&" mc.exe -o -d -c -U "$($_.FullName)" -h "$nativeResourcesFolder" -r "$nativeResourcesFolder"
"@
log " Executing mc.exe Command: $command"
Start-NativeExecution { Invoke-Expression -Command:$command 2>&1 }
}
}
function Build-NativeWindowsBinaries {
param(
# Describes wither it should build the CoreCLR or FullCLR version
[ValidateSet("ON", "OFF")]
[string]$OneCoreValue,
# Array of file names to copy from the local build directory to the packaging directory
[string[]]$FilesToCopy
)
# Disabling until I figure out if it is necessary
# $overrideFlags = "-DCMAKE_USER_MAKE_RULES_OVERRIDE=$PSScriptRoot\src\powershell-native\windows-compiler-override.txt"
$overrideFlags = ""
$location = Get-Location
$command = @"
cmd.exe /C cd /d "$location" "&" "$($vcVarsPath)\vcvarsall.bat" "$NativeHostArch" "&" cmake "$overrideFlags" -DBUILD_ONECORE=$OneCoreValue -DBUILD_TARGET_ARCH=$NativeHostArch -G "$cmakeGenerator" . "&" msbuild ALL_BUILD.vcxproj "/p:Configuration=$msbuildConfiguration"
"@
log " Executing Build Command: $command"
Start-NativeExecution { Invoke-Expression -Command:$command }
$clrTarget = "FullClr"
if ($OneCoreValue -eq "ON")
{
$clrTarget = "CoreClr"
}
# Copy the binaries from the local build directory to the packaging directory
$dstPath = ($script:Options).Top
$FilesToCopy | % {
$srcPath = Join-Path (Join-Path (Join-Path (Get-Location) "bin") $msbuildConfiguration) "$clrTarget/$_"
log " Copying $srcPath to $dstPath"
Copy-Item $srcPath $dstPath
}
}
if ($FullCLR) {
$fullBinaries = @(
'powershell.exe',
'powershell.pdb',
'pwrshplugin.dll',
'pwrshplugin.pdb'
)
Build-NativeWindowsBinaries "OFF" $fullBinaries
}
else
{
$coreClrBinaries = @(
'pwrshplugin.dll',
'pwrshplugin.pdb'
)
Build-NativeWindowsBinaries "ON" $coreClrBinaries
# Place the remoting configuration script in the same directory
# as the binary so it will get published.
Copy-Item .\Install-PowerShellRemoting.ps1 ($script:Options).Top
}
} finally {
Pop-Location
}
}
# handle TypeGen
if ($TypeGen -or -not (Test-Path "$PSScriptRoot/src/Microsoft.PowerShell.CoreCLR.AssemblyLoadContext/CorePsTypeCatalog.cs")) {
log "Run TypeGen (generating CorePsTypeCatalog.cs)"
Start-TypeGen
}
try {
# Relative paths do not work well if cwd is not changed to project
Push-Location $Options.Top
log "Run dotnet $Arguments from $pwd"
Start-NativeExecution { dotnet $Arguments }
if ($CrossGen) {
$publishPath = Split-Path $Options.Output
Start-CrossGen -PublishPath $publishPath -Runtime $script:Options.Runtime
log "PowerShell.exe with ngen binaries is available at: $($Options.Output)"
} else {
log "PowerShell output: $($Options.Output)"
}
} finally {
Pop-Location
}
if($PSModuleRestore)
{
$ProgressPreference = "SilentlyContinue"
# Downloading the PowerShellGet and PackageManagement modules.
# $Options.Output is pointing to something like "...\src\powershell-win-core\bin\Debug\netcoreapp1.1\win10-x64\publish\powershell.exe",
# so we need to get its parent directory
$publishPath = Split-Path $Options.Output -Parent
log "Restore PowerShell modules to $publishPath"
# PowerShellGet depends on PackageManagement module, so PackageManagement module will be installed with the PowerShellGet module.
Restore-PSModule -Name @('PowerShellGet') -Destination (Join-Path -Path $publishPath -ChildPath "Modules")
}
}
function Compress-TestContent {
[CmdletBinding()]
param(
$Destination
)
$powerShellTestRoot = Join-Path $PSScriptRoot 'test\powershell'
Add-Type -AssemblyName System.IO.Compression.FileSystem
$resolvedPath = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($Destination)
[System.IO.Compression.ZipFile]::CreateFromDirectory($powerShellTestRoot, $resolvedPath)
}
function New-PSOptions {
[CmdletBinding()]
param(
[ValidateSet("Linux", "Debug", "Release", "CodeCoverage", "")]
[string]$Configuration,
[ValidateSet("netcoreapp1.1", "net451")]
[string]$Framework,
# These are duplicated from Start-PSBuild
# We do not use ValidateScript since we want tab completion
[ValidateSet("",
"ubuntu.14.04-x64",
"ubuntu.16.04-x64",
"debian.8-x64",
"centos.7-x64",
"fedora.24-x64",
"win7-x86",
"win7-x64",
"win81-x64",
"win10-x64",
"osx.10.11-x64",
"osx.10.12-x64")]
[string]$Runtime,
[switch]$Publish,
[switch]$CrossGen,
[string]$Output,
[switch]$FullCLR,
[switch]$SMAOnly
)
# Add .NET CLI tools to PATH
Find-Dotnet
$ConfigWarningMsg = "The passed-in Configuration value '{0}' is not supported on '{1}'. Use '{2}' instead."
if (-not $Configuration) {
$Configuration = if ($IsLinux -or $IsOSX) {
"Linux"
} elseif ($IsWindows) {
"Debug"
}
} else {
switch ($Configuration) {
"Linux" {
if ($IsWindows) {
$Configuration = "Debug"
Write-Warning ($ConfigWarningMsg -f $switch.Current, "Windows", $Configuration)
}
}
"CodeCoverage" {
if(-not $IsWindows) {
$Configuration = "Linux"
Write-Warning ($ConfigWarningMsg -f $switch.Current, $LinuxInfo.PRETTY_NAME, $Configuration)
}
}
Default {
if ($IsLinux -or $IsOSX) {
$Configuration = "Linux"
Write-Warning ($ConfigWarningMsg -f $switch.Current, $LinuxInfo.PRETTY_NAME, $Configuration)
}
}
}
}
Write-Verbose "Using configuration '$Configuration'"
$PowerShellDir = if ($FullCLR) {
"powershell-win-full"
} elseif ($Configuration -eq 'Linux') {
"powershell-unix"
} else {
"powershell-win-core"
}
$Top = [IO.Path]::Combine($PSScriptRoot, "src", $PowerShellDir)
Write-Verbose "Top project directory is $Top"
if (-not $Framework) {
$Framework = if ($FullCLR) {
"net451"
} else {
"netcoreapp1.1"
}
Write-Verbose "Using framework '$Framework'"
}
if (-not $Runtime) {
$Runtime = dotnet --info | % {
if ($_ -match "RID") {
$_ -split "\s+" | Select-Object -Last 1
}
}
if (-not $Runtime) {
Throw "Could not determine Runtime Identifier, please update dotnet"
} else {
Write-Verbose "Using runtime '$Runtime'"
}
}
$Executable = if ($IsLinux -or $IsOSX) {
"powershell"
} elseif ($IsWindows) {
"powershell.exe"
}
# Build the Output path
if (!$Output) {
$Output = [IO.Path]::Combine($Top, "bin", $Configuration, $Framework, $Runtime)
# Publish injects the publish directory
if ($Publish -or $FullCLR) {
$Output = [IO.Path]::Combine($Output, "publish")
}
$Output = [IO.Path]::Combine($Output, $Executable)
}
$RealFramework = $Framework
if ($SMAOnly)
{
$Top = [IO.Path]::Combine($PSScriptRoot, "src", "System.Management.Automation")
if ($Framework -match 'netcoreapp')
{
$RealFramework = 'netstandard1.6'
}
}
return @{ Top = $Top;
Configuration = $Configuration;
Framework = $RealFramework;
Runtime = $Runtime;
Output = $Output;
Publish = $Publish;
CrossGen = $CrossGen }
}
function Get-PSOutput {
[CmdletBinding()]param(
[hashtable]$Options
)
if ($Options) {
return $Options.Output
} elseif ($script:Options) {
return $script:Options.Output
} else {
return (New-PSOptions).Output
}
}
function Get-PesterTag {
param ( [Parameter(Position=0)][string]$testbase = "$PSScriptRoot/test/powershell" )
$alltags = @{}
$warnings = @()
get-childitem -Recurse $testbase -File |?{$_.name -match "tests.ps1"}| %{
$fullname = $_.fullname
$tok = $err = $null
$ast = [System.Management.Automation.Language.Parser]::ParseFile($FullName, [ref]$tok,[ref]$err)
$des = $ast.FindAll({$args[0] -is "System.Management.Automation.Language.CommandAst" -and $args[0].CommandElements[0].Value -eq "Describe"},$true)
foreach( $describe in $des) {
$elements = $describe.CommandElements
$lineno = $elements[0].Extent.StartLineNumber
$foundPriorityTags = @()
for ( $i = 0; $i -lt $elements.Count; $i++) {
if ( $elements[$i].extent.text -match "^-t" ) {
$vAst = $elements[$i+1]
if ( $vAst.FindAll({$args[0] -is "System.Management.Automation.Language.VariableExpressionAst"},$true) ) {
$warnings += "TAGS must be static strings, error in ${fullname}, line $lineno"
}
$values = $vAst.FindAll({$args[0] -is "System.Management.Automation.Language.StringConstantExpressionAst"},$true).Value
$values | % {
if (@('REQUIREADMINONWINDOWS', 'SLOW') -contains $_) {
# These are valid tags also, but they are not the priority tags
}
elseif (@('CI', 'FEATURE', 'SCENARIO') -contains $_) {
$foundPriorityTags += $_
}
else {
$warnings += "${fullname} includes improper tag '$_', line '$lineno'"
}
$alltags[$_]++
}
}
}
if ( $foundPriorityTags.Count -eq 0 ) {
$warnings += "${fullname}:$lineno does not include -Tag in Describe"
}
elseif ( $foundPriorityTags.Count -gt 1 ) {
$warnings += "${fullname}:$lineno includes more then one scope -Tag: $foundPriorityTags"
}
}
}
if ( $Warnings.Count -gt 0 ) {
$alltags['Result'] = "Fail"
}
else {
$alltags['Result'] = "Pass"
}
$alltags['Warnings'] = $warnings
$o = [pscustomobject]$alltags
$o.psobject.TypeNames.Add("DescribeTagsInUse")
$o
}
function Publish-PSTestTools {
[CmdletBinding()]
param()
Find-Dotnet
$tools = "$PSScriptRoot/test/tools/EchoArgs","$PSScriptRoot/test/tools/CreateChildProcess"
# Publish EchoArgs so it can be run by tests
foreach ($tool in $tools)
{
Push-Location $tool
try {
dotnet publish --output bin
} finally {
Pop-Location
}
}
}
function Start-PSPester {
[CmdletBinding()]
param(
[string]$OutputFormat = "NUnitXml",
[string]$OutputFile = "pester-tests.xml",
[string[]]$ExcludeTag = 'Slow',
[string[]]$Tag = "CI",
[string]$Path = "$PSScriptRoot/test/powershell",
[switch]$ThrowOnFailure,
[switch]$FullCLR,
[string]$binDir = (Split-Path (New-PSOptions -FullCLR:$FullCLR).Output),
[string]$powershell = (Join-Path $binDir 'powershell'),
[string]$Pester = ([IO.Path]::Combine($binDir, "Modules", "Pester")),
[switch]$Unelevate,
[switch]$Quiet
)
# we need to do few checks and if user didn't provide $ExcludeTag explicitly, we should alternate the default
if ($Unelevate)
{
if (-not $IsWindows)
{
throw '-Unelevate is currently not supported on non-Windows platforms'
}
if (-not $IsAdmin)
{
throw '-Unelevate cannot be applied because the current user is not Administrator'
}
if (-not $PSBoundParameters.ContainsKey('ExcludeTag'))
{
$ExcludeTag += 'RequireAdminOnWindows'
}
}
elseif ($IsWindows -and (-not $IsAdmin))
{
if (-not $PSBoundParameters.ContainsKey('ExcludeTag'))
{
$ExcludeTag += 'RequireAdminOnWindows'
}
}
Write-Verbose "Running pester tests at '$path' with tag '$($Tag -join ''', ''')' and ExcludeTag '$($ExcludeTag -join ''', ''')'" -Verbose
Publish-PSTestTools
# All concatenated commands/arguments are suffixed with the delimiter (space)
$Command = ""
# Windows needs the execution policy adjusted
if ($IsWindows) {
$Command += "Set-ExecutionPolicy -Scope Process Unrestricted; "
}
$startParams = @{binDir=$binDir}
if(!$FullCLR)
{
$Command += "Import-Module '$Pester'; "
}
if ($Unelevate)
{
$outputBufferFilePath = [System.IO.Path]::GetTempFileName()
}
$Command += "Invoke-Pester "
$Command += "-OutputFormat ${OutputFormat} -OutputFile ${OutputFile} "
if ($ExcludeTag -and ($ExcludeTag -ne "")) {
$Command += "-ExcludeTag @('" + (${ExcludeTag} -join "','") + "') "
}
if ($Tag) {
$Command += "-Tag @('" + (${Tag} -join "','") + "') "
}
# sometimes we need to eliminate Pester output, especially when we're
# doing a daily build as the log file is too large
if ( $Quiet ) {
$Command += "-Quiet "
}
$Command += "'" + $Path + "'"
if ($Unelevate)
{
$Command += " *> $outputBufferFilePath; '__UNELEVATED_TESTS_THE_END__' >> $outputBufferFilePath"
}
Write-Verbose $Command
# To ensure proper testing, the module path must not be inherited by the spawned process
if($FullCLR)
{
Start-DevPowerShell -binDir $binDir -FullCLR -NoNewWindow -ArgumentList '-noprofile', '-noninteractive' -Command $command
}
else {
try {
$originalModulePath = $env:PSMODULEPATH
if ($Unelevate)
{
Start-UnelevatedProcess -process $powershell -arguments @('-noprofile', '-c', $Command)
$currentLines = 0
while ($true)
{
$lines = Get-Content $outputBufferFilePath | Select-Object -Skip $currentLines
$lines | Write-Host
if ($lines | ? { $_ -eq '__UNELEVATED_TESTS_THE_END__'})
{
break
}
$count = ($lines | measure-object).Count
if ($count -eq 0)
{
sleep 1
}
else
{
$currentLines += $count
}
}
}
else
{
& $powershell -noprofile -c $Command
}
} finally {
$env:PSMODULEPATH = $originalModulePath
if ($Unelevate)
{
Remove-Item $outputBufferFilePath
}
}
}
if($ThrowOnFailure)
{
Test-PSPesterResults -TestResultsFile $OutputFile
}
}
function script:Start-UnelevatedProcess
{
param(
[string]$process,
[string[]]$arguments
)
if (-not $IsWindows)
{
throw "Start-UnelevatedProcess is currently not supported on non-Windows platforms"
}
runas.exe /trustlevel:0x20000 "$process $arguments"
}
function Show-PSPesterError
{
param ( [Xml.XmlElement]$testFailure )
logerror ("Description: " + $testFailure.description)
logerror ("Name: " + $testFailure.name)
logerror "message:"
logerror $testFailure.failure.message
logerror "stack-trace:"
logerror $testFailure.failure."stack-trace"
}
#
# Read the test result file and
# Throw if a test failed
function Test-PSPesterResults
{
param(
[string]$TestResultsFile = "pester-tests.xml",
[string]$TestArea = 'test/powershell'
)
if(!(Test-Path $TestResultsFile))
{
throw "Test result file '$testResultsFile' not found for $TestArea."
}
$x = [xml](Get-Content -raw $testResultsFile)
if ([int]$x.'test-results'.failures -gt 0)
{
logerror "TEST FAILURES"
# switch between methods, SelectNode is not available on dotnet core
if ( "System.Xml.XmlDocumentXPathExtensions" -as [Type] ) {
$failures = [System.Xml.XmlDocumentXPathExtensions]::SelectNodes($x."test-results",'.//test-case[@result = "Failure"]')
}
else {
$failures = $x.SelectNodes('.//test-case[@result = "Failure"]')
}
foreach ( $testfail in $failures )
{
Show-PSPesterError $testfail
}
throw "$($x.'test-results'.failures) tests in $TestArea failed"
}
}
function Start-PSxUnit {
[CmdletBinding()]param()
log "xUnit tests are currently disabled pending fixes due to API and AssemblyLoadContext changes - @andschwa"
return
if ($IsWindows) {
throw "xUnit tests are only currently supported on Linux / OS X"
}
if ($IsOSX) {
log "Not yet supported on OS X, pretending they passed..."
return
}
# Add .NET CLI tools to PATH
Find-Dotnet
$Arguments = "--configuration", "Linux", "-parallel", "none"
if ($PSCmdlet.MyInvocation.BoundParameters["Verbose"].IsPresent) {
$Arguments += "-verbose"
}
$Content = Split-Path -Parent (Get-PSOutput)
if (-not (Test-Path $Content)) {
throw "PowerShell must be built before running tests!"
}
try {
Push-Location $PSScriptRoot/test/csharp
# Path manipulation to obtain test project output directory
$Output = Join-Path $pwd ((Split-Path -Parent (Get-PSOutput)) -replace (New-PSOptions).Top)
Write-Verbose "Output is $Output"
Copy-Item -ErrorAction SilentlyContinue -Recurse -Path $Content/* -Include Modules,libpsl-native* -Destination $Output
Start-NativeExecution { dotnet test $Arguments }
if ($LASTEXITCODE -ne 0) {
throw "$LASTEXITCODE xUnit tests failed"
}
} finally {
Pop-Location
}
}
function Install-Dotnet {
[CmdletBinding()]
param(
[string]$Channel = "preview",
[string]$Version,
[switch]$NoSudo
)
# This allows sudo install to be optional; needed when running in containers / as root
# Note that when it is null, Invoke-Expression (but not &) must be used to interpolate properly
$sudo = if (!$NoSudo) { "sudo" }
$obtainUrl = "https://raw.githubusercontent.com/dotnet/cli/v1.0.0-preview2-1-3177/scripts/obtain"
# Install for Linux and OS X
if ($IsLinux -or $IsOSX) {
# Uninstall all previous dotnet packages
$uninstallScript = if ($IsUbuntu) {
"dotnet-uninstall-debian-packages.sh"
} elseif ($IsOSX) {
"dotnet-uninstall-pkgs.sh"
}
if ($uninstallScript) {
Start-NativeExecution {
curl -sO $obtainUrl/uninstall/$uninstallScript
Invoke-Expression "$sudo bash ./$uninstallScript"
}
} else {
Write-Warning "This script only removes prior versions of dotnet for Ubuntu 14.04 and OS X"
}
# Install new dotnet 1.1.0 preview packages
$installScript = "dotnet-install.sh"
Start-NativeExecution {
curl -sO $obtainUrl/$installScript
bash ./$installScript -c $Channel -v $Version
}
# .NET Core's crypto library needs brew's OpenSSL libraries added to its rpath
if ($IsOSX) {
# This is the library shipped with .NET Core
# This is allowed to fail as the user may have installed other versions of dotnet
Write-Warning ".NET Core links the incorrect OpenSSL, correcting .NET CLI libraries..."
find $env:HOME/.dotnet -name System.Security.Cryptography.Native.OpenSsl.dylib | % { sudo install_name_tool -add_rpath /usr/local/opt/openssl/lib $_ }
}
} elseif ($IsWindows) {
Remove-Item -ErrorAction SilentlyContinue -Recurse -Force ~\AppData\Local\Microsoft\dotnet
$installScript = "dotnet-install.ps1"
Invoke-WebRequest -Uri $obtainUrl/$installScript -OutFile $installScript
& ./$installScript -Channel $Channel -Version $Version
}
}
function Get-RedHatPackageManager {
if ($IsRedHatFamily -and $IsCentOS) {
"yum"
} elseif ($IsRedHatFamily -and $IsFedora) {
"dnf"
} else {
throw "Error determining package manager for this distribution."
}
}
function Start-PSBootstrap {
[CmdletBinding(
SupportsShouldProcess=$true,
ConfirmImpact="High")]
param(
[string]$Channel = "preview",
# we currently pin dotnet-cli version, because tool
# is currently migrating to msbuild toolchain
# and requires constant updates to our build process.
[string]$Version = "1.0.0-preview2-1-003177",
[switch]$Package,
[switch]$NoSudo,
[switch]$Force
)
log "Installing PowerShell build dependencies"
Push-Location $PSScriptRoot/tools
# This allows sudo install to be optional; needed when running in containers / as root
# Note that when it is null, Invoke-Expression (but not &) must be used to interpolate properly
$sudo = if (!$NoSudo) { "sudo" }