| |
| |
|
|
| [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingConvertToSecureStringWithPlainText', '')] |
| param() |
|
|
| Set-StrictMode -Version 3.0 |
|
|
| $ErrorActionPreference = 'continue' |
| $repoRoot = Join-Path $PSScriptRoot '..' |
| $script:administratorsGroupSID = "S-1-5-32-544" |
| $script:usersGroupSID = "S-1-5-32-545" |
|
|
| |
| $dotNetPath = "$env:USERPROFILE\Appdata\Local\Microsoft\dotnet" |
| if(Test-Path $dotNetPath) |
| { |
| $env:PATH = $dotNetPath + ';' + $env:PATH |
| } |
|
|
| |
| |
| Write-Verbose "Importing build.psm1" -Verbose |
| Import-Module (Join-Path $repoRoot 'build.psm1') -Scope Global -ArgumentList $true |
| $buildCommands = Get-Command -Module build |
| Write-Verbose "Imported build.psm1 commands: $($buildCommands.Count)" -Verbose |
|
|
| Write-Verbose "Importing packaging.psm1" -Verbose |
| Import-Module (Join-Path $repoRoot 'tools\packaging') -Scope Global |
| $packagingCommands = Get-Command -Module packaging |
| Write-Verbose "Imported packaging.psm1 commands: $($packagingCommands.Count)" -Verbose |
|
|
| |
| if($PSVersionTable.PSEdition -eq 'Desktop' -or $IsWindows) |
| { |
| Import-Module (Join-Path $PSScriptRoot 'WindowsCI.psm1') -Scope Global |
| } |
|
|
| |
| |
| |
| Function Test-DailyBuild |
| { |
| $trueString = 'True' |
| if(($env:PS_DAILY_BUILD -eq $trueString) -or $env:BUILD_REASON -eq 'Schedule') |
| { |
| return $true |
| } |
| return $false |
| } |
|
|
| |
| Function Set-BuildVariable |
| { |
| param( |
| [Parameter(Mandatory=$true)] |
| [string] |
| $Name, |
|
|
| [Parameter(Mandatory=$true)] |
| [string] |
| $Value, |
|
|
| [switch] |
| $IsOutput |
| ) |
|
|
| $IsOutputString = if ($IsOutput) { 'true' } else { 'false' } |
| $command = "vso[task.setvariable variable=$Name;isOutput=$IsOutputString]$Value" |
|
|
| |
| Write-Verbose -Message "sending command: $command" -Verbose |
|
|
| if ($env:TF_BUILD) { |
| |
| Write-Host "##$command" |
| |
| } |
|
|
| |
| Set-Item env:/$name -Value $Value |
| } |
|
|
| |
| function Invoke-CIFull |
| { |
| param( |
| [switch] $CleanRepo |
| ) |
| if($CleanRepo) |
| { |
| Clear-PSRepo |
| } |
|
|
| Invoke-CIInstall |
| Invoke-CIBuild |
| Invoke-CITest -ErrorAction Continue |
| Invoke-CIFinish |
| } |
|
|
| |
| function Invoke-CIBuild |
| { |
| param( |
| [ValidateSet('Debug', 'Release', 'CodeCoverage', 'StaticAnalysis')] |
| [string]$Configuration = 'Release' |
| ) |
|
|
| $releaseTag = Get-ReleaseTag |
| |
| $result = Get-PesterTag |
| if ( $result.Result -ne "Pass" ) |
| { |
| $result.Warnings |
| throw "Tags must be CI, Feature, Scenario, or Slow" |
| } |
|
|
| if(Test-DailyBuild) |
| { |
| Start-PSBuild -Configuration 'CodeCoverage' -PSModuleRestore -CI -ReleaseTag $releaseTag |
| } |
|
|
| Start-PSBuild -PSModuleRestore -Configuration $Configuration -CI -ReleaseTag $releaseTag -UseNuGetOrg |
| Save-PSOptions |
|
|
| $options = (Get-PSOptions) |
|
|
| $path = Split-Path -Path $options.Output |
|
|
| $psOptionsPath = (Join-Path -Path $PSScriptRoot -ChildPath '../psoptions.json') |
| $buildZipPath = (Join-Path -Path $PSScriptRoot -ChildPath '../build.zip') |
|
|
| Compress-Archive -Path $path -DestinationPath $buildZipPath |
|
|
| Push-Artifact -Path $psOptionsPath -Name 'build' |
| Push-Artifact -Path $buildZipPath -Name 'build' |
| } |
|
|
| |
| function Invoke-CIInstall |
| { |
| param( |
| [switch] |
| $SkipUser |
| ) |
|
|
| |
| Switch-PSNugetConfig -Source Public |
|
|
| |
| Sync-PSTags -AddRemoteIfMissing |
|
|
| if(Test-DailyBuild) |
| { |
| if ($env:BUILD_REASON -eq 'Schedule') |
| { |
| Write-Host "##vso[build.updatebuildnumber]Daily-$env:BUILD_SOURCEBRANCHNAME-$env:BUILD_SOURCEVERSION-$((Get-Date).ToString("yyyyMMddhhss"))" |
| } |
| } |
|
|
| if ($env:TF_BUILD -and !$SkipUser.IsPresent) |
| { |
| |
| Write-Verbose "Creating account for remoting tests in CI." -Verbose |
|
|
| |
| $randomObj = [System.Random]::new() |
| $password = "" |
| 1..(Get-Random -Minimum 15 -Maximum 126) | ForEach-Object { $password = $password + [char]$randomObj.next(45,126) } |
|
|
| |
| $userName = 'ciRemote' |
| New-LocalUser -username $userName -Password $password |
| Add-UserToGroup -username $userName -groupSid $script:administratorsGroupSID |
|
|
| |
| $ss = ConvertTo-SecureString -String $password -AsPlainText -Force |
| $ciRemoteCredential = [PSCredential]::new("$env:COMPUTERNAME\$userName", $ss) |
| $ciRemoteCredential | Export-Clixml -Path "$env:TEMP\CIRemoteCred.xml" -Force |
|
|
| |
| |
| Write-Log -Message "Checking for LocalAccountTokenFilterPolicy in the CI." |
| $haveLocalAccountTokenFilterPolicy = $false |
| try |
| { |
| $haveLocalAccountTokenFilterPolicy = ((Get-ItemPropertyValue -Path HKLM:SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System -Name LocalAccountTokenFilterPolicy) -eq 1) |
| } |
| |
| catch {} |
| if (!$haveLocalAccountTokenFilterPolicy) |
| { |
| Write-Verbose "Setting the LocalAccountTokenFilterPolicy for remoting tests" |
| Set-ItemProperty -Path HKLM:SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System -Name LocalAccountTokenFilterPolicy -Value 1 |
| } |
| } |
|
|
| Set-BuildVariable -Name TestPassed -Value False |
| } |
|
|
| function Invoke-CIxUnit |
| { |
| param( |
| [switch] |
| $SkipFailing |
| ) |
| $env:CoreOutput = Split-Path -Parent (Get-PSOutput -Options (Get-PSOptions)) |
| $path = "$env:CoreOutput\pwsh.exe" |
| if($IsMacOS -or $IsLinux) |
| { |
| $path = "$env:CoreOutput\pwsh" |
| } |
|
|
| if(!(Test-Path $path)) |
| { |
| throw "CoreCLR pwsh.exe was not built" |
| } |
|
|
| $xUnitTestResultsFile = Join-Path -Path $PWD -ChildPath "xUnitTestResults.xml" |
|
|
| Start-PSxUnit -xUnitTestResultsFile $xUnitTestResultsFile |
| Push-Artifact -Path $xUnitTestResultsFile -name xunit |
|
|
| if(!$SkipFailing.IsPresent) |
| { |
| |
| Test-XUnitTestResults -TestResultsFile $xUnitTestResultsFile |
| } |
| } |
|
|
| |
| function Install-CIPester |
| { |
| [CmdletBinding()] |
| param( |
| [string]$MinimumVersion = '5.0.0', |
| [string]$MaximumVersion = '5.99.99', |
| [switch]$Force |
| ) |
|
|
| Write-Verbose "Checking for Pester module (required: $MinimumVersion - $MaximumVersion)" -Verbose |
|
|
| |
| $installedPester = Get-Module -Name Pester -ListAvailable | |
| Where-Object { $_.Version -ge $MinimumVersion -and $_.Version -le $MaximumVersion } | |
| Sort-Object -Property Version -Descending | |
| Select-Object -First 1 |
|
|
| if ($installedPester -and -not $Force) { |
| Write-Host "Pester version $($installedPester.Version) is already installed and meets requirements" -ForegroundColor Green |
| return |
| } |
|
|
| if ($Force) { |
| Write-Host "Installing Pester module (forced)" -ForegroundColor Yellow |
| } else { |
| Write-Host "Installing Pester module" -ForegroundColor Yellow |
| } |
|
|
| try { |
| Install-Module -Name Pester -Force -SkipPublisherCheck -MaximumVersion $MaximumVersion -ErrorAction Stop |
| Write-Host "Successfully installed Pester module" -ForegroundColor Green |
| } |
| catch { |
| Write-Error "Failed to install Pester module: $_" |
| throw |
| } |
| } |
|
|
| |
| function Invoke-CITest |
| { |
| [CmdletBinding()] |
| param( |
| [ValidateSet('UnelevatedPesterTests', 'ElevatedPesterTests')] |
| [string] $Purpose, |
| [ValidateSet('CI', 'Others')] |
| [string] $TagSet, |
| [string] $TitlePrefix, |
| [string] $OutputFormat = "NUnitXml" |
| ) |
|
|
| Write-Verbose -Verbose "CI test: OutputFormat: $OutputFormat" |
|
|
| |
| Set-CorrectLocale |
|
|
| |
| |
| |
| switch ($TagSet) { |
| 'CI' { |
| Write-Host -Foreground Green 'Running "CI" CoreCLR tests..' |
| $ExcludeTag = @('Slow', 'Feature', 'Scenario') |
| } |
| 'Others' { |
| Write-Host -Foreground Green 'Running non-CI CoreCLR tests..' |
| $ExcludeTag = @('CI') |
| } |
| Default { |
| throw "Unknown TagSet: '$TagSet'" |
| } |
| } |
|
|
| if($IsLinux -or $IsMacOS) |
| { |
| return Invoke-LinuxTestsCore -Purpose $Purpose -ExcludeTag $ExcludeTag -TagSet $TagSet -TitlePrefix $TitlePrefix -OutputFormat $OutputFormat |
| } |
|
|
| |
|
|
| $env:CoreOutput = Split-Path -Parent (Get-PSOutput -Options (Get-PSOptions)) |
| Write-Host -Foreground Green 'Run CoreCLR tests' |
| $testResultsNonAdminFile = "$PWD\TestsResultsNonAdmin-$TagSet.xml" |
| $testResultsAdminFile = "$PWD\TestsResultsAdmin-$TagSet.xml" |
| if(!(Test-Path "$env:CoreOutput\pwsh.exe")) |
| { |
| throw "CoreCLR pwsh.exe was not built" |
| } |
|
|
| |
| $ExperimentalFeatureTests = Get-ExperimentalFeatureTests |
|
|
| if ($Purpose -eq 'UnelevatedPesterTests') { |
| $unelevate = $true |
| $environment = Get-EnvironmentInformation |
| if ($environment.OSArchitecture -eq 'arm64') { |
| Write-Verbose -Verbose "running on arm64, running unelevated tests as elevated" |
| $unelevate = $false |
| } |
|
|
| $arguments = @{ |
| Bindir = $env:CoreOutput |
| OutputFile = $testResultsNonAdminFile |
| Unelevate = $unelevate |
| Terse = $true |
| Tag = @() |
| ExcludeTag = $ExcludeTag + 'RequireAdminOnWindows' |
| OutputFormat = $OutputFormat |
| } |
|
|
| $title = "Pester Unelevated - $TagSet" |
| if ($TitlePrefix) { |
| $title = "$TitlePrefix - $title" |
| } |
| Write-Verbose -Verbose "Starting Pester with output format $($arguments.OutputFormat)" |
| Start-PSPester @arguments -Title $title |
|
|
| |
| Test-PSPesterResults -TestResultsFile $testResultsNonAdminFile |
|
|
| |
| foreach ($entry in $ExperimentalFeatureTests.GetEnumerator()) |
| { |
| $featureName = $entry.Key |
| $testFiles = $entry.Value |
|
|
| $expFeatureTestResultFile = "$PWD\TestsResultsNonAdmin.$featureName.xml" |
| $arguments['OutputFile'] = $expFeatureTestResultFile |
| $arguments['ExperimentalFeatureName'] = $featureName |
| if ($testFiles.Count -eq 0) { |
| |
| |
| $arguments.Remove('Path') |
| } else { |
| |
| $arguments['Path'] = $testFiles |
| } |
|
|
| $title = "Pester Experimental Unelevated - $featureName" |
| if ($TitlePrefix) { |
| $title = "$TitlePrefix - $title" |
| } |
|
|
| |
| Write-Verbose -Verbose "Starting Pester with output format $($arguments.OutputFormat)" |
| Start-PSPester @arguments -Title $title -SkipTestToolBuild |
|
|
| |
| Test-PSPesterResults -TestResultsFile $expFeatureTestResultFile |
| } |
| } |
|
|
| if ($Purpose -eq 'ElevatedPesterTests') { |
| $arguments = @{ |
| Terse = $true |
| Bindir = $env:CoreOutput |
| OutputFile = $testResultsAdminFile |
| Tag = @('RequireAdminOnWindows') |
| ExcludeTag = $ExcludeTag |
| OutputFormat = $OutputFormat |
| } |
|
|
| $title = "Pester Elevated - $TagSet" |
| if ($TitlePrefix) { |
| $title = "$TitlePrefix - $title" |
| } |
|
|
| Write-Verbose -Verbose "Starting Pester with output format $($arguments.OutputFormat)" |
| Start-PSPester @arguments -Title $title |
|
|
| |
| Test-PSPesterResults -TestResultsFile $testResultsAdminFile |
|
|
| |
| foreach ($entry in $ExperimentalFeatureTests.GetEnumerator()) |
| { |
| $featureName = $entry.Key |
| $testFiles = $entry.Value |
|
|
| $expFeatureTestResultFile = "$PWD\TestsResultsAdmin.$featureName.xml" |
| $arguments['OutputFile'] = $expFeatureTestResultFile |
| $arguments['ExperimentalFeatureName'] = $featureName |
| if ($testFiles.Count -eq 0) |
| { |
| |
| |
| $arguments.Remove('Path') |
| } |
| else |
| { |
| |
| $arguments['Path'] = $testFiles |
| } |
|
|
| $title = "Pester Experimental >levated - $featureName" |
| if ($TitlePrefix) { |
| $title = "$TitlePrefix - $title" |
| } |
|
|
| Write-Verbose -Verbose "Starting Pester with output format $($arguments.OutputFormat)" |
| |
| Start-PSPester @arguments -Title $title -SkipTestToolBuild |
|
|
| |
| Test-PSPesterResults -TestResultsFile $expFeatureTestResultFile |
| } |
| } |
|
|
| Set-BuildVariable -Name TestPassed -Value True |
| } |
|
|
| function New-CodeCoverageAndTestPackage |
| { |
| [CmdletBinding()] |
| param() |
|
|
| if (Test-DailyBuild) |
| { |
| Start-PSBuild -Configuration 'CodeCoverage' -Clean |
|
|
| $codeCoverageOutput = Split-Path -Parent (Get-PSOutput) |
| $codeCoverageArtifacts = Compress-CoverageArtifacts -CodeCoverageOutput $codeCoverageOutput |
|
|
| $destBasePath = if ($env:TF_BUILD) { |
| $env:BUILD_ARTIFACTSTAGINGDIRECTORY |
| } else { |
| Join-Path (Get-Location).Path "out" |
| } |
|
|
| if (-not (Test-Path $destBasePath)) |
| { |
| $null = New-Item -ItemType Directory -Path $destBasePath |
| } |
|
|
| Write-Host -ForegroundColor Green 'Upload CodeCoverage artifacts' |
| $codeCoverageArtifacts | ForEach-Object { |
| Copy-Item -Path $_ -Destination $destBasePath |
| $newPath = Join-Path $destBasePath (Split-Path $_ -Leaf) |
| Push-Artifact -Path $newPath -Name 'CodeCoverage' |
| } |
|
|
| New-TestPackage -Destination $destBasePath |
| $testPackageFullName = Join-Path $destBasePath 'TestPackage.zip' |
| Write-Verbose "Created TestPackage.zip" -Verbose |
| Write-Host -ForegroundColor Green 'Upload test package' |
| Push-Artifact $testPackageFullName -Name 'CodeCoverage' |
| } |
| } |
|
|
| |
| function Push-Artifact |
| { |
| param( |
| [Parameter(Mandatory)] |
| [ValidateScript({Test-Path -Path $_})] |
| $Path, |
| [string] |
| $Name |
| ) |
|
|
| if(!$Name) |
| { |
| $artifactName = [system.io.path]::GetFileName($Path) |
| } |
| else |
| { |
| $artifactName = $Name |
| } |
|
|
| if ($env:TF_BUILD) { |
| |
| Write-Host "##vso[artifact.upload containerfolder=$artifactName;artifactname=$artifactName;]$Path" |
| } elseif ($env:GITHUB_WORKFLOW -and $env:RUNNER_WORKSPACE) { |
| |
| $destinationPath = Join-Path -Path $env:RUNNER_WORKSPACE -ChildPath $artifactName |
|
|
| |
| if (!(Test-Path -Path $destinationPath)) { |
| $null = New-Item -ItemType Directory -Path $destinationPath -Force |
| } |
|
|
| Copy-Item -Path $Path -Destination $destinationPath -Force -Verbose |
| } else { |
| Write-Warning "Push-Artifact is not supported in this environment." |
| } |
| } |
|
|
| function Compress-CoverageArtifacts |
| { |
| param([string] $CodeCoverageOutput) |
|
|
| |
| $artifacts = New-Object System.Collections.ArrayList |
|
|
| Add-Type -AssemblyName System.IO.Compression.FileSystem |
| $resolvedPath = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath((Join-Path $PSScriptRoot '..\test\tools\OpenCover')) |
| $zipOpenCoverPath = Join-Path $PWD 'OpenCover.zip' |
| [System.IO.Compression.ZipFile]::CreateFromDirectory($resolvedPath, $zipOpenCoverPath) |
| $null = $artifacts.Add($zipOpenCoverPath) |
|
|
| $zipCodeCoveragePath = Join-Path $PWD "CodeCoverage.zip" |
| Write-Verbose "Zipping ${CodeCoverageOutput} into $zipCodeCoveragePath" -Verbose |
| [System.IO.Compression.ZipFile]::CreateFromDirectory($CodeCoverageOutput, $zipCodeCoveragePath) |
| $null = $artifacts.Add($zipCodeCoveragePath) |
|
|
| return $artifacts |
| } |
|
|
| function Get-ReleaseTag |
| { |
| $metaDataPath = Join-Path -Path $PSScriptRoot -ChildPath 'metadata.json' |
| $metaData = Get-Content $metaDataPath | ConvertFrom-Json |
| $releaseTag = $metadata.NextReleaseTag |
| if($env:BUILD_BUILID) |
| { |
| $releaseTag = $releaseTag.split('.')[0..2] -join '.' |
| $releaseTag = $releaseTag + '.' + $env:BUILD_BUILID |
| } |
| return $releaseTag |
| } |
|
|
| |
| function Invoke-CIFinish |
| { |
| param( |
| [string] $Runtime = 'win7-x64', |
| [string] $Channel = 'preview', |
| [Validateset('Build','Package')] |
| [string[]] $Stage = ('Build','Package') |
| ) |
|
|
| |
| Switch-PSNugetConfig -Source Public |
|
|
| if ($PSEdition -eq 'Core' -and ($IsLinux -or $IsMacOS) -and $Stage -contains 'Build') { |
| return New-LinuxPackage |
| } |
|
|
| $artifacts = New-Object System.Collections.ArrayList |
| try { |
| $buildFolder = "${env:SYSTEM_ARTIFACTSDIRECTORY}/mainBuild" |
|
|
| if ($Stage -contains "Build") { |
| if ($Channel -eq 'preview') { |
| $releaseTag = Get-ReleaseTag |
|
|
| $previewVersion = $releaseTag.Split('-') |
| $previewPrefix = $previewVersion[0] |
| $previewLabel = $previewVersion[1].replace('.','') |
|
|
| if (Test-DailyBuild) { |
| $previewLabel = "daily{0}" -f $previewLabel |
| } |
|
|
| $prereleaseIteration = (get-date).Day |
| $preReleaseVersion = "$previewPrefix-$previewLabel.$prereleaseIteration" |
| |
| Start-PSBuild -PSModuleRestore -Configuration 'Release' -ReleaseTag $preReleaseVersion -Clean -Runtime $Runtime -output $buildFolder -PSOptionsPath "${buildFolder}/psoptions.json" |
| $options = Get-PSOptions |
| |
| $filter = Join-Path -Path (Split-Path $options.Output) -ChildPath '*.pdb' |
| Write-Verbose "Removing symbol files from $filter" -Verbose |
| Remove-Item $filter -Force -Recurse |
| } else { |
| $releaseTag = Get-ReleaseTag |
| $releaseTagParts = $releaseTag.split('.') |
| $preReleaseVersion = $releaseTagParts[0]+ ".9.9" |
| Write-Verbose "newPSReleaseTag: $preReleaseVersion" -Verbose |
| Start-PSBuild -PSModuleRestore -Configuration 'Release' -ReleaseTag $preReleaseVersion -Clean -Runtime $Runtime -output $buildFolder -PSOptionsPath "${buildFolder}/psoptions.json" |
| $options = Get-PSOptions |
| |
| $filter = Join-Path -Path (Split-Path $options.Output) -ChildPath '*.pdb' |
| Write-Verbose "Removing symbol files from $filter" -Verbose |
| Remove-Item $filter -Force -Recurse |
| } |
|
|
| |
| $env:CI_FINISH_RELASETAG=$preReleaseVersion |
| $vstsCommandString = "vso[task.setvariable variable=CI_FINISH_RELASETAG]$preReleaseVersion" |
| Write-Verbose -Message "$vstsCommandString" -Verbose |
| Write-Host -Object "##$vstsCommandString" |
| } |
|
|
| if ($Stage -contains "Package") { |
| Restore-PSOptions -PSOptionsPath "${buildFolder}/psoptions.json" |
| $preReleaseVersion = $env:CI_FINISH_RELASETAG |
|
|
| |
| $packageTypes = 'zip', 'zip-pdb', 'msix' |
|
|
| $packages = Start-PSPackage -Type $packageTypes -ReleaseTag $preReleaseVersion -SkipReleaseChecks -WindowsRuntime $Runtime |
|
|
| foreach ($package in $packages) { |
| if (Test-Path $package -ErrorAction Ignore) { |
| Write-Log "Package found: $package" |
| } else { |
| Write-Warning -Message "Package NOT found: $package" |
| } |
|
|
| if ($package -is [string]) { |
| $null = $artifacts.Add($package) |
| } |
| } |
|
|
| } |
| } catch { |
| Get-Error -InputObject $_ |
| throw |
| } finally { |
| $pushedAllArtifacts = $true |
|
|
| $artifacts | ForEach-Object { |
| Write-Log -Message "Pushing $_ as CI artifact" |
| if (Test-Path $_) { |
| Push-Artifact -Path $_ -Name 'artifacts' |
| } else { |
| $pushedAllArtifacts = $false |
| Write-Warning "Artifact $_ does not exist." |
| } |
| } |
|
|
| if (!$pushedAllArtifacts) { |
| throw "Some artifacts did not exist!" |
| } |
| } |
| } |
|
|
| function Set-Path |
| { |
| param |
| ( |
| [Parameter(Mandatory)] |
| [string] |
| $Path, |
|
|
| [Parameter(Mandatory)] |
| [switch] |
| $Append |
| ) |
|
|
| $machinePathString = [System.Environment]::GetEnvironmentVariable('path',[System.EnvironmentVariableTarget]::Machine) |
| $machinePath = $machinePathString -split ';' |
|
|
| if($machinePath -inotcontains $path) |
| { |
| $newPath = "$machinePathString;$path" |
| Write-Verbose "Adding $path to path..." -Verbose |
| [System.Environment]::SetEnvironmentVariable('path',$newPath,[System.EnvironmentVariableTarget]::Machine) |
| Write-Verbose "Added $path to path." -Verbose |
| } |
| else |
| { |
| Write-Verbose "$path already in path." -Verbose |
| } |
| } |
|
|
| |
| function Show-Environment |
| { |
| Write-LogGroupStart -Title 'Environment' |
| Get-ChildItem -Path env: | Out-String -width 9999 -Stream | Write-Verbose -Verbose |
| Write-LogGroupEnd -Title 'Environment' |
| } |
|
|
| |
| function Invoke-BootstrapStage |
| { |
| $createPackages = Test-DailyBuild |
| Write-Log -Message "Executing ci.psm1 Bootstrap Stage" |
| |
| Sync-PSTags -AddRemoteIfMissing |
| Start-PSBootstrap -Scenario Package:$createPackages |
| } |
|
|
| |
| function Invoke-LinuxTestsCore |
| { |
| [CmdletBinding()] |
| param( |
| [ValidateSet('UnelevatedPesterTests', 'ElevatedPesterTests', 'All')] |
| [string] $Purpose = 'All', |
| [string[]] $ExcludeTag = @('Slow', 'Feature', 'Scenario'), |
| [string] $TagSet = 'CI', |
| [string] $TitlePrefix, |
| [string] $OutputFormat = "NUnitXml" |
| ) |
|
|
| $output = Split-Path -Parent (Get-PSOutput -Options (Get-PSOptions)) |
| $testResultsNoSudo = "$PWD/TestResultsNoSudo.xml" |
| $testResultsSudo = "$PWD/TestResultsSudo.xml" |
| $testExcludeTag = $ExcludeTag + 'RequireSudoOnUnix' |
| $pesterPassThruNoSudoObject = $null |
| $pesterPassThruSudoObject = $null |
| $noSudoResultsWithExpFeatures = $null |
| $sudoResultsWithExpFeatures = $null |
|
|
| $noSudoPesterParam = @{ |
| 'BinDir' = $output |
| 'PassThru' = $true |
| 'Terse' = $true |
| 'Tag' = @() |
| 'ExcludeTag' = $testExcludeTag |
| 'OutputFile' = $testResultsNoSudo |
| 'OutputFormat' = $OutputFormat |
| } |
|
|
| |
| $ExperimentalFeatureTests = Get-ExperimentalFeatureTests |
|
|
| |
| if($Purpose -eq 'UnelevatedPesterTests' -or $Purpose -eq 'All') |
| { |
| $title = "Pester No Sudo - $TagSet" |
| if ($TitlePrefix) { |
| $title = "$TitlePrefix - $title" |
| } |
| $pesterPassThruNoSudoObject = Start-PSPester @noSudoPesterParam -Title $title |
|
|
| |
| $noSudoResultsWithExpFeatures = @() |
| foreach ($entry in $ExperimentalFeatureTests.GetEnumerator()) { |
| $featureName = $entry.Key |
| $testFiles = $entry.Value |
|
|
| $expFeatureTestResultFile = "$PWD\TestResultsNoSudo.$featureName.xml" |
| $noSudoPesterParam['OutputFile'] = $expFeatureTestResultFile |
| $noSudoPesterParam['ExperimentalFeatureName'] = $featureName |
| if ($testFiles.Count -eq 0) { |
| |
| |
| $noSudoPesterParam.Remove('Path') |
| } |
| else |
| { |
| |
| $noSudoPesterParam['Path'] = $testFiles |
| } |
| $title = "Pester Experimental No Sudo - $featureName - $TagSet" |
| if ($TitlePrefix) { |
| $title = "$TitlePrefix - $title" |
| } |
| $passThruResult = Start-PSPester @noSudoPesterParam -Title $title -SkipTestToolBuild |
|
|
| $noSudoResultsWithExpFeatures += $passThruResult |
| } |
| } |
|
|
| |
| if($Purpose -eq 'ElevatedPesterTests' -or $Purpose -eq 'All') |
| { |
| $sudoPesterParam = $noSudoPesterParam.Clone() |
| $sudoPesterParam.Remove('Path') |
| $sudoPesterParam['Tag'] = @('RequireSudoOnUnix') |
| $sudoPesterParam['ExcludeTag'] = $ExcludeTag |
| $sudoPesterParam['Sudo'] = $true |
| $sudoPesterParam['OutputFile'] = $testResultsSudo |
| $sudoPesterParam['OutputFormat'] = $OutputFormat |
|
|
| $title = "Pester Sudo - $TagSet" |
| if ($TitlePrefix) { |
| $title = "$TitlePrefix - $title" |
| } |
| $pesterPassThruSudoObject = Start-PSPester @sudoPesterParam -Title $title |
|
|
| |
| $sudoResultsWithExpFeatures = @() |
| foreach ($entry in $ExperimentalFeatureTests.GetEnumerator()) { |
| $featureName = $entry.Key |
| $testFiles = $entry.Value |
|
|
| $expFeatureTestResultFile = "$PWD\TestResultsSudo.$featureName.xml" |
| $sudoPesterParam['OutputFile'] = $expFeatureTestResultFile |
| $sudoPesterParam['ExperimentalFeatureName'] = $featureName |
| if ($testFiles.Count -eq 0) |
| { |
| |
| |
| $sudoPesterParam.Remove('Path') |
| } |
| else |
| { |
| |
| $sudoPesterParam['Path'] = $testFiles |
| } |
|
|
| $title = "Pester Experimental Sudo - $featureName - $TagSet" |
| if ($TitlePrefix) { |
| $title = "$TitlePrefix - $title" |
| } |
|
|
| |
| $passThruResult = Start-PSPester @sudoPesterParam -Title $title -SkipTestToolBuild |
|
|
| $sudoResultsWithExpFeatures += $passThruResult |
| } |
| } |
|
|
| |
| try { |
| $allTestResultsWithNoExpFeature = @($pesterPassThruNoSudoObject, $pesterPassThruSudoObject) |
| $allTestResultsWithExpFeatures = @($noSudoResultsWithExpFeatures, $sudoResultsWithExpFeatures) |
| |
| $allTestResultsWithNoExpFeature | Where-Object {$null -ne $_} | ForEach-Object { Test-PSPesterResults -ResultObject $_ } |
| $allTestResultsWithExpFeatures | Where-Object {$null -ne $_} | ForEach-Object { Test-PSPesterResults -ResultObject $_ -CanHaveNoResult } |
| $result = "PASS" |
| } catch { |
| |
| $resultError = $_ |
| $result = "FAIL" |
| } |
|
|
| |
| if ( $result -eq "FAIL" ) |
| { |
| Write-Warning "Tests failed. See the issue below." |
| Throw $resultError |
| } |
| else |
| { |
| Write-Verbose "Tests did not fail! Nice job!" |
| } |
| } |
|
|
| function New-LinuxPackage |
| { |
|
|
| $isFullBuild = Test-DailyBuild |
| $releaseTag = Get-ReleaseTag |
| $packageParams = @{} |
| $packageParams += @{ReleaseTag=$releaseTag} |
|
|
| |
| |
| $packages = @(Start-PSPackage @packageParams -SkipReleaseChecks -Type deb, rpm, rpm-fxdependent-arm64, tar) |
| foreach($package in $packages) |
| { |
| if (Test-Path $package) |
| { |
| Write-Log "Package found: $package" |
| } |
| else |
| { |
| Write-Error -Message "Package NOT found: $package" |
| } |
|
|
| if ($package -isnot [System.IO.FileInfo]) |
| { |
| $packageObj = Get-Item $package |
| Write-Error -Message "The PACKAGE is not a FileInfo object" |
| } |
| else |
| { |
| $packageObj = $package |
| } |
|
|
| |
| $artifactsDir = if ($env:GITHUB_ACTIONS -eq 'true') { |
| "${env:GITHUB_WORKSPACE}/../packages" |
| } else { |
| "${env:BUILD_ARTIFACTSTAGINGDIRECTORY}" |
| } |
|
|
| |
| if (-not (Test-Path $artifactsDir)) { |
| New-Item -ItemType Directory -Path $artifactsDir -Force | Out-Null |
| } |
|
|
| Write-Log -message "Artifacts directory: $artifactsDir" |
| Copy-Item $packageObj.FullName -Destination $artifactsDir -Force |
| } |
|
|
| if ($IsLinux) |
| { |
| |
| $artifactsDir = if ($env:GITHUB_ACTIONS -eq 'true') { |
| "${env:GITHUB_WORKSPACE}/../packages" |
| } else { |
| "${env:BUILD_ARTIFACTSTAGINGDIRECTORY}" |
| } |
|
|
| |
| |
| Start-PSBuild -PSModuleRestore -Clean -Runtime linux-arm -Configuration 'Release' |
| $armPackage = Start-PSPackage @packageParams -Type tar-arm -SkipReleaseChecks |
| Copy-Item $armPackage -Destination $artifactsDir -Force |
| } |
| } |
|
|
| function Invoke-InitializeContainerStage { |
| param( |
| [string] |
| $ContainerPattern = '.' |
| ) |
|
|
| Write-Verbose "Invoking InitializeContainerStage with ContainerPattern: ${ContainerPattern}" -Verbose |
|
|
| $fallbackSeed = (get-date).DayOfYear |
| Write-Verbose "Fall back seed: $fallbackSeed" -Verbose |
|
|
| |
| $seed = $env:SYSTEM_PULLREQUEST_PULLREQUESTID |
|
|
| if(!$seed) { |
| |
| $seed = $fallbackSeed |
| } |
|
|
| |
| if ($seed -ge [Int32]::MaxValue) { |
| $seed = [int]($seed -band [int]::MaxValue) |
| } |
|
|
| Write-Verbose "Seed: $seed" -Verbose |
|
|
| |
| $matrix = ./PowerShell-Docker/build.ps1 -GenerateMatrixJson -FullJson -Channel preview | ConvertFrom-Json |
|
|
| |
| $linuxImages = $matrix.preview | |
| Where-Object {$_.IsLinux -and $_.UseInCi -and $_.DistributionState -match 'Validat.*' -and $_.JobName -match $ContainerPattern -and $_.JobName -notlike "*arm*"} | |
| Select-Object JobName, Taglist | |
| Sort-Object -property JobName |
|
|
| |
| $index = Get-Random -Minimum 0 -Maximum $linuxImages.Count -SetSeed $seed |
| $selectedImage = $linuxImages[$index] |
|
|
| |
| $tag = $selectedImage.Taglist -split ';' | Where-Object {$_ -match 'preview-\D+'} | Select-Object -First 1 |
|
|
| |
| $containerName = "mcr.microsoft.com/powershell/test-deps:$tag" |
|
|
| Set-BuildVariable -Name containerName -Value $containerName -IsOutput |
| Set-BuildVariable -Name containerBuildName -Value $selectedImage.JobName -IsOutput |
|
|
| if($env:BUILD_REASON -eq 'PullRequest') { |
| Write-Host "##vso[build.updatebuildnumber]PR-${env:SYSTEM_PULLREQUEST_PULLREQUESTNUMBER}-$($selectedImage.JobName)-$((get-date).ToString("yyyyMMddhhmmss"))" |
| } else { |
| Write-Host "##vso[build.updatebuildnumber]${env:BUILD_SOURCEBRANCHNAME}-$($selectedImage.JobName)-${env:BUILD_SOURCEVERSION}-$((get-date).ToString("yyyyMMddhhmmss"))" |
|
|
| |
| Write-Host "##vso[build.addbuildtag]$($selectedImage.JobName)" |
| } |
| } |
|
|
| Function Test-MergeConflictMarker |
| { |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| [CmdletBinding()] |
| param( |
| [Parameter()] |
| [AllowEmptyCollection()] |
| [string[]] $File = @(), |
|
|
| [Parameter()] |
| [string] $WorkspacePath = $PWD, |
|
|
| [Parameter()] |
| [string] $OutputPath = $env:GITHUB_OUTPUT, |
|
|
| [Parameter()] |
| [string] $SummaryPath = $env:GITHUB_STEP_SUMMARY |
| ) |
|
|
| Write-Host "Starting merge conflict marker check..." -ForegroundColor Cyan |
|
|
| |
| function Write-NoFilesOutput { |
| param( |
| [string]$Message, |
| [string]$OutputPath, |
| [string]$SummaryPath |
| ) |
|
|
| |
| if ($OutputPath) { |
| "files-checked=0" | Out-File -FilePath $OutputPath -Append -Encoding utf8 |
| "conflicts-found=0" | Out-File -FilePath $OutputPath -Append -Encoding utf8 |
| } |
|
|
| |
| if ($SummaryPath) { |
| $summaryContent = @" |
| # Merge Conflict Marker Check Results |
| |
| ## Summary |
| - **Files Checked:** 0 |
| - **Files with Conflicts:** 0 |
| |
| ## ℹ️ No Files to Check |
| |
| $Message |
| |
| "@ |
| $summaryContent | Out-File -FilePath $SummaryPath -Encoding utf8 |
| } |
| } |
|
|
| |
| if ($File.Count -eq 0) { |
| Write-Host "No files to check (empty file list)" -ForegroundColor Yellow |
| Write-NoFilesOutput -Message "No files were provided for checking (this can happen when a PR only deletes files)." -OutputPath $OutputPath -SummaryPath $SummaryPath |
| return |
| } |
|
|
| |
| $filesToCheck = @($File | Where-Object { $_ -notlike "*.cs" }) |
| $filteredCount = $File.Count - $filesToCheck.Count |
|
|
| if ($filteredCount -gt 0) { |
| Write-Host "Filtered out $filteredCount *.cs file(s) from merge conflict checking" -ForegroundColor Yellow |
| } |
|
|
| if ($filesToCheck.Count -eq 0) { |
| Write-Host "No files to check after filtering (all files were *.cs)" -ForegroundColor Yellow |
| Write-NoFilesOutput -Message "All $filteredCount file(s) were filtered out (*.cs files are excluded from merge conflict checking)." -OutputPath $OutputPath -SummaryPath $SummaryPath |
| return |
| } |
|
|
| Write-Host "Checking $($filesToCheck.Count) changed files for merge conflict markers" -ForegroundColor Cyan |
|
|
| |
| $absolutePaths = $filesToCheck | ForEach-Object { |
| if ([System.IO.Path]::IsPathRooted($_)) { |
| $_ |
| } else { |
| Join-Path $WorkspacePath $_ |
| } |
| } |
|
|
| $filesWithConflicts = @() |
| $filesChecked = 0 |
|
|
| foreach ($filePath in $absolutePaths) { |
| |
| if (-not (Test-Path $filePath)) { |
| Write-Verbose " Skipping deleted file: $filePath" |
| continue |
| } |
|
|
| |
| if ((Get-Item $filePath) -is [System.IO.DirectoryInfo]) { |
| continue |
| } |
|
|
| $filesChecked++ |
|
|
| |
| $relativePath = if ($WorkspacePath -and $filePath.StartsWith($WorkspacePath)) { |
| $filePath.Substring($WorkspacePath.Length).TrimStart([System.IO.Path]::DirectorySeparatorChar, [System.IO.Path]::AltDirectorySeparatorChar) |
| } else { |
| $filePath |
| } |
|
|
| Write-Host " Checking: $relativePath" -ForegroundColor Gray |
|
|
| |
| try { |
| |
| |
| |
| |
| |
| $pattern = '^(<{7}|={7}|>{7})(\s|$)' |
| $matchedLines = Select-String -Path $filePath -Pattern $pattern -AllMatches -ErrorAction Stop |
|
|
| if ($matchedLines) { |
| |
| $markerDetails = @() |
|
|
| foreach ($match in $matchedLines) { |
| $markerDetails += [PSCustomObject]@{ |
| Marker = $match.Matches[0].Groups[1].Value |
| Line = $match.LineNumber |
| } |
| } |
|
|
| $filesWithConflicts += [PSCustomObject]@{ |
| File = $relativePath |
| MarkerDetails = $markerDetails |
| } |
|
|
| Write-Host " ❌ CONFLICT MARKERS FOUND in $relativePath" -ForegroundColor Red |
| foreach ($detail in $markerDetails) { |
| Write-Host " Line $($detail.Line): $($detail.Marker)" -ForegroundColor Red |
| } |
| } |
| } |
| catch { |
| |
| Write-Verbose " Skipping unreadable file: $relativePath" |
| } |
| } |
|
|
| |
| if ($OutputPath) { |
| "files-checked=$filesChecked" | Out-File -FilePath $OutputPath -Append -Encoding utf8 |
| "conflicts-found=$($filesWithConflicts.Count)" | Out-File -FilePath $OutputPath -Append -Encoding utf8 |
| } |
|
|
| Write-Host "`nSummary:" -ForegroundColor Cyan |
| Write-Host " Files checked: $filesChecked" -ForegroundColor Cyan |
| Write-Host " Files with conflicts: $($filesWithConflicts.Count)" -ForegroundColor Cyan |
|
|
| |
| if ($SummaryPath) { |
| $summaryContent = @" |
| # Merge Conflict Marker Check Results |
| |
| ## Summary |
| - **Files Checked:** $filesChecked |
| - **Files with Conflicts:** $($filesWithConflicts.Count) |
| |
| "@ |
|
|
| if ($filesWithConflicts.Count -gt 0) { |
| Write-Host "`n❌ Merge conflict markers detected in the following files:" -ForegroundColor Red |
|
|
| $summaryContent += "`n## ❌ Conflicts Detected`n`n" |
| $summaryContent += "The following files contain merge conflict markers:`n`n" |
|
|
| foreach ($fileInfo in $filesWithConflicts) { |
| Write-Host " - $($fileInfo.File)" -ForegroundColor Red |
|
|
| $summaryContent += "### 📄 ``$($fileInfo.File)```n`n" |
| $summaryContent += "| Line | Marker |`n" |
| $summaryContent += "|------|--------|`n" |
|
|
| foreach ($detail in $fileInfo.MarkerDetails) { |
| Write-Host " Line $($detail.Line): $($detail.Marker)" -ForegroundColor Red |
| $summaryContent += "| $($detail.Line) | ``$($detail.Marker)`` |`n" |
| } |
| $summaryContent += "`n" |
| } |
|
|
| $summaryContent += "`n**Action Required:** Please resolve these conflicts before merging.`n" |
| Write-Host "`nPlease resolve these conflicts before merging." -ForegroundColor Red |
| } else { |
| Write-Host "`n✅ No merge conflict markers found" -ForegroundColor Green |
| $summaryContent += "`n## ✅ No Conflicts Found`n`nAll checked files are free of merge conflict markers.`n" |
| } |
|
|
| $summaryContent | Out-File -FilePath $SummaryPath -Encoding utf8 |
| } |
|
|
| |
| if ($filesWithConflicts.Count -gt 0) { |
| throw "Merge conflict markers detected in $($filesWithConflicts.Count) file(s)" |
| } |
| } |
|
|