Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 'ToLower' before name comparison and split #1738

Merged
merged 25 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
5 changes: 4 additions & 1 deletion src/code/InstallHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -813,7 +813,7 @@ private Hashtable BeginPackageInstall(
{
return packagesHash;
}

alerickson marked this conversation as resolved.
Show resolved Hide resolved
pkgToInstall.RepositorySourceLocation = repository.Uri.ToString();
pkgToInstall.AdditionalMetadata.TryGetValue("NormalizedVersion", out string pkgVersion);
if (pkgVersion == null) {
Expand Down Expand Up @@ -972,6 +972,8 @@ private bool TryInstallToTempPath(
try
{
var pathToFile = Path.Combine(tempInstallPath, $"{pkgName}.{normalizedPkgVersion}.zip");
_cmdletPassedIn.WriteVerbose($"pathToFile IS: {pathToFile}.");

using var fs = File.Create(pathToFile);
responseStream.Seek(0, System.IO.SeekOrigin.Begin);
responseStream.CopyTo(fs);
Expand All @@ -980,6 +982,7 @@ private bool TryInstallToTempPath(
// Expand the zip file
var pkgVersion = pkgToInstall.Version.ToString();
var tempDirNameVersion = Path.Combine(tempInstallPath, pkgName.ToLower(), pkgVersion);

Directory.CreateDirectory(tempDirNameVersion);

if (!TryExtractToDirectory(pathToFile, tempDirNameVersion, out error))
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
18 changes: 16 additions & 2 deletions test/InstallPSResourceTests/InstallPSResourceLocal.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,27 @@ Describe 'Test Install-PSResource for local repositories' -tags 'CI' {
}

It "Install resource with cmdlet names from a module already installed with -NoClobber (should not clobber)" {
Install-PSResource -Name $testModuleClobber -Repository $localRepo -TrustRepository
Install-PSResource -Name $testModuleClobber -Repository $localRepo -TrustRepository -Verbose
$pkg = Get-InstalledPSResource $testModuleClobber
$pkg.Name | Should -Be $testModuleClobber
$pkg.Version | Should -Be "1.0.0"

Install-PSResource -Name $testModuleClobber2 -Repository $localRepo -TrustRepository -NoClobber -ErrorVariable ev -ErrorAction SilentlyContinue
Install-PSResource -Name $testModuleClobber2 -Repository $localRepo -TrustRepository -NoClobber -ErrorVariable ev -ErrorAction SilentlyContinue -verbose
$pkg = Get-InstalledPSResource $testModuleClobber2 -ErrorAction SilentlyContinue

if (!$pkg) {
# Get the first available module named 'clobbertestmodule1' and its exported commands
$module2 = (Get-Module -ListAvailable $testModuleClobber2)[0]
$module2Idx = $module[0]
# Iterate through each exported command in the module
foreach ($command in $module2Idx.ExportedCommands.Values) {
# Output the command's name and details
Write-Verbose -Verbose "Command Name: $($command.Name)"
Write-Verbose -Verbose "Command Type: $($command.CommandType)"
Write-Verbose -Verbose "----------------------------------"
}
}

$pkg | Should -BeNullOrEmpty
$ev.Count | Should -Be 1
$ev[0] | Should -Be "'testModuleClobber2' package could not be installed with error: The following commands are already available on this system: 'Test-Cmdlet1, Test-Cmdlet1'. This module 'testModuleClobber2' may override the existing commands. If you still want to install this module 'testModuleClobber2', remove the -NoClobber parameter."
Expand Down
Loading