fix module manifest update logic

This commit is contained in:
OCram85 2022-07-27 10:35:22 +02:00
parent 6a314eeb48
commit 0784cddb6f
2 changed files with 74 additions and 7 deletions

View File

@ -18,4 +18,40 @@ Describe 'Update-ModuleMeta' {
$foo.Description[0].Text.Length | Should -BeGreaterThan 5 $foo.Description[0].Text.Length | Should -BeGreaterThan 5
} }
} }
Context 'Unit Tests' -Tag 'Unit' {
BeforeAll {
# Mock Drone env if its not present
if (!$Env:DRONE) {
$Env:DRONE = $true
$Env:DRONE_BUILD_EVENT = 'tag'
$Env:DRONE_SEMVER = '9.9.9-dev9'
$Env:DRONE_SEMVER_SHORT = '9.9.9'
$Env:DRONE_SEMVER_PRERELEASE = 'dev9'
}
Mock 'Get-RepoPath' -ModuleName 'DroneHelper' {
return @{
Src = @{
Manifest = @{
Item = @{
FullName = './resources/MyModule.psd1'
}
}
}
}
}
}
It 'Update test module manifest' {
{ Update-ModuleMeta } | Should -Not -Throw
}
It 'Updated manifest is valid' {
{ Test-ModuleManifest -Path './resources/MyModule.psd1' } | Should -Not -Throw
}
It 'External dependencies prop is untouched' {
$d = Test-ModuleManifest -Path './resources/MyModule.psd1'
$d.PrivateData.PSData.ExternalModuleDependencies.Count | Should -Be '2'
}
}
} }

View File

@ -39,22 +39,53 @@ function Update-ModuleMeta {
if ($null -ne $Env:DRONE_SEMVER_PRERELEASE) { if ($null -ne $Env:DRONE_SEMVER_PRERELEASE) {
$nPreRelease = $Env:DRONE_SEMVER_PRERELEASE $nPreRelease = $Env:DRONE_SEMVER_PRERELEASE
} }
$ModManifestParams = @{ $DataParams = @{
Path = $Repo.Src.Manifest.Item.FullName Path = $Repo.Src.Manifest.Item.FullName
ModuleVersion = $nVersion ErrorAction = 'Stop'
ErrorAction = 'Stop'
} }
# Getting the module manifest as imported object
try {
$ModManifestData = Import-PowerShellDataFile @DataParams
}
catch {
$_.Exception.Message | Write-Debug
$ErrorParams = @{
Message = "Could not import the module manifest file."
ErrorAction = 'Stop'
}
Write-Error @ErrorParams
}
# Updating the new module version
$ModManifestData.ModuleVersion = $nVersion
# Updating the prerelease property if there is one
if ($nPreRelease) { if ($nPreRelease) {
$ModManifestParams.PreRelease = $nPreRelease $ModManifestData.PrivateData.PSData.Prerelease = $nPreRelease
} }
$ManifestData = Test-ModuleManifest -Path $Repo.Src.Manifest.Item.FullName $ManifestData = Test-ModuleManifest -Path $Repo.Src.Manifest.Item.FullName
if ( if (
($nVersion -ne $ManifestData.Version) -or ($nVersion -ne $ManifestData.Version) -or
($nVersion -ne $ManifestData.PrivateData.PSData.Prerelease) ($nPreRelease -ne $ManifestData.PrivateData.PSData.Prerelease)
) { ) {
Update-ModuleManifest @ModManifestParams $OutputFileParams = @{
Path = $Repo.Src.Manifest.Item.FullName
PassThru = $true
Encoding = 'utf8NoBom'
Force = $true
}
try {
$ModManifestData | ConvertTo-Psd | Set-Content @OutputFileParams
}
catch {
$_.Exception.Message | Write-Debug
$ErrorParams = @{
Message = "Failed to update the module manifest file"
ErrorAction = 'Stop'
}
Write-Error @ErrorParams
}
} }
else { else {
Write-Verbose -Message 'Identical version given. Skipping update.' Write-Verbose -Message 'Identical version given. Skipping update.'