|
| 1 | +# Copyright (c) Microsoft Corporation. |
| 2 | +# Licensed under the MIT License. |
| 3 | + |
| 4 | +$modPath = "$psscriptroot/../PSGetTestUtils.psm1" |
| 5 | +Write-Verbose -Verbose -Message "PSGetTestUtils path: $modPath" |
| 6 | +Import-Module $modPath -Force -Verbose |
| 7 | + |
| 8 | +Describe "Test Reset-PSResourceRepository" -tags 'CI' { |
| 9 | + BeforeEach { |
| 10 | + $PSGalleryName = Get-PSGalleryName |
| 11 | + $PSGalleryUri = Get-PSGalleryLocation |
| 12 | + Get-NewPSResourceRepositoryFile |
| 13 | + } |
| 14 | + |
| 15 | + AfterEach { |
| 16 | + Get-RevertPSResourceRepositoryFile |
| 17 | + } |
| 18 | + |
| 19 | + It "Reset repository store without PassThru parameter" { |
| 20 | + # Arrange: Add a test repository |
| 21 | + $TestRepoName = "testRepository" |
| 22 | + $tmpDirPath = Join-Path -Path $TestDrive -ChildPath "tmpDir1" |
| 23 | + New-Item -ItemType Directory -Path $tmpDirPath -Force | Out-Null |
| 24 | + Register-PSResourceRepository -Name $TestRepoName -Uri $tmpDirPath |
| 25 | + |
| 26 | + # Verify repository was added |
| 27 | + $repos = Get-PSResourceRepository |
| 28 | + $repos.Count | Should -BeGreaterThan 1 |
| 29 | + |
| 30 | + # Act: Reset repository store |
| 31 | + Reset-PSResourceRepository -Confirm:$false |
| 32 | + |
| 33 | + # Assert: Only PSGallery should exist |
| 34 | + $repos = Get-PSResourceRepository |
| 35 | + $repos.Count | Should -Be 1 |
| 36 | + $repos.Name | Should -Be $PSGalleryName |
| 37 | + $repos.Uri | Should -Be $PSGalleryUri |
| 38 | + } |
| 39 | + |
| 40 | + It "Reset repository store with PassThru parameter returns PSGallery" { |
| 41 | + # Arrange: Add a test repository |
| 42 | + $TestRepoName = "testRepository" |
| 43 | + $tmpDirPath = Join-Path -Path $TestDrive -ChildPath "tmpDir1" |
| 44 | + New-Item -ItemType Directory -Path $tmpDirPath -Force | Out-Null |
| 45 | + Register-PSResourceRepository -Name $TestRepoName -Uri $tmpDirPath |
| 46 | + |
| 47 | + # Act: Reset repository store with PassThru |
| 48 | + $result = Reset-PSResourceRepository -Confirm:$false -PassThru |
| 49 | + |
| 50 | + # Assert: Result should be PSGallery repository info |
| 51 | + $result | Should -Not -BeNullOrEmpty |
| 52 | + $result.Name | Should -Be $PSGalleryName |
| 53 | + $result.Uri | Should -Be $PSGalleryUri |
| 54 | + $result.Trusted | Should -Be $false |
| 55 | + $result.Priority | Should -Be 50 |
| 56 | + |
| 57 | + # Verify only PSGallery exists |
| 58 | + $repos = Get-PSResourceRepository |
| 59 | + $repos.Count | Should -Be 1 |
| 60 | + } |
| 61 | + |
| 62 | + It "Reset repository store should support -WhatIf" { |
| 63 | + # Arrange: Add a test repository |
| 64 | + $TestRepoName = "testRepository" |
| 65 | + $tmpDirPath = Join-Path -Path $TestDrive -ChildPath "tmpDir1" |
| 66 | + New-Item -ItemType Directory -Path $tmpDirPath -Force | Out-Null |
| 67 | + Register-PSResourceRepository -Name $TestRepoName -Uri $tmpDirPath |
| 68 | + |
| 69 | + # Capture repository count before WhatIf |
| 70 | + $reposBefore = Get-PSResourceRepository |
| 71 | + $countBefore = $reposBefore.Count |
| 72 | + |
| 73 | + # Act: Run with WhatIf |
| 74 | + Reset-PSResourceRepository -WhatIf |
| 75 | + |
| 76 | + # Assert: Repositories should not have changed |
| 77 | + $reposAfter = Get-PSResourceRepository |
| 78 | + $reposAfter.Count | Should -Be $countBefore |
| 79 | + } |
| 80 | + |
| 81 | + It "Reset repository store when corrupted should succeed" { |
| 82 | + # Arrange: Corrupt the repository file |
| 83 | + $powerShellGetPath = Join-Path -Path ([Environment]::GetFolderPath([System.Environment+SpecialFolder]::LocalApplicationData)) -ChildPath "PSResourceGet" |
| 84 | + $repoFilePath = Join-Path -Path $powerShellGetPath -ChildPath "PSResourceRepository.xml" |
| 85 | + |
| 86 | + # Write invalid XML to corrupt the file |
| 87 | + "This is not valid XML" | Set-Content -Path $repoFilePath -Force |
| 88 | + |
| 89 | + # Act: Reset the repository store |
| 90 | + $result = Reset-PSResourceRepository -Confirm:$false -PassThru |
| 91 | + |
| 92 | + # Assert: Should successfully reset and return PSGallery |
| 93 | + $result | Should -Not -BeNullOrEmpty |
| 94 | + $result.Name | Should -Be $PSGalleryName |
| 95 | + |
| 96 | + # Verify we can now read repositories |
| 97 | + $repos = Get-PSResourceRepository |
| 98 | + $repos.Count | Should -Be 1 |
| 99 | + $repos.Name | Should -Be $PSGalleryName |
| 100 | + } |
| 101 | + |
| 102 | + It "Reset repository store when file doesn't exist should succeed" { |
| 103 | + # Arrange: Delete the repository file |
| 104 | + $powerShellGetPath = Join-Path -Path ([Environment]::GetFolderPath([System.Environment+SpecialFolder]::LocalApplicationData)) -ChildPath "PSResourceGet" |
| 105 | + $repoFilePath = Join-Path -Path $powerShellGetPath -ChildPath "PSResourceRepository.xml" |
| 106 | + |
| 107 | + if (Test-Path -Path $repoFilePath) { |
| 108 | + Remove-Item -Path $repoFilePath -Force |
| 109 | + } |
| 110 | + |
| 111 | + # Act: Reset the repository store |
| 112 | + $result = Reset-PSResourceRepository -Confirm:$false -PassThru |
| 113 | + |
| 114 | + # Assert: Should successfully reset and return PSGallery |
| 115 | + $result | Should -Not -BeNullOrEmpty |
| 116 | + $result.Name | Should -Be $PSGalleryName |
| 117 | + |
| 118 | + # Verify PSGallery is registered |
| 119 | + $repos = Get-PSResourceRepository |
| 120 | + $repos.Count | Should -Be 1 |
| 121 | + $repos.Name | Should -Be $PSGalleryName |
| 122 | + } |
| 123 | + |
| 124 | + It "Reset repository store with multiple repositories should only keep PSGallery" { |
| 125 | + # Arrange: Register multiple repositories |
| 126 | + $tmpDir1Path = Join-Path -Path $TestDrive -ChildPath "tmpDir1" |
| 127 | + $tmpDir2Path = Join-Path -Path $TestDrive -ChildPath "tmpDir2" |
| 128 | + $tmpDir3Path = Join-Path -Path $TestDrive -ChildPath "tmpDir3" |
| 129 | + New-Item -ItemType Directory -Path $tmpDir1Path -Force | Out-Null |
| 130 | + New-Item -ItemType Directory -Path $tmpDir2Path -Force | Out-Null |
| 131 | + New-Item -ItemType Directory -Path $tmpDir3Path -Force | Out-Null |
| 132 | + |
| 133 | + Register-PSResourceRepository -Name "testRepo1" -Uri $tmpDir1Path |
| 134 | + Register-PSResourceRepository -Name "testRepo2" -Uri $tmpDir2Path |
| 135 | + Register-PSResourceRepository -Name "testRepo3" -Uri $tmpDir3Path |
| 136 | + |
| 137 | + # Verify multiple repositories exist |
| 138 | + $reposBefore = Get-PSResourceRepository |
| 139 | + $reposBefore.Count | Should -BeGreaterThan 1 |
| 140 | + |
| 141 | + # Act: Reset repository store |
| 142 | + Reset-PSResourceRepository -Confirm:$false |
| 143 | + |
| 144 | + # Assert: Only PSGallery should remain |
| 145 | + $reposAfter = Get-PSResourceRepository |
| 146 | + $reposAfter.Count | Should -Be 1 |
| 147 | + $reposAfter.Name | Should -Be $PSGalleryName |
| 148 | + } |
| 149 | +} |
0 commit comments