Skip to content

Commit 2a03c90

Browse files
Do not throw when GP not enabled
1 parent 70a6c7c commit 2a03c90

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

src/code/GroupPolicyRepositoryEnforcement.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public static bool IsGroupPolicyEnabled()
7272

7373
if (!IsGroupPolicyEnabled())
7474
{
75-
throw new InvalidOperationException("Group policy is not enabled.");
75+
return null;
7676
}
7777
else
7878
{

test/GroupPolicyEnforcement.Tests.ps1

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,21 @@ Describe 'GroupPolicyEnforcement API Tests' -Tags 'CI' {
1515
[Microsoft.PowerShell.PSResourceGet.Cmdlets.GroupPolicyRepositoryEnforcement]::IsGroupPolicyEnabled() | Should -BeTrue
1616
}
1717

18-
It 'Group Policy must be enabled before getting allowed repositories' -Skip:(-not $IsWindows) {
18+
It 'GetAllowedRepositoryURIs return null if Group Policy is not enabled' -Skip:(-not $IsWindows) {
19+
[Microsoft.PowerShell.PSResourceGet.Cmdlets.GroupPolicyRepositoryEnforcement]::GetAllowedRepositoryURIs() | Should -BeNullOrEmpty
20+
1921
try {
20-
[Microsoft.PowerShell.PSResourceGet.Cmdlets.GroupPolicyRepositoryEnforcement]::GetAllowedRepositoryURIs()
22+
[Microsoft.PowerShell.PSResourceGet.UtilClasses.InternalHooks]::SetTestHook('EnableGPRegistryHook', $true)
23+
[Microsoft.PowerShell.PSResourceGet.UtilClasses.InternalHooks]::SetTestHook('GPEnabledStatus', $true)
24+
[Microsoft.PowerShell.PSResourceGet.UtilClasses.InternalHooks]::SetTestHook('AllowedUri', "https://www.example.com/")
25+
26+
$allowedReps = [Microsoft.PowerShell.PSResourceGet.Cmdlets.GroupPolicyRepositoryEnforcement]::GetAllowedRepositoryURIs()
27+
$allowedReps.AbsoluteUri | Should -Be @("https://www.example.com/")
2128
}
22-
catch {
23-
$_.Exception.InnerException.Message | Should -Be 'Group policy is not enabled.'
29+
finally {
30+
[Microsoft.PowerShell.PSResourceGet.UtilClasses.InternalHooks]::SetTestHook('EnableGPRegistryHook', $false)
31+
[Microsoft.PowerShell.PSResourceGet.UtilClasses.InternalHooks]::SetTestHook('GPEnabledStatus', $false)
32+
[Microsoft.PowerShell.PSResourceGet.UtilClasses.InternalHooks]::SetTestHook('AllowedUri', $null)
2433
}
2534
}
2635
}
@@ -38,11 +47,6 @@ Describe 'GroupPolicyEnforcement Cmdlet Tests' -Tags 'CI' {
3847
[Microsoft.PowerShell.PSResourceGet.UtilClasses.InternalHooks]::SetTestHook('AllowedUri', $null)
3948
}
4049

41-
It 'Getting allowed repositories works as expected' -Skip:(-not $IsWindows) {
42-
$allowedReps = [Microsoft.PowerShell.PSResourceGet.Cmdlets.GroupPolicyRepositoryEnforcement]::GetAllowedRepositoryURIs()
43-
$allowedReps.AbsoluteUri | Should -Be @("https://www.example.com/")
44-
}
45-
4650
It 'Get-PSResourceRepository lists the allowed repository' -Skip:(-not $IsWindows) {
4751
try {
4852
Register-PSResourceRepository -Name 'Example' -Uri 'https://www.example.com/'

0 commit comments

Comments
 (0)