-
Notifications
You must be signed in to change notification settings - Fork 105
Expand file tree
/
Copy pathResetPSResourceRepository.Tests.ps1
More file actions
149 lines (119 loc) · 6.08 KB
/
ResetPSResourceRepository.Tests.ps1
File metadata and controls
149 lines (119 loc) · 6.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# 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
}
AfterEach {
Get-RevertPSResourceRepositoryFile
}
It "Reset repository store without PassThru parameter" {
# Arrange: Add a test repository
$TestRepoName = "testRepository"
$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.Name | Should -Be $PSGalleryName
$repos.Uri | Should -Be $PSGalleryUri
}
It "Reset repository store with PassThru parameter returns PSGallery" {
# Arrange: Add a test repository
$TestRepoName = "testRepository"
$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
}
It "Reset repository store should support -WhatIf" {
# Arrange: Add a test repository
$TestRepoName = "testRepository"
$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
}
It "Reset repository store when corrupted should succeed" {
# 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.Name | Should -Be $PSGalleryName
}
It "Reset repository store when file doesn't exist should succeed" {
# 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.Name | Should -Be $PSGalleryName
}
It "Reset repository store with multiple repositories should only keep PSGallery" {
# Arrange: Register multiple repositories
$tmpDir1Path = Join-Path -Path $TestDrive -ChildPath "tmpDir1"
$tmpDir2Path = Join-Path -Path $TestDrive -ChildPath "tmpDir2"
$tmpDir3Path = Join-Path -Path $TestDrive -ChildPath "tmpDir3"
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.Name | Should -Be $PSGalleryName
}
}