From e3c6d81c32e1b2e7cd4682bd1463d337ee3171e0 Mon Sep 17 00:00:00 2001 From: Aditya Patwardhan Date: Tue, 3 Mar 2026 14:11:11 -0800 Subject: [PATCH 1/3] Register MAR as default trusted repository --- src/code/RegisterPSResourceRepository.cs | 13 +- src/code/RepositorySettings.cs | 32 +++++ src/code/ResetPSResourceRepository.cs | 8 +- src/code/SetPSResourceRepository.cs | 3 +- test/PSGetTestUtils.psm1 | 13 ++ .../MARRepository.Tests.ps1 | 132 ++++++++++++++++++ test/testRepositories.xml | 1 + 7 files changed, 196 insertions(+), 6 deletions(-) create mode 100644 test/ResourceRepositoryTests/MARRepository.Tests.ps1 diff --git a/src/code/RegisterPSResourceRepository.cs b/src/code/RegisterPSResourceRepository.cs index a4994df8a..c3777f176 100644 --- a/src/code/RegisterPSResourceRepository.cs +++ b/src/code/RegisterPSResourceRepository.cs @@ -323,7 +323,7 @@ private PSRepositoryInfo RepoValidationHelper(Hashtable repo) return null; } - if (repo["Name"].ToString().Equals("PSGallery")) + if (repo["Name"].ToString().Equals("PSGallery", StringComparison.OrdinalIgnoreCase)) { WriteError(new ErrorRecord( new PSInvalidOperationException("Cannot register PSGallery with -Name parameter. Try: Register-PSResourceRepository -PSGallery"), @@ -334,6 +334,17 @@ private PSRepositoryInfo RepoValidationHelper(Hashtable repo) return null; } + if (repo["Name"].ToString().Equals("MAR", StringComparison.OrdinalIgnoreCase)) + { + WriteError(new ErrorRecord( + new PSInvalidOperationException("Cannot register MAR with -Name parameter. The MAR repository is automatically registered. Try: Reset-PSResourceRepository to restore default repositories."), + "MARProvidedAsNameRepoPSet", + ErrorCategory.InvalidArgument, + this)); + + return null; + } + if (!repo.ContainsKey("Uri") || repo["Uri"] == null || String.IsNullOrEmpty(repo["Uri"].ToString())) { WriteError(new ErrorRecord( diff --git a/src/code/RepositorySettings.cs b/src/code/RepositorySettings.cs index 7cf4f9261..d4560e093 100644 --- a/src/code/RepositorySettings.cs +++ b/src/code/RepositorySettings.cs @@ -26,8 +26,12 @@ internal static class RepositorySettings // The repository store file's location is currently only at '%LOCALAPPDATA%\PSResourceGet' for the user account. private const string PSGalleryRepoName = "PSGallery"; private const string PSGalleryRepoUri = "https://www.powershellgallery.com/api/v2"; + private const string MARRepoName = "MAR"; + private const string MARRepoUri = "https://mcr.microsoft.com"; private const int DefaultPriority = 50; + private const int MARDefaultPriority = 40; private const bool DefaultTrusted = false; + private const bool MARDefaultTrusted = true; private const string RepositoryFileName = "PSResourceRepository.xml"; private static readonly string RepositoryPath = Path.Combine(Environment.GetFolderPath( Environment.SpecialFolder.LocalApplicationData), "PSResourceGet"); @@ -63,6 +67,10 @@ public static void CheckRepositoryStore() // Add PSGallery to the newly created store Uri psGalleryUri = new Uri(PSGalleryRepoUri); Add(PSGalleryRepoName, psGalleryUri, DefaultPriority, DefaultTrusted, repoCredentialInfo: null, repoCredentialProvider: CredentialProviderType.None, APIVersion.V2, force: false); + + // Add MAR to the newly created store + Uri marUri = new Uri(MARRepoUri); + Add(MARRepoName, marUri, MARDefaultPriority, MARDefaultTrusted, repoCredentialInfo: null, repoCredentialProvider: CredentialProviderType.None, APIVersion.ContainerRegistry, force: false); } // Open file (which should exist now), if cannot/is corrupted then throw error @@ -85,6 +93,12 @@ public static PSRepositoryInfo AddRepository(string repoName, Uri repoUri, int r return null; } + if (repoName.Equals("MAR", StringComparison.OrdinalIgnoreCase)) + { + errorMsg = "Cannot register MAR with -Name parameter. The MAR repository is automatically registered. Try: Reset-PSResourceRepository to restore default repositories."; + return null; + } + return AddToRepositoryStore(repoName, repoUri, repoPriority, repoTrusted, apiVersion, repoCredentialInfo, repoCredentialProvider, force, cmdletPassedIn, out errorMsg); } @@ -187,6 +201,20 @@ public static PSRepositoryInfo UpdateRepositoryStore(string repoName, Uri repoUr return null; } + // check MAR Uri is not trying to be set + if (repoName.Equals("MAR", StringComparison.OrdinalIgnoreCase) && repoUri != null) + { + errorMsg = "The MAR repository has a predefined Uri. Setting the -Uri parameter for this repository is not allowed. Please run 'Reset-PSResourceRepository' to restore default repositories."; + return null; + } + + // check MAR CredentialInfo is not trying to be set + if (repoName.Equals("MAR", StringComparison.OrdinalIgnoreCase) && repoCredentialInfo != null) + { + errorMsg = "Setting the -CredentialInfo parameter for MAR is not allowed. Run 'Reset-PSResourceRepository' to restore default repositories."; + return null; + } + // determine trusted value to pass in (true/false if set, null otherwise, hence the nullable bool variable) bool? _trustedNullable = isSet ? new bool?(repoTrusted) : new bool?(); @@ -908,6 +936,10 @@ public static PSRepositoryInfo Reset(out string errorMsg) Uri psGalleryUri = new Uri(PSGalleryRepoUri); PSRepositoryInfo psGalleryRepo = Add(PSGalleryRepoName, psGalleryUri, DefaultPriority, DefaultTrusted, repoCredentialInfo: null, repoCredentialProvider: CredentialProviderType.None, APIVersion.V2, force: false); + // Add MAR to the newly created store + Uri marUri = new Uri(MARRepoUri); + Add(MARRepoName, marUri, MARDefaultPriority, MARDefaultTrusted, repoCredentialInfo: null, repoCredentialProvider: CredentialProviderType.None, APIVersion.ContainerRegistry, force: false); + // Clean up backup file on success if (!string.IsNullOrEmpty(backupFilePath) && File.Exists(backupFilePath)) { diff --git a/src/code/ResetPSResourceRepository.cs b/src/code/ResetPSResourceRepository.cs index 2c217aab3..bc1d4848f 100644 --- a/src/code/ResetPSResourceRepository.cs +++ b/src/code/ResetPSResourceRepository.cs @@ -10,7 +10,7 @@ namespace Microsoft.PowerShell.PSResourceGet.Cmdlets /// /// The Reset-PSResourceRepository cmdlet resets the repository store by creating a new PSRepositories.xml file. /// This is useful when the repository store becomes corrupted. - /// It will create a new repository store with only the PSGallery repository registered. + /// It will create a new repository store with PSGallery and MAR repositories registered. /// [Cmdlet(VerbsCommon.Reset, "PSResourceRepository", @@ -39,8 +39,8 @@ protected override void ProcessRecord() "PSResourceRepository.xml"); WriteVerbose($"Resetting repository store at: {repositoryStorePath}"); - - if (!ShouldProcess(repositoryStorePath, "Reset repository store and create new PSRepositories.xml file with PSGallery registered")) + + if (!ShouldProcess(repositoryStorePath, "Reset repository store and create new PSRepositories.xml file with PSGallery and MAR registered")) { return; } @@ -57,7 +57,7 @@ protected override void ProcessRecord() return; } - WriteVerbose("Repository store reset successfully. PSGallery has been registered."); + WriteVerbose("Repository store reset successfully. PSGallery and MAR have been registered."); if (PassThru) { diff --git a/src/code/SetPSResourceRepository.cs b/src/code/SetPSResourceRepository.cs index 8dc17fe21..9e69a136d 100644 --- a/src/code/SetPSResourceRepository.cs +++ b/src/code/SetPSResourceRepository.cs @@ -110,10 +110,11 @@ public SwitchParameter Trusted public object GetDynamicParameters() { PSRepositoryInfo repository = RepositorySettings.Read(new[] { Name }, out string[] _).FirstOrDefault(); - // Dynamic parameter '-CredentialProvider' should not appear for PSGallery, or any container registry repository. + // Dynamic parameter '-CredentialProvider' should not appear for PSGallery, MAR, or any container registry repository. // It should also not appear when using the 'Repositories' parameter set. if (repository is not null && (repository.Name.Equals("PSGallery", StringComparison.OrdinalIgnoreCase) || + repository.Name.Equals("MAR", StringComparison.OrdinalIgnoreCase) || ParameterSetName.Equals(RepositoriesParameterSet) || repository.IsContainerRegistry())) { diff --git a/test/PSGetTestUtils.psm1 b/test/PSGetTestUtils.psm1 index 6a384c17c..90f164c9b 100644 --- a/test/PSGetTestUtils.psm1 +++ b/test/PSGetTestUtils.psm1 @@ -19,6 +19,9 @@ $script:IsCoreCLR = $PSVersionTable.ContainsKey('PSEdition') -and $PSVersionTabl $script:PSGalleryName = 'PSGallery' $script:PSGalleryLocation = 'https://www.powershellgallery.com/api/v2' +$script:MARName = 'MAR' +$script:MARLocation = 'https://mcr.microsoft.com' + $script:NuGetGalleryName = 'NuGetGallery' $script:NuGetGalleryLocation = 'https://api.nuget.org/v3/index.json' @@ -153,6 +156,16 @@ function Get-PSGalleryName function Get-PSGalleryLocation { return $script:PSGalleryLocation } + +function Get-MARName +{ + return $script:MARName +} + +function Get-MARLocation { + return $script:MARLocation +} + function Get-NewTestDirs { Param( [string[]] diff --git a/test/ResourceRepositoryTests/MARRepository.Tests.ps1 b/test/ResourceRepositoryTests/MARRepository.Tests.ps1 new file mode 100644 index 000000000..9debdd33b --- /dev/null +++ b/test/ResourceRepositoryTests/MARRepository.Tests.ps1 @@ -0,0 +1,132 @@ +# Copyright (c) Microsoft Corporation. +# Licensed under the MIT License. + +$modPath = "$psscriptroot/../PSGetTestUtils.psm1" +Write-Verbose -Verbose -Message "PSGetTestUtils path: $modPath" +Import-Module $modPath -Force -Verbose + +Describe "Test MAR Repository Registration" -tags 'CI' { + BeforeEach { + $MARName = Get-MARName + $MARUri = Get-MARLocation + $PSGalleryName = Get-PSGalleryName + $PSGalleryUri = Get-PSGalleryLocation + Get-NewPSResourceRepositoryFile + } + AfterEach { + Get-RevertPSResourceRepositoryFile + } + + Context "MAR is auto-registered with expected values" { + It "MAR repository should be present with expected default values" { + $res = Get-PSResourceRepository -Name $MARName + $res | Should -Not -BeNullOrEmpty + $res.Name | Should -Be $MARName + $res.Uri | Should -Be "$MARUri/" + $res.Trusted | Should -Be True + $res.Priority | Should -Be 40 + $res.ApiVersion | Should -Be 'ContainerRegistry' + } + + It "MAR repository should have lower priority number (higher priority) than PSGallery" { + $mar = Get-PSResourceRepository -Name $MARName + $psGallery = Get-PSResourceRepository -Name $PSGalleryName + $mar.Priority | Should -BeLessThan $psGallery.Priority + } + } + + Context "MAR name protection" { + It "should not allow registering MAR with -Name parameter" { + { Register-PSResourceRepository -Name "MAR" -Uri "https://mcr.microsoft.com" -ErrorAction Stop } | Should -Throw -ErrorId "ErrorInNameParameterSet,Microsoft.PowerShell.PSResourceGet.Cmdlets.RegisterPSResourceRepository" + } + + It "should not allow registering MAR (case insensitive) with -Name parameter" { + { Register-PSResourceRepository -Name "mar" -Uri "https://mcr.microsoft.com" -ErrorAction Stop } | Should -Throw -ErrorId "ErrorInNameParameterSet,Microsoft.PowerShell.PSResourceGet.Cmdlets.RegisterPSResourceRepository" + } + + It "should not allow registering MAR with -Name parameter in hashtable" { + Unregister-PSResourceRepository -Name $MARName + $hashtable = @{Name = "MAR"; Uri = "https://mcr.microsoft.com"} + Register-PSResourceRepository -Repository $hashtable -ErrorVariable err -ErrorAction SilentlyContinue + $err.Count | Should -BeGreaterThan 0 + $err[0].FullyQualifiedErrorId | Should -BeExactly "MARProvidedAsNameRepoPSet,Microsoft.PowerShell.PSResourceGet.Cmdlets.RegisterPSResourceRepository" + } + + It "should not allow setting Uri for MAR repository" { + { Set-PSResourceRepository -Name $MARName -Uri "https://example.com" -ErrorAction Stop } | Should -Throw -ErrorId "ErrorInNameParameterSet,Microsoft.PowerShell.PSResourceGet.Cmdlets.SetPSResourceRepository" + } + + It "should not allow setting CredentialInfo for MAR repository" { + $randomSecret = [System.IO.Path]::GetRandomFileName() + $credentialInfo = New-Object Microsoft.PowerShell.PSResourceGet.UtilClasses.PSCredentialInfo ("testvault", $randomSecret) + { Set-PSResourceRepository -Name $MARName -CredentialInfo $credentialInfo -ErrorAction Stop } | Should -Throw -ErrorId "ErrorInNameParameterSet,Microsoft.PowerShell.PSResourceGet.Cmdlets.SetPSResourceRepository" + } + + It "should allow setting Trusted for MAR repository" { + Set-PSResourceRepository -Name $MARName -Trusted:$false + $res = Get-PSResourceRepository -Name $MARName + $res.Trusted | Should -Be False + } + + It "should allow setting Priority for MAR repository" { + Set-PSResourceRepository -Name $MARName -Priority 10 + $res = Get-PSResourceRepository -Name $MARName + $res.Priority | Should -Be 10 + } + } + + Context "Reset repository store includes MAR" { + It "Reset-PSResourceRepository should register MAR alongside PSGallery" { + Reset-PSResourceRepository -Force + $res = Get-PSResourceRepository -Name $MARName + $res | Should -Not -BeNullOrEmpty + $res.Name | Should -Be $MARName + $res.Uri | Should -Be "$MARUri/" + $res.Trusted | Should -Be True + $res.Priority | Should -Be 40 + $res.ApiVersion | Should -Be 'ContainerRegistry' + + $psGallery = Get-PSResourceRepository -Name $PSGalleryName + $psGallery | Should -Not -BeNullOrEmpty + } + + It "Reset-PSResourceRepository should restore MAR after unregistration" { + Unregister-PSResourceRepository -Name $MARName + $res = Get-PSResourceRepository -Name $MARName -ErrorAction SilentlyContinue + $res | Should -BeNullOrEmpty + + Reset-PSResourceRepository -Force + $res = Get-PSResourceRepository -Name $MARName + $res | Should -Not -BeNullOrEmpty + $res.Name | Should -Be $MARName + $res.Uri | Should -Be "$MARUri/" + $res.Trusted | Should -Be True + $res.Priority | Should -Be 40 + } + + It "Reset-PSResourceRepository should restore both PSGallery and MAR" { + Unregister-PSResourceRepository -Name $MARName + Unregister-PSResourceRepository -Name $PSGalleryName + Reset-PSResourceRepository -Force + + $mar = Get-PSResourceRepository -Name $MARName + $mar | Should -Not -BeNullOrEmpty + $mar.Priority | Should -Be 40 + $mar.Trusted | Should -Be True + $mar.ApiVersion | Should -Be 'ContainerRegistry' + + $psGallery = Get-PSResourceRepository -Name $PSGalleryName + $psGallery | Should -Not -BeNullOrEmpty + $psGallery.Priority | Should -Be 50 + } + } + + Context "MAR first due to higher priority" { + It "Find-PSResource Az.Accounts module from MAR" { + $res = Find-PSResource -Name "Az.Accounts" + $res | Should -Not -BeNullOrEmpty + $res.Name | Should -Be "Az.Accounts" + $res.Repository | Should -Be $MARName + } + } +} diff --git a/test/testRepositories.xml b/test/testRepositories.xml index 1e330dda9..b47e891e3 100644 --- a/test/testRepositories.xml +++ b/test/testRepositories.xml @@ -1,5 +1,6 @@ + From c3747b32ca7721c7464ab3ea62334244153ac4ab Mon Sep 17 00:00:00 2001 From: Aditya Patwardhan Date: Tue, 3 Mar 2026 14:12:07 -0800 Subject: [PATCH 2/3] Add fallback test --- test/ResourceRepositoryTests/MARRepository.Tests.ps1 | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/test/ResourceRepositoryTests/MARRepository.Tests.ps1 b/test/ResourceRepositoryTests/MARRepository.Tests.ps1 index 9debdd33b..aa89b7ab0 100644 --- a/test/ResourceRepositoryTests/MARRepository.Tests.ps1 +++ b/test/ResourceRepositoryTests/MARRepository.Tests.ps1 @@ -128,5 +128,12 @@ Describe "Test MAR Repository Registration" -tags 'CI' { $res.Name | Should -Be "Az.Accounts" $res.Repository | Should -Be $MARName } + + It 'Find-PSResource fallback to PSGallery if module not in MAR' { + $res = Find-PSResource -Name "Pscx" + $res | Should -Not -BeNullOrEmpty + $res.Name | Should -Be "Pscx" + $res.Repository | Should -Be $PSGalleryName + } } } From 0899e4983a64e056f4cdeada732b96d7ee86df02 Mon Sep 17 00:00:00 2001 From: Aditya Patwardhan Date: Wed, 4 Mar 2026 14:13:13 -0800 Subject: [PATCH 3/3] Fix tests --- ...SResourceContainerRegistryServer.Tests.ps1 | 11 +--- ...SResourceContainerRegistryServer.Tests.ps1 | 9 ---- ...SResourceContainerRegistryServer.Tests.ps1 | 9 ---- .../MARRepository.Tests.ps1 | 6 +-- .../ResetPSResourceRepository.Tests.ps1 | 52 +++++++++---------- 5 files changed, 31 insertions(+), 56 deletions(-) diff --git a/test/FindPSResourceTests/FindPSResourceContainerRegistryServer.Tests.ps1 b/test/FindPSResourceTests/FindPSResourceContainerRegistryServer.Tests.ps1 index e372720b0..209dbd5f9 100644 --- a/test/FindPSResourceTests/FindPSResourceContainerRegistryServer.Tests.ps1 +++ b/test/FindPSResourceTests/FindPSResourceContainerRegistryServer.Tests.ps1 @@ -273,13 +273,6 @@ Describe 'Test HTTP Find-PSResource for ACR Server Protocol' -tags 'CI' { } Describe 'Test Find-PSResource for MAR Repository' -tags 'CI' { - BeforeAll { - Register-PSResourceRepository -Name "MAR" -Uri "https://mcr.microsoft.com" -ApiVersion "ContainerRegistry" - } - - AfterAll { - Unregister-PSResourceRepository -Name "MAR" - } It "Should find resource given specific Name, Version null" { $res = Find-PSResource -Name "Az.Accounts" -Repository "MAR" @@ -313,10 +306,10 @@ Describe 'Test Find-PSResource for MAR Repository' -tags 'CI' { It "Should find version range for Az dependencies" { # Target known version to know the output from the API won't change $res = Find-PSResource -Repository 'MAR' -Name 'Az' -Version '14.4.0' - + # Version defined by "ModuleVersion" $res.Dependencies.Where{$_.'Name' -eq 'Az.Accounts'}.'VersionRange'.ToString() | Should -Be '[5.3.0, )' - + # Version defined by "RequiredVersion" $res.Dependencies.Where{$_.'Name' -eq 'Az.Resources'}.'VersionRange'.ToString() | Should -Be '[8.1.0, 8.1.0]' } diff --git a/test/InstallPSResourceTests/InstallPSResourceContainerRegistryServer.Tests.ps1 b/test/InstallPSResourceTests/InstallPSResourceContainerRegistryServer.Tests.ps1 index afe81d2c3..ff86cc2cd 100644 --- a/test/InstallPSResourceTests/InstallPSResourceContainerRegistryServer.Tests.ps1 +++ b/test/InstallPSResourceTests/InstallPSResourceContainerRegistryServer.Tests.ps1 @@ -351,15 +351,6 @@ Describe 'Test Install-PSResource for Container Registry scenarios - Manual Vali } Describe 'Test Install-PSResource for MAR Repository' -tags 'CI' { - BeforeAll { - [Microsoft.PowerShell.PSResourceGet.UtilClasses.InternalHooks]::SetTestHook("MARPrefix", "azure-powershell/"); - Register-PSResourceRepository -Name "MAR" -Uri "https://mcr.microsoft.com" -ApiVersion "ContainerRegistry" - } - - AfterAll { - [Microsoft.PowerShell.PSResourceGet.UtilClasses.InternalHooks]::SetTestHook("MARPrefix", $null); - Unregister-PSResourceRepository -Name "MAR" - } It "Should find resource given specific Name, Version null" { try { diff --git a/test/PublishPSResourceTests/PublishPSResourceContainerRegistryServer.Tests.ps1 b/test/PublishPSResourceTests/PublishPSResourceContainerRegistryServer.Tests.ps1 index bd59d69d0..5964d3888 100644 --- a/test/PublishPSResourceTests/PublishPSResourceContainerRegistryServer.Tests.ps1 +++ b/test/PublishPSResourceTests/PublishPSResourceContainerRegistryServer.Tests.ps1 @@ -590,15 +590,6 @@ Describe "Test Publish-PSResource" -tags 'CI' { } Describe 'Test Publish-PSResource for MAR Repository' -tags 'CI' { - BeforeAll { - [Microsoft.PowerShell.PSResourceGet.UtilClasses.InternalHooks]::SetTestHook("MARPrefix", "azure-powershell/"); - Register-PSResourceRepository -Name "MAR" -Uri "https://mcr.microsoft.com" -ApiVersion "ContainerRegistry" - } - - AfterAll { - [Microsoft.PowerShell.PSResourceGet.UtilClasses.InternalHooks]::SetTestHook("MARPrefix", $null); - Unregister-PSResourceRepository -Name "MAR" - } It "Should find resource given specific Name, Version null" { $fileName = "NonExistent.psd1" diff --git a/test/ResourceRepositoryTests/MARRepository.Tests.ps1 b/test/ResourceRepositoryTests/MARRepository.Tests.ps1 index aa89b7ab0..a05b27fe6 100644 --- a/test/ResourceRepositoryTests/MARRepository.Tests.ps1 +++ b/test/ResourceRepositoryTests/MARRepository.Tests.ps1 @@ -77,7 +77,7 @@ Describe "Test MAR Repository Registration" -tags 'CI' { Context "Reset repository store includes MAR" { It "Reset-PSResourceRepository should register MAR alongside PSGallery" { - Reset-PSResourceRepository -Force + Reset-PSResourceRepository $res = Get-PSResourceRepository -Name $MARName $res | Should -Not -BeNullOrEmpty $res.Name | Should -Be $MARName @@ -95,7 +95,7 @@ Describe "Test MAR Repository Registration" -tags 'CI' { $res = Get-PSResourceRepository -Name $MARName -ErrorAction SilentlyContinue $res | Should -BeNullOrEmpty - Reset-PSResourceRepository -Force + Reset-PSResourceRepository $res = Get-PSResourceRepository -Name $MARName $res | Should -Not -BeNullOrEmpty $res.Name | Should -Be $MARName @@ -107,7 +107,7 @@ Describe "Test MAR Repository Registration" -tags 'CI' { It "Reset-PSResourceRepository should restore both PSGallery and MAR" { Unregister-PSResourceRepository -Name $MARName Unregister-PSResourceRepository -Name $PSGalleryName - Reset-PSResourceRepository -Force + Reset-PSResourceRepository $mar = Get-PSResourceRepository -Name $MARName $mar | Should -Not -BeNullOrEmpty diff --git a/test/ResourceRepositoryTests/ResetPSResourceRepository.Tests.ps1 b/test/ResourceRepositoryTests/ResetPSResourceRepository.Tests.ps1 index f49570229..a05d02a38 100644 --- a/test/ResourceRepositoryTests/ResetPSResourceRepository.Tests.ps1 +++ b/test/ResourceRepositoryTests/ResetPSResourceRepository.Tests.ps1 @@ -22,17 +22,17 @@ Describe "Test Reset-PSResourceRepository" -tags 'CI' { $tmpDirPath = Join-Path -Path $TestDrive -ChildPath "tmpDir1" New-Item -ItemType Directory -Path $tmpDirPath -Force | Out-Null Register-PSResourceRepository -Name $TestRepoName -Uri $tmpDirPath - + # Verify repository was added $repos = Get-PSResourceRepository $repos.Count | Should -BeGreaterThan 1 - + # Act: Reset repository store Reset-PSResourceRepository -Confirm:$false - + # Assert: Only PSGallery should exist $repos = Get-PSResourceRepository - $repos.Count | Should -Be 1 + $repos.Count | Should -Be 2 $repos.Name | Should -Be $PSGalleryName $repos.Uri | Should -Be $PSGalleryUri } @@ -43,20 +43,20 @@ Describe "Test Reset-PSResourceRepository" -tags 'CI' { $tmpDirPath = Join-Path -Path $TestDrive -ChildPath "tmpDir1" New-Item -ItemType Directory -Path $tmpDirPath -Force | Out-Null Register-PSResourceRepository -Name $TestRepoName -Uri $tmpDirPath - + # Act: Reset repository store with PassThru $result = Reset-PSResourceRepository -Confirm:$false -PassThru - + # Assert: Result should be PSGallery repository info $result | Should -Not -BeNullOrEmpty $result.Name | Should -Be $PSGalleryName $result.Uri | Should -Be $PSGalleryUri $result.Trusted | Should -Be $false $result.Priority | Should -Be 50 - + # Verify only PSGallery exists $repos = Get-PSResourceRepository - $repos.Count | Should -Be 1 + $repos.Count | Should -Be 2 } It "Reset repository store should support -WhatIf" { @@ -65,14 +65,14 @@ Describe "Test Reset-PSResourceRepository" -tags 'CI' { $tmpDirPath = Join-Path -Path $TestDrive -ChildPath "tmpDir1" New-Item -ItemType Directory -Path $tmpDirPath -Force | Out-Null Register-PSResourceRepository -Name $TestRepoName -Uri $tmpDirPath - + # Capture repository count before WhatIf $reposBefore = Get-PSResourceRepository $countBefore = $reposBefore.Count - + # Act: Run with WhatIf Reset-PSResourceRepository -WhatIf - + # Assert: Repositories should not have changed $reposAfter = Get-PSResourceRepository $reposAfter.Count | Should -Be $countBefore @@ -82,20 +82,20 @@ Describe "Test Reset-PSResourceRepository" -tags 'CI' { # Arrange: Corrupt the repository file $powerShellGetPath = Join-Path -Path ([Environment]::GetFolderPath([System.Environment+SpecialFolder]::LocalApplicationData)) -ChildPath "PSResourceGet" $repoFilePath = Join-Path -Path $powerShellGetPath -ChildPath "PSResourceRepository.xml" - + # Write invalid XML to corrupt the file "This is not valid XML" | Set-Content -Path $repoFilePath -Force - + # Act: Reset the repository store $result = Reset-PSResourceRepository -Confirm:$false -PassThru - + # Assert: Should successfully reset and return PSGallery $result | Should -Not -BeNullOrEmpty $result.Name | Should -Be $PSGalleryName - + # Verify we can now read repositories $repos = Get-PSResourceRepository - $repos.Count | Should -Be 1 + $repos.Count | Should -Be 2 $repos.Name | Should -Be $PSGalleryName } @@ -103,21 +103,21 @@ Describe "Test Reset-PSResourceRepository" -tags 'CI' { # Arrange: Delete the repository file $powerShellGetPath = Join-Path -Path ([Environment]::GetFolderPath([System.Environment+SpecialFolder]::LocalApplicationData)) -ChildPath "PSResourceGet" $repoFilePath = Join-Path -Path $powerShellGetPath -ChildPath "PSResourceRepository.xml" - + if (Test-Path -Path $repoFilePath) { Remove-Item -Path $repoFilePath -Force } - + # Act: Reset the repository store $result = Reset-PSResourceRepository -Confirm:$false -PassThru - + # Assert: Should successfully reset and return PSGallery $result | Should -Not -BeNullOrEmpty $result.Name | Should -Be $PSGalleryName - + # Verify PSGallery is registered $repos = Get-PSResourceRepository - $repos.Count | Should -Be 1 + $repos.Count | Should -Be 2 $repos.Name | Should -Be $PSGalleryName } @@ -129,21 +129,21 @@ Describe "Test Reset-PSResourceRepository" -tags 'CI' { New-Item -ItemType Directory -Path $tmpDir1Path -Force | Out-Null New-Item -ItemType Directory -Path $tmpDir2Path -Force | Out-Null New-Item -ItemType Directory -Path $tmpDir3Path -Force | Out-Null - + Register-PSResourceRepository -Name "testRepo1" -Uri $tmpDir1Path Register-PSResourceRepository -Name "testRepo2" -Uri $tmpDir2Path Register-PSResourceRepository -Name "testRepo3" -Uri $tmpDir3Path - + # Verify multiple repositories exist $reposBefore = Get-PSResourceRepository $reposBefore.Count | Should -BeGreaterThan 1 - + # Act: Reset repository store Reset-PSResourceRepository -Confirm:$false - + # Assert: Only PSGallery should remain $reposAfter = Get-PSResourceRepository - $reposAfter.Count | Should -Be 1 + $reposAfter.Count | Should -Be 2 $reposAfter.Name | Should -Be $PSGalleryName } }