From b1604ac4dedbf9f23ece817a806ac5733c936923 Mon Sep 17 00:00:00 2001 From: Anam Navied Date: Tue, 24 Aug 2021 12:55:47 -0400 Subject: [PATCH 01/12] support Type and Tag parameter set --- src/code/FindPSResource.cs | 80 +++++++++++++++++++------------------- 1 file changed, 39 insertions(+), 41 deletions(-) diff --git a/src/code/FindPSResource.cs b/src/code/FindPSResource.cs index 1e576ad29..e5cc22dd6 100644 --- a/src/code/FindPSResource.cs +++ b/src/code/FindPSResource.cs @@ -134,11 +134,11 @@ public sealed class FindPSResource : PSCmdlet protected override void BeginProcessing() { _source = new CancellationTokenSource(); - _cancellationToken = _source.Token; - - // Create a respository story (the PSResourceRepository.xml file) if it does not already exist - // This is to create a better experience for those who have just installed v3 and want to get up and running quickly - RepositorySettings.CheckRepositoryStore(); + _cancellationToken = _source.Token; + + // Create a respository story (the PSResourceRepository.xml file) if it does not already exist + // This is to create a better experience for those who have just installed v3 and want to get up and running quickly + RepositorySettings.CheckRepositoryStore(); } protected override void StopProcessing() @@ -184,52 +184,50 @@ private void ProcessResourceNameParameterSet() { if (!MyInvocation.BoundParameters.ContainsKey(nameof(Name))) { - // TODO: Add support for Tag and Type parameters without Name parameter being specified. - if (MyInvocation.BoundParameters.ContainsKey(nameof(Type)) || MyInvocation.BoundParameters.ContainsKey(nameof(Tag))) + // only cases where Name is allowed to not be specified is if Type or Tag parameters are + if (!MyInvocation.BoundParameters.ContainsKey(nameof(Type)) && !MyInvocation.BoundParameters.ContainsKey(nameof(Tag))) { ThrowTerminatingError( new ErrorRecord( - new PSNotImplementedException("Search by Tag or Type parameter is not yet implemented."), - "TagTypeSearchNotYetImplemented", - ErrorCategory.NotImplemented, + new PSInvalidOperationException("Name parameter must be provided."), + "NameParameterNotProvided", + ErrorCategory.InvalidOperation, this)); } - ThrowTerminatingError( - new ErrorRecord( - new PSInvalidOperationException("Name parameter must be provided."), - "NameParameterNotProvided", - ErrorCategory.InvalidOperation, - this)); + Name = new string[] {"*"}; } - - var namesToSearch = Utils.ProcessNameWildcards(Name, out string[] errorMsgs, out bool nameContainsWildcard); - - foreach (string error in errorMsgs) + else { - WriteError(new ErrorRecord( - new PSInvalidOperationException(error), - "ErrorFilteringNamesForUnsupportedWildcards", - ErrorCategory.InvalidArgument, - this)); - } + var namesToSearch = Utils.ProcessNameWildcards(Name, out string[] errorMsgs, out bool nameContainsWildcard); + + foreach (string error in errorMsgs) + { + WriteError(new ErrorRecord( + new PSInvalidOperationException(error), + "ErrorFilteringNamesForUnsupportedWildcards", + ErrorCategory.InvalidArgument, + this)); + } - // this catches the case where Name wasn't passed in as null or empty, - // but after filtering out unsupported wildcard names there are no elements left in namesToSearch - if (namesToSearch.Length == 0) - { - return; - } + // this catches the case where Name wasn't passed in as null or empty, + // but after filtering out unsupported wildcard names there are no elements left in namesToSearch + if (namesToSearch.Length == 0) + { + return; + } - if (String.Equals(namesToSearch[0], "*", StringComparison.InvariantCultureIgnoreCase)) - { - // WriteVerbose("Package names were detected to be (or contain an element equal to): '*', so all packages will be updated"); - WriteError(new ErrorRecord( - new PSInvalidOperationException("-Name '*' is not supported for Find-PSResource so all Name entries will be discarded."), - "NameEqualsWildcardIsNotSupported", - ErrorCategory.InvalidArgument, - this)); - return; + + // if (String.Equals(namesToSearch[0], "*", StringComparison.InvariantCultureIgnoreCase) && !MyInvocation.BoundParameters.ContainsKey(nameof(Type)) && !MyInvocation.BoundParameters.ContainsKey(nameof(Tag))) + if (String.Equals(namesToSearch[0], "*", StringComparison.InvariantCultureIgnoreCase)) + { + WriteError(new ErrorRecord( + new PSInvalidOperationException("-Name '*' is not supported for Find-PSResource so all Name entries will be discarded."), + "NameEqualsWildcardIsNotSupported", + ErrorCategory.InvalidArgument, + this)); + return; + } } FindHelper findHelper = new FindHelper(_cancellationToken, this); From ca9337d482f7036c2797e94faef56e9ba01356b1 Mon Sep 17 00:00:00 2001 From: Anam Navied Date: Wed, 25 Aug 2021 14:07:05 -0400 Subject: [PATCH 02/12] support -Name *, Type parameter set, and Tag parameter set for Find --- src/code/FindPSResource.cs | 46 ++++++++------------- test/FindPSResource.Tests.ps1 | 76 ++++++++++++++++++++++++++++++++--- 2 files changed, 86 insertions(+), 36 deletions(-) diff --git a/src/code/FindPSResource.cs b/src/code/FindPSResource.cs index e5cc22dd6..cead1265a 100644 --- a/src/code/FindPSResource.cs +++ b/src/code/FindPSResource.cs @@ -197,39 +197,25 @@ private void ProcessResourceNameParameterSet() Name = new string[] {"*"}; } - else - { - var namesToSearch = Utils.ProcessNameWildcards(Name, out string[] errorMsgs, out bool nameContainsWildcard); - - foreach (string error in errorMsgs) - { - WriteError(new ErrorRecord( - new PSInvalidOperationException(error), - "ErrorFilteringNamesForUnsupportedWildcards", - ErrorCategory.InvalidArgument, - this)); - } - - // this catches the case where Name wasn't passed in as null or empty, - // but after filtering out unsupported wildcard names there are no elements left in namesToSearch - if (namesToSearch.Length == 0) - { - return; - } - - // if (String.Equals(namesToSearch[0], "*", StringComparison.InvariantCultureIgnoreCase) && !MyInvocation.BoundParameters.ContainsKey(nameof(Type)) && !MyInvocation.BoundParameters.ContainsKey(nameof(Tag))) - if (String.Equals(namesToSearch[0], "*", StringComparison.InvariantCultureIgnoreCase)) - { - WriteError(new ErrorRecord( - new PSInvalidOperationException("-Name '*' is not supported for Find-PSResource so all Name entries will be discarded."), - "NameEqualsWildcardIsNotSupported", - ErrorCategory.InvalidArgument, - this)); - return; - } + var namesToSearch = Utils.ProcessNameWildcards(Name, out string[] errorMsgs, out bool nameContainsWildcard); + + foreach (string error in errorMsgs) + { + WriteError(new ErrorRecord( + new PSInvalidOperationException(error), + "ErrorFilteringNamesForUnsupportedWildcards", + ErrorCategory.InvalidArgument, + this)); } + // this catches the case where Name wasn't passed in as null or empty, + // but after filtering out unsupported wildcard names there are no elements left in namesToSearch + if (namesToSearch.Length == 0) + { + return; + } + FindHelper findHelper = new FindHelper(_cancellationToken, this); List foundPackages = new List(); diff --git a/test/FindPSResource.Tests.ps1 b/test/FindPSResource.Tests.ps1 index 71805f03c..ca036b084 100644 --- a/test/FindPSResource.Tests.ps1 +++ b/test/FindPSResource.Tests.ps1 @@ -38,7 +38,8 @@ Describe 'Test Find-PSResource for Module' { $res = Find-PSResource -Name "AzureS*" -Repository $PSGalleryName $res.Count | Should -BeGreaterThan 1 # should find Module and Script resources - foreach ($item in $res) { + foreach ($item in $res) + { if ($item.Type -eq "Script") { $foundScript = $true @@ -48,10 +49,50 @@ Describe 'Test Find-PSResource for Module' { $foundScript | Should -BeTrue } - It "should not find resources given Name that equals wildcard, '*'" { - Find-PSResource -Name "*" -ErrorVariable err -ErrorAction SilentlyContinue - $err.Count | Should -Not -Be 0 - $err[0].FullyQualifiedErrorId | Should -BeExactly "NameEqualsWildcardIsNotSupported,Microsoft.PowerShell.PowerShellGet.Cmdlets.FindPSResource" + It "should find all resources given Name that equals wildcard, '*'" { + $foundScript = $False + $foundPreview = $False + $res = Find-PSResource -Name "*" -Repository $PSGalleryName + $res.Count | Should -BeGreaterThan 1 + #should find Module and Script resources + foreach ($item in $res) + { + if ($item.Type -eq "Script") + { + $foundScript = $True + } + + if(-not [string]::IsNullOrEmpty($item.PrereleaseLabel)) + { + $foundPreview = $True + } + } + + $foundScript | Should -Be $True + $foundPreview | Should -Be $False + } + + It "should find all resources (including prerelease) given Name that equals wildcard, '*' and Prerelease parameter" { + $foundScript = $False + $foundPreview = $False + $res = Find-PSResource -Name "*" -Prerelease -Repository $PSGalleryName + $res.Count | Should -BeGreaterThan 1 + #should find Module and Script resources + foreach ($item in $res) + { + if ($item.Type -eq "Script") + { + $foundScript = $True + } + + if(-not [string]::IsNullOrEmpty($item.PrereleaseLabel)) + { + $foundPreview = $True + } + } + + $foundScript | Should -Be $True + $foundPreview | Should -Be $True } It "find resource given Name from V3 endpoint repository (NuGetGallery)" { @@ -191,7 +232,21 @@ Describe 'Test Find-PSResource for Module' { } } - It "find resuources given Tag parameter" { + It "find all resources of Type Module when Type parameter set is used" { + $foundScript = $False + $res = Find-PSResource -Type Module -Repository $PSGalleryName + $res.Count | Should -BeGreaterThan 1 + foreach ($item in $res) { + if ($item.Type -eq "Script") + { + $foundScript = $True + } + } + + $foundScript | Should -Be $False + } + + It "find resources given Tag parameter" { $resWithEitherExpectedTag = @("NetworkingDsc", "DSCR_FileContent", "SS.PowerShell") $res = Find-PSResource -Name "NetworkingDsc", "HPCMSL", "DSCR_FileContent", "SS.PowerShell", "PowerShellGet" -Tag "Dsc", "json" -Repository $PSGalleryName foreach ($item in $res) { @@ -199,6 +254,15 @@ Describe 'Test Find-PSResource for Module' { } } + It "find all resources with specified tag given Tag property" { + $tagToFind = "Dell" + $res = Find-PSResource -Tag $tagToFind -Repository $PSGalleryName + $res.Count | Should -BeGreaterOrEqual 14 + foreach ($item in $res) { + $item.Tags -contains $tagToFind | Should -Be $True + } + } + It "find resource with IncludeDependencies parameter" { $res = Find-PSResource -Name "Az.Compute" -IncludeDependencies -Repository $PSGalleryName $isDependencyNamePresent = $False From ec1b430fc638709b0bbbfabc4e6a75c655c194b5 Mon Sep 17 00:00:00 2001 From: Anam Navied Date: Fri, 27 Aug 2021 00:10:15 -0400 Subject: [PATCH 03/12] fix bug by not deleting name with wildcard from packagesToFind list so all repos can be searched --- src/code/FindHelper.cs | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/src/code/FindHelper.cs b/src/code/FindHelper.cs index 34d79f289..efeb90648 100644 --- a/src/code/FindHelper.cs +++ b/src/code/FindHelper.cs @@ -369,18 +369,8 @@ private IEnumerable FindFromPackageSourceSearchAPI( foundPackagesMetadata.AddRange(wildcardPkgs.Where( p => nameWildcardPattern.IsMatch(p.Identity.Id)).ToList()); - // if the Script Uri endpoint still needs to be searched, don't remove the wildcard name from _pkgsLeftToFind - // PSGallery + Type == null -> M, S - // PSGallery + Type == M -> M - // PSGallery + Type == S -> S (but PSGallery would be skipped early on, only PSGalleryScripts would be checked) - // PSGallery + Type == C -> M - // PSGallery + Type == D -> M - - bool needToCheckPSGalleryScriptsRepo = String.Equals(repositoryName, _psGalleryRepoName, StringComparison.InvariantCultureIgnoreCase) && _type == ResourceType.None; - if (foundPackagesMetadata.Any() && !needToCheckPSGalleryScriptsRepo) - { - _pkgsLeftToFind.Remove(pkgName); - } + // We don't remove the pkgName from _pkgsLeftToFind list because there may be matches for that name wildcard pattern + // in each repository, so search each specified repository. } if (foundPackagesMetadata.Count == 0) From d668565aa2ca28ae1e8fc6f9303e27bfe5223a24 Mon Sep 17 00:00:00 2001 From: Anam Navied Date: Mon, 30 Aug 2021 11:56:21 -0400 Subject: [PATCH 04/12] add support for repository taking wildcards --- src/code/FindHelper.cs | 45 +++++++++++++++++++++++++++++++++++++----- 1 file changed, 40 insertions(+), 5 deletions(-) diff --git a/src/code/FindHelper.cs b/src/code/FindHelper.cs index efeb90648..078d4933a 100644 --- a/src/code/FindHelper.cs +++ b/src/code/FindHelper.cs @@ -38,6 +38,7 @@ internal class FindHelper private readonly string _psGalleryRepoName = "PSGallery"; private readonly string _psGalleryScriptsRepoName = "PSGalleryScripts"; private bool _isADOFeedRepository; + private bool _repositoryNameContainsWildcard; // NuGet's SearchAsync() API takes a top parameter of 6000, but testing shows for PSGallery // usually a max of around 5990 is returned while more are left to retrieve in a second SearchAsync() call @@ -74,10 +75,23 @@ public IEnumerable FindByResourceName( List repositoriesToSearch; + //determine if repository array of names of repositories input to be searched contains wildcard + if (repository != null) + { + repository = Utils.ProcessNameWildcards(repository, out string[] errorMsgs, out _repositoryNameContainsWildcard); + foreach (string error in errorMsgs) + { + _cmdletPassedIn.WriteError(new ErrorRecord( + new PSInvalidOperationException(error), + "ErrorFilteringNamesForUnsupportedWildcards", + ErrorCategory.InvalidArgument, + this)); + } + } + try { repositoriesToSearch = RepositorySettings.Read(repository, out string[] errorList); - foreach (string error in errorList) { _cmdletPassedIn.WriteError(new ErrorRecord( @@ -309,7 +323,13 @@ private IEnumerable FindFromPackageSourceSearchAPI( } foundPackagesMetadata.AddRange(retrievedPkgs.ToList()); - _pkgsLeftToFind.Remove(pkgName); + + // _pkgsLeftToFind.Remove(pkgName); + + if (!_repositoryNameContainsWildcard) + { + _pkgsLeftToFind.Remove(pkgName); + } } else { @@ -326,7 +346,6 @@ private IEnumerable FindFromPackageSourceSearchAPI( IEnumerable wildcardPkgs = null; try { - _cmdletPassedIn.WriteVerbose("searching with name: " + pkgName); // SearchAsync() API returns the latest version only for all packages that match the wild-card name wildcardPkgs = pkgSearchResource.SearchAsync( searchTerm: pkgName, @@ -369,8 +388,24 @@ private IEnumerable FindFromPackageSourceSearchAPI( foundPackagesMetadata.AddRange(wildcardPkgs.Where( p => nameWildcardPattern.IsMatch(p.Identity.Id)).ToList()); - // We don't remove the pkgName from _pkgsLeftToFind list because there may be matches for that name wildcard pattern - // in each repository, so search each specified repository. + if (!_repositoryNameContainsWildcard) + { + // if the Script Uri endpoint still needs to be searched, don't remove the wildcard name from _pkgsLeftToFind + // PSGallery + Type == null -> M, S + // PSGallery + Type == M -> M + // PSGallery + Type == S -> S (but PSGallery would be skipped early on, only PSGalleryScripts would be checked) + // PSGallery + Type == C -> M + // PSGallery + Type == D -> M + + bool needToCheckPSGalleryScriptsRepo = String.Equals(repositoryName, _psGalleryRepoName, StringComparison.InvariantCultureIgnoreCase) && _type == ResourceType.None; + if (foundPackagesMetadata.Any() && !needToCheckPSGalleryScriptsRepo) + { + _pkgsLeftToFind.Remove(pkgName); + } + } + + // if repository names did contain wildcard, we want to do an exhaustive search across all the repositories + // which matched the input repository name search term. } if (foundPackagesMetadata.Count == 0) From 28e34ebc65568c5fc225e83e0bc7a4486b1c0d5c Mon Sep 17 00:00:00 2001 From: Anam Navied Date: Mon, 30 Aug 2021 12:00:42 -0400 Subject: [PATCH 05/12] update help doc --- help/Find-PSResource.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/help/Find-PSResource.md b/help/Find-PSResource.md index cac8dfa5b..f34df535f 100644 --- a/help/Find-PSResource.md +++ b/help/Find-PSResource.md @@ -159,7 +159,7 @@ Accept wildcard characters: False ``` ### -Repository -Specifies one or more repository names to search. +Specifies one or more repository names to search, which can include wildcard. If not specified, search will include all currently registered repositories, in order of highest priority, until a repository is found that contains the package. ```yaml @@ -171,7 +171,7 @@ Required: False Position: Named Default value: None Accept pipeline input: False -Accept wildcard characters: False +Accept wildcard characters: True ``` ### -Tag From a49c1ac202d5accd9c59e42d29cf01475723bd14 Mon Sep 17 00:00:00 2001 From: Anam Navied Date: Mon, 30 Aug 2021 12:12:12 -0400 Subject: [PATCH 06/12] display Repository parameter for PSResourceInfo objects --- src/PSGet.Format.ps1xml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/PSGet.Format.ps1xml b/src/PSGet.Format.ps1xml index 80decb5de..79d680b96 100644 --- a/src/PSGet.Format.ps1xml +++ b/src/PSGet.Format.ps1xml @@ -11,6 +11,7 @@ + @@ -19,6 +20,7 @@ Name Version PrereleaseLabel + Repository Description From 9fb72763e83e2f82892db07958690628a69cadc4 Mon Sep 17 00:00:00 2001 From: Anam Navied Date: Mon, 30 Aug 2021 12:27:30 -0400 Subject: [PATCH 07/12] make resources returned unique by Name,Version,Repository --- src/code/FindPSResource.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/code/FindPSResource.cs b/src/code/FindPSResource.cs index cead1265a..2310fbbc8 100644 --- a/src/code/FindPSResource.cs +++ b/src/code/FindPSResource.cs @@ -233,7 +233,7 @@ private void ProcessResourceNameParameterSet() } foreach (var uniquePackageVersion in foundPackages.GroupBy( - m => new {m.Name, m.Version}).Select( + m => new {m.Name, m.Version, m.Repository}).Select( group => group.First()).ToList()) { WriteObject(uniquePackageVersion); From 9a8e98c9c83e0e91027275a873431a4d9b6f2026 Mon Sep 17 00:00:00 2001 From: Anam Navied Date: Mon, 30 Aug 2021 14:13:03 -0400 Subject: [PATCH 08/12] add support for getting Script resources from PoshTestGallery and update tests to use poshtestgallery --- src/code/FindHelper.cs | 41 ++++++++++++++++++++++++++++++----- test/FindPSResource.Tests.ps1 | 8 +++---- 2 files changed, 39 insertions(+), 10 deletions(-) diff --git a/src/code/FindHelper.cs b/src/code/FindHelper.cs index 078d4933a..5d08e3366 100644 --- a/src/code/FindHelper.cs +++ b/src/code/FindHelper.cs @@ -37,6 +37,9 @@ internal class FindHelper private SwitchParameter _includeDependencies = false; private readonly string _psGalleryRepoName = "PSGallery"; private readonly string _psGalleryScriptsRepoName = "PSGalleryScripts"; + private readonly string _poshTestGalleryRepoName = "PoshTestGallery"; + private readonly string _poshTestGalleryScriptsRepoName = "PoshTestGalleryScripts"; + private readonly string _poshTestGalleryURL = "https://www.poshtestgallery.com/api/v2"; private bool _isADOFeedRepository; private bool _repositoryNameContainsWildcard; @@ -114,6 +117,7 @@ public IEnumerable FindByResourceName( // loop through repositoriesToSearch and if PSGallery add it to list with same priority as PSGallery repo for (int i = 0; i < repositoriesToSearch.Count; i++) { + _cmdletPassedIn.WriteVerbose("uri absolute uri: " + repositoriesToSearch[i].Url.AbsoluteUri); if (String.Equals(repositoriesToSearch[i].Name, _psGalleryRepoName, StringComparison.InvariantCultureIgnoreCase)) { // for PowerShellGallery, Module and Script resources have different endpoints so separate repositories have to be registered @@ -129,11 +133,32 @@ public IEnumerable FindByResourceName( } else if (_type != ResourceType.None && _type == ResourceType.Script) { - _cmdletPassedIn.WriteVerbose("Type Script provided, so add PSGalleryScripts and remove PSGallery (Modules only)"); + _cmdletPassedIn.WriteVerbose("Type Script provided, so add PSGalleryScripts and remove PSGallery (Modules only) from search consideration"); repositoriesToSearch.Insert(i + 1, psGalleryScripts); repositoriesToSearch.RemoveAt(i); // remove PSGallery } + } + else if (String.Equals(repositoriesToSearch[i].Url.AbsoluteUri, _poshTestGalleryURL, StringComparison.InvariantCultureIgnoreCase)) + { + // for PoshTestGallery, Module and Script resources have different endpoints so separate repositories have to be registered + // with those endpoints in order for the NuGet APIs to search across both in the case where name includes '*' + + // detect if Script repository needs to be added and/or Module repository needs to be skipped + Uri poshTestGalleryScriptsUrl = new Uri("https://www.poshtestgallery.com/api/v2/items/psscript/"); + PSRepositoryInfo poshTestGalleryScripts = new PSRepositoryInfo(_poshTestGalleryScriptsRepoName, poshTestGalleryScriptsUrl, repositoriesToSearch[i].Priority, false); + if (_type == ResourceType.None) + { + _cmdletPassedIn.WriteVerbose("Null Type provided, so add PoshTestGalleryScripts repository"); + repositoriesToSearch.Insert(i + 1, poshTestGalleryScripts); + } + else if (_type != ResourceType.None && _type == ResourceType.Script) + { + _cmdletPassedIn.WriteVerbose("Type Script provided, so add PoshTestGalleryScripts and remove PoshTestGallery (Modules only) from search consideration"); + repositoriesToSearch.Insert(i + 1, poshTestGalleryScripts); + repositoriesToSearch.RemoveAt(i); // remove PSGallery + } } + } for (int i = 0; i < repositoriesToSearch.Count && _pkgsLeftToFind.Any(); i++) @@ -217,7 +242,7 @@ public IEnumerable SearchFromRepository( context = new SourceCacheContext(); foreach(PSResourceInfo pkg in SearchAcrossNamesInRepository( - repositoryName: repositoryName, + repositoryName: String.Equals(repositoryUrl.AbsoluteUri, _poshTestGalleryURL, StringComparison.InvariantCultureIgnoreCase) ? _poshTestGalleryRepoName : repositoryName, pkgSearchResource: resourceSearch, pkgMetadataResource: resourceMetadata, searchFilter: filter, @@ -397,11 +422,15 @@ private IEnumerable FindFromPackageSourceSearchAPI( // PSGallery + Type == C -> M // PSGallery + Type == D -> M - bool needToCheckPSGalleryScriptsRepo = String.Equals(repositoryName, _psGalleryRepoName, StringComparison.InvariantCultureIgnoreCase) && _type == ResourceType.None; - if (foundPackagesMetadata.Any() && !needToCheckPSGalleryScriptsRepo) + if (String.Equals(repositoryName, _psGalleryRepoName, StringComparison.InvariantCultureIgnoreCase) || + String.Equals(repositoryName, _poshTestGalleryRepoName, StringComparison.InvariantCultureIgnoreCase)) { - _pkgsLeftToFind.Remove(pkgName); - } + if (foundPackagesMetadata.Any() && _type != ResourceType.None) + { + _pkgsLeftToFind.Remove(pkgName); + } + } + } // if repository names did contain wildcard, we want to do an exhaustive search across all the repositories diff --git a/test/FindPSResource.Tests.ps1 b/test/FindPSResource.Tests.ps1 index ca036b084..5b5edd606 100644 --- a/test/FindPSResource.Tests.ps1 +++ b/test/FindPSResource.Tests.ps1 @@ -52,8 +52,8 @@ Describe 'Test Find-PSResource for Module' { It "should find all resources given Name that equals wildcard, '*'" { $foundScript = $False $foundPreview = $False - $res = Find-PSResource -Name "*" -Repository $PSGalleryName - $res.Count | Should -BeGreaterThan 1 + $res = Find-PSResource -Name "*" -Repository $TestGalleryName + $res.Count | Should -BeGreaterOrEqual 2004 #should find Module and Script resources foreach ($item in $res) { @@ -75,8 +75,8 @@ Describe 'Test Find-PSResource for Module' { It "should find all resources (including prerelease) given Name that equals wildcard, '*' and Prerelease parameter" { $foundScript = $False $foundPreview = $False - $res = Find-PSResource -Name "*" -Prerelease -Repository $PSGalleryName - $res.Count | Should -BeGreaterThan 1 + $res = Find-PSResource -Name "*" -Prerelease -Repository $TestGalleryName + $res.Count | Should -BeGreaterOrEqual 2781 #should find Module and Script resources foreach ($item in $res) { From 28ae4dbfe2e9a2702ef420c79788b1057c035547 Mon Sep 17 00:00:00 2001 From: Anam Navied Date: Mon, 30 Aug 2021 14:32:19 -0400 Subject: [PATCH 09/12] tests checking for package count should test against PoshTestGallery packages --- test/FindPSResource.Tests.ps1 | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/FindPSResource.Tests.ps1 b/test/FindPSResource.Tests.ps1 index 5b5edd606..e6f38c516 100644 --- a/test/FindPSResource.Tests.ps1 +++ b/test/FindPSResource.Tests.ps1 @@ -255,9 +255,9 @@ Describe 'Test Find-PSResource for Module' { } It "find all resources with specified tag given Tag property" { - $tagToFind = "Dell" - $res = Find-PSResource -Tag $tagToFind -Repository $PSGalleryName - $res.Count | Should -BeGreaterOrEqual 14 + $tagToFind = "Tag1" + $res = Find-PSResource -Tag $tagToFind -Repository $TestGalleryName + $res.Count | Should -BeGreaterOrEqual 43 foreach ($item in $res) { $item.Tags -contains $tagToFind | Should -Be $True } From fa70ec062c9ed4156b83da3dcb334ee48ebe9a70 Mon Sep 17 00:00:00 2001 From: Anam Navied Date: Wed, 1 Sep 2021 01:10:58 -0400 Subject: [PATCH 10/12] special case repositories by URL not name --- src/code/FindHelper.cs | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/code/FindHelper.cs b/src/code/FindHelper.cs index 5d08e3366..8ed8db1cc 100644 --- a/src/code/FindHelper.cs +++ b/src/code/FindHelper.cs @@ -37,6 +37,7 @@ internal class FindHelper private SwitchParameter _includeDependencies = false; private readonly string _psGalleryRepoName = "PSGallery"; private readonly string _psGalleryScriptsRepoName = "PSGalleryScripts"; + private readonly string _psGalleryURL = "https://www.powershellgallery.com/api/v2"; private readonly string _poshTestGalleryRepoName = "PoshTestGallery"; private readonly string _poshTestGalleryScriptsRepoName = "PoshTestGalleryScripts"; private readonly string _poshTestGalleryURL = "https://www.poshtestgallery.com/api/v2"; @@ -114,13 +115,14 @@ public IEnumerable FindByResourceName( yield break; } - // loop through repositoriesToSearch and if PSGallery add it to list with same priority as PSGallery repo + // loop through repositoriesToSearch and if PSGallery or PoshTestGallery add its Scripts endpoint repo + // to list with same priority as PSGallery repo + // This special casing is done to handle PSGallery and PoshTestGallery having 2 endpoints currently for different resources. for (int i = 0; i < repositoriesToSearch.Count; i++) { - _cmdletPassedIn.WriteVerbose("uri absolute uri: " + repositoriesToSearch[i].Url.AbsoluteUri); - if (String.Equals(repositoriesToSearch[i].Name, _psGalleryRepoName, StringComparison.InvariantCultureIgnoreCase)) + if (String.Equals(repositoriesToSearch[i].Url.AbsoluteUri, _psGalleryURL, StringComparison.InvariantCultureIgnoreCase)) { - // for PowerShellGallery, Module and Script resources have different endpoints so separate repositories have to be registered + // special case: for PowerShellGallery, Module and Script resources have different endpoints so separate repositories have to be registered // with those endpoints in order for the NuGet APIs to search across both in the case where name includes '*' // detect if Script repository needs to be added and/or Module repository needs to be skipped @@ -140,7 +142,7 @@ public IEnumerable FindByResourceName( } else if (String.Equals(repositoriesToSearch[i].Url.AbsoluteUri, _poshTestGalleryURL, StringComparison.InvariantCultureIgnoreCase)) { - // for PoshTestGallery, Module and Script resources have different endpoints so separate repositories have to be registered + // special case: for PoshTestGallery, Module and Script resources have different endpoints so separate repositories have to be registered // with those endpoints in order for the NuGet APIs to search across both in the case where name includes '*' // detect if Script repository needs to be added and/or Module repository needs to be skipped @@ -155,7 +157,7 @@ public IEnumerable FindByResourceName( { _cmdletPassedIn.WriteVerbose("Type Script provided, so add PoshTestGalleryScripts and remove PoshTestGallery (Modules only) from search consideration"); repositoriesToSearch.Insert(i + 1, poshTestGalleryScripts); - repositoriesToSearch.RemoveAt(i); // remove PSGallery + repositoriesToSearch.RemoveAt(i); // remove PoshTestGallery } } @@ -242,7 +244,8 @@ public IEnumerable SearchFromRepository( context = new SourceCacheContext(); foreach(PSResourceInfo pkg in SearchAcrossNamesInRepository( - repositoryName: String.Equals(repositoryUrl.AbsoluteUri, _poshTestGalleryURL, StringComparison.InvariantCultureIgnoreCase) ? _poshTestGalleryRepoName : repositoryName, + repositoryName: String.Equals(repositoryUrl.AbsoluteUri, _psGalleryURL, StringComparison.InvariantCultureIgnoreCase) ? _psGalleryRepoName : + (String.Equals(repositoryUrl.AbsoluteUri, _poshTestGalleryURL, StringComparison.InvariantCultureIgnoreCase) ? _poshTestGalleryRepoName : repositoryName), pkgSearchResource: resourceSearch, pkgMetadataResource: resourceMetadata, searchFilter: filter, From 29de00a75cf33d02adcd9bcfafe7996a30ee6fc0 Mon Sep 17 00:00:00 2001 From: Anam Navied Date: Thu, 9 Sep 2021 12:36:15 -0400 Subject: [PATCH 11/12] fix bug in Find so that if valid names are present with invalid ones valid are still considered --- src/code/FindPSResource.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/code/FindPSResource.cs b/src/code/FindPSResource.cs index 2310fbbc8..f6c7a39a7 100644 --- a/src/code/FindPSResource.cs +++ b/src/code/FindPSResource.cs @@ -198,7 +198,7 @@ private void ProcessResourceNameParameterSet() Name = new string[] {"*"}; } - var namesToSearch = Utils.ProcessNameWildcards(Name, out string[] errorMsgs, out bool nameContainsWildcard); + Name = Utils.ProcessNameWildcards(Name, out string[] errorMsgs, out bool nameContainsWildcard); foreach (string error in errorMsgs) { @@ -211,7 +211,7 @@ private void ProcessResourceNameParameterSet() // this catches the case where Name wasn't passed in as null or empty, // but after filtering out unsupported wildcard names there are no elements left in namesToSearch - if (namesToSearch.Length == 0) + if (Name.Length == 0) { return; } From e2f8d38491acd4c3784f58aa93d98cc00493f75c Mon Sep 17 00:00:00 2001 From: Anam Navied Date: Mon, 20 Sep 2021 21:56:26 -0400 Subject: [PATCH 12/12] refactor tests to test for specific module and script resources --- test/FindPSResource.Tests.ps1 | 50 +++++++++++++++++++++++++++-------- 1 file changed, 39 insertions(+), 11 deletions(-) diff --git a/test/FindPSResource.Tests.ps1 b/test/FindPSResource.Tests.ps1 index e6f38c516..d1b9a3820 100644 --- a/test/FindPSResource.Tests.ps1 +++ b/test/FindPSResource.Tests.ps1 @@ -9,6 +9,8 @@ Describe 'Test Find-PSResource for Module' { $TestGalleryName = Get-PoshTestGalleryName $PSGalleryName = Get-PSGalleryName $NuGetGalleryName = Get-NuGetGalleryName + $testModuleName = "testmodule" + $testScriptName = "test_script" Get-NewPSResourceRepositoryFile Register-LocalRepos } @@ -50,16 +52,21 @@ Describe 'Test Find-PSResource for Module' { } It "should find all resources given Name that equals wildcard, '*'" { - $foundScript = $False $foundPreview = $False + $foundTestScript = $False + $foundTestModule = $False $res = Find-PSResource -Name "*" -Repository $TestGalleryName - $res.Count | Should -BeGreaterOrEqual 2004 #should find Module and Script resources foreach ($item in $res) { - if ($item.Type -eq "Script") + if ($item.Name -eq $testModuleName) { - $foundScript = $True + $foundTestModule = $True + } + + if ($item.Name -eq $testScriptName) + { + $foundTestScript = $True } if(-not [string]::IsNullOrEmpty($item.PrereleaseLabel)) @@ -68,21 +75,27 @@ Describe 'Test Find-PSResource for Module' { } } - $foundScript | Should -Be $True $foundPreview | Should -Be $False + $foundTestScript | Should -Be $True + $foundTestModule | Should -Be $True } It "should find all resources (including prerelease) given Name that equals wildcard, '*' and Prerelease parameter" { - $foundScript = $False $foundPreview = $False + $foundTestScript = $False + $foundTestModule = $False $res = Find-PSResource -Name "*" -Prerelease -Repository $TestGalleryName - $res.Count | Should -BeGreaterOrEqual 2781 #should find Module and Script resources foreach ($item in $res) { - if ($item.Type -eq "Script") + if ($item.Name -eq $testModuleName) { - $foundScript = $True + $foundTestModule = $True + } + + if ($item.Name -eq $testScriptName) + { + $foundTestScript = $True } if(-not [string]::IsNullOrEmpty($item.PrereleaseLabel)) @@ -91,8 +104,9 @@ Describe 'Test Find-PSResource for Module' { } } - $foundScript | Should -Be $True $foundPreview | Should -Be $True + $foundTestScript | Should -Be $True + $foundTestModule | Should -Be $True } It "find resource given Name from V3 endpoint repository (NuGetGallery)" { @@ -255,12 +269,26 @@ Describe 'Test Find-PSResource for Module' { } It "find all resources with specified tag given Tag property" { + $foundTestModule = $False + $foundTestScript = $False $tagToFind = "Tag1" $res = Find-PSResource -Tag $tagToFind -Repository $TestGalleryName - $res.Count | Should -BeGreaterOrEqual 43 foreach ($item in $res) { $item.Tags -contains $tagToFind | Should -Be $True + + if ($item.Name -eq $testModuleName) + { + $foundTestModule = $True + } + + if ($item.Name -eq $testScriptName) + { + $foundTestScript = $True + } } + + $foundTestModule | Should -Be $True + $foundTestScript | Should -Be $True } It "find resource with IncludeDependencies parameter" {