Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 2 additions & 37 deletions .build/CodeFormatter.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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" })
Expand Down
53 changes: 53 additions & 0 deletions .build/HelpFunctions/Get-CommitFilesOnBranch.ps1
Original file line number Diff line number Diff line change
@@ -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
}
20 changes: 18 additions & 2 deletions .build/Pester.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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" })
Expand Down Expand Up @@ -118,7 +133,8 @@ begin {

Write-Host

if (-not $NoProgress) {
if (-not $NoProgress -and
$null -ne $parentProgress) {
Write-Progress @parentProgress -Completed
}
$sumTotalSeconds = 0
Expand Down
4 changes: 3 additions & 1 deletion azure-pipeline-merge.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down