Skip to content

Commit e578368

Browse files
committed
Formatting cleanup and improve test
1 parent 0b7b19d commit e578368

2 files changed

Lines changed: 39 additions & 23 deletions

File tree

ModuleFast.ps1

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,7 @@ function Install-ModuleFastHelper {
478478

479479
#Installation jobs are captured here, we will check them once all downloads have completed
480480
[List[Job2]]$installJobs = @()
481-
481+
#TODO: Filestreams should be disposed in a try/catch in case of cancellation. In PS 7.3+, should be a clean() block
482482
while ($downloadTasks.count -gt 0) {
483483
#TODO: Check on in jobs and if there's a failure, cancel the rest of the jobs
484484
$noTasksYetCompleted = -1
@@ -535,10 +535,10 @@ function Install-ModuleFastOperation {
535535
[string]$Destination
536536
)
537537
$ErrorActionPreference = 'Stop'
538-
Write-Host "Installing $Name $Version from $DownloadPath to $Destination"
539-
$Destination = Join-Path $Destination $Name $Version
540-
Expand-Archive -Path $DownloadPath -DestinationPath $Destination -Force
541-
Write-Host "Installed $Name $Version from $DownloadPath to $Destination"
538+
$ModuleDestination = Join-Path $Destination $Name $Version
539+
Write-Host "Installing $Name $Version from $DownloadPath to $ModuleDestination"
540+
Expand-Archive -Path $DownloadPath -DestinationPath $ModuleDestination -Force
541+
Write-Host "Installed $Name $Version from $DownloadPath to $ModuleDestination"
542542
}
543543

544544
#region Classes
@@ -582,10 +582,10 @@ class ModuleFastSpec : IComparable {
582582
}
583583
}
584584

585-
#ModuleSpecification Compatible Aliases
586-
hidden [SemanticVersion]Get_RequiredVersion() { return [ModuleFastSpec]::ParseSemanticVersion($this.Required) }
587-
hidden [SemanticVersion]Get_Version() { return [ModuleFastSpec]::ParseSemanticVersion($this.Min) }
588-
hidden [SemanticVersion]Get_MaximumVersion() { return [ModuleFastSpec]::ParseSemanticVersion($this.Max) }
585+
#ModuleSpecification Compatible Getters
586+
hidden [Version]Get_RequiredVersion() { return [ModuleFastSpec]::ParseSemanticVersion($this.Required) }
587+
hidden [Version]Get_Version() { return [ModuleFastSpec]::ParseSemanticVersion($this.Min) }
588+
hidden [Version]Get_MaximumVersion() { return [ModuleFastSpec]::ParseSemanticVersion($this.Max) }
589589

590590
#Constructors
591591

@@ -665,20 +665,20 @@ class ModuleFastSpec : IComparable {
665665

666666
### Version Helper Methods
667667
#Determines if a version is within range of the spec.
668-
[bool] Matches ([SemanticVersion]$Version) {
668+
[bool] Matches([SemanticVersion]$Version) {
669669
if ($null -eq $Version) { return $false }
670670
if ($Version -ge $this.Min -and $Version -le $this.Max) { return $true }
671671
return $false
672672
}
673-
[bool] Matches ([Version]$Version) {
673+
[bool] Matches([Version]$Version) {
674674
return $this.Matches([ModuleFastSpec]::ParseVersion($Version))
675675
}
676-
[bool] Matches ([String]$Version) {
676+
[bool] Matches([String]$Version) {
677677
return $this.Matches([ModuleFastSpec]::ParseVersionString($Version))
678678
}
679679

680680
#Determines if this spec is at least partially inside of the supplied spec
681-
[bool] Overlaps ([ModuleFastSpec]$Spec) {
681+
[bool] Overlaps([ModuleFastSpec]$Spec) {
682682
if ($null -eq $Spec) { return $false }
683683
if ($Spec.Name -ne $this.Name) { throw "Supplied Spec Name $($Spec.Name) does not match this spec name $($this.Name)" }
684684
if ($Spec.Guid -ne $this.Guid) { throw "Supplied Spec Guid $($Spec.Name) does not match this spec guid $($this.Name)" }
@@ -691,17 +691,17 @@ class ModuleFastSpec : IComparable {
691691
# Parses either a assembly version or semver to a semver string
692692
static [SemanticVersion] ParseVersionString([string]$Version) {
693693
if (-not $Version) { throw [NotSupportedException]'Null or empty strings are not supported' }
694-
$result = $Version -match [ModuleFastSpec]::SYSTEM_VERSION_REGEX `
695-
? [SemanticVersion]::ParseVersion([Version]$Version)
696-
: [SemanticVersion]$Version
697-
return $result
694+
if ($Version -as [Version]) {
695+
return [ModuleFastSpec]::ParseVersion($Version)
696+
}
697+
return $Version
698698
}
699699

700700
# A version number with 4 octets wont cast to semanticversion properly, this is a helper method for that.
701701
# We treat "revision" as "build" and "build" as patch for purposes of translation
702702
# Needed because SemVer can't parse builds correctly
703703
#https://github.com/PowerShell/PowerShell/issues/14605
704-
static [SemanticVersion]ParseVersion([Version]$Version) {
704+
static [SemanticVersion] ParseVersion([Version]$Version) {
705705
if (-not $Version) { throw [NotSupportedException]'Null or empty strings are not supported' }
706706

707707
[list[string]]$buildLabels = @()
@@ -731,9 +731,14 @@ class ModuleFastSpec : IComparable {
731731
}
732732

733733
# A way to go back from SemanticVersion, the anticedent to ParseVersion
734-
static [Version]ParseSemanticVersion([SemanticVersion]$Version) {
734+
static [Version] ParseSemanticVersion([SemanticVersion]$Version) {
735735
if ($null -eq $Version) { throw [NotSupportedException]'Null or empty strings are not supported' }
736736

737+
#If this only has a build "version" but no Prerelease tag, we can translate that to the revision
738+
if (-not $Version.PreReleaseLabel -and $Version.BuildLabel -and $Version.BuildLabel -as [int]) {
739+
return [Version]::new($Version.Major, $Version.Minor, $Version.Patch, $Version.BuildLabel)
740+
}
741+
737742
[string[]]$buildFlags = $Version.BuildLabel -split '\.'
738743
if ($BuildFlags -notcontains [ModuleFastSpec]::SYSTEM_VERSION_LABEL) {
739744
#This is a semantic-compatible version, we can just return it
@@ -761,7 +766,7 @@ class ModuleFastSpec : IComparable {
761766

762767
#This string will be unique for each spec type, and can (probably)? Be safely used as a hashcode
763768
#TODO: Implement parsing of this string to the parser to allow it to be "reserialized" to a module spec
764-
[string]ToString() {
769+
[string] ToString() {
765770
$name = $this._Name + ($this._Guid -ne [Guid]::Empty ? " [$($this._Guid)]" : '')
766771
$versionString = switch ($true) {
767772
($this.Min -eq [ModuleFastSpec]::MinVersion -and $this.Max -eq [ModuleFastSpec]::MaxVersion) {
@@ -780,7 +785,7 @@ class ModuleFastSpec : IComparable {
780785
#We can however just add Equals() method
781786

782787
#Implementation of https://learn.microsoft.com/en-us/dotnet/api/system.iequatable-1.equals?view=net-6.0
783-
[boolean] Equals ([Object]$obj) {
788+
[boolean] Equals([Object]$obj) {
784789
if ($null -eq $obj) { return $false }
785790
switch ($obj.GetType()) {
786791
#Comparing ModuleSpecs means that we want to ensure they are structurally the same

ModuleFast.tests.ps1

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -347,8 +347,9 @@ Describe 'Get-ModuleFastPlan' -Tag 'E2E' {
347347
#TODO: Mocks
348348
Get-ModuleFastPlan 'Az' | Should -HaveCount 78
349349
}
350-
It 'Gets Module with 4 section version numbers (VMware.PowerCLI)' {
351-
Get-ModuleFastPlan 'VMware.PowerCLI' | Should -HaveCount 75
350+
It 'Gets Module with 4 section version number and a 4 section version number dependency (VMware.VimAutomation.Common)' {
351+
Get-ModuleFastPlan 'VMware.VimAutomation.Common' | Should -HaveCount 2
352+
352353
}
353354
It 'Gets multiple modules' {
354355
Get-ModuleFastPlan 'Az', 'VMware.PowerCLI' | Should -HaveCount 153
@@ -370,6 +371,16 @@ Describe 'Install-ModuleFast' -Tag 'E2E' {
370371
It 'Installs Module with lots of dependencies (Az)' {
371372
Install-ModuleFast 'Az' -Destination (Get-Item testdrive:).fullname
372373
}
374+
It 'Installs Module with 4 section version numbers (VMware.PowerCLI)' {
375+
$testDrivePath = (Get-Item testdrive:).fullname
376+
Install-ModuleFast 'VMware.VimAutomation.Common' -Destination $testDrivePath
377+
Get-Item TestDrive:\*\*\*.psd1 | ForEach-Object {
378+
$moduleFolderVersion = $_ | Split-Path | Split-Path -Leaf
379+
Import-PowerShellDataFile -Path $_.FullName | ForEach-Object ModuleVersion | Should -Be $moduleFolderVersion
380+
}
381+
$env:PSModulePath = $testDrivePath
382+
Get-Module VMWare* -ListAvailable | Should -HaveCount 2
383+
}
373384
AfterAll {
374385
$env:PSModulePath = $SCRIPT:__existingPSModulePath
375386
}

0 commit comments

Comments
 (0)