Skip to content

Commit f9e3d57

Browse files
committed
Merge branch 'master' of https://github.com/PowerShell/PSResourceGet into bugfix-package-regex
2 parents 664c060 + 455722d commit f9e3d57

File tree

8 files changed

+100
-79
lines changed

8 files changed

+100
-79
lines changed

.ci/test.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ jobs:
4848
- ${{ parameters.powershellExecutable }}: |
4949
$modulePath = Join-Path -Path $env:AGENT_TEMPDIRECTORY -ChildPath 'TempModules'
5050
Write-Verbose -Verbose "Install Microsoft.PowerShell.PSResourceGet to temp module path"
51-
Save-Module -Name Microsoft.PowerShell.PSResourceGet -MinimumVersion 0.9.0-rc1 -Path $modulePath -AllowPrerelease -Force
51+
Save-Module -Name Microsoft.PowerShell.PSResourceGet -Path $modulePath -Force -Verbose
5252
Write-Verbose -Verbose "Install Pester 4.X to temp module path"
5353
Save-Module -Name "Pester" -MaximumVersion 4.99 -Path $modulePath -Force
5454
displayName: Install Microsoft.PowerShell.PSResourceGet and Pester
@@ -59,7 +59,7 @@ jobs:
5959
Write-Verbose -Verbose "Importing build utilities (buildtools.psd1)"
6060
Import-Module -Name (Join-Path -Path '${{ parameters.buildDirectory }}' -ChildPath 'buildtools.psd1') -Force
6161
#
62-
Install-ModulePackageForTest -PackagePath "$(System.ArtifactsDirectory)"
62+
Install-ModulePackageForTest -PackagePath "$(System.ArtifactsDirectory)" -ErrorAction stop -Verbose
6363
displayName: Install module for test from downloaded artifact
6464
workingDirectory: ${{ parameters.buildDirectory }}
6565

buildtools.psm1

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,23 @@ function Install-ModulePackageForTest {
120120
}
121121

122122
Write-Verbose -Verbose -Message "Installing module $($config.ModuleName) to build output path $installationPath"
123-
Save-PSResource -Name $config.ModuleName -Repository $localRepoName -Path $installationPath -SkipDependencyCheck -Prerelease -Confirm:$false -TrustRepository
123+
$psgetModuleBase = (get-command save-psresource).Module.ModuleBase
124+
$psgetVersion = (get-command save-psresource).Module.Version.ToString()
125+
$psgetPrerelease = (get-command find-psresource).module.PrivateData.PSData.Prerelease
126+
Write-Verbose -Verbose -Message "PSResourceGet module base imported: $psgetModuleBase"
127+
Write-Verbose -Verbose -Message "PSResourceGet version base imported: $psgetVersion"
128+
Write-Verbose -Verbose -Message "PSResourceGet prerelease base imported: $psgetPrerelease"
129+
#Save-PSResource -Name $config.ModuleName -Repository $localRepoName -Path $installationPath -SkipDependencyCheck -Prerelease -Confirm:$false -TrustRepository
130+
131+
Register-PSRepository -Name $localRepoName -SourceLocation $packagePathWithNupkg -InstallationPolicy Trusted -Verbose
132+
$psgetv2ModuleBase = (get-command save-module).Module.ModuleBase
133+
$psgetv2Version = (get-command save-module).Module.Version.ToString()
134+
$psgetv2Prerelease = (get-command save-module).module.PrivateData.PSData.Prerelease
135+
Write-Verbose -Verbose -Message "PowerShellGet module base imported: $psgetv2ModuleBase"
136+
Write-Verbose -Verbose -Message "PowerShellGet version base imported: $psgetv2Version"
137+
Write-Verbose -Verbose -Message "PowerShellGet prerelease base imported: $psgetv2Prerelease"
138+
Save-Module -Name $config.ModuleName -Repository $localRepoName -Path $installationPath -Force -Verbose -AllowPrerelease -Confirm:$false
139+
Unregister-PSRepository -Name $localRepoName
124140

125141
Write-Verbose -Verbose -Message "Unregistering local package repo: $localRepoName"
126142
Unregister-PSResourceRepository -Name $localRepoName -Confirm:$false

src/code/ContainerRegistryServerAPICalls.cs

Lines changed: 36 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ internal class ContainerRegistryServerAPICalls : ServerApiCall
4343
const string containerRegistryOAuthTokenUrlTemplate = "https://{0}/oauth2/token"; // 0 - registry
4444
const string containerRegistryManifestUrlTemplate = "https://{0}/v2/{1}/manifests/{2}"; // 0 - registry, 1 - repo(modulename), 2 - tag(version)
4545
const string containerRegistryBlobDownloadUrlTemplate = "https://{0}/v2/{1}/blobs/{2}"; // 0 - registry, 1 - repo(modulename), 2 - layer digest
46-
const string containerRegistryFindImageVersionUrlTemplate = "https://{0}/acr/v1/{1}/_tags{2}"; // 0 - registry, 1 - repo(modulename), 2 - /tag(version)
46+
const string containerRegistryFindImageVersionUrlTemplate = "https://{0}/v2/{1}/tags/list"; // 0 - registry, 1 - repo(modulename)
4747
const string containerRegistryStartUploadTemplate = "https://{0}/v2/{1}/blobs/uploads/"; // 0 - registry, 1 - packagename
4848
const string containerRegistryEndUploadTemplate = "https://{0}{1}&digest=sha256:{2}"; // 0 - registry, 1 - location, 2 - digest
4949

@@ -413,6 +413,7 @@ internal string GetContainerRegistryAccessToken(out ErrorRecord errRecord)
413413
else
414414
{
415415
_cmdletPassedIn.WriteVerbose("Repository is unauthenticated");
416+
return null;
416417
}
417418
}
418419

@@ -572,27 +573,19 @@ internal async Task<HttpContent> GetContainerRegistryBlobAsync(string packageNam
572573
/// </summary>
573574
internal JObject FindContainerRegistryImageTags(string packageName, string version, string containerRegistryAccessToken, out ErrorRecord errRecord)
574575
{
575-
/* response returned looks something like:
576-
* "registry": "myregistry.azurecr.io"
577-
* "imageName": "hello-world"
578-
* "tags": [
579-
* {
580-
* ""name"": ""1.0.0"",
581-
* ""digest"": ""sha256:92c7f9c92844bbbb5d0a101b22f7c2a7949e40f8ea90c8b3bc396879d95e899a"",
582-
* ""createdTime"": ""2023-12-23T18:06:48.9975733Z"",
583-
* ""lastUpdateTime"": ""2023-12-23T18:06:48.9975733Z"",
584-
* ""signed"": false,
585-
* ""changeableAttributes"": {
586-
* ""deleteEnabled"": true,
587-
* ""writeEnabled"": true,
588-
* ""readEnabled"": true,
589-
* ""listEnabled"": true
590-
* }
591-
* }]
592-
*/
576+
/*
577+
{
578+
"name": "<name>",
579+
"tags": [
580+
"<tag1>",
581+
"<tag2>",
582+
"<tag3>"
583+
]
584+
}
585+
*/
593586
_cmdletPassedIn.WriteDebug("In ContainerRegistryServerAPICalls::FindContainerRegistryImageTags()");
594587
string resolvedVersion = string.Equals(version, "*", StringComparison.OrdinalIgnoreCase) ? null : $"/{version}";
595-
string findImageUrl = string.Format(containerRegistryFindImageVersionUrlTemplate, Registry, packageName, resolvedVersion);
588+
string findImageUrl = string.Format(containerRegistryFindImageVersionUrlTemplate, Registry, packageName);
596589
var defaultHeaders = GetDefaultHeaders(containerRegistryAccessToken);
597590
return GetHttpResponseJObjectUsingDefaultHeaders(findImageUrl, HttpMethod.Get, defaultHeaders, out errRecord);
598591
}
@@ -1664,51 +1657,36 @@ private Hashtable[] FindPackagesWithVersionHelper(string packageName, VersionTyp
16641657

16651658
foreach (var pkgVersionTagInfo in allPkgVersions)
16661659
{
1667-
using (JsonDocument pkgVersionEntry = JsonDocument.Parse(pkgVersionTagInfo.ToString()))
1660+
string pkgVersionString = pkgVersionTagInfo.ToString();
1661+
// determine if the package version that is a repository tag is a valid NuGetVersion
1662+
if (!NuGetVersion.TryParse(pkgVersionString, out NuGetVersion pkgVersion))
16681663
{
1669-
JsonElement rootDom = pkgVersionEntry.RootElement;
1670-
if (!rootDom.TryGetProperty("name", out JsonElement pkgVersionElement))
1671-
{
1672-
errRecord = new ErrorRecord(
1673-
new InvalidOrEmptyResponse($"Response does not contain version element ('name') for package '{packageName}' in '{Repository.Name}'."),
1674-
"FindNameFailure",
1675-
ErrorCategory.InvalidResult,
1676-
this);
1677-
1678-
return null;
1679-
}
1680-
1681-
string pkgVersionString = pkgVersionElement.ToString();
1682-
// determine if the package version that is a repository tag is a valid NuGetVersion
1683-
if (!NuGetVersion.TryParse(pkgVersionString, out NuGetVersion pkgVersion))
1684-
{
1685-
errRecord = new ErrorRecord(
1686-
new ArgumentException($"Version {pkgVersionString} to be parsed from metadata is not a valid NuGet version."),
1687-
"FindNameFailure",
1688-
ErrorCategory.InvalidArgument,
1689-
this);
1664+
errRecord = new ErrorRecord(
1665+
new ArgumentException($"Version {pkgVersionString} to be parsed from metadata is not a valid NuGet version."),
1666+
"FindNameFailure",
1667+
ErrorCategory.InvalidArgument,
1668+
this);
16901669

1691-
return null;
1692-
}
1670+
return null;
1671+
}
16931672

1694-
_cmdletPassedIn.WriteDebug($"'{packageName}' version parsed as '{pkgVersion}'");
1673+
_cmdletPassedIn.WriteDebug($"'{packageName}' version parsed as '{pkgVersion}'");
16951674

1696-
if (isSpecificVersionSearch)
1675+
if (isSpecificVersionSearch)
1676+
{
1677+
if (pkgVersion.ToNormalizedString() == specificVersion.ToNormalizedString())
16971678
{
1698-
if (pkgVersion.ToNormalizedString() == specificVersion.ToNormalizedString())
1699-
{
1700-
// accounts for FindVersion() scenario
1701-
sortedPkgs.Add(pkgVersion, pkgVersionString);
1702-
break;
1703-
}
1679+
// accounts for FindVersion() scenario
1680+
sortedPkgs.Add(pkgVersion, pkgVersionString);
1681+
break;
17041682
}
1705-
else
1683+
}
1684+
else
1685+
{
1686+
if (versionRange.Satisfies(pkgVersion) && (!pkgVersion.IsPrerelease || includePrerelease))
17061687
{
1707-
if (versionRange.Satisfies(pkgVersion) && (!pkgVersion.IsPrerelease || includePrerelease))
1708-
{
1709-
// accounts for FindVersionGlobbing() and FindName() scenario
1710-
sortedPkgs.Add(pkgVersion, pkgVersionString);
1711-
}
1688+
// accounts for FindVersionGlobbing() and FindName() scenario
1689+
sortedPkgs.Add(pkgVersion, pkgVersionString);
17121690
}
17131691
}
17141692
}

src/code/LocalServerApiCalls.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -685,7 +685,7 @@ private Hashtable GetMetadataFromNupkg(string packageName, string packagePath, s
685685
string psd1FilePath = String.Empty;
686686
string ps1FilePath = String.Empty;
687687
string nuspecFilePath = String.Empty;
688-
Utils.GetMetadataFilesFromPath(tempDiscoveryPath, packageName, out psd1FilePath, out ps1FilePath, out nuspecFilePath);
688+
Utils.GetMetadataFilesFromPath(tempDiscoveryPath, packageName, out psd1FilePath, out ps1FilePath, out nuspecFilePath, out string properCasingPkgName);
689689

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

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

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

732732
pkgMetadata = parsedScript.ToHashtable();
733-
pkgMetadata.Add("Id", packageName);
733+
pkgMetadata.Add("Id", properCasingPkgName);
734734
pkgMetadata.Add(_fileTypeKey, Utils.MetadataFileType.ScriptFile);
735735
pkgTags.AddRange(pkgMetadata["Tags"] as string[]);
736736

@@ -916,7 +916,7 @@ private NuGetVersion GetInfoFromFileName(string packageFullName, string packageN
916916

917917
string[] packageWithoutName = packageFullName.ToLower().Split(new string[]{ $"{packageName.ToLower()}." }, StringSplitOptions.RemoveEmptyEntries);
918918
string packageVersionAndExtension = packageWithoutName[0];
919-
string[] originalFileNameParts = packageFullName.Split(new string[]{ $".{packageVersionAndExtension}" }, StringSplitOptions.RemoveEmptyEntries);
919+
string[] originalFileNameParts = packageFullName.ToLower().Split(new string[]{ $".{packageVersionAndExtension.ToLower()}" }, StringSplitOptions.RemoveEmptyEntries);
920920
actualName = String.IsNullOrEmpty(originalFileNameParts[0]) ? packageName : originalFileNameParts[0];
921921
int extensionDot = packageVersionAndExtension.LastIndexOf('.');
922922
string version = packageVersionAndExtension.Substring(0, extensionDot);

src/code/Utils.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1172,11 +1172,12 @@ internal static HashSet<string> GetInstalledPackages(List<string> pathsToSearch,
11721172
return pkgsInstalledOnMachine;
11731173
}
11741174

1175-
internal static void GetMetadataFilesFromPath(string dirPath, string packageName, out string psd1FilePath, out string ps1FilePath, out string nuspecFilePath)
1175+
internal static void GetMetadataFilesFromPath(string dirPath, string packageName, out string psd1FilePath, out string ps1FilePath, out string nuspecFilePath, out string properCasingPkgName)
11761176
{
11771177
psd1FilePath = String.Empty;
11781178
ps1FilePath = String.Empty;
11791179
nuspecFilePath = String.Empty;
1180+
properCasingPkgName = packageName;
11801181

11811182
var discoveredFiles = Directory.GetFiles(dirPath, "*.*", SearchOption.AllDirectories);
11821183
string pkgNamePattern = $"{packageName}*";
@@ -1185,16 +1186,29 @@ internal static void GetMetadataFilesFromPath(string dirPath, string packageName
11851186
{
11861187
if (rgx.IsMatch(file))
11871188
{
1188-
if (file.EndsWith("psd1"))
1189+
string fileName = Path.GetFileName(file);
1190+
if (fileName.EndsWith("psd1"))
11891191
{
1192+
if (string.Compare($"{packageName}.psd1", fileName, StringComparison.OrdinalIgnoreCase) == 0)
1193+
{
1194+
properCasingPkgName = Path.GetFileNameWithoutExtension(file);
1195+
}
11901196
psd1FilePath = file;
11911197
}
11921198
else if (file.EndsWith("nuspec"))
11931199
{
1200+
if (string.Compare($"{packageName}.nuspec", fileName, StringComparison.OrdinalIgnoreCase) == 0)
1201+
{
1202+
properCasingPkgName = Path.GetFileNameWithoutExtension(file);
1203+
}
11941204
nuspecFilePath = file;
11951205
}
11961206
else if (file.EndsWith("ps1"))
11971207
{
1208+
if (string.Compare($"{packageName}.ps1", fileName, StringComparison.OrdinalIgnoreCase) == 0)
1209+
{
1210+
properCasingPkgName = Path.GetFileNameWithoutExtension(file);
1211+
}
11981212
ps1FilePath = file;
11991213
}
12001214
}

test/FindPSResourceTests/FindPSResourceLocal.Tests.ps1

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Describe 'Test Find-PSResource for local repositories' -tags 'CI' {
1414
$localUNCRepo = 'psgettestlocal3'
1515
$testModuleName = "test_local_mod"
1616
$testModuleName2 = "test_local_mod2"
17+
$testModuleName3 = "Test_Local_Mod3"
1718
$similarTestModuleName = "test_local_mod.similar"
1819
$commandName = "cmd1"
1920
$dscResourceName = "dsc1"
@@ -33,6 +34,8 @@ Describe 'Test Find-PSResource for local repositories' -tags 'CI' {
3334
New-TestModule -moduleName $testModuleName2 -repoName $localRepo -packageVersion "5.0.0" -prereleaseLabel "" -tags $tagsEscaped
3435
New-TestModule -moduleName $testModuleName2 -repoName $localRepo -packageVersion "5.2.5" -prereleaseLabel $prereleaseLabel -tags $tagsEscaped
3536

37+
New-TestModule -moduleName $testModuleName3 -repoName $localRepo -packageVersion "1.0.0" -prereleaseLabel "" -tags @()
38+
3639
New-TestModule -moduleName $similarTestModuleName -repoName $localRepo -packageVersion "4.0.0" -prereleaseLabel "" -tags $tagsEscaped
3740
New-TestModule -moduleName $similarTestModuleName -repoName $localRepo -packageVersion "5.0.0" -prereleaseLabel "" -tags $tagsEscaped
3841
}
@@ -48,6 +51,13 @@ Describe 'Test Find-PSResource for local repositories' -tags 'CI' {
4851
$res.Version | Should -Be "5.0.0"
4952
}
5053

54+
It "find resource given specific Name with incorrect casing (should return correct casing)" {
55+
# FindName()
56+
$res = Find-PSResource -Name "test_local_mod3" -Repository $localRepo
57+
$res.Name | Should -Be $testModuleName3
58+
$res.Version | Should -Be "1.0.0"
59+
}
60+
5161
It "find resource given specific Name, Version null (module) from a UNC-based local repository" {
5262
# FindName()
5363
$res = Find-PSResource -Name $testModuleName -Repository $localUNCRepo

test/InstallPSResourceTests/InstallPSResourceLocal.Tests.ps1

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ Describe 'Test Install-PSResource for local repositories' -tags 'CI' {
2525
Register-LocalRepos
2626
Register-LocalTestNupkgsRepo
2727

28-
$prereleaseLabel = "alpha001"
28+
$prereleaseLabel = "Alpha001"
2929
$tags = @()
3030

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

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

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

206206
# Windows only
207207
It "Install resource under AllUsers scope - Windows only" -Skip:(!((Get-IsWindows) -and (Test-IsAdmin))) {
208-
Install-PSResource -Name $testModuleName -Repository $localRepo -TrustRepository -Scope AllUsers -Verbose
208+
Install-PSResource -Name $testModuleName -Repository $localRepo -TrustRepository -Scope AllUsers
209209
$pkg = Get-InstalledPSResource $testModuleName -Scope AllUsers
210210
$pkg.Name | Should -Be $testModuleName
211211
$pkg.InstalledLocation.ToString().Contains("Program Files") | Should -Be $true
@@ -290,10 +290,8 @@ Describe 'Test Install-PSResource for local repositories' -tags 'CI' {
290290
$nupkgName = "Microsoft.Web.Webview2"
291291
$nupkgVersion = "1.0.2792.45"
292292
$repoPath = Get-PSResourceRepository $localNupkgRepo
293-
Write-Verbose -Verbose "repoPath $($repoPath.Uri)"
294293
$searchPkg = Find-PSResource -Name $nupkgName -Version $nupkgVersion -Repository $localNupkgRepo
295-
Write-Verbose -Verbose "search name: $($searchPkg.Name)"
296-
Install-PSResource -Name $nupkgName -Version $nupkgVersion -Repository $localNupkgRepo -TrustRepository -Verbose
294+
Install-PSResource -Name $nupkgName -Version $nupkgVersion -Repository $localNupkgRepo -TrustRepository
297295
$pkg = Get-InstalledPSResource $nupkgName
298296
$pkg.Name | Should -Be $nupkgName
299297
$pkg.Version | Should -Be $nupkgVersion

0 commit comments

Comments
 (0)