-
Couldn't load subscription status.
- Fork 99
Add Reset-PSResourceRepository cmdlet for repository store recovery #1894
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using Microsoft.PowerShell.PSResourceGet.UtilClasses; | ||
| using System; | ||
| using System.Management.Automation; | ||
|
|
||
| namespace Microsoft.PowerShell.PSResourceGet.Cmdlets | ||
| { | ||
| /// <summary> | ||
| /// The Reset-PSResourceRepository cmdlet creates a fresh repository store by deleting | ||
| /// the existing PSResourceRepository.xml file and creating a new one with only PSGallery registered. | ||
| /// This is useful when the repository store becomes corrupted. | ||
| /// </summary> | ||
| [Cmdlet(VerbsCommon.Reset, | ||
| "PSResourceRepository", | ||
| SupportsShouldProcess = true, | ||
| ConfirmImpact = ConfirmImpact.High)] | ||
| [OutputType(typeof(PSRepositoryInfo))] | ||
| public sealed class ResetPSResourceRepository : PSCmdlet | ||
| { | ||
| #region Parameters | ||
|
|
||
| /// <summary> | ||
| /// When specified, displays the PSGallery repository that was registered. | ||
| /// </summary> | ||
| [Parameter] | ||
| public SwitchParameter PassThru { get; set; } | ||
|
|
||
| #endregion | ||
|
|
||
| #region Method overrides | ||
|
|
||
| protected override void ProcessRecord() | ||
| { | ||
| string repositoryStorePath = System.IO.Path.Combine( | ||
| Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), | ||
| "PSResourceGet", | ||
| "PSResourceRepository.xml"); | ||
|
|
||
| string actionMessage = $"Reset repository store at '{repositoryStorePath}'. This will delete all registered repositories and register only PSGallery."; | ||
|
|
||
| if (!ShouldProcess(repositoryStorePath, actionMessage)) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| try | ||
| { | ||
| WriteVerbose("Resetting repository store"); | ||
| PSRepositoryInfo psGallery = RepositorySettings.ResetRepositoryStore(); | ||
| WriteVerbose($"Repository store has been reset. PSGallery registered at: {repositoryStorePath}"); | ||
|
|
||
| if (PassThru) | ||
| { | ||
| WriteObject(psGallery); | ||
| } | ||
| } | ||
| catch (Exception e) | ||
| { | ||
| ThrowTerminatingError(new ErrorRecord( | ||
| new PSInvalidOperationException($"Failed to reset repository store: {e.Message}", e), | ||
| "ResetRepositoryStoreFailed", | ||
| ErrorCategory.InvalidOperation, | ||
| this)); | ||
| } | ||
| } | ||
|
|
||
| #endregion | ||
| } | ||
| } |
129 changes: 129 additions & 0 deletions
test/ResourceRepositoryTests/ResetPSResourceRepository.Tests.ps1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| # 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 Reset-PSResourceRepository" -tags 'CI' { | ||
| BeforeEach { | ||
| $PSGalleryName = Get-PSGalleryName | ||
| $PSGalleryUri = Get-PSGalleryLocation | ||
| Get-NewPSResourceRepositoryFile | ||
| $tmpDir1Path = Join-Path -Path $TestDrive -ChildPath "tmpDir1" | ||
| $tmpDir2Path = Join-Path -Path $TestDrive -ChildPath "tmpDir2" | ||
| $tmpDirPaths = @($tmpDir1Path, $tmpDir2Path) | ||
| Get-NewTestDirs($tmpDirPaths) | ||
| } | ||
| AfterEach { | ||
| Get-RevertPSResourceRepositoryFile | ||
| $tmpDir1Path = Join-Path -Path $TestDrive -ChildPath "tmpDir1" | ||
| $tmpDir2Path = Join-Path -Path $TestDrive -ChildPath "tmpDir2" | ||
| $tmpDirPaths = @($tmpDir1Path, $tmpDir2Path) | ||
| Get-RemoveTestDirs($tmpDirPaths) | ||
| } | ||
|
|
||
| It "reset repository store with multiple repositories registered" { | ||
| # Register multiple repositories | ||
| Register-PSResourceRepository -Name "testRepository1" -Uri $tmpDir1Path | ||
| Register-PSResourceRepository -Name "testRepository2" -Uri $tmpDir2Path | ||
|
|
||
| # Verify multiple repositories exist | ||
| $repos = Get-PSResourceRepository | ||
| $repos.Count | Should -BeGreaterThan 2 | ||
|
|
||
| # Reset repository store | ||
| Reset-PSResourceRepository -Confirm:$false | ||
|
|
||
| # Verify only PSGallery exists | ||
| $repos = Get-PSResourceRepository | ||
| $repos.Count | Should -Be 1 | ||
| $repos.Name | Should -Be $PSGalleryName | ||
| $repos.Uri | Should -Be $PSGalleryUri | ||
| $repos.Priority | Should -Be 50 | ||
| $repos.Trusted | Should -Be $false | ||
| } | ||
|
|
||
| It "reset repository store when only PSGallery is registered" { | ||
| # Reset repository store | ||
| Reset-PSResourceRepository -Confirm:$false | ||
|
|
||
| # Verify only PSGallery exists | ||
| $repos = Get-PSResourceRepository | ||
| $repos.Count | Should -Be 1 | ||
| $repos.Name | Should -Be $PSGalleryName | ||
| } | ||
|
|
||
| It "reset repository store with -PassThru returns PSGallery" { | ||
| # Register a test repository | ||
| Register-PSResourceRepository -Name "testRepository1" -Uri $tmpDir1Path | ||
|
|
||
| # Reset repository store with PassThru | ||
| $result = Reset-PSResourceRepository -Confirm:$false -PassThru | ||
|
|
||
| # Verify PassThru returns PSGallery | ||
| $result.Name | Should -Be $PSGalleryName | ||
| $result.Uri | Should -Be $PSGalleryUri | ||
| $result.Priority | Should -Be 50 | ||
| $result.Trusted | Should -Be $false | ||
|
|
||
| # Verify only PSGallery exists | ||
| $repos = Get-PSResourceRepository | ||
| $repos.Count | Should -Be 1 | ||
| } | ||
|
|
||
| It "reset repository store respects -WhatIf" { | ||
| # Register test repositories | ||
| Register-PSResourceRepository -Name "testRepository1" -Uri $tmpDir1Path | ||
| Register-PSResourceRepository -Name "testRepository2" -Uri $tmpDir2Path | ||
|
|
||
| # Get count before reset | ||
| $reposBefore = Get-PSResourceRepository | ||
| $countBefore = $reposBefore.Count | ||
|
|
||
| # Reset with WhatIf | ||
| Reset-PSResourceRepository -WhatIf | ||
|
|
||
| # Verify repositories still exist (WhatIf should not make changes) | ||
| $reposAfter = Get-PSResourceRepository | ||
| $reposAfter.Count | Should -Be $countBefore | ||
| $reposAfter.Name | Should -Contain "testRepository1" | ||
| $reposAfter.Name | Should -Contain "testRepository2" | ||
| } | ||
|
|
||
| It "reset repository store when PSGallery was unregistered" { | ||
| # Unregister PSGallery | ||
| Unregister-PSResourceRepository -Name $PSGalleryName | ||
|
|
||
| # Verify PSGallery is not registered | ||
| $repos = Get-PSResourceRepository -ErrorAction SilentlyContinue | ||
| $repos.Name | Should -Not -Contain $PSGalleryName | ||
|
|
||
| # Reset repository store | ||
| Reset-PSResourceRepository -Confirm:$false | ||
|
|
||
| # Verify PSGallery is back | ||
| $repos = Get-PSResourceRepository | ||
| $repos.Count | Should -Be 1 | ||
| $repos.Name | Should -Be $PSGalleryName | ||
| } | ||
|
|
||
| It "reset repository store with custom PSGallery settings" { | ||
| # Unregister default PSGallery and register with custom settings | ||
| Unregister-PSResourceRepository -Name $PSGalleryName | ||
| Register-PSResourceRepository -PSGallery -Priority 10 -Trusted | ||
|
|
||
| # Verify custom settings | ||
| $repo = Get-PSResourceRepository -Name $PSGalleryName | ||
| $repo.Priority | Should -Be 10 | ||
| $repo.Trusted | Should -Be $true | ||
|
|
||
| # Reset repository store | ||
| Reset-PSResourceRepository -Confirm:$false | ||
|
|
||
| # Verify PSGallery is reset to default settings | ||
| $repo = Get-PSResourceRepository -Name $PSGalleryName | ||
| $repo.Priority | Should -Be 50 | ||
| $repo.Trusted | Should -Be $false | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.