From 8f2f1455df6913da8fde51a8e6b839279217f8c6 Mon Sep 17 00:00:00 2001 From: Aditya Patwardhan Date: Fri, 13 Jun 2025 11:36:29 -0700 Subject: [PATCH 01/26] Add debug statements --- src/code/RegisterPSResourceRepository.cs | 31 ++++++++++++------- src/code/Utils.cs | 14 +++++---- ...SResourceContainerRegistryServer.Tests.ps1 | 2 ++ 3 files changed, 29 insertions(+), 18 deletions(-) diff --git a/src/code/RegisterPSResourceRepository.cs b/src/code/RegisterPSResourceRepository.cs index 1a86db210..c95660d7e 100644 --- a/src/code/RegisterPSResourceRepository.cs +++ b/src/code/RegisterPSResourceRepository.cs @@ -102,7 +102,7 @@ class RegisterPSResourceRepository : PSCmdlet /// [Parameter] public SwitchParameter PassThru { get; set; } - + /// /// When specified, will overwrite information for any existing repository with the same name. /// @@ -115,10 +115,14 @@ class RegisterPSResourceRepository : PSCmdlet protected override void BeginProcessing() { + WriteVerbose("In RegisterPSResourceRepository::BeginProcessing()"); RepositorySettings.CheckRepositoryStore(); + WriteVerbose("Done RegisterPSResourceRepository::BeginProcessing()"); + } protected override void ProcessRecord() { + WriteVerbose("In RegisterPSResourceRepository::ProcessRecord()"); List items = new List(); PSRepositoryInfo.APIVersion? repoApiVersion = null; @@ -130,6 +134,7 @@ protected override void ProcessRecord() switch (ParameterSetName) { case NameParameterSet: + WriteDebug("In RegisterPSResourceRepository::NameParameterSet"); if (!Utils.TryCreateValidUri(uriString: Uri, cmdletPassedIn: this, uriResult: out _uri, @@ -140,6 +145,7 @@ protected override void ProcessRecord() try { + WriteDebug($"Registering repository '{Name}' with uri '{_uri}'"); items.Add(RepositorySettings.AddRepository(Name, _uri, Priority, Trusted, repoApiVersion, CredentialInfo, Force, this, out string errorMsg)); if (!string.IsNullOrEmpty(errorMsg)) @@ -177,6 +183,7 @@ protected override void ProcessRecord() break; case RepositoriesParameterSet: + WriteDebug("In RegisterPSResourceRepository::RepositoriesParameterSet"); try { items = RepositoriesParameterSetHelper(); @@ -212,14 +219,14 @@ private PSRepositoryInfo PSGalleryParameterSetHelper(int repoPriority, bool repo WriteDebug("In RegisterPSResourceRepository::PSGalleryParameterSetHelper()"); Uri psGalleryUri = new Uri(PSGalleryRepoUri); WriteDebug("Internal name and uri values for PSGallery are hardcoded and validated. Priority and trusted values, if passed in, also validated"); - var addedRepo = RepositorySettings.AddToRepositoryStore(PSGalleryRepoName, - psGalleryUri, - repoPriority, - repoTrusted, + var addedRepo = RepositorySettings.AddToRepositoryStore(PSGalleryRepoName, + psGalleryUri, + repoPriority, + repoTrusted, apiVersion: null, - repoCredentialInfo: null, - Force, - this, + repoCredentialInfo: null, + Force, + this, out string errorMsg); if (!string.IsNullOrEmpty(errorMsg)) @@ -313,7 +320,7 @@ private PSRepositoryInfo RepoValidationHelper(Hashtable repo) "NullUriForRepositoriesParameterSetRegistration", ErrorCategory.InvalidArgument, this)); - + return null; } @@ -337,10 +344,10 @@ private PSRepositoryInfo RepoValidationHelper(Hashtable repo) return null; } - if (repo.ContainsKey("ApiVersion") && + if (repo.ContainsKey("ApiVersion") && (repo["ApiVersion"] == null || String.IsNullOrEmpty(repo["ApiVersion"].ToString()) || - !(repo["ApiVersion"].ToString().Equals("Local", StringComparison.OrdinalIgnoreCase) || repo["ApiVersion"].ToString().Equals("V2", StringComparison.OrdinalIgnoreCase) || - repo["ApiVersion"].ToString().Equals("V3", StringComparison.OrdinalIgnoreCase) || repo["ApiVersion"].ToString().Equals("NugetServer", StringComparison.OrdinalIgnoreCase) || + !(repo["ApiVersion"].ToString().Equals("Local", StringComparison.OrdinalIgnoreCase) || repo["ApiVersion"].ToString().Equals("V2", StringComparison.OrdinalIgnoreCase) || + repo["ApiVersion"].ToString().Equals("V3", StringComparison.OrdinalIgnoreCase) || repo["ApiVersion"].ToString().Equals("NugetServer", StringComparison.OrdinalIgnoreCase) || repo["ApiVersion"].ToString().Equals("Unknown", StringComparison.OrdinalIgnoreCase)))) { WriteError(new ErrorRecord( diff --git a/src/code/Utils.cs b/src/code/Utils.cs index 89de7cd10..c8641d8a2 100644 --- a/src/code/Utils.cs +++ b/src/code/Utils.cs @@ -429,6 +429,8 @@ public static bool TryCreateValidUri( out Uri uriResult, out ErrorRecord errorRecord) { + cmdletPassedIn.WriteVerbose($"Validating Uri: {uriString}"); + errorRecord = null; if (Uri.TryCreate(uriString, UriKind.Absolute, out uriResult)) { @@ -1874,9 +1876,9 @@ public static Hashtable GetMetadataFromNuspec(string nuspecFilePath, PSCmdlet cm catch (Exception e) { errorRecord = new ErrorRecord( - exception: e, - "GetHashtableForNuspecFailure", - ErrorCategory.ReadError, + exception: e, + "GetHashtableForNuspecFailure", + ErrorCategory.ReadError, cmdletPassedIn); } @@ -1895,9 +1897,9 @@ public static XmlDocument LoadXmlDocument(string filePath, PSCmdlet cmdletPassed catch (Exception e) { errRecord = new ErrorRecord( - exception: e, - "LoadXmlDocumentFailure", - ErrorCategory.ReadError, + exception: e, + "LoadXmlDocumentFailure", + ErrorCategory.ReadError, cmdletPassedIn); } diff --git a/test/FindPSResourceTests/FindPSResourceContainerRegistryServer.Tests.ps1 b/test/FindPSResourceTests/FindPSResourceContainerRegistryServer.Tests.ps1 index 5b2de751b..96ad0bec8 100644 --- a/test/FindPSResourceTests/FindPSResourceContainerRegistryServer.Tests.ps1 +++ b/test/FindPSResourceTests/FindPSResourceContainerRegistryServer.Tests.ps1 @@ -22,6 +22,8 @@ Describe 'Test HTTP Find-PSResource for ACR Server Protocol' -tags 'CI' { { Write-Verbose -Verbose "Using Az module for authentication" Register-PSResourceRepository -Name $ACRRepoName -ApiVersion 'ContainerRegistry' -Uri $ACRRepoUri -Verbose + Write-Verbose -Verbose "Registering ACR repository with Az authentication completed" + Get-PSResourceRepository } else { From fcbbc8cc4d0b8c0e39d714e77ac03bf5ec1bce64 Mon Sep 17 00:00:00 2001 From: Aditya Patwardhan Date: Fri, 13 Jun 2025 11:52:43 -0700 Subject: [PATCH 02/26] More debug --- src/code/GetPSResourceRepository.cs | 8 ++++++++ .../FindPSResourceContainerRegistryServer.Tests.ps1 | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/code/GetPSResourceRepository.cs b/src/code/GetPSResourceRepository.cs index d1ad451ce..cbb210367 100644 --- a/src/code/GetPSResourceRepository.cs +++ b/src/code/GetPSResourceRepository.cs @@ -37,15 +37,19 @@ public sealed class GetPSResourceRepository : PSCmdlet protected override void BeginProcessing() { + WriteVerbose("Beginning Get-PSResourceRepository processing"); RepositorySettings.CheckRepositoryStore(); + WriteVerbose("Repository store checked successfully"); } protected override void ProcessRecord() { + WriteVerbose("Processing Get-PSResourceRepository cmdlet"); string nameArrayAsString = (Name == null || !Name.Any() || string.Equals(Name[0], "*") || Name[0] == null) ? "all" : string.Join(", ", Name); WriteDebug($"Reading repository info for '{nameArrayAsString}'"); List items = RepositorySettings.Read(Name, out string[] errorList); + WriteVerbose($"Read {items.Count} repositories"); // handle non-terminating errors foreach (string error in errorList) @@ -57,10 +61,14 @@ protected override void ProcessRecord() this)); } + WriteVerbose($"Returning {items.Count} repositories"); + foreach (PSRepositoryInfo repo in items) { WriteObject(repo); } + + WriteVerbose("Get-PSResourceRepository cmdlet processing complete"); } #endregion diff --git a/test/FindPSResourceTests/FindPSResourceContainerRegistryServer.Tests.ps1 b/test/FindPSResourceTests/FindPSResourceContainerRegistryServer.Tests.ps1 index 96ad0bec8..96fead1f6 100644 --- a/test/FindPSResourceTests/FindPSResourceContainerRegistryServer.Tests.ps1 +++ b/test/FindPSResourceTests/FindPSResourceContainerRegistryServer.Tests.ps1 @@ -23,7 +23,7 @@ Describe 'Test HTTP Find-PSResource for ACR Server Protocol' -tags 'CI' { Write-Verbose -Verbose "Using Az module for authentication" Register-PSResourceRepository -Name $ACRRepoName -ApiVersion 'ContainerRegistry' -Uri $ACRRepoUri -Verbose Write-Verbose -Verbose "Registering ACR repository with Az authentication completed" - Get-PSResourceRepository + Get-PSResourceRepository -Name $ACRRepoName -Verbose } else { From 1571e5c600bc7dff63b15b2998fed3ca4b330808 Mon Sep 17 00:00:00 2001 From: Aditya Patwardhan Date: Fri, 13 Jun 2025 12:06:01 -0700 Subject: [PATCH 03/26] Add more logging --- src/code/FindPSResource.cs | 19 ++++++++++++------- ...SResourceContainerRegistryServer.Tests.ps1 | 5 ++++- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/src/code/FindPSResource.cs b/src/code/FindPSResource.cs index d2a78b1a6..2d781ce5e 100644 --- a/src/code/FindPSResource.cs +++ b/src/code/FindPSResource.cs @@ -24,7 +24,7 @@ namespace Microsoft.PowerShell.PSResourceGet.Cmdlets public sealed class FindPSResource : PSCmdlet { #region Members - + private const string NameParameterSet = "NameParameterSet"; private const string CommandNameParameterSet = "CommandNameParameterSet"; private const string DscResourceNameParameterSet = "DscResourceNameParameterSet"; @@ -39,7 +39,7 @@ public sealed class FindPSResource : PSCmdlet /// Specifies name of a resource or resources to find. Accepts wild card characters. /// [SupportsWildcards] - [Parameter(Position = 0, + [Parameter(Position = 0, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true, ParameterSetName = NameParameterSet)] @@ -115,15 +115,20 @@ public sealed class FindPSResource : PSCmdlet protected override void BeginProcessing() { + WriteVerbose("Beginning Find-PSResource processing"); _cancellationTokenSource = new CancellationTokenSource(); var networkCred = Credential != null ? new NetworkCredential(Credential.UserName, Credential.Password) : null; + WriteVerbose("Creating FindHelper instance"); + _findHelper = new FindHelper( cancellationToken: _cancellationTokenSource.Token, cmdletPassedIn: this, networkCredential: networkCred); + WriteVerbose("FindHelper instance created successfully"); + // Create a repository 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(); @@ -192,7 +197,7 @@ private void ProcessResourceNameParameterSet() WriteDebug("Filtering package name(s) on wildcards"); Name = Utils.ProcessNameWildcards(Name, removeWildcardEntries:false, out string[] errorMsgs, out bool nameContainsWildcard); - + foreach (string error in errorMsgs) { WriteError(new ErrorRecord( @@ -208,7 +213,7 @@ private void ProcessResourceNameParameterSet() { WriteDebug("Package name(s) could not be resolved"); return; - } + } // determine/parse out Version param VersionType versionType = VersionType.VersionRange; @@ -232,7 +237,7 @@ private void ProcessResourceNameParameterSet() "IncorrectVersionFormat", ErrorCategory.InvalidArgument, this)); - + return; } } @@ -289,7 +294,7 @@ private void ProcessCommandOrDscParameterSet(bool isSearchingForCommands) WriteDebug("Command or DSCResource name(s) could not be resolved"); return; } - + foreach (PSCommandResourceInfo cmdPkg in _findHelper.FindByCommandOrDscResource( isSearchingForCommands: isSearchingForCommands, prerelease: Prerelease, @@ -325,7 +330,7 @@ private void ProcessTags() WriteDebug("Tags(s) could not be resolved"); return; } - + foreach (PSResourceInfo tagPkg in _findHelper.FindByTag( type: Type, prerelease: Prerelease, diff --git a/test/FindPSResourceTests/FindPSResourceContainerRegistryServer.Tests.ps1 b/test/FindPSResourceTests/FindPSResourceContainerRegistryServer.Tests.ps1 index 96fead1f6..b8d943e40 100644 --- a/test/FindPSResourceTests/FindPSResourceContainerRegistryServer.Tests.ps1 +++ b/test/FindPSResourceTests/FindPSResourceContainerRegistryServer.Tests.ps1 @@ -24,6 +24,7 @@ Describe 'Test HTTP Find-PSResource for ACR Server Protocol' -tags 'CI' { Register-PSResourceRepository -Name $ACRRepoName -ApiVersion 'ContainerRegistry' -Uri $ACRRepoUri -Verbose Write-Verbose -Verbose "Registering ACR repository with Az authentication completed" Get-PSResourceRepository -Name $ACRRepoName -Verbose + Write-Verbose -Verbose "Get-PSResourceRepository completed" } else { @@ -38,7 +39,9 @@ Describe 'Test HTTP Find-PSResource for ACR Server Protocol' -tags 'CI' { It "Find resource given specific Name, Version null" { # FindName() - $res = Find-PSResource -Name $testModuleName -Repository $ACRRepoName + Write-Verbose -Verbose "Finding resource with Name: $testModuleName" + $res = Find-PSResource -Name $testModuleName -Repository $ACRRepoName -Verbose -Debug + Write-Verbose -Verbose "Find-PSResource completed" $res.Name | Should -Be $testModuleName $res.Version | Should -Be "5.0.0" } From 782364ee7d51ef812bef92de6cff1f866dbd51b8 Mon Sep 17 00:00:00 2001 From: Aditya Patwardhan Date: Fri, 13 Jun 2025 12:26:31 -0700 Subject: [PATCH 04/26] Add more logging 2 --- src/code/FindPSResource.cs | 160 ++++++++++-------- ...SResourceContainerRegistryServer.Tests.ps1 | 8 +- 2 files changed, 94 insertions(+), 74 deletions(-) diff --git a/src/code/FindPSResource.cs b/src/code/FindPSResource.cs index 2d781ce5e..9b03ff8d6 100644 --- a/src/code/FindPSResource.cs +++ b/src/code/FindPSResource.cs @@ -172,99 +172,113 @@ protected override void ProcessRecord() private void ProcessResourceNameParameterSet() { - WriteDebug("In FindPSResource::ProcessResourceNameParameterSet()"); - // only cases where Name is allowed to not be specified is if Type or Tag parameters are - if (!MyInvocation.BoundParameters.ContainsKey(nameof(Name))) + try { - if (MyInvocation.BoundParameters.ContainsKey(nameof(Tag))) + WriteDebug("In FindPSResource::ProcessResourceNameParameterSet()"); + // only cases where Name is allowed to not be specified is if Type or Tag parameters are + if (!MyInvocation.BoundParameters.ContainsKey(nameof(Name))) { - ProcessTags(); - return; - } - else if (MyInvocation.BoundParameters.ContainsKey(nameof(Type))) - { - Name = new string[] {"*"}; + WriteDebug("Name parameter not provided, checking for Type or Tag parameters"); + if (MyInvocation.BoundParameters.ContainsKey(nameof(Tag))) + { + ProcessTags(); + return; + } + else if (MyInvocation.BoundParameters.ContainsKey(nameof(Type))) + { + Name = new string[] { "*" }; + } + else + { + ThrowTerminatingError(new ErrorRecord( + new PSInvalidOperationException("Name parameter must be provided, unless Tag or Type parameters are used."), + "NameParameterNotProvided", + ErrorCategory.InvalidOperation, + this)); + } } - else + + WriteVerbose("Processing Name parameter for Find-PSResource cmdlet"); + WriteDebug("Filtering package name(s) on wildcards"); + Name = Utils.ProcessNameWildcards(Name, removeWildcardEntries: false, out string[] errorMsgs, out bool nameContainsWildcard); + WriteVerbose($"Name parameter processed, contains {Name.Length} entries"); + + foreach (string error in errorMsgs) { - ThrowTerminatingError(new ErrorRecord( - new PSInvalidOperationException("Name parameter must be provided, unless Tag or Type parameters are used."), - "NameParameterNotProvided", - ErrorCategory.InvalidOperation, + WriteError(new ErrorRecord( + new PSInvalidOperationException(error), + "ErrorFilteringNamesForUnsupportedWildcards", + ErrorCategory.InvalidArgument, this)); } - } - - WriteDebug("Filtering package name(s) on wildcards"); - Name = Utils.ProcessNameWildcards(Name, removeWildcardEntries:false, 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 (Name.Length == 0) - { - WriteDebug("Package name(s) could not be resolved"); - 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 (Name.Length == 0) + { + WriteDebug("Package name(s) could not be resolved"); + return; + } - // determine/parse out Version param - VersionType versionType = VersionType.VersionRange; - NuGetVersion nugetVersion = null; - VersionRange versionRange = null; + // determine/parse out Version param + VersionType versionType = VersionType.VersionRange; + NuGetVersion nugetVersion = null; + VersionRange versionRange = null; - if (Version != null) - { - WriteDebug("Parsing package version"); - if (!NuGetVersion.TryParse(Version, out nugetVersion)) + if (Version != null) { - if (Version.Trim().Equals("*")) + WriteDebug("Parsing package version"); + if (!NuGetVersion.TryParse(Version, out nugetVersion)) { - versionRange = VersionRange.All; - versionType = VersionType.VersionRange; + if (Version.Trim().Equals("*")) + { + versionRange = VersionRange.All; + versionType = VersionType.VersionRange; + } + else if (!VersionRange.TryParse(Version, out versionRange)) + { + WriteError(new ErrorRecord( + new ArgumentException("Argument for -Version parameter is not in the proper format"), + "IncorrectVersionFormat", + ErrorCategory.InvalidArgument, + this)); + + return; + } } - else if (!VersionRange.TryParse(Version, out versionRange)) + else { - WriteError(new ErrorRecord( - new ArgumentException("Argument for -Version parameter is not in the proper format"), - "IncorrectVersionFormat", - ErrorCategory.InvalidArgument, - this)); - - return; + versionType = VersionType.SpecificVersion; } } else { - versionType = VersionType.SpecificVersion; + versionType = VersionType.NoVersion; } - } - else - { - versionType = VersionType.NoVersion; - } - foreach (PSResourceInfo pkg in _findHelper.FindByResourceName( - name: Name, - type: Type, - versionRange: versionRange, - nugetVersion: nugetVersion, - versionType: versionType, - version: Version, - prerelease: Prerelease, - tag: Tag, - repository: Repository, - includeDependencies: IncludeDependencies, - suppressErrors: false)) + foreach (PSResourceInfo pkg in _findHelper.FindByResourceName( + name: Name, + type: Type, + versionRange: versionRange, + nugetVersion: nugetVersion, + versionType: versionType, + version: Version, + prerelease: Prerelease, + tag: Tag, + repository: Repository, + includeDependencies: IncludeDependencies, + suppressErrors: false)) + { + WriteObject(pkg); + } + } + catch (Exception ex) { - WriteObject(pkg); + WriteError(new ErrorRecord( + ex, + "FindPSResourceError", + ErrorCategory.NotSpecified, + this)); } } diff --git a/test/FindPSResourceTests/FindPSResourceContainerRegistryServer.Tests.ps1 b/test/FindPSResourceTests/FindPSResourceContainerRegistryServer.Tests.ps1 index b8d943e40..63b8ab4d6 100644 --- a/test/FindPSResourceTests/FindPSResourceContainerRegistryServer.Tests.ps1 +++ b/test/FindPSResourceTests/FindPSResourceContainerRegistryServer.Tests.ps1 @@ -40,7 +40,13 @@ Describe 'Test HTTP Find-PSResource for ACR Server Protocol' -tags 'CI' { It "Find resource given specific Name, Version null" { # FindName() Write-Verbose -Verbose "Finding resource with Name: $testModuleName" - $res = Find-PSResource -Name $testModuleName -Repository $ACRRepoName -Verbose -Debug + $res = Find-PSResource -Name $testModuleName -Repository $ACRRepoName -Verbose -Debug -ErrorAction Stop + + if ($Error.Count -gt 0) { + Write-Error "Error occurred while finding resource: $($Error[0])" + Get-Error + } + Write-Verbose -Verbose "Find-PSResource completed" $res.Name | Should -Be $testModuleName $res.Version | Should -Be "5.0.0" From 3a41d1315a760d3cbe72d6eff35ccfbf7ee8de6d Mon Sep 17 00:00:00 2001 From: Aditya Patwardhan Date: Fri, 13 Jun 2025 12:36:50 -0700 Subject: [PATCH 05/26] Add more logging 3 --- src/code/FindPSResource.cs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/code/FindPSResource.cs b/src/code/FindPSResource.cs index 9b03ff8d6..c45681965 100644 --- a/src/code/FindPSResource.cs +++ b/src/code/FindPSResource.cs @@ -175,6 +175,13 @@ private void ProcessResourceNameParameterSet() try { WriteDebug("In FindPSResource::ProcessResourceNameParameterSet()"); + + WriteVerbose("checking if Name parameter is specified"); + + var check = MyInvocation.BoundParameters.ContainsKey(nameof(Name)); + + WriteVerbose("Value of check for Name parameter: " + check); + // only cases where Name is allowed to not be specified is if Type or Tag parameters are if (!MyInvocation.BoundParameters.ContainsKey(nameof(Name))) { From 98cede896282d5eb3ad1fcdda6f1b32d287707c7 Mon Sep 17 00:00:00 2001 From: Aditya Patwardhan Date: Fri, 13 Jun 2025 14:08:47 -0700 Subject: [PATCH 06/26] Add more logging 4 --- 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 c45681965..c24c75d38 100644 --- a/src/code/FindPSResource.cs +++ b/src/code/FindPSResource.cs @@ -175,7 +175,7 @@ private void ProcessResourceNameParameterSet() try { WriteDebug("In FindPSResource::ProcessResourceNameParameterSet()"); - + WriteDebug("Testing"); WriteVerbose("checking if Name parameter is specified"); var check = MyInvocation.BoundParameters.ContainsKey(nameof(Name)); From 4367c78ff806b3e6a59e90fa9f77ca0518bd8559 Mon Sep 17 00:00:00 2001 From: Aditya Patwardhan Date: Fri, 13 Jun 2025 14:24:12 -0700 Subject: [PATCH 07/26] Remove first test --- .../FindPSResourceContainerRegistryServer.Tests.ps1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/FindPSResourceTests/FindPSResourceContainerRegistryServer.Tests.ps1 b/test/FindPSResourceTests/FindPSResourceContainerRegistryServer.Tests.ps1 index 63b8ab4d6..1ce2b10c8 100644 --- a/test/FindPSResourceTests/FindPSResourceContainerRegistryServer.Tests.ps1 +++ b/test/FindPSResourceTests/FindPSResourceContainerRegistryServer.Tests.ps1 @@ -37,7 +37,7 @@ Describe 'Test HTTP Find-PSResource for ACR Server Protocol' -tags 'CI' { Get-RevertPSResourceRepositoryFile } - It "Find resource given specific Name, Version null" { + <#It "Find resource given specific Name, Version null" { # FindName() Write-Verbose -Verbose "Finding resource with Name: $testModuleName" $res = Find-PSResource -Name $testModuleName -Repository $ACRRepoName -Verbose -Debug -ErrorAction Stop @@ -50,7 +50,7 @@ Describe 'Test HTTP Find-PSResource for ACR Server Protocol' -tags 'CI' { Write-Verbose -Verbose "Find-PSResource completed" $res.Name | Should -Be $testModuleName $res.Version | Should -Be "5.0.0" - } + }#> It "Should not find resource given nonexistant Name" { # FindName() From 11a1060c8f2ef6a3e55e36cb297c28d19f3ab92e Mon Sep 17 00:00:00 2001 From: Aditya Patwardhan Date: Fri, 13 Jun 2025 14:52:01 -0700 Subject: [PATCH 08/26] More debug --- src/code/FindPSResource.cs | 2 +- .../FindPSResourceContainerRegistryServer.Tests.ps1 | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/code/FindPSResource.cs b/src/code/FindPSResource.cs index c24c75d38..046eee0fd 100644 --- a/src/code/FindPSResource.cs +++ b/src/code/FindPSResource.cs @@ -174,7 +174,7 @@ private void ProcessResourceNameParameterSet() { try { - WriteDebug("In FindPSResource::ProcessResourceNameParameterSet()"); + WriteDebug("In FindPSResource::ProcessResourceNameParameterSet() XXXXXXXXXXX"); WriteDebug("Testing"); WriteVerbose("checking if Name parameter is specified"); diff --git a/test/FindPSResourceTests/FindPSResourceContainerRegistryServer.Tests.ps1 b/test/FindPSResourceTests/FindPSResourceContainerRegistryServer.Tests.ps1 index 1ce2b10c8..63b8ab4d6 100644 --- a/test/FindPSResourceTests/FindPSResourceContainerRegistryServer.Tests.ps1 +++ b/test/FindPSResourceTests/FindPSResourceContainerRegistryServer.Tests.ps1 @@ -37,7 +37,7 @@ Describe 'Test HTTP Find-PSResource for ACR Server Protocol' -tags 'CI' { Get-RevertPSResourceRepositoryFile } - <#It "Find resource given specific Name, Version null" { + It "Find resource given specific Name, Version null" { # FindName() Write-Verbose -Verbose "Finding resource with Name: $testModuleName" $res = Find-PSResource -Name $testModuleName -Repository $ACRRepoName -Verbose -Debug -ErrorAction Stop @@ -50,7 +50,7 @@ Describe 'Test HTTP Find-PSResource for ACR Server Protocol' -tags 'CI' { Write-Verbose -Verbose "Find-PSResource completed" $res.Name | Should -Be $testModuleName $res.Version | Should -Be "5.0.0" - }#> + } It "Should not find resource given nonexistant Name" { # FindName() From e7551d60e7f0096b5e6f9b9ae1036ccf560f1fbf Mon Sep 17 00:00:00 2001 From: Aditya Patwardhan Date: Fri, 13 Jun 2025 15:05:31 -0700 Subject: [PATCH 09/26] More debug 2 --- .../FindPSResourceContainerRegistryServer.Tests.ps1 | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/test/FindPSResourceTests/FindPSResourceContainerRegistryServer.Tests.ps1 b/test/FindPSResourceTests/FindPSResourceContainerRegistryServer.Tests.ps1 index 63b8ab4d6..9c9d2a546 100644 --- a/test/FindPSResourceTests/FindPSResourceContainerRegistryServer.Tests.ps1 +++ b/test/FindPSResourceTests/FindPSResourceContainerRegistryServer.Tests.ps1 @@ -40,10 +40,12 @@ Describe 'Test HTTP Find-PSResource for ACR Server Protocol' -tags 'CI' { It "Find resource given specific Name, Version null" { # FindName() Write-Verbose -Verbose "Finding resource with Name: $testModuleName" - $res = Find-PSResource -Name $testModuleName -Repository $ACRRepoName -Verbose -Debug -ErrorAction Stop - if ($Error.Count -gt 0) { - Write-Error "Error occurred while finding resource: $($Error[0])" + try { + $res = Find-PSResource -Name $testModuleName -Repository $ACRRepoName -Verbose -Debug -ErrorAction Stop + } + catch { + Write-Error "Error occurred while finding resource: $_" Get-Error } @@ -54,6 +56,7 @@ Describe 'Test HTTP Find-PSResource for ACR Server Protocol' -tags 'CI' { It "Should not find resource given nonexistant Name" { # FindName() + Write-Verbose -Verbose "Moved to the next test case to find non-existant resource" $res = Find-PSResource -Name NonExistantModule -Repository $ACRRepoName -ErrorVariable err -ErrorAction SilentlyContinue $res | Should -BeNullOrEmpty $err.Count | Should -BeGreaterThan 0 From a8d51711f69073a0a7b0aeb14ca98b15c1b3e54f Mon Sep 17 00:00:00 2001 From: Aditya Patwardhan Date: Fri, 13 Jun 2025 15:33:58 -0700 Subject: [PATCH 10/26] Stacktrace --- .../FindPSResourceContainerRegistryServer.Tests.ps1 | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/FindPSResourceTests/FindPSResourceContainerRegistryServer.Tests.ps1 b/test/FindPSResourceTests/FindPSResourceContainerRegistryServer.Tests.ps1 index 9c9d2a546..3b3c70d90 100644 --- a/test/FindPSResourceTests/FindPSResourceContainerRegistryServer.Tests.ps1 +++ b/test/FindPSResourceTests/FindPSResourceContainerRegistryServer.Tests.ps1 @@ -46,7 +46,8 @@ Describe 'Test HTTP Find-PSResource for ACR Server Protocol' -tags 'CI' { } catch { Write-Error "Error occurred while finding resource: $_" - Get-Error + Write-Verbose -Verbose "Error details: $($_.Exception.Message)" + Write-Verbose -Verbose "Stack trace: $($_.ScriptStackTrace)" } Write-Verbose -Verbose "Find-PSResource completed" From e59d092f23a91f506a4cec8455ef03d6621e6f1f Mon Sep 17 00:00:00 2001 From: Aditya Patwardhan Date: Fri, 13 Jun 2025 15:45:55 -0700 Subject: [PATCH 11/26] Stacktrace --- 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 046eee0fd..7a13a3024 100644 --- a/src/code/FindPSResource.cs +++ b/src/code/FindPSResource.cs @@ -174,11 +174,11 @@ private void ProcessResourceNameParameterSet() { try { - WriteDebug("In FindPSResource::ProcessResourceNameParameterSet() XXXXXXXXXXX"); WriteDebug("Testing"); + WriteDebug("In FindPSResource::ProcessResourceNameParameterSet() XXXXXXXXXXX"); WriteVerbose("checking if Name parameter is specified"); - var check = MyInvocation.BoundParameters.ContainsKey(nameof(Name)); + var check = MyInvocation?.BoundParameters?.ContainsKey(nameof(Name)); WriteVerbose("Value of check for Name parameter: " + check); From ad389cff4139f130c53889ff160c28443bfd7608 Mon Sep 17 00:00:00 2001 From: Aditya Patwardhan Date: Fri, 13 Jun 2025 15:46:09 -0700 Subject: [PATCH 12/26] Stacktrace 2 From 94b09fadc0bdc3d24bbd3d6bdf5933e74d5a4edd Mon Sep 17 00:00:00 2001 From: Aditya Patwardhan Date: Sat, 14 Jun 2025 06:53:01 -0700 Subject: [PATCH 13/26] Add test outside of pester --- .ci/test.yml | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/.ci/test.yml b/.ci/test.yml index cd8c8c88f..7674ae3b6 100644 --- a/.ci/test.yml +++ b/.ci/test.yml @@ -111,6 +111,17 @@ jobs: inline: | $modulePath = Join-Path -Path $env:AGENT_TEMPDIRECTORY -ChildPath 'TempModules' $env:PSModulePath = $modulePath + [System.IO.Path]::PathSeparator + $env:PSModulePath + + $ACRRepoName = "ACRRepo" + $ACRRepoUri = "https://psresourcegettest.azurecr.io" + Register-PSResourceRepository -Name $ACRRepoName -ApiVersion 'ContainerRegistry' -Uri $ACRRepoUri -Verbose + Write-Verbose -Verbose "Registering ACR repository with Az authentication completed" + Get-PSResourceRepository -Name $ACRRepoName -Verbose + Write-Verbose -Verbose "Get-PSResourceRepository completed" + + Write-Verbose -Verbose "Finding resource with Name: $testModuleName" + Find-PSResource -Name $testModuleName -Repository $ACRRepoName -Verbose -Debug -ErrorAction Stop + Write-Verbose -Verbose "Importing build utilities (buildtools.psd1)" Import-Module -Name (Join-Path -Path '${{ parameters.buildDirectory }}' -ChildPath 'buildtools.psd1') -Force Invoke-ModuleTestsACR -Type Functional From fce994a692a772927b2379693592a529a8b6ef70 Mon Sep 17 00:00:00 2001 From: Aditya Patwardhan Date: Sat, 14 Jun 2025 07:01:05 -0700 Subject: [PATCH 14/26] fix test --- .ci/test.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.ci/test.yml b/.ci/test.yml index 7674ae3b6..6142441fa 100644 --- a/.ci/test.yml +++ b/.ci/test.yml @@ -112,6 +112,7 @@ jobs: $modulePath = Join-Path -Path $env:AGENT_TEMPDIRECTORY -ChildPath 'TempModules' $env:PSModulePath = $modulePath + [System.IO.Path]::PathSeparator + $env:PSModulePath + $testModuleName = "test-module" $ACRRepoName = "ACRRepo" $ACRRepoUri = "https://psresourcegettest.azurecr.io" Register-PSResourceRepository -Name $ACRRepoName -ApiVersion 'ContainerRegistry' -Uri $ACRRepoUri -Verbose From f649af54fb56022ecfb87e7ee2930b509442fad8 Mon Sep 17 00:00:00 2001 From: Aditya Patwardhan Date: Sat, 14 Jun 2025 07:26:02 -0700 Subject: [PATCH 15/26] Remove debug --- .ci/test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.ci/test.yml b/.ci/test.yml index 6142441fa..dd7bcb3cb 100644 --- a/.ci/test.yml +++ b/.ci/test.yml @@ -121,7 +121,7 @@ jobs: Write-Verbose -Verbose "Get-PSResourceRepository completed" Write-Verbose -Verbose "Finding resource with Name: $testModuleName" - Find-PSResource -Name $testModuleName -Repository $ACRRepoName -Verbose -Debug -ErrorAction Stop + Find-PSResource -Name $testModuleName -Repository $ACRRepoName -Verbose -ErrorAction Stop Write-Verbose -Verbose "Importing build utilities (buildtools.psd1)" Import-Module -Name (Join-Path -Path '${{ parameters.buildDirectory }}' -ChildPath 'buildtools.psd1') -Force From 162db8eec1467ad7cbd7c4b8ea3e0297ae7cc70a Mon Sep 17 00:00:00 2001 From: Aditya Patwardhan Date: Sat, 14 Jun 2025 07:32:14 -0700 Subject: [PATCH 16/26] Move to PS7 --- .ci/test.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.ci/test.yml b/.ci/test.yml index dd7bcb3cb..36748635a 100644 --- a/.ci/test.yml +++ b/.ci/test.yml @@ -133,6 +133,7 @@ jobs: MAPPED_ADO_PRIVATE_REPO_URL: $(ado_private_repo_url) displayName: 'Execute functional tests with AzAuth' condition: eq(${{ parameters.useAzAuth }}, true) + pwsh: true - ${{ parameters.powershellExecutable }}: | $modulePath = Join-Path -Path $env:AGENT_TEMPDIRECTORY -ChildPath 'TempModules' From dabfd957dd91b605160426a0a35b93b5f232852e Mon Sep 17 00:00:00 2001 From: Aditya Patwardhan Date: Sat, 14 Jun 2025 07:39:10 -0700 Subject: [PATCH 17/26] Move to PS7 - 2 --- .ci/test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.ci/test.yml b/.ci/test.yml index 36748635a..2544e9b3f 100644 --- a/.ci/test.yml +++ b/.ci/test.yml @@ -107,6 +107,7 @@ jobs: inputs: azureSubscription: PSResourceGetACR azurePowerShellVersion: LatestVersion + pwsh: true ScriptType: InlineScript inline: | $modulePath = Join-Path -Path $env:AGENT_TEMPDIRECTORY -ChildPath 'TempModules' @@ -133,7 +134,6 @@ jobs: MAPPED_ADO_PRIVATE_REPO_URL: $(ado_private_repo_url) displayName: 'Execute functional tests with AzAuth' condition: eq(${{ parameters.useAzAuth }}, true) - pwsh: true - ${{ parameters.powershellExecutable }}: | $modulePath = Join-Path -Path $env:AGENT_TEMPDIRECTORY -ChildPath 'TempModules' From ed3218169ba3f14022c7cc10ffbee7eb493bcc27 Mon Sep 17 00:00:00 2001 From: Aditya Patwardhan Date: Mon, 16 Jun 2025 10:51:43 -0700 Subject: [PATCH 18/26] Add timeout for GetTokenAsync --- src/code/ContainerRegistryServerAPICalls.cs | 8 +++- src/code/Utils.cs | 43 +++++++++++++++------ 2 files changed, 38 insertions(+), 13 deletions(-) diff --git a/src/code/ContainerRegistryServerAPICalls.cs b/src/code/ContainerRegistryServerAPICalls.cs index 785c7aeae..dcb8cf8fc 100644 --- a/src/code/ContainerRegistryServerAPICalls.cs +++ b/src/code/ContainerRegistryServerAPICalls.cs @@ -394,6 +394,10 @@ internal string GetContainerRegistryAccessToken(out ErrorRecord errRecord) else { bool isRepositoryUnauthenticated = IsContainerRegistryUnauthenticated(Repository.Uri.ToString(), out errRecord, out accessToken); + _cmdletPassedIn.WriteDebug($"Is repository unauthenticated: {isRepositoryUnauthenticated}"); + _cmdletPassedIn.WriteDebug($"Access token: {accessToken}"); + _cmdletPassedIn.WriteDebug($"Error Record: {errRecord}"); + if (errRecord != null) { return null; @@ -407,7 +411,7 @@ internal string GetContainerRegistryAccessToken(out ErrorRecord errRecord) if (!isRepositoryUnauthenticated) { - accessToken = Utils.GetAzAccessToken(); + accessToken = Utils.GetAzAccessToken(_cmdletPassedIn); if (string.IsNullOrEmpty(accessToken)) { errRecord = new ErrorRecord( @@ -488,6 +492,8 @@ internal bool IsContainerRegistryUnauthenticated(string containerRegistyUrl, out // get the anonymous access token var url = $"{realm}?service={service}{defaultScope}"; + _cmdletPassedIn.WriteDebug($"Getting anonymous access token from the realm: {url}"); + // we dont check the errorrecord here because we want to return false if we get a 401 and not throw an error var results = GetHttpResponseJObjectUsingContentHeaders(url, HttpMethod.Get, content, contentHeaders, out _); diff --git a/src/code/Utils.cs b/src/code/Utils.cs index c8641d8a2..6c83cd959 100644 --- a/src/code/Utils.cs +++ b/src/code/Utils.cs @@ -650,25 +650,44 @@ public static PSCredential GetRepositoryCredentialFromSecretManagement( } } - public static string GetAzAccessToken() + public static string GetAzAccessToken(PSCmdlet cmdletPassedIn) { + cmdletPassedIn.WriteVerbose("Getting Azure access token using DefaultAzureCredential"); + var credOptions = new DefaultAzureCredentialOptions { - ExcludeEnvironmentCredential = true, - ExcludeVisualStudioCodeCredential = true, - ExcludeVisualStudioCredential = true, - ExcludeWorkloadIdentityCredential = true, - ExcludeManagedIdentityCredential = true, // ManagedIdentityCredential makes the experience slow - ExcludeSharedTokenCacheCredential = true, // SharedTokenCacheCredential is not supported on macOS - ExcludeAzureCliCredential = false, - ExcludeAzurePowerShellCredential = false, - ExcludeInteractiveBrowserCredential = false + ExcludeEnvironmentCredential = true, + ExcludeVisualStudioCodeCredential = true, + ExcludeVisualStudioCredential = true, + ExcludeWorkloadIdentityCredential = true, + ExcludeManagedIdentityCredential = true, // ManagedIdentityCredential makes the experience slow + ExcludeSharedTokenCacheCredential = true, // SharedTokenCacheCredential is not supported on macOS + ExcludeAzureCliCredential = false, + ExcludeAzurePowerShellCredential = false, + ExcludeInteractiveBrowserCredential = false }; var dCred = new DefaultAzureCredential(credOptions); var tokenRequestContext = new TokenRequestContext(new string[] { "https://management.azure.com/.default" }); - var token = dCred.GetTokenAsync(tokenRequestContext).Result; - return token.Token; + + try + { + using (var cts = new CancellationTokenSource(TimeSpan.FromSeconds(30))) + { + var token = dCred.GetTokenAsync(tokenRequestContext, cts.Token).GetAwaiter().GetResult(); + return token.Token; + } + } + catch (OperationCanceledException) + { + cmdletPassedIn.WriteWarning("Timeout occurred while acquiring Azure access token."); + throw; + } + catch (Exception ex) + { + cmdletPassedIn.WriteWarning($"Failed to acquire Azure access token: {ex.Message}"); + throw; + } } public static string GetContainerRegistryAccessTokenFromSecretManagement( From fb3b869761e84cf9092ea722e107a9e4b19f9661 Mon Sep 17 00:00:00 2001 From: Aditya Patwardhan Date: Mon, 16 Jun 2025 12:57:44 -0700 Subject: [PATCH 19/26] Return null when failing to get access token --- .../Microsoft.PowerShell.PSResourceGet.csproj | 2 +- src/code/Utils.cs | 22 +++++++++---------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/code/Microsoft.PowerShell.PSResourceGet.csproj b/src/code/Microsoft.PowerShell.PSResourceGet.csproj index 1d657e1a1..225c4292e 100644 --- a/src/code/Microsoft.PowerShell.PSResourceGet.csproj +++ b/src/code/Microsoft.PowerShell.PSResourceGet.csproj @@ -24,7 +24,7 @@ - + diff --git a/src/code/Utils.cs b/src/code/Utils.cs index 6c83cd959..e9d5ffbc3 100644 --- a/src/code/Utils.cs +++ b/src/code/Utils.cs @@ -656,15 +656,15 @@ public static string GetAzAccessToken(PSCmdlet cmdletPassedIn) var credOptions = new DefaultAzureCredentialOptions { - ExcludeEnvironmentCredential = true, - ExcludeVisualStudioCodeCredential = true, - ExcludeVisualStudioCredential = true, - ExcludeWorkloadIdentityCredential = true, - ExcludeManagedIdentityCredential = true, // ManagedIdentityCredential makes the experience slow - ExcludeSharedTokenCacheCredential = true, // SharedTokenCacheCredential is not supported on macOS - ExcludeAzureCliCredential = false, - ExcludeAzurePowerShellCredential = false, - ExcludeInteractiveBrowserCredential = false + ExcludeEnvironmentCredential = true, + ExcludeVisualStudioCodeCredential = true, + ExcludeVisualStudioCredential = true, + ExcludeWorkloadIdentityCredential = true, + ExcludeManagedIdentityCredential = true, // ManagedIdentityCredential makes the experience slow + ExcludeSharedTokenCacheCredential = true, // SharedTokenCacheCredential is not supported on macOS + ExcludeAzureCliCredential = false, + ExcludeAzurePowerShellCredential = false, + ExcludeInteractiveBrowserCredential = false }; var dCred = new DefaultAzureCredential(credOptions); @@ -681,12 +681,12 @@ public static string GetAzAccessToken(PSCmdlet cmdletPassedIn) catch (OperationCanceledException) { cmdletPassedIn.WriteWarning("Timeout occurred while acquiring Azure access token."); - throw; + return null; } catch (Exception ex) { cmdletPassedIn.WriteWarning($"Failed to acquire Azure access token: {ex.Message}"); - throw; + return null; } } From e59c249c0e2e97aad5df665639d5b7ee5acfbac5 Mon Sep 17 00:00:00 2001 From: Aditya Patwardhan Date: Mon, 16 Jun 2025 14:46:13 -0700 Subject: [PATCH 20/26] Return null on timeout --- src/code/Microsoft.PowerShell.PSResourceGet.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/code/Microsoft.PowerShell.PSResourceGet.csproj b/src/code/Microsoft.PowerShell.PSResourceGet.csproj index 225c4292e..1d657e1a1 100644 --- a/src/code/Microsoft.PowerShell.PSResourceGet.csproj +++ b/src/code/Microsoft.PowerShell.PSResourceGet.csproj @@ -24,7 +24,7 @@ - + From 942b52180c943b17d9188aa854afe86fd3b4658c Mon Sep 17 00:00:00 2001 From: Aditya Patwardhan Date: Mon, 16 Jun 2025 14:56:46 -0700 Subject: [PATCH 21/26] Update .NET SDK --- global.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/global.json b/global.json index 77d62424a..fb6d6a3df 100644 --- a/global.json +++ b/global.json @@ -1,5 +1,5 @@ { "sdk": { - "version": "8.0.406" + "version": "8.0.411" } } From cd36646452ba880404c7acf0c12c7bd729e55595 Mon Sep 17 00:00:00 2001 From: Aditya Patwardhan Date: Mon, 16 Jun 2025 15:07:14 -0700 Subject: [PATCH 22/26] Update Azure Identity --- src/code/Microsoft.PowerShell.PSResourceGet.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/code/Microsoft.PowerShell.PSResourceGet.csproj b/src/code/Microsoft.PowerShell.PSResourceGet.csproj index 1d657e1a1..225c4292e 100644 --- a/src/code/Microsoft.PowerShell.PSResourceGet.csproj +++ b/src/code/Microsoft.PowerShell.PSResourceGet.csproj @@ -24,7 +24,7 @@ - + From 82383ca7ad390ebc6a56e48159f60e9e2abfe858 Mon Sep 17 00:00:00 2001 From: Aditya Patwardhan Date: Mon, 16 Jun 2025 15:19:29 -0700 Subject: [PATCH 23/26] Remove debug from test.yml --- .ci/test.yml | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/.ci/test.yml b/.ci/test.yml index 2544e9b3f..e59b14ad1 100644 --- a/.ci/test.yml +++ b/.ci/test.yml @@ -113,17 +113,6 @@ jobs: $modulePath = Join-Path -Path $env:AGENT_TEMPDIRECTORY -ChildPath 'TempModules' $env:PSModulePath = $modulePath + [System.IO.Path]::PathSeparator + $env:PSModulePath - $testModuleName = "test-module" - $ACRRepoName = "ACRRepo" - $ACRRepoUri = "https://psresourcegettest.azurecr.io" - Register-PSResourceRepository -Name $ACRRepoName -ApiVersion 'ContainerRegistry' -Uri $ACRRepoUri -Verbose - Write-Verbose -Verbose "Registering ACR repository with Az authentication completed" - Get-PSResourceRepository -Name $ACRRepoName -Verbose - Write-Verbose -Verbose "Get-PSResourceRepository completed" - - Write-Verbose -Verbose "Finding resource with Name: $testModuleName" - Find-PSResource -Name $testModuleName -Repository $ACRRepoName -Verbose -ErrorAction Stop - Write-Verbose -Verbose "Importing build utilities (buildtools.psd1)" Import-Module -Name (Join-Path -Path '${{ parameters.buildDirectory }}' -ChildPath 'buildtools.psd1') -Force Invoke-ModuleTestsACR -Type Functional From b4aba68b80de61dfcaf4203f69e585218341ae92 Mon Sep 17 00:00:00 2001 From: Aditya Patwardhan Date: Mon, 16 Jun 2025 15:58:13 -0700 Subject: [PATCH 24/26] Remove extra debug statements --- src/code/ContainerRegistryServerAPICalls.cs | 2 - src/code/FindPSResource.cs | 171 ++++++++---------- src/code/GetPSResourceRepository.cs | 8 - src/code/RegisterPSResourceRepository.cs | 6 - src/code/Utils.cs | 4 - ...SResourceContainerRegistryServer.Tests.ps1 | 17 +- 6 files changed, 73 insertions(+), 135 deletions(-) diff --git a/src/code/ContainerRegistryServerAPICalls.cs b/src/code/ContainerRegistryServerAPICalls.cs index dcb8cf8fc..5dab4c49e 100644 --- a/src/code/ContainerRegistryServerAPICalls.cs +++ b/src/code/ContainerRegistryServerAPICalls.cs @@ -395,8 +395,6 @@ internal string GetContainerRegistryAccessToken(out ErrorRecord errRecord) { bool isRepositoryUnauthenticated = IsContainerRegistryUnauthenticated(Repository.Uri.ToString(), out errRecord, out accessToken); _cmdletPassedIn.WriteDebug($"Is repository unauthenticated: {isRepositoryUnauthenticated}"); - _cmdletPassedIn.WriteDebug($"Access token: {accessToken}"); - _cmdletPassedIn.WriteDebug($"Error Record: {errRecord}"); if (errRecord != null) { diff --git a/src/code/FindPSResource.cs b/src/code/FindPSResource.cs index 7a13a3024..2709073e7 100644 --- a/src/code/FindPSResource.cs +++ b/src/code/FindPSResource.cs @@ -115,20 +115,15 @@ public sealed class FindPSResource : PSCmdlet protected override void BeginProcessing() { - WriteVerbose("Beginning Find-PSResource processing"); _cancellationTokenSource = new CancellationTokenSource(); var networkCred = Credential != null ? new NetworkCredential(Credential.UserName, Credential.Password) : null; - WriteVerbose("Creating FindHelper instance"); - _findHelper = new FindHelper( cancellationToken: _cancellationTokenSource.Token, cmdletPassedIn: this, networkCredential: networkCred); - WriteVerbose("FindHelper instance created successfully"); - // Create a repository 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(); @@ -172,120 +167,98 @@ protected override void ProcessRecord() private void ProcessResourceNameParameterSet() { - try + // only cases where Name is allowed to not be specified is if Type or Tag parameters are + if (!MyInvocation.BoundParameters.ContainsKey(nameof(Name))) { - WriteDebug("Testing"); - WriteDebug("In FindPSResource::ProcessResourceNameParameterSet() XXXXXXXXXXX"); - WriteVerbose("checking if Name parameter is specified"); - - var check = MyInvocation?.BoundParameters?.ContainsKey(nameof(Name)); - - WriteVerbose("Value of check for Name parameter: " + check); - - // only cases where Name is allowed to not be specified is if Type or Tag parameters are - if (!MyInvocation.BoundParameters.ContainsKey(nameof(Name))) + if (MyInvocation.BoundParameters.ContainsKey(nameof(Tag))) { - WriteDebug("Name parameter not provided, checking for Type or Tag parameters"); - if (MyInvocation.BoundParameters.ContainsKey(nameof(Tag))) - { - ProcessTags(); - return; - } - else if (MyInvocation.BoundParameters.ContainsKey(nameof(Type))) - { - Name = new string[] { "*" }; - } - else - { - ThrowTerminatingError(new ErrorRecord( - new PSInvalidOperationException("Name parameter must be provided, unless Tag or Type parameters are used."), - "NameParameterNotProvided", - ErrorCategory.InvalidOperation, - this)); - } + ProcessTags(); + return; } - - WriteVerbose("Processing Name parameter for Find-PSResource cmdlet"); - WriteDebug("Filtering package name(s) on wildcards"); - Name = Utils.ProcessNameWildcards(Name, removeWildcardEntries: false, out string[] errorMsgs, out bool nameContainsWildcard); - WriteVerbose($"Name parameter processed, contains {Name.Length} entries"); - - foreach (string error in errorMsgs) + else if (MyInvocation.BoundParameters.ContainsKey(nameof(Type))) { - WriteError(new ErrorRecord( - new PSInvalidOperationException(error), - "ErrorFilteringNamesForUnsupportedWildcards", - ErrorCategory.InvalidArgument, - this)); + Name = new string[] { "*" }; } - - // 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 (Name.Length == 0) + else { - WriteDebug("Package name(s) could not be resolved"); - return; + ThrowTerminatingError(new ErrorRecord( + new PSInvalidOperationException("Name parameter must be provided, unless Tag or Type parameters are used."), + "NameParameterNotProvided", + ErrorCategory.InvalidOperation, + this)); } + } - // determine/parse out Version param - VersionType versionType = VersionType.VersionRange; - NuGetVersion nugetVersion = null; - VersionRange versionRange = null; + WriteDebug("Filtering package name(s) on wildcards"); + Name = Utils.ProcessNameWildcards(Name, removeWildcardEntries: false, out string[] errorMsgs, out bool nameContainsWildcard); - if (Version != null) + 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 (Name.Length == 0) + { + WriteDebug("Package name(s) could not be resolved"); + return; + } + + // determine/parse out Version param + VersionType versionType = VersionType.VersionRange; + NuGetVersion nugetVersion = null; + VersionRange versionRange = null; + + if (Version != null) + { + WriteDebug("Parsing package version"); + if (!NuGetVersion.TryParse(Version, out nugetVersion)) { - WriteDebug("Parsing package version"); - if (!NuGetVersion.TryParse(Version, out nugetVersion)) + if (Version.Trim().Equals("*")) { - if (Version.Trim().Equals("*")) - { - versionRange = VersionRange.All; - versionType = VersionType.VersionRange; - } - else if (!VersionRange.TryParse(Version, out versionRange)) - { - WriteError(new ErrorRecord( - new ArgumentException("Argument for -Version parameter is not in the proper format"), - "IncorrectVersionFormat", - ErrorCategory.InvalidArgument, - this)); - - return; - } + versionRange = VersionRange.All; + versionType = VersionType.VersionRange; } - else + else if (!VersionRange.TryParse(Version, out versionRange)) { - versionType = VersionType.SpecificVersion; + WriteError(new ErrorRecord( + new ArgumentException("Argument for -Version parameter is not in the proper format"), + "IncorrectVersionFormat", + ErrorCategory.InvalidArgument, + this)); + + return; } } else { - versionType = VersionType.NoVersion; - } - - foreach (PSResourceInfo pkg in _findHelper.FindByResourceName( - name: Name, - type: Type, - versionRange: versionRange, - nugetVersion: nugetVersion, - versionType: versionType, - version: Version, - prerelease: Prerelease, - tag: Tag, - repository: Repository, - includeDependencies: IncludeDependencies, - suppressErrors: false)) - { - WriteObject(pkg); + versionType = VersionType.SpecificVersion; } } - catch (Exception ex) + else { - WriteError(new ErrorRecord( - ex, - "FindPSResourceError", - ErrorCategory.NotSpecified, - this)); + versionType = VersionType.NoVersion; + } + + foreach (PSResourceInfo pkg in _findHelper.FindByResourceName( + name: Name, + type: Type, + versionRange: versionRange, + nugetVersion: nugetVersion, + versionType: versionType, + version: Version, + prerelease: Prerelease, + tag: Tag, + repository: Repository, + includeDependencies: IncludeDependencies, + suppressErrors: false)) + { + WriteObject(pkg); } } diff --git a/src/code/GetPSResourceRepository.cs b/src/code/GetPSResourceRepository.cs index cbb210367..d1ad451ce 100644 --- a/src/code/GetPSResourceRepository.cs +++ b/src/code/GetPSResourceRepository.cs @@ -37,19 +37,15 @@ public sealed class GetPSResourceRepository : PSCmdlet protected override void BeginProcessing() { - WriteVerbose("Beginning Get-PSResourceRepository processing"); RepositorySettings.CheckRepositoryStore(); - WriteVerbose("Repository store checked successfully"); } protected override void ProcessRecord() { - WriteVerbose("Processing Get-PSResourceRepository cmdlet"); string nameArrayAsString = (Name == null || !Name.Any() || string.Equals(Name[0], "*") || Name[0] == null) ? "all" : string.Join(", ", Name); WriteDebug($"Reading repository info for '{nameArrayAsString}'"); List items = RepositorySettings.Read(Name, out string[] errorList); - WriteVerbose($"Read {items.Count} repositories"); // handle non-terminating errors foreach (string error in errorList) @@ -61,14 +57,10 @@ protected override void ProcessRecord() this)); } - WriteVerbose($"Returning {items.Count} repositories"); - foreach (PSRepositoryInfo repo in items) { WriteObject(repo); } - - WriteVerbose("Get-PSResourceRepository cmdlet processing complete"); } #endregion diff --git a/src/code/RegisterPSResourceRepository.cs b/src/code/RegisterPSResourceRepository.cs index c95660d7e..a6e1967ec 100644 --- a/src/code/RegisterPSResourceRepository.cs +++ b/src/code/RegisterPSResourceRepository.cs @@ -115,14 +115,11 @@ class RegisterPSResourceRepository : PSCmdlet protected override void BeginProcessing() { - WriteVerbose("In RegisterPSResourceRepository::BeginProcessing()"); RepositorySettings.CheckRepositoryStore(); - WriteVerbose("Done RegisterPSResourceRepository::BeginProcessing()"); } protected override void ProcessRecord() { - WriteVerbose("In RegisterPSResourceRepository::ProcessRecord()"); List items = new List(); PSRepositoryInfo.APIVersion? repoApiVersion = null; @@ -134,7 +131,6 @@ protected override void ProcessRecord() switch (ParameterSetName) { case NameParameterSet: - WriteDebug("In RegisterPSResourceRepository::NameParameterSet"); if (!Utils.TryCreateValidUri(uriString: Uri, cmdletPassedIn: this, uriResult: out _uri, @@ -145,7 +141,6 @@ protected override void ProcessRecord() try { - WriteDebug($"Registering repository '{Name}' with uri '{_uri}'"); items.Add(RepositorySettings.AddRepository(Name, _uri, Priority, Trusted, repoApiVersion, CredentialInfo, Force, this, out string errorMsg)); if (!string.IsNullOrEmpty(errorMsg)) @@ -183,7 +178,6 @@ protected override void ProcessRecord() break; case RepositoriesParameterSet: - WriteDebug("In RegisterPSResourceRepository::RepositoriesParameterSet"); try { items = RepositoriesParameterSetHelper(); diff --git a/src/code/Utils.cs b/src/code/Utils.cs index e9d5ffbc3..bf307453a 100644 --- a/src/code/Utils.cs +++ b/src/code/Utils.cs @@ -429,8 +429,6 @@ public static bool TryCreateValidUri( out Uri uriResult, out ErrorRecord errorRecord) { - cmdletPassedIn.WriteVerbose($"Validating Uri: {uriString}"); - errorRecord = null; if (Uri.TryCreate(uriString, UriKind.Absolute, out uriResult)) { @@ -652,8 +650,6 @@ public static PSCredential GetRepositoryCredentialFromSecretManagement( public static string GetAzAccessToken(PSCmdlet cmdletPassedIn) { - cmdletPassedIn.WriteVerbose("Getting Azure access token using DefaultAzureCredential"); - var credOptions = new DefaultAzureCredentialOptions { ExcludeEnvironmentCredential = true, diff --git a/test/FindPSResourceTests/FindPSResourceContainerRegistryServer.Tests.ps1 b/test/FindPSResourceTests/FindPSResourceContainerRegistryServer.Tests.ps1 index 3b3c70d90..5de7ccf86 100644 --- a/test/FindPSResourceTests/FindPSResourceContainerRegistryServer.Tests.ps1 +++ b/test/FindPSResourceTests/FindPSResourceContainerRegistryServer.Tests.ps1 @@ -22,9 +22,6 @@ Describe 'Test HTTP Find-PSResource for ACR Server Protocol' -tags 'CI' { { Write-Verbose -Verbose "Using Az module for authentication" Register-PSResourceRepository -Name $ACRRepoName -ApiVersion 'ContainerRegistry' -Uri $ACRRepoUri -Verbose - Write-Verbose -Verbose "Registering ACR repository with Az authentication completed" - Get-PSResourceRepository -Name $ACRRepoName -Verbose - Write-Verbose -Verbose "Get-PSResourceRepository completed" } else { @@ -39,25 +36,13 @@ Describe 'Test HTTP Find-PSResource for ACR Server Protocol' -tags 'CI' { It "Find resource given specific Name, Version null" { # FindName() - Write-Verbose -Verbose "Finding resource with Name: $testModuleName" - - try { - $res = Find-PSResource -Name $testModuleName -Repository $ACRRepoName -Verbose -Debug -ErrorAction Stop - } - catch { - Write-Error "Error occurred while finding resource: $_" - Write-Verbose -Verbose "Error details: $($_.Exception.Message)" - Write-Verbose -Verbose "Stack trace: $($_.ScriptStackTrace)" - } - - Write-Verbose -Verbose "Find-PSResource completed" + $res = Find-PSResource -Name $testModuleName -Repository $ACRRepoName -Verbose -Debug -ErrorAction Stop $res.Name | Should -Be $testModuleName $res.Version | Should -Be "5.0.0" } It "Should not find resource given nonexistant Name" { # FindName() - Write-Verbose -Verbose "Moved to the next test case to find non-existant resource" $res = Find-PSResource -Name NonExistantModule -Repository $ACRRepoName -ErrorVariable err -ErrorAction SilentlyContinue $res | Should -BeNullOrEmpty $err.Count | Should -BeGreaterThan 0 From 35bf642ca4fbba3de13e9669480b6de7e9c1c506 Mon Sep 17 00:00:00 2001 From: Aditya Patwardhan Date: Mon, 16 Jun 2025 16:00:09 -0700 Subject: [PATCH 25/26] Remove extra debug --- src/code/RegisterPSResourceRepository.cs | 1 - .../FindPSResourceContainerRegistryServer.Tests.ps1 | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/src/code/RegisterPSResourceRepository.cs b/src/code/RegisterPSResourceRepository.cs index a6e1967ec..97e18bdc9 100644 --- a/src/code/RegisterPSResourceRepository.cs +++ b/src/code/RegisterPSResourceRepository.cs @@ -116,7 +116,6 @@ class RegisterPSResourceRepository : PSCmdlet protected override void BeginProcessing() { RepositorySettings.CheckRepositoryStore(); - } protected override void ProcessRecord() { diff --git a/test/FindPSResourceTests/FindPSResourceContainerRegistryServer.Tests.ps1 b/test/FindPSResourceTests/FindPSResourceContainerRegistryServer.Tests.ps1 index 5de7ccf86..5b2de751b 100644 --- a/test/FindPSResourceTests/FindPSResourceContainerRegistryServer.Tests.ps1 +++ b/test/FindPSResourceTests/FindPSResourceContainerRegistryServer.Tests.ps1 @@ -36,7 +36,7 @@ Describe 'Test HTTP Find-PSResource for ACR Server Protocol' -tags 'CI' { It "Find resource given specific Name, Version null" { # FindName() - $res = Find-PSResource -Name $testModuleName -Repository $ACRRepoName -Verbose -Debug -ErrorAction Stop + $res = Find-PSResource -Name $testModuleName -Repository $ACRRepoName $res.Name | Should -Be $testModuleName $res.Version | Should -Be "5.0.0" } From 3f189b22c2df203ddd6ce9d7d20ccadc292d6636 Mon Sep 17 00:00:00 2001 From: Aditya Patwardhan Date: Mon, 16 Jun 2025 17:26:00 -0700 Subject: [PATCH 26/26] Remove obsolete property --- src/code/Utils.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/code/Utils.cs b/src/code/Utils.cs index bf307453a..5ca14d0e2 100644 --- a/src/code/Utils.cs +++ b/src/code/Utils.cs @@ -653,7 +653,6 @@ public static string GetAzAccessToken(PSCmdlet cmdletPassedIn) var credOptions = new DefaultAzureCredentialOptions { ExcludeEnvironmentCredential = true, - ExcludeVisualStudioCodeCredential = true, ExcludeVisualStudioCredential = true, ExcludeWorkloadIdentityCredential = true, ExcludeManagedIdentityCredential = true, // ManagedIdentityCredential makes the experience slow