Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
131 changes: 122 additions & 9 deletions src/code/LocalServerApiCalls.cs
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,24 @@ private FindResults FindNameHelper(string packageName, string[] tags, bool inclu
string regexPattern = $"{packageName}" + @"(\.\d+){1,3}(?:[a-zA-Z0-9-.]+|.\d)?\.nupkg";
_cmdletPassedIn.WriteDebug($"package file name pattern to be searched for is: {regexPattern}");

foreach (string path in Directory.GetFiles(Repository.Uri.LocalPath))
string[] foundFiles = Utils.EmptyStrArray;
try
{
foundFiles = Directory.GetFiles(Repository.Uri.LocalPath);
}
catch (Exception e)
{
_cmdletPassedIn.WriteWarning($"Unable to resolve repository source '{Repository.Uri.LocalPath}' due to exception: {e.Message}");
errRecord = new ErrorRecord(
exception: e,
"FileAccessFailure",
ErrorCategory.ReadError,
this);

return findResponse;
}

foreach (string path in foundFiles)
{
string packageFullName = Path.GetFileName(path);
bool isMatch = Regex.IsMatch(packageFullName, regexPattern, RegexOptions.IgnoreCase);
Expand Down Expand Up @@ -333,7 +350,12 @@ private FindResults FindNameGlobbingHelper(string packageName, string[] tags, bo
List<Hashtable> pkgsFound = new List<Hashtable>();
errRecord = null;

Hashtable pkgVersionsFound = GetMatchingFilesGivenNamePattern(packageNameWithWildcard: packageName, includePrerelease: includePrerelease);
Hashtable pkgVersionsFound = GetMatchingFilesGivenNamePattern(packageNameWithWildcard: packageName, includePrerelease: includePrerelease, out errRecord);
if (errRecord != null)
{
// ErrorRecord errRecord is only set if directory access to retrieve files failed (i.e network error when accessing file share, incorrect directory path, etc), not if desired files within directory are not found since this is a wildcard scenario.
return findResponse;
}

List<string> pkgNamesList = pkgVersionsFound.Keys.Cast<string>().ToList();
foreach(string pkgFound in pkgNamesList)
Expand Down Expand Up @@ -382,7 +404,24 @@ private FindResults FindVersionHelper(string packageName, string version, string
string pkgPath = String.Empty;
string actualPkgName = String.Empty;

foreach (string path in Directory.GetFiles(Repository.Uri.LocalPath))
string[] foundFiles = Utils.EmptyStrArray;
try
{
foundFiles = Directory.GetFiles(Repository.Uri.LocalPath);
}
catch (Exception e)
{
_cmdletPassedIn.WriteWarning($"Unable to resolve repository source '{Repository.Uri.LocalPath}' due to exception: {e.Message}");
errRecord = new ErrorRecord(
exception: e,
"FileAccessFailure",
ErrorCategory.ReadError,
this);

return findResponse;
}

foreach (string path in foundFiles)
{
string packageFullName = Path.GetFileName(path);
bool isMatch = Regex.IsMatch(packageFullName, regexPattern, RegexOptions.IgnoreCase);
Expand Down Expand Up @@ -450,7 +489,12 @@ private FindResults FindTagsHelper(string[] tags, bool includePrerelease, out Er
List<Hashtable> pkgsFound = new List<Hashtable>();
errRecord = null;

Hashtable pkgVersionsFound = GetMatchingFilesGivenNamePattern(packageNameWithWildcard: String.Empty, includePrerelease: includePrerelease);
Hashtable pkgVersionsFound = GetMatchingFilesGivenNamePattern(packageNameWithWildcard: String.Empty, includePrerelease: includePrerelease, errRecord: out errRecord);
if (errRecord != null)
{
// ErrorRecord errRecord is only set if directory access to retrieve files failed (i.e network error when accessing file share, incorrect directory path, etc), not if desired files within directory are not found since this is a wildcard scenario.
return findResponse;
}

List<string> pkgNamesList = pkgVersionsFound.Keys.Cast<string>().ToList();
foreach(string pkgFound in pkgNamesList)
Expand Down Expand Up @@ -496,7 +540,24 @@ private Stream InstallName(string packageName, bool includePrerelease, out Error
NuGetVersion latestVersion = new NuGetVersion("0.0.0.0");
String latestVersionPath = String.Empty;

foreach (string path in Directory.GetFiles(Repository.Uri.LocalPath))
string[] foundFiles = Utils.EmptyStrArray;
try
{
foundFiles = Directory.GetFiles(Repository.Uri.LocalPath);
}
catch (Exception e)
{
_cmdletPassedIn.WriteWarning($"Unable to resolve repository source '{Repository.Uri.LocalPath}' due to exception: {e.Message}");
errRecord = new ErrorRecord(
exception: e,
"FileAccessFailure",
ErrorCategory.ReadError,
this);

return fs;
}

foreach (string path in foundFiles)
{
string packageFullName = Path.GetFileName(path);

Expand Down Expand Up @@ -581,7 +642,24 @@ private Stream InstallVersion(string packageName, string version, out ErrorRecor
WildcardPattern pkgNamePattern = new WildcardPattern($"{packageName}.{version}.nupkg*", WildcardOptions.IgnoreCase);
String pkgVersionPath = String.Empty;

foreach (string path in Directory.GetFiles(Repository.Uri.LocalPath))
string[] foundFiles = Utils.EmptyStrArray;
try
{
foundFiles = Directory.GetFiles(Repository.Uri.LocalPath);
}
catch (Exception e)
{
_cmdletPassedIn.WriteWarning($"Unable to resolve repository source '{Repository.Uri.LocalPath}' due to exception: {e.Message}");
errRecord = new ErrorRecord(
exception: e,
"FileAccessFailure",
ErrorCategory.ReadError,
this);

return fs;
}

foreach (string path in foundFiles)
{
string packageFullName = Path.GetFileName(path);

Expand Down Expand Up @@ -778,7 +856,24 @@ private Hashtable GetMatchingFilesGivenSpecificName(string packageName, bool inc
Hashtable pkgVersionsFound = new Hashtable(StringComparer.OrdinalIgnoreCase);
errRecord = null;

foreach (string path in Directory.GetFiles(Repository.Uri.LocalPath))
string[] foundFiles = Utils.EmptyStrArray;
try
{
foundFiles = Directory.GetFiles(Repository.Uri.LocalPath);
}
catch (Exception e)
{
_cmdletPassedIn.WriteWarning($"Unable to resolve repository source '{Repository.Uri.LocalPath}' due to exception: {e.Message}");
errRecord = new ErrorRecord(
exception: e,
"FileAccessFailure",
ErrorCategory.ReadError,
this);

return pkgVersionsFound;
}

foreach (string path in foundFiles)
{
string packageFullName = Path.GetFileName(path);

Expand Down Expand Up @@ -809,9 +904,10 @@ private Hashtable GetMatchingFilesGivenSpecificName(string packageName, bool inc
/// hashtable with those that match the name wildcard pattern and prerelease requirements provided.
/// This helper method is called for FindAll(), FindTags(), FindNameGlobbing() scenarios.
/// </summary>
private Hashtable GetMatchingFilesGivenNamePattern(string packageNameWithWildcard, bool includePrerelease)
private Hashtable GetMatchingFilesGivenNamePattern(string packageNameWithWildcard, bool includePrerelease, out ErrorRecord errRecord)
{
_cmdletPassedIn.WriteDebug("In LocalServerApiCalls::GetMatchingFilesGivenNamePattern()");
errRecord = null;
bool isNameFilteringRequired = !String.IsNullOrEmpty(packageNameWithWildcard);

// wildcard name possibilities: power*, *get, power*get
Expand All @@ -820,7 +916,24 @@ private Hashtable GetMatchingFilesGivenNamePattern(string packageNameWithWildcar
Regex rx = new Regex(@"\.\d+\.", RegexOptions.Compiled | RegexOptions.IgnoreCase);
Hashtable pkgVersionsFound = new Hashtable(StringComparer.OrdinalIgnoreCase);

foreach (string path in Directory.GetFiles(Repository.Uri.LocalPath))
string[] foundFiles = Utils.EmptyStrArray;
try
{
foundFiles = Directory.GetFiles(Repository.Uri.LocalPath);
}
catch (Exception e)
{
_cmdletPassedIn.WriteWarning($"Unable to resolve repository source '{Repository.Uri.LocalPath}' due to exception: {e.Message}");
errRecord = new ErrorRecord(
exception: e,
"FileAccessFailure",
ErrorCategory.ReadError,
this);

return pkgVersionsFound;
}

foreach (string path in foundFiles)
{
string packageFullName = Path.GetFileName(path);
MatchCollection matches = rx.Matches(packageFullName);
Expand Down
19 changes: 19 additions & 0 deletions test/FindPSResourceTests/FindPSResourceLocal.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Describe 'Test Find-PSResource for local repositories' -tags 'CI' {
BeforeAll{
$localRepo = "psgettestlocal"
$localUNCRepo = 'psgettestlocal3'
$localPrivateRepo = "psgettestlocal5"
$testModuleName = "test_local_mod"
$testModuleName2 = "test_local_mod2"
$testModuleName3 = "Test_Local_Mod3"
Expand Down Expand Up @@ -347,4 +348,22 @@ Describe 'Test Find-PSResource for local repositories' -tags 'CI' {
$res = Find-PSResource -Name 'Az.KeyVault' -Repository $localRepo
$res.Version | Should -Be "6.3.1"
}

It "Find should not silently fail if network connection to local private repository cannot be established and remainder repositories should be searched" {
$privateRepo = Get-PSResourceRepository $localPrivateRepo
$res = Find-PSResource -Name $testModuleName -WarningVariable WarningVar -WarningAction SilentlyContinue
$WarningVar | Should -Not -BeNullOrEmpty
$WarningVar[0] | Should -Match "$($privateRepo.Uri.LocalPath)"
$res.Name | Should -Contain $testModuleName
$res.Version | Should -Be "1.0.0"
}

It "Find should not silently fail if network connection to local private repository cannot be established and package version was provided and remainder repositories should be searched" {
$privateRepo = Get-PSResourceRepository $localPrivateRepo
$res = Find-PSResource -Name $testModuleName -Version "1.0.0" -WarningVariable WarningVar -WarningAction SilentlyContinue
$WarningVar | Should -Not -BeNullOrEmpty
$WarningVar[0] | Should -Match "$($privateRepo.Uri.LocalPath)"
$res.Name | Should -Contain $testModuleName
$res.Version | Should -Be "1.0.0"
}
}
19 changes: 19 additions & 0 deletions test/InstallPSResourceTests/InstallPSResourceLocal.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Describe 'Test Install-PSResource for local repositories' -tags 'CI' {
BeforeAll {
$localRepo = "psgettestlocal"
$localUNCRepo = "psgettestlocal3"
$localPrivateRepo = "psgettestlocal5"
$localNupkgRepo = "localNupkgRepo"
$testModuleName = "test_local_mod"
$testModuleName2 = "test_local_mod2"
Expand Down Expand Up @@ -296,4 +297,22 @@ Describe 'Test Install-PSResource for local repositories' -tags 'CI' {
$pkg.Name | Should -Be $nupkgName
$pkg.Version | Should -Be $nupkgVersion
}

It "Install should not silently fail if network connection to local private repository cannot be established and remainder repositories should be searched" {
$privateRepo = Get-PSResourceRepository $localPrivateRepo
$res = Install-PSResource -Name $testModuleName -TrustRepository -PassThru -WarningVariable WarningVar -WarningAction SilentlyContinue
$WarningVar | Should -Not -BeNullOrEmpty
$WarningVar[0] | Should -Match "$($privateRepo.Uri.LocalPath)"
$res.Name | Should -Contain $testModuleName
$res.Version | Should -Be "1.0.0"
}

It "Install should not silently fail if network connection to local private repository cannot be established and package version was provided and remainder repositories should be searched" {
$privateRepo = Get-PSResourceRepository $localPrivateRepo
$res = Install-PSResource -Name $testModuleName -Version "1.0.0" -TrustRepository -PassThru -WarningVariable WarningVar -WarningAction SilentlyContinue
$WarningVar | Should -Not -BeNullOrEmpty
$WarningVar[0] | Should -Match "$($privateRepo.Uri.LocalPath)"
$res.Name | Should -Contain $testModuleName
$res.Version | Should -Be "1.0.0"
}
}
11 changes: 10 additions & 1 deletion test/PSGetTestUtils.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -269,8 +269,17 @@ function Register-LocalRepos {
Trusted = $false
}
Register-PSResourceRepository @localRepoParams2

$path4 = "\\localhost\PSRepoLocal"
$localRepoParams2 = @{
Name = "psgettestlocal5"
Uri = $path4
Priority = 30
Trusted = $false
}
Register-PSResourceRepository @localRepoParams2

Write-Verbose "registered psgettestlocal, psgettestlocal2, psgettestlocal3, psgettestlocal4"
Write-Verbose "registered psgettestlocal, psgettestlocal2, psgettestlocal3, psgettestlocal4, psgettestlocal5"
}

function Register-LocalTestNupkgsRepo {
Expand Down
Loading