Skip to content

Commit

Permalink
Add 'ToLower' before name comparison and split (#1738)
Browse files Browse the repository at this point in the history
  • Loading branch information
alerickson authored Oct 29, 2024
1 parent de7732e commit 94dff16
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 21 deletions.
4 changes: 2 additions & 2 deletions .ci/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ jobs:
- ${{ parameters.powershellExecutable }}: |
$modulePath = Join-Path -Path $env:AGENT_TEMPDIRECTORY -ChildPath 'TempModules'
Write-Verbose -Verbose "Install Microsoft.PowerShell.PSResourceGet to temp module path"
Save-Module -Name Microsoft.PowerShell.PSResourceGet -MinimumVersion 0.9.0-rc1 -Path $modulePath -AllowPrerelease -Force
Save-Module -Name Microsoft.PowerShell.PSResourceGet -Path $modulePath -Force -Verbose
Write-Verbose -Verbose "Install Pester 4.X to temp module path"
Save-Module -Name "Pester" -MaximumVersion 4.99 -Path $modulePath -Force
displayName: Install Microsoft.PowerShell.PSResourceGet and Pester
Expand All @@ -59,7 +59,7 @@ jobs:
Write-Verbose -Verbose "Importing build utilities (buildtools.psd1)"
Import-Module -Name (Join-Path -Path '${{ parameters.buildDirectory }}' -ChildPath 'buildtools.psd1') -Force
#
Install-ModulePackageForTest -PackagePath "$(System.ArtifactsDirectory)"
Install-ModulePackageForTest -PackagePath "$(System.ArtifactsDirectory)" -ErrorAction stop -Verbose
displayName: Install module for test from downloaded artifact
workingDirectory: ${{ parameters.buildDirectory }}
Expand Down
18 changes: 17 additions & 1 deletion buildtools.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,23 @@ function Install-ModulePackageForTest {
}

Write-Verbose -Verbose -Message "Installing module $($config.ModuleName) to build output path $installationPath"
Save-PSResource -Name $config.ModuleName -Repository $localRepoName -Path $installationPath -SkipDependencyCheck -Prerelease -Confirm:$false -TrustRepository
$psgetModuleBase = (get-command save-psresource).Module.ModuleBase
$psgetVersion = (get-command save-psresource).Module.Version.ToString()
$psgetPrerelease = (get-command find-psresource).module.PrivateData.PSData.Prerelease
Write-Verbose -Verbose -Message "PSResourceGet module base imported: $psgetModuleBase"
Write-Verbose -Verbose -Message "PSResourceGet version base imported: $psgetVersion"
Write-Verbose -Verbose -Message "PSResourceGet prerelease base imported: $psgetPrerelease"
#Save-PSResource -Name $config.ModuleName -Repository $localRepoName -Path $installationPath -SkipDependencyCheck -Prerelease -Confirm:$false -TrustRepository

Register-PSRepository -Name $localRepoName -SourceLocation $packagePathWithNupkg -InstallationPolicy Trusted -Verbose
$psgetv2ModuleBase = (get-command save-module).Module.ModuleBase
$psgetv2Version = (get-command save-module).Module.Version.ToString()
$psgetv2Prerelease = (get-command save-module).module.PrivateData.PSData.Prerelease
Write-Verbose -Verbose -Message "PowerShellGet module base imported: $psgetv2ModuleBase"
Write-Verbose -Verbose -Message "PowerShellGet version base imported: $psgetv2Version"
Write-Verbose -Verbose -Message "PowerShellGet prerelease base imported: $psgetv2Prerelease"
Save-Module -Name $config.ModuleName -Repository $localRepoName -Path $installationPath -Force -Verbose -AllowPrerelease -Confirm:$false
Unregister-PSRepository -Name $localRepoName

Write-Verbose -Verbose -Message "Unregistering local package repo: $localRepoName"
Unregister-PSResourceRepository -Name $localRepoName -Confirm:$false
Expand Down
8 changes: 4 additions & 4 deletions src/code/LocalServerApiCalls.cs
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,7 @@ private Hashtable GetMetadataFromNupkg(string packageName, string packagePath, s
string psd1FilePath = String.Empty;
string ps1FilePath = String.Empty;
string nuspecFilePath = String.Empty;
Utils.GetMetadataFilesFromPath(tempDiscoveryPath, packageName, out psd1FilePath, out ps1FilePath, out nuspecFilePath);
Utils.GetMetadataFilesFromPath(tempDiscoveryPath, packageName, out psd1FilePath, out ps1FilePath, out nuspecFilePath, out string properCasingPkgName);

List<string> pkgTags = new List<string>();

Expand All @@ -710,7 +710,7 @@ private Hashtable GetMetadataFromNupkg(string packageName, string packagePath, s
pkgMetadata.Add("ProjectUri", projectUri);
pkgMetadata.Add("IconUri", iconUri);
pkgMetadata.Add("ReleaseNotes", releaseNotes);
pkgMetadata.Add("Id", packageName);
pkgMetadata.Add("Id", properCasingPkgName);
pkgMetadata.Add(_fileTypeKey, Utils.MetadataFileType.ModuleManifest);

pkgTags.AddRange(pkgHashTags);
Expand All @@ -730,7 +730,7 @@ private Hashtable GetMetadataFromNupkg(string packageName, string packagePath, s
}

pkgMetadata = parsedScript.ToHashtable();
pkgMetadata.Add("Id", packageName);
pkgMetadata.Add("Id", properCasingPkgName);
pkgMetadata.Add(_fileTypeKey, Utils.MetadataFileType.ScriptFile);
pkgTags.AddRange(pkgMetadata["Tags"] as string[]);

Expand Down Expand Up @@ -916,7 +916,7 @@ private NuGetVersion GetInfoFromFileName(string packageFullName, string packageN

string[] packageWithoutName = packageFullName.ToLower().Split(new string[]{ $"{packageName.ToLower()}." }, StringSplitOptions.RemoveEmptyEntries);
string packageVersionAndExtension = packageWithoutName[0];
string[] originalFileNameParts = packageFullName.Split(new string[]{ $".{packageVersionAndExtension}" }, StringSplitOptions.RemoveEmptyEntries);
string[] originalFileNameParts = packageFullName.ToLower().Split(new string[]{ $".{packageVersionAndExtension.ToLower()}" }, StringSplitOptions.RemoveEmptyEntries);
actualName = String.IsNullOrEmpty(originalFileNameParts[0]) ? packageName : originalFileNameParts[0];
int extensionDot = packageVersionAndExtension.LastIndexOf('.');
string version = packageVersionAndExtension.Substring(0, extensionDot);
Expand Down
18 changes: 16 additions & 2 deletions src/code/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1172,11 +1172,12 @@ internal static HashSet<string> GetInstalledPackages(List<string> pathsToSearch,
return pkgsInstalledOnMachine;
}

internal static void GetMetadataFilesFromPath(string dirPath, string packageName, out string psd1FilePath, out string ps1FilePath, out string nuspecFilePath)
internal static void GetMetadataFilesFromPath(string dirPath, string packageName, out string psd1FilePath, out string ps1FilePath, out string nuspecFilePath, out string properCasingPkgName)
{
psd1FilePath = String.Empty;
ps1FilePath = String.Empty;
nuspecFilePath = String.Empty;
properCasingPkgName = packageName;

var discoveredFiles = Directory.GetFiles(dirPath, "*.*", SearchOption.AllDirectories);
string pkgNamePattern = $"{packageName}*";
Expand All @@ -1185,16 +1186,29 @@ internal static void GetMetadataFilesFromPath(string dirPath, string packageName
{
if (rgx.IsMatch(file))
{
if (file.EndsWith("psd1"))
string fileName = Path.GetFileName(file);
if (fileName.EndsWith("psd1"))
{
if (string.Compare($"{packageName}.psd1", fileName, StringComparison.OrdinalIgnoreCase) == 0)
{
properCasingPkgName = Path.GetFileNameWithoutExtension(file);
}
psd1FilePath = file;
}
else if (file.EndsWith("nuspec"))
{
if (string.Compare($"{packageName}.nuspec", fileName, StringComparison.OrdinalIgnoreCase) == 0)
{
properCasingPkgName = Path.GetFileNameWithoutExtension(file);
}
nuspecFilePath = file;
}
else if (file.EndsWith("ps1"))
{
if (string.Compare($"{packageName}.ps1", fileName, StringComparison.OrdinalIgnoreCase) == 0)
{
properCasingPkgName = Path.GetFileNameWithoutExtension(file);
}
ps1FilePath = file;
}
}
Expand Down
10 changes: 10 additions & 0 deletions test/FindPSResourceTests/FindPSResourceLocal.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Describe 'Test Find-PSResource for local repositories' -tags 'CI' {
$localUNCRepo = 'psgettestlocal3'
$testModuleName = "test_local_mod"
$testModuleName2 = "test_local_mod2"
$testModuleName3 = "Test_Local_Mod3"
$similarTestModuleName = "test_local_mod.similar"
$commandName = "cmd1"
$dscResourceName = "dsc1"
Expand All @@ -33,6 +34,8 @@ Describe 'Test Find-PSResource for local repositories' -tags 'CI' {
New-TestModule -moduleName $testModuleName2 -repoName $localRepo -packageVersion "5.0.0" -prereleaseLabel "" -tags $tagsEscaped
New-TestModule -moduleName $testModuleName2 -repoName $localRepo -packageVersion "5.2.5" -prereleaseLabel $prereleaseLabel -tags $tagsEscaped

New-TestModule -moduleName $testModuleName3 -repoName $localRepo -packageVersion "1.0.0" -prereleaseLabel "" -tags @()

New-TestModule -moduleName $similarTestModuleName -repoName $localRepo -packageVersion "4.0.0" -prereleaseLabel "" -tags $tagsEscaped
New-TestModule -moduleName $similarTestModuleName -repoName $localRepo -packageVersion "5.0.0" -prereleaseLabel "" -tags $tagsEscaped
}
Expand All @@ -48,6 +51,13 @@ Describe 'Test Find-PSResource for local repositories' -tags 'CI' {
$res.Version | Should -Be "5.0.0"
}

It "find resource given specific Name with incorrect casing (should return correct casing)" {
# FindName()
$res = Find-PSResource -Name "test_local_mod3" -Repository $localRepo
$res.Name | Should -Be $testModuleName3
$res.Version | Should -Be "1.0.0"
}

It "find resource given specific Name, Version null (module) from a UNC-based local repository" {
# FindName()
$res = Find-PSResource -Name $testModuleName -Repository $localUNCRepo
Expand Down
12 changes: 5 additions & 7 deletions test/InstallPSResourceTests/InstallPSResourceLocal.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Describe 'Test Install-PSResource for local repositories' -tags 'CI' {
Register-LocalRepos
Register-LocalTestNupkgsRepo

$prereleaseLabel = "alpha001"
$prereleaseLabel = "Alpha001"
$tags = @()

New-TestModule -moduleName $testModuleName -repoName $localRepo -packageVersion "1.0.0" -prereleaseLabel "" -tags $tags
Expand Down Expand Up @@ -131,12 +131,12 @@ Describe 'Test Install-PSResource for local repositories' -tags 'CI' {
$pkg.Version | Should -Be "3.0.0"
}

It "Install resource with latest (including prerelease) version given Prerelease parameter" {
It "Install resource with latest (including prerelease) version given Prerelease parameter (prerelease casing should be correct)" {
Install-PSResource -Name $testModuleName -Prerelease -Repository $localRepo -TrustRepository
$pkg = Get-InstalledPSResource $testModuleName
$pkg.Name | Should -Be $testModuleName
$pkg.Version | Should -Be "5.2.5"
$pkg.Prerelease | Should -Be "alpha001"
$pkg.Prerelease | Should -Be "Alpha001"
}

It "Install resource with cmdlet names from a module already installed with -NoClobber (should not clobber)" {
Expand Down Expand Up @@ -205,7 +205,7 @@ Describe 'Test Install-PSResource for local repositories' -tags 'CI' {

# Windows only
It "Install resource under AllUsers scope - Windows only" -Skip:(!((Get-IsWindows) -and (Test-IsAdmin))) {
Install-PSResource -Name $testModuleName -Repository $localRepo -TrustRepository -Scope AllUsers -Verbose
Install-PSResource -Name $testModuleName -Repository $localRepo -TrustRepository -Scope AllUsers
$pkg = Get-InstalledPSResource $testModuleName -Scope AllUsers
$pkg.Name | Should -Be $testModuleName
$pkg.InstalledLocation.ToString().Contains("Program Files") | Should -Be $true
Expand Down Expand Up @@ -290,10 +290,8 @@ Describe 'Test Install-PSResource for local repositories' -tags 'CI' {
$nupkgName = "Microsoft.Web.Webview2"
$nupkgVersion = "1.0.2792.45"
$repoPath = Get-PSResourceRepository $localNupkgRepo
Write-Verbose -Verbose "repoPath $($repoPath.Uri)"
$searchPkg = Find-PSResource -Name $nupkgName -Version $nupkgVersion -Repository $localNupkgRepo
Write-Verbose -Verbose "search name: $($searchPkg.Name)"
Install-PSResource -Name $nupkgName -Version $nupkgVersion -Repository $localNupkgRepo -TrustRepository -Verbose
Install-PSResource -Name $nupkgName -Version $nupkgVersion -Repository $localNupkgRepo -TrustRepository
$pkg = Get-InstalledPSResource $nupkgName
$pkg.Name | Should -Be $nupkgName
$pkg.Version | Should -Be $nupkgVersion
Expand Down
15 changes: 10 additions & 5 deletions test/PublishPSResourceTests/PublishPSResource.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -270,16 +270,20 @@ Describe "Test Publish-PSResource" -tags 'CI' {
$dependencyVersion = "2.0.0"
New-ModuleManifest -Path (Join-Path -Path $script:DependencyModuleBase -ChildPath "$script:DependencyModuleName.psd1") -ModuleVersion $dependencyVersion -Description "$script:DependencyModuleName module"

Publish-PSResource -Path $script:DependencyModuleBase
Publish-PSResource -Path $script:DependencyModuleBase -Repository $testRepository2
$pkg1 = Find-PSResource $script:DependencyModuleName -Repository $testRepository2
$pkg1 | Should -Not -BeNullOrEmpty
$pkg1.Version | Should -Be $dependencyVersion

# Create module to test
$version = "1.0.0"
New-ModuleManifest -Path (Join-Path -Path $script:PublishModuleBase -ChildPath "$script:PublishModuleName.psd1") -ModuleVersion $version -Description "$script:PublishModuleName module" -RequiredModules @(@{ModuleName = 'PackageManagement'; ModuleVersion = '2.0.0' })

Publish-PSResource -Path $script:PublishModuleBase
Publish-PSResource -Path $script:PublishModuleBase -Repository $testRepository2

$nupkg = Get-ChildItem $script:repositoryPath | select-object -Last 1
$nupkg.Name | Should -Be "$script:PublishModuleName.$version.nupkg"
$pkg2 = Find-PSResource $script:DependencyModuleName -Repository $testRepository2
$pkg2 | Should -Not -BeNullOrEmpty
$pkg2.Version | Should -Be $dependencyVersion
}

It "Publish a module with a dependency that is not published, should throw" {
Expand Down Expand Up @@ -685,7 +689,7 @@ Describe "Test Publish-PSResource" -tags 'CI' {
$expectedPath = Join-Path -Path $script:repositoryPath2 -ChildPath "$ParentModuleName.$ParentVersion.nupkg"
(Get-ChildItem $script:repositoryPath2).FullName | Should -Contain $expectedPath
}

<#
It "Publish a module with required modules (both in string format and hashtable format)" {
# look at functions in test utils for creating a module with prerelease
$ModuleName = "ParentModule"
Expand Down Expand Up @@ -720,4 +724,5 @@ Describe "Test Publish-PSResource" -tags 'CI' {
$expectedPath = Join-Path -Path $script:repositoryPath2 -ChildPath "$ModuleName.$ModuleVersion.nupkg"
(Get-ChildItem $script:repositoryPath2).FullName | Should -Contain $expectedPath
}
#>
}

0 comments on commit 94dff16

Please sign in to comment.