diff --git a/.build/CodeFormatter.ps1 b/.build/CodeFormatter.ps1 index 0d56309082..1e47bc4c06 100644 --- a/.build/CodeFormatter.ps1 +++ b/.build/CodeFormatter.ps1 @@ -21,6 +21,7 @@ Set-StrictMode -Version Latest . $PSScriptRoot\CodeFormatterChecks\CheckScriptFileHasComplianceHeader.ps1 . $PSScriptRoot\CodeFormatterChecks\CheckKeywordCasing.ps1 . $PSScriptRoot\CodeFormatterChecks\CheckScriptFormat.ps1 +. $PSScriptRoot\HelpFunctions\Get-CommitFilesOnBranch.ps1 if (-not (Load-Module -Name PSScriptAnalyzer -MinimumVersion "1.20")) { throw "PSScriptAnalyzer module could not be loaded" @@ -33,46 +34,10 @@ if (-not (Load-Module -Name EncodingAnalyzer)) { $repoRoot = Get-Item "$PSScriptRoot\.." $optimizeCodeFormatter = [string]::IsNullOrEmpty($Branch) -eq $false -$filesFullPath = New-Object 'System.Collections.Generic.HashSet[string]' # Get only the files that are changed in this PR if ($optimizeCodeFormatter) { - Write-Verbose "Checking commits only" - # Get all the commits between origin/$Branch and HEAD. - $gitlog = git log --format="%H %cd" --date=rfc origin/$Branch..HEAD - $m = $gitlog | Select-String "^(\S+) (.*)$" - - foreach ($commitMatch in $m) { - $commitHash = $commitMatch.Matches.Groups[1].Value - $filesChangedInCommit = git diff-tree --no-commit-id --name-only -r $commitHash - - foreach ($fileChanged in $filesChangedInCommit) { - $fullPath = Join-Path $repoRoot $fileChanged - - if (-not (Test-Path $fullPath)) { - # not typical scenario, but want to have the pipeline continue - Write-Verbose "File no longer exists, skip file: $fullPath" - continue - } - - Write-Verbose "Adding commit file to list: $fullPath" - [void]$filesFullPath.Add($fullPath) - } - } - - # Also include modified files, but not committed yet for local work. - $gitStatus = git status --short - $m = $gitStatus | Select-String "M (.*)" - foreach ($match in $m) { - $file = $match.Matches.Groups[1].Value.Trim() - $fullPath = Join-Path $PSScriptRoot $file - - Write-Verbose "Adding modified file to list: $fullPath" - [void]$filesFullPath.Add($fullPath) - } - - Write-Verbose "Files changed or modified" - $filesFullPath | Write-Verbose + $filesFullPath = Get-CommitFilesOnBranch -Branch $Branch # Only optimize CodeFormatter IF any CodeFormatter related files were not modified or PSScriptAnalyzerSettings.psd1 $optimizeCodeFormatter = $null -eq ($filesFullPath | Where-Object { $_ -like "*.build\CodeFormatter*" -or $_ -like "*\PSScriptAnalyzerSettings.psd1" }) diff --git a/.build/HelpFunctions/Get-CommitFilesOnBranch.ps1 b/.build/HelpFunctions/Get-CommitFilesOnBranch.ps1 new file mode 100644 index 0000000000..01e5d57b52 --- /dev/null +++ b/.build/HelpFunctions/Get-CommitFilesOnBranch.ps1 @@ -0,0 +1,53 @@ +# Copyright (c) Microsoft Corporation. +# Licensed under the MIT License. + +# This should only be called if we have a branch that we want to compare against +function Get-CommitFilesOnBranch { + [CmdletBinding()] + param( + [Parameter(Mandatory = $true)] + [string] + $Branch + ) + $filesFullPath = New-Object 'System.Collections.Generic.HashSet[string]' + $repoRoot = Get-Item "$PSScriptRoot\..\.." + + Write-Verbose "Checking commits only" + # Get all the commits between origin/$Branch and HEAD. + $gitlog = git log --format="%H %cd" --date=rfc origin/$Branch..HEAD + $m = $gitlog | Select-String "^(\S+) (.*)$" + + foreach ($commitMatch in $m) { + $commitHash = $commitMatch.Matches.Groups[1].Value + $filesChangedInCommit = git diff-tree --no-commit-id --name-only -r $commitHash + + foreach ($fileChanged in $filesChangedInCommit) { + $fullPath = Join-Path $repoRoot $fileChanged + + if (-not (Test-Path $fullPath)) { + # not typical scenario, but want to have the pipeline continue + Write-Verbose "File no longer exists, skip file: $fullPath" + continue + } + + Write-Verbose "Adding commit file to list: $fullPath" + [void]$filesFullPath.Add($fullPath) + } + } + + # Also include modified files, but not committed yet for local work. + $gitStatus = git status --short + $m = $gitStatus | Select-String "M (.*)" + foreach ($match in $m) { + $file = $match.Matches.Groups[1].Value.Trim() + $fullPath = Join-Path $PSScriptRoot $file + + Write-Verbose "Adding modified file to list: $fullPath" + [void]$filesFullPath.Add($fullPath) + } + + Write-Verbose "Files changed or modified" + $filesFullPath | Write-Verbose + + return $filesFullPath +} diff --git a/.build/Pester.ps1 b/.build/Pester.ps1 index f91f6cb599..09b8bb0db3 100644 --- a/.build/Pester.ps1 +++ b/.build/Pester.ps1 @@ -3,11 +3,16 @@ [CmdletBinding()] param( - [switch]$NoProgress + [switch] + $NoProgress, + + [string] + $Branch ) begin { . $PSScriptRoot\Load-Module.ps1 + . $PSScriptRoot\HelpFunctions\Get-CommitFilesOnBranch.ps1 if (-not (Load-Module -Name Pester -MinimumVersion 5.2.0)) { throw "Pester module could not be loaded" @@ -25,6 +30,16 @@ begin { $stopWatch = [System.Diagnostics.Stopwatch]::StartNew() } process { + if (-not ([string]::IsNullOrEmpty($Branch))) { + Write-Host "Checking commits on Branch $Branch" + $committedFiles = Get-CommitFilesOnBranch -Branch $Branch + # if the branch only has doc changes return + if ($null -eq ($committedFiles | Where-Object { $_.EndsWith(".ps1") } )) { + Write-Host "No Commits on PS1 files, skipping over pester testing." + return + } + } + $root = Get-Item "$PSScriptRoot\.." $scripts = @(Get-ChildItem -Recurse $root | Where-Object { $_.Name -like "*.Tests.ps1" }) @@ -118,7 +133,8 @@ begin { Write-Host - if (-not $NoProgress) { + if (-not $NoProgress -and + $null -ne $parentProgress) { Write-Progress @parentProgress -Completed } $sumTotalSeconds = 0 diff --git a/azure-pipeline-merge.yml b/azure-pipeline-merge.yml index dad275aff1..be7009b9ee 100644 --- a/azure-pipeline-merge.yml +++ b/azure-pipeline-merge.yml @@ -25,8 +25,10 @@ steps: - pwsh: | cd .\.build - .\Pester.ps1 -NoProgress + .\Pester.ps1 -NoProgress -Branch $env:TargetBranchName displayName: "Running Invoke-Pester" + env: + TargetBranchName: $(System.PullRequest.TargetBranch) - pwsh: | cd .\.build