-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
[WinGet DSC] Add WinGetConfigRoot cmdlet #4990
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -52,6 +52,12 @@ enum WinGetTrustLevel | |
Trusted | ||
} | ||
|
||
enum WinGetScope | ||
{ | ||
User | ||
Machine | ||
} | ||
|
||
#endregion enums | ||
|
||
#region DscResources | ||
|
@@ -446,6 +452,78 @@ class WinGetPackageManager | |
} | ||
} | ||
|
||
[DSCResource()] | ||
class WinGetConfigRoot | ||
{ | ||
[DscProperty(Key)] | ||
[string]$WinGetConfigRoot = "WinGetConfigRoot" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since this property is settable by a user, there should be a test for what happens if it isn't this specific string. |
||
|
||
[DscProperty()] | ||
[string]$Path = "" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There should be validation that |
||
|
||
[DscProperty()] | ||
[bool]$Exist = $false | ||
|
||
[DscProperty()] | ||
[WinGetScope]$WinGetScope = [WinGetScope]::User | ||
|
||
[WinGetConfigRoot] Get() | ||
{ | ||
$currentState = [WinGetConfigRoot]::new() | ||
$wingetConfigRootValue = [System.Environment]::GetEnvironmentVariable($this.WinGetConfigRoot, $this.WinGetScope) | ||
|
||
if (-not [string]::IsNullOrEmpty($wingetConfigRootValue)) | ||
{ | ||
$currentState.Exist = $true | ||
$currentState.Path = $wingetConfigRootValue | ||
} | ||
|
||
$currentState.WinGetScope = $this.WinGetScope | ||
return $currentState | ||
} | ||
|
||
[bool] Test() | ||
{ | ||
$currentState = $this.Get() | ||
if ($this.Exist -ne $currentState.Exist) | ||
{ | ||
return $false | ||
} | ||
|
||
if ($this.Path -ne $currentState.Path) | ||
{ | ||
return $false | ||
} | ||
|
||
if ($this.WinGetScope -ne $currentState.WinGetScope) | ||
{ | ||
return $false | ||
} | ||
|
||
if ($this.WinGetConfigRoot -ne $currentState.WinGetConfigRoot) | ||
{ | ||
return $false | ||
} | ||
|
||
return $true | ||
} | ||
|
||
[void] Set() | ||
{ | ||
if (-not $this.Test()) | ||
{ | ||
if ($this.Exist) | ||
{ | ||
[System.Environment]::SetEnvironmentVariable($this.WinGetConfigRoot, $this.Path, $this.WinGetScope) | ||
} | ||
else | ||
{ | ||
[System.Environment]::SetEnvironmentVariable($this.WinGetConfigRoot, "", $this.WinGetScope) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: this could set the variable to |
||
} | ||
} | ||
} | ||
} | ||
|
||
[DSCResource()] | ||
class WinGetPackage | ||
{ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,9 +32,9 @@ BeforeAll { | |
|
||
Describe 'List available DSC resources'{ | ||
It 'Shows DSC Resources'{ | ||
$expectedDSCResources = "WinGetAdminSettings", "WinGetPackage", "WinGetPackageManager", "WinGetSource", "WinGetUserSettings" | ||
$expectedDSCResources = "WinGetAdminSettings", "WinGetPackage", "WinGetPackageManager", "WinGetSource", "WinGetUserSettings", "WinGetConfigRoot" | ||
$availableDSCResources = (Get-DscResource -Module Microsoft.WinGet.DSC).Name | ||
$availableDSCResources.length | Should -Be 5 | ||
$availableDSCResources.length | Should -Be 6 | ||
$availableDSCResources | Where-Object {$expectedDSCResources -notcontains $_} | Should -BeNullOrEmpty -ErrorAction Stop | ||
} | ||
} | ||
|
@@ -242,3 +242,36 @@ Describe 'WinGetPackageManager' { | |
|
||
# TODO: Add test to verify Set method for WinGetPackageManager | ||
} | ||
|
||
Describe 'WinGetConfigRoot' { | ||
It 'Sets and removes $WinGetConfigRoot by scope' -ForEach 'User','Machine' { | ||
$get = InvokeWinGetDSC -Name WinGetConfigRoot -Method Get -Property @{ WinGetScope = $_ } | ||
$get.WinGetConfigRoot = "WinGetConfigRoot" | ||
$get.Exist | Should -Be $false | ||
$get.Path | Should -Be "" | ||
$get.WinGetScope | Should -Be $_ | ||
|
||
$test = InvokeWinGetDSC -Name WinGetConfigRoot -Method Test -Property @{ Exist = $true; Path = 'C:\Foo\Bar'; WinGetScope = $_ } | ||
$test.InDesiredState | Should -Be $false | ||
|
||
InvokeWinGetDSC -Name WinGetConfigRoot -Method Set -Property @{ Exist = $true; Path = 'C:\Foo\Bar'; WinGetScope = $_ } | ||
|
||
# Verify that $WinGetConfigRoot variable is set. | ||
$result = InvokeWinGetDSC -Name WinGetConfigRoot -Method Get -Property @{WinGetScope = $_} | ||
$result.Exist | Should -Be $true | ||
$result.Path | Should -Be 'C:\Foo\Bar' | ||
$result.WinGetScope | Should -Be $_ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the expected result of test if the path is not specified, but the user has requested |
||
|
||
# Remove $WinGetConfigRoot variable. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Before you remove it, it may be a good idea to check that if a different Path is specified while the variable exists, then the test returns false. |
||
$test2 = InvokeWinGetDSC -Name WinGetConfigRoot -Method Test -Property @{ Exist = $false; WinGetScope = $_ } | ||
$test2.InDesiredState | Should -Be $false | ||
|
||
InvokeWinGetDSC -Name WinGetConfigRoot -Method Set -Property @{ Exist = $false; WinGetScope = $_ } | ||
|
||
$result2 = InvokeWinGetDSC -Name WinGetConfigRoot -Method Get -Property @{WinGetScope = $_} | ||
$result2.WinGetConfigRoot = "WinGetConfigRoot" | ||
$result2.Exist | Should -Be $false | ||
$result2.Path | Should -Be "" | ||
$result2.WinGetScope | Should -Be $_ | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems like this needs to be a subset of
EnvironmentVariableTarget
. I didn't know you could use enums like thatSmall nit: if it is related to the other enum, I would add a comment for the future