Skip to content

Commit

Permalink
Adds option to not recurse when searching for files (#2045)
Browse files Browse the repository at this point in the history
* Adds option to not recurse when searching for PSD files
  • Loading branch information
rfennell authored Dec 12, 2024
1 parent 04f2dc9 commit c8d4244
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ $VersionNumber = Get-VstsInput -Name "VersionNumber"
$InjectVersion = Get-VstsInput -Name "InjectVersion"
$VersionRegex = Get-VstsInput -Name "VersionRegex"
$outputversion = Get-VstsInput -Name "outputversion"
$recurse = Get-VstsInput -Name "recurse"

$VersionNumber,$Prerelease = $VersionNumber -split '-' -replace '"' -replace "'"
# Get and validate the version data
Expand Down Expand Up @@ -71,10 +72,17 @@ Write-Verbose -Message "Loading Configuration module shipped with tasks"
Import-Module "$PSScriptRoot\Configuration\1.5.0\Configuration.psd1" -force
$Null = Get-Command -Module Configuration

Write-Verbose -Message "Finding all the module psd1 files in the specified path"
$ModuleFiles = Get-ChildItem -Path $Path -Filter *.psd1 -Recurse |
Select-String -Pattern 'RootModule' |
Select-Object -ExpandProperty Path -Unique
if ([System.Convert]::ToBoolean($recurse)) {
Write-Verbose -Message "Finding all the module psd1 files in the specified path recursively"
$ModuleFiles = Get-ChildItem -Path $Path -Filter *.psd1 -Recurse |
Select-String -Pattern 'RootModule' |
Select-Object -ExpandProperty Path -Unique
} else {
Write-Verbose -Message "Finding all the module psd1 files in the specified path only"
$ModuleFiles = Get-ChildItem -Path $Path -Filter *.psd1 |
Select-String -Pattern 'RootModule' |
Select-Object -ExpandProperty Path -Unique
}

Write-Verbose "Found $($ModuleFiles.Count) modules. Beginning to apply updated version number $VersionNumber."

Expand Down
9 changes: 9 additions & 0 deletions Extensions/Versioning/VersionPowerShellModule/src/task.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,15 @@
"helpMarkDown": "If True no regex processing will be done to the build number.",
"groupName":"advanced"
},
{
"name": "recurse",
"type": "boolean",
"label": "Recursive Search",
"defaultValue": "True",
"required": false,
"helpMarkDown": "Recurse when searching for PSD1 files",
"groupName":"advanced"
},
{
"name": "VersionRegex",
"type": "string",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,103 +6,138 @@ Describe "Testing Update-PowerShellModuleVersion.ps1" {

BeforeEach {
$sut = Join-Path -Path $PSScriptRoot -ChildPath '..\src\Update-PowerShellModuleVersion.ps1' -Resolve
function Get-VstsInput {param ($Name)}
function Get-VstsInput { param ($Name) }

Function Update-MetaData {}
Function Get-MetaData {}
# Mock -CommandName Write-Verbose -MockWith {}
# Mock -CommandName Write-Verbose -MockWith {}
Mock -CommandName Write-Warning -MockWith {}
Mock -CommandName Write-Error -MockWith {}
Mock -CommandName Get-PackageProvider -MockWith {}
Mock -CommandName Install-PackageProvider -MockWith {}
Mock Find-Module {
[PsCustomObject]@{Version=[version]::new(4,3,0);Repository='OtherRepository'}
[PsCustomObject]@{Version = [version]::new(4, 3, 0); Repository = 'OtherRepository' }
}
Mock -CommandName Install-Module -MockWith {}
Mock -CommandName Import-Module -MockWith {}
Mock -CommandName Update-MetaData -MockWith {}
Mock -CommandName Write-Host -MockWith {}
Mock -CommandName Get-ChildItem -MockWith {@(
[PSCustomObject]@{
Fullname = 'TestDrive:\First.psd1'
},
[PSCustomObject]@{
Fullname = 'TestDrive:\Second.psd1'
}
)}
Mock -CommandName Get-ChildItem -MockWith { @(
[PSCustomObject]@{
Fullname = 'TestDrive:\First.psd1'
},
[PSCustomObject]@{
Fullname = 'TestDrive:\Second.psd1'
},
[PSCustomObject]@{
Fullname = 'TestDrive:\SubFolder\Third.psd1'
}
) }
Mock -CommandName Select-String -MockWith {
[PSCustomObject]@{
Path = 'TestDrive:\First.psd1'
}
}
Mock -CommandName Get-MetaData -MockWith {$true}
Mock -CommandName Get-MetaData -MockWith { $true }
#Mock -CommandName Select-Object -MockWith {}
}

It "Should write an error when the version number isn't a valid format" {
Mock Get-VstsInput -ParameterFilter {$Name -eq "Path"} {return 'TestDrive:\First.psd1'}
Mock Get-VstsInput -ParameterFilter {$Name -eq "VersionNumber"} {return "FakeNumber"}
Mock Get-VstsInput -ParameterFilter {$Name -eq "InjectVersion"} {return "true"}
Mock Get-VstsInput -ParameterFilter {$Name -eq "VersionRegex"} {return "\d+\.\d+\.\d+\.\d+"}
Mock Get-VstsInput -ParameterFilter {$Name -eq "outputversion"} {return ""}
Mock Get-VstsInput -ParameterFilter { $Name -eq "Path" } { return 'TestDrive:\First.psd1' }
Mock Get-VstsInput -ParameterFilter { $Name -eq "VersionNumber" } { return "FakeNumber" }
Mock Get-VstsInput -ParameterFilter { $Name -eq "InjectVersion" } { return "true" }
Mock Get-VstsInput -ParameterFilter { $Name -eq "VersionRegex" } { return "\d+\.\d+\.\d+\.\d+" }
Mock Get-VstsInput -ParameterFilter { $Name -eq "outputversion" } { return "" }
Mock Get-VstsInput -ParameterFilter { $Name -eq "recurse" } { return "true" }


&$Sut
Assert-MockCalled -CommandName Write-Error -Scope It -Times 1
}

It "Should attempt to update 1 module in the target path" {
Mock Get-VstsInput -ParameterFilter {$Name -eq "Path"} {return 'TestDrive:\'}
Mock Get-VstsInput -ParameterFilter {$Name -eq "VersionNumber"} {return "1.2.3.4"}
Mock Get-VstsInput -ParameterFilter {$Name -eq "InjectVersion"} {return "true"}
Mock Get-VstsInput -ParameterFilter {$Name -eq "VersionRegex"} {return "\d+\.\d+\.\d+\.\d+"}
Mock Get-VstsInput -ParameterFilter {$Name -eq "outputversion"} {return ""}
Mock Get-VstsInput -ParameterFilter { $Name -eq "Path" } { return 'TestDrive:\' }
Mock Get-VstsInput -ParameterFilter { $Name -eq "VersionNumber" } { return "1.2.3.4" }
Mock Get-VstsInput -ParameterFilter { $Name -eq "InjectVersion" } { return "true" }
Mock Get-VstsInput -ParameterFilter { $Name -eq "VersionRegex" } { return "\d+\.\d+\.\d+\.\d+" }
Mock Get-VstsInput -ParameterFilter { $Name -eq "outputversion" } { return "" }
Mock Get-VstsInput -ParameterFilter { $Name -eq "recurse" } { return "true" }


Mock -CommandName Select-Object -MockWith {'TestDrive:\First.psd1'} -ParameterFilter {$InputObject.Path -eq 'TestDrive:\First.psd1'}
Mock -CommandName Select-Object -MockWith { 'TestDrive:\First.psd1' } -ParameterFilter { $InputObject.Path -eq 'TestDrive:\First.psd1' }

&$Sut

Assert-MockCalled -CommandName Update-Metadata -Scope It -Times 1
}
It "Should attempt to update 2 modules in the target path" {
It "Should attempt to update 3 modules in the target path (recursive)" {

Mock Get-VstsInput -ParameterFilter { $Name -eq "Path" } { return 'TestDrive:\' }
Mock Get-VstsInput -ParameterFilter { $Name -eq "VersionNumber" } { return "1.2.3.4" }
Mock Get-VstsInput -ParameterFilter { $Name -eq "InjectVersion" } { return "true" }
Mock Get-VstsInput -ParameterFilter { $Name -eq "VersionRegex" } { return "\d+\.\d+\.\d+\.\d+" }
Mock Get-VstsInput -ParameterFilter { $Name -eq "outputversion" } { return "" }
Mock Get-VstsInput -ParameterFilter { $Name -eq "recurse" } { return "true" }

Mock Get-VstsInput -ParameterFilter {$Name -eq "Path"} {return 'TestDrive:\'}
Mock Get-VstsInput -ParameterFilter {$Name -eq "VersionNumber"} {return "1.2.3.4"}
Mock Get-VstsInput -ParameterFilter {$Name -eq "InjectVersion"} {return "true"}
Mock Get-VstsInput -ParameterFilter {$Name -eq "VersionRegex"} {return "\d+\.\d+\.\d+\.\d+"}
Mock Get-VstsInput -ParameterFilter {$Name -eq "outputversion"} {return ""}

&$Sut

Assert-MockCalled -CommandName Update-Metadata -Scope It -Times 2
Assert-MockCalled -CommandName Update-Metadata -Scope It -Exactly 6 # for each file runs twice
}
It "Should attempt to update 2 modules in the target path (non-recursive)" {

Mock Get-VstsInput -ParameterFilter { $Name -eq "Path" } { return 'TestDrive:\' }
Mock Get-VstsInput -ParameterFilter { $Name -eq "VersionNumber" } { return "1.2.3.4" }
Mock Get-VstsInput -ParameterFilter { $Name -eq "InjectVersion" } { return "true" }
Mock Get-VstsInput -ParameterFilter { $Name -eq "VersionRegex" } { return "\d+\.\d+\.\d+\.\d+" }
Mock Get-VstsInput -ParameterFilter { $Name -eq "outputversion" } { return "" }
Mock Get-VstsInput -ParameterFilter { $Name -eq "recurse" } { return "false" }

Mock -CommandName Get-ChildItem -MockWith { @(
[PSCustomObject]@{
Fullname = 'TestDrive:\First.psd1'
},
[PSCustomObject]@{
Fullname = 'TestDrive:\Second.psd1'
}
) }

&$Sut

Assert-MockCalled -CommandName Update-Metadata -Scope It -Exactly 4 # for each file runs twice
}
It "Should attempt to update the version number and private data in 1 module in the target path when a prerelease version is passed" {

Mock Get-VstsInput -ParameterFilter {$Name -eq "Path"} {return 'TestDrive:\'}
Mock Get-VstsInput -ParameterFilter {$Name -eq "VersionNumber"} {return "1.2.3.4-alpha"}
Mock Get-VstsInput -ParameterFilter {$Name -eq "InjectVersion"} {return "true"}
Mock Get-VstsInput -ParameterFilter {$Name -eq "VersionRegex"} {return "\d+\.\d+\.\d+\.\d+"}
Mock Get-VstsInput -ParameterFilter {$Name -eq "outputversion"} {return ""}
Mock -CommandName Select-Object -MockWith {'TestDrive:\First.psd1'} -ParameterFilter {$InputObject.Path -eq 'TestDrive:\First.psd1'}
Mock Get-VstsInput -ParameterFilter { $Name -eq "Path" } { return 'TestDrive:\' }
Mock Get-VstsInput -ParameterFilter { $Name -eq "VersionNumber" } { return "1.2.3.4-alpha" }
Mock Get-VstsInput -ParameterFilter { $Name -eq "InjectVersion" } { return "true" }
Mock Get-VstsInput -ParameterFilter { $Name -eq "VersionRegex" } { return "\d+\.\d+\.\d+\.\d+" }
Mock Get-VstsInput -ParameterFilter { $Name -eq "outputversion" } { return "" }
Mock Get-VstsInput -ParameterFilter { $Name -eq "recurse" } { return "true" }

Mock -CommandName Select-Object -MockWith { 'TestDrive:\First.psd1' } -ParameterFilter { $InputObject.Path -eq 'TestDrive:\First.psd1' }

&$Sut

Assert-MockCalled -CommandName Update-Metadata -Scope It -Times 2
}
It "Should write a warning when the PSData Prerelease section isn't in the manifest when a prerelease version is passed" {

Mock Get-VstsInput -ParameterFilter {$Name -eq "Path"} {return 'TestDrive:\'}
Mock Get-VstsInput -ParameterFilter {$Name -eq "VersionNumber"} {return "1.2.3.4-alpha"}
Mock Get-VstsInput -ParameterFilter {$Name -eq "InjectVersion"} {return "true"}
Mock Get-VstsInput -ParameterFilter {$Name -eq "VersionRegex"} {return "\d+\.\d+\.\d+\.\d+"}
Mock Get-VstsInput -ParameterFilter {$Name -eq "outputversion"} {return ""}
Mock -CommandName Select-Object -MockWith {'TestDrive:\First.psd1'} -ParameterFilter {$InputObject.Path -eq 'TestDrive:\First.psd1'}
Mock -CommandName Get-MetaData -MockWith {$null}
Mock Get-VstsInput -ParameterFilter { $Name -eq "Path" } { return 'TestDrive:\' }
Mock Get-VstsInput -ParameterFilter { $Name -eq "VersionNumber" } { return "1.2.3.4-alpha" }
Mock Get-VstsInput -ParameterFilter { $Name -eq "InjectVersion" } { return "true" }
Mock Get-VstsInput -ParameterFilter { $Name -eq "VersionRegex" } { return "\d+\.\d+\.\d+\.\d+" }
Mock Get-VstsInput -ParameterFilter { $Name -eq "outputversion" } { return "" }
Mock Get-VstsInput -ParameterFilter { $Name -eq "recurse" } { return "true" }

Mock -CommandName Select-Object -MockWith { 'TestDrive:\First.psd1' } -ParameterFilter { $InputObject.Path -eq 'TestDrive:\First.psd1' }
Mock -CommandName Get-MetaData -MockWith { $null }
&$Sut

Assert-MockCalled -CommandName Update-Metadata -Scope It -Times 1
Assert-MockCalled -CommandName Write-Warning -Scope It -Times 1 -ParameterFilter {
$Message -eq ("Cannot set Prerelease in module manifest. Add an empty Prerelease to your module manifest, like:`n" +
' PrivateData = @{ PSData = @{ Prerelease = "" } }')
' PrivateData = @{ PSData = @{ Prerelease = "" } }')
}
}
}
Expand Down
8 changes: 6 additions & 2 deletions Extensions/Versioning/azure-pipelines-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,14 @@ stages:
rootDirectory: '$(Build.SourcesDirectory)/$(ExtensionRoot)'
targetFiles: '**\\task.json'

- task: Pester.PesterRunner.Pester-Task.Pester@10
- task: Pester.PesterRunner.Pester-Task.Pester@9
displayName: 'DACPac Versioning Pester Test Runner'
inputs:
scriptFolder: '$(Build.SourcesDirectory)\$(ExtensionRoot)\VersionDacpacTask\test'
resultsFile: '$(Build.SourcesDirectory)\Test-VersionDacpacTaskPester.XML'
run32Bit: false
PesterVersion: 'OtherVersion'
preferredPesterVersion: '4.10.1'
continueOnError: true

- task: PublishTestResults@2
Expand All @@ -57,12 +59,14 @@ stages:
testResultsFiles: '$(Build.SourcesDirectory)\Test-VersionDacpacTaskPester.XML'
testRunTitle: 'DACPac Pester Tests'

- task: Pester.PesterRunner.Pester-Task.Pester@10
- task: Pester.PesterRunner.Pester-Task.Pester@9
displayName: 'PowerShellModule Versioning Tests'
inputs:
scriptFolder: '$(Build.SourcesDirectory)\$(ExtensionRoot)\VersionPowerShellModule\test'
resultsFile: '$(Build.SourcesDirectory)\Test-VersionPowerShellTaskPester.XML'
run32Bit: false
PesterVersion: 'OtherVersion'
preferredPesterVersion: '4.10.1'

- task: PublishTestResults@2
displayName: 'Publish Test Results $(Build.SourcesDirectory)\Test-VersionPowerShellTaskPester.XML'
Expand Down

0 comments on commit c8d4244

Please sign in to comment.