From 29af0608bf939ef29822a7cc196386c71b78fba5 Mon Sep 17 00:00:00 2001 From: Ryan Fu Date: Mon, 18 Nov 2024 16:00:42 -0800 Subject: [PATCH 1/4] add wingetconfigroot dsc --- .../Microsoft.WinGet.DSC.psd1 | 1 + .../Microsoft.WinGet.DSC.psm1 | 65 +++++++++++++++++++ .../tests/Microsoft.WinGet.DSC.Tests.ps1 | 28 ++++++++ 3 files changed, 94 insertions(+) diff --git a/src/PowerShell/Microsoft.WinGet.DSC/Microsoft.WinGet.DSC.psd1 b/src/PowerShell/Microsoft.WinGet.DSC/Microsoft.WinGet.DSC.psd1 index fcbe96f023..1c8d6a57bf 100644 --- a/src/PowerShell/Microsoft.WinGet.DSC/Microsoft.WinGet.DSC.psd1 +++ b/src/PowerShell/Microsoft.WinGet.DSC/Microsoft.WinGet.DSC.psd1 @@ -85,6 +85,7 @@ 'WinGetSource' 'WinGetPackageManager' 'WinGetPackage' + 'WinGetConfigRoot' ) # List of all modules packaged with this module diff --git a/src/PowerShell/Microsoft.WinGet.DSC/Microsoft.WinGet.DSC.psm1 b/src/PowerShell/Microsoft.WinGet.DSC/Microsoft.WinGet.DSC.psm1 index 96938c707a..fad1abc57d 100644 --- a/src/PowerShell/Microsoft.WinGet.DSC/Microsoft.WinGet.DSC.psm1 +++ b/src/PowerShell/Microsoft.WinGet.DSC/Microsoft.WinGet.DSC.psm1 @@ -52,6 +52,12 @@ enum WinGetTrustLevel Trusted } +enum Scope +{ + User + Machine +} + #endregion enums #region DscResources @@ -446,6 +452,65 @@ class WinGetPackageManager } } +[DSCResource()] +class WinGetConfigRoot +{ + # We need a key. Do not set. + [DscProperty(Key)] + [string]$SID + + [DscProperty()] + [bool]$Exists + + [DscProperty()] + [string]$Path + + [DscProperty()] + [Scope]$Scope = [Scope]::User + + hidden [string] $WinGetConfigRoot = 'WinGetConfigRoot' + + [WinGetConfigRoot] Get() + { + $currentState = [WinGetConfigRoot]::new() + $currentState.SID = '' + $wingetConfigRootValue = [System.Environment]::GetEnvironmentVariable($this.WinGetConfigRoot, $this.Scope) + + if ([string]::IsNullOrEmpty($wingetConfigRootValue)) + { + $currentState.Exists = $false + } + else + { + $currentState.Exists = $true + $currentState.Path = $wingetConfigRootValue + } + + return $currentState + } + + [bool] Test() + { + $currentState = $this.Get() + return $this.Exists -eq $currentState.Exists + } + + [void] Set() + { + if (-not $this.Test) + { + if ($this.Exists) + { + [System.Environment]::SetEnvironmentVariable($this.WinGetConfigRoot, $this.Path, $this.Scope) + } + else + { + [System.Environment]::SetEnvironmentVariable($this.WinGetConfigRoot, "", $this.Scope) + } + } + } +} + [DSCResource()] class WinGetPackage { diff --git a/src/PowerShell/tests/Microsoft.WinGet.DSC.Tests.ps1 b/src/PowerShell/tests/Microsoft.WinGet.DSC.Tests.ps1 index 0dc01f6dd8..6a1104f5cf 100644 --- a/src/PowerShell/tests/Microsoft.WinGet.DSC.Tests.ps1 +++ b/src/PowerShell/tests/Microsoft.WinGet.DSC.Tests.ps1 @@ -242,3 +242,31 @@ Describe 'WinGetPackageManager' { # TODO: Add test to verify Set method for WinGetPackageManager } + +Describe 'WinGetConfigRoot' { + It 'Get WinGetConfigRoot' { + $result = InvokeWinGetDSC -Name WinGetConfigRoot -Method Get -Property @{} + $result.Exists | Should -Be $false + $result.Path | Should -Be $null + $result.Scope | Should -Be 'User' + } + + It 'Test WinGetConfigRoot' { + $result = InvokeWinGetDSC -Name WinGetConfigRoot -Method Test -Property @{ Exists = $true; Path = 'C:\Foo\Bar' } + $result.InDesiredState | Should -Be $false + } + + It 'Set WinGetConfigRoot' { + InvokeWinGetDSC -Name WinGetConfigRoot -Method Set -Property @{ Exists = $true; Path = 'C:\Foo\Bar' } + + # Verify $winGetConfigRoot is set + $result = InvokeWinGetDSC -Name WinGetConfigRoot -Method Get -Property @{ Exists = $true; Path = 'C:\Foo\Bar' } + $result.Exists | Should -Be $true + $result.Path | Should -Be 'C:\Foo\Bar' + $result.Scope | Should -Be 'User' + } + + AfterAll { + InvokeWinGetDSC -Name WinGetPackage -Method Set -Property @{ Ensure = 'Absent' } + } +} From 42254f8bb39d5a39cf7ca22c411890a5e673d3dc Mon Sep 17 00:00:00 2001 From: --global Date: Tue, 19 Nov 2024 12:03:48 -0800 Subject: [PATCH 2/4] add WinGetconfigRoot --- .../Microsoft.WinGet.DSC.psm1 | 55 +++++++++------ .../tests/Microsoft.WinGet.DSC.Tests.ps1 | 69 ++++++++++++++----- 2 files changed, 87 insertions(+), 37 deletions(-) diff --git a/src/PowerShell/Microsoft.WinGet.DSC/Microsoft.WinGet.DSC.psm1 b/src/PowerShell/Microsoft.WinGet.DSC/Microsoft.WinGet.DSC.psm1 index fad1abc57d..a2fa47ee03 100644 --- a/src/PowerShell/Microsoft.WinGet.DSC/Microsoft.WinGet.DSC.psm1 +++ b/src/PowerShell/Microsoft.WinGet.DSC/Microsoft.WinGet.DSC.psm1 @@ -52,7 +52,7 @@ enum WinGetTrustLevel Trusted } -enum Scope +enum WinGetScope { User Machine @@ -455,57 +455,70 @@ class WinGetPackageManager [DSCResource()] class WinGetConfigRoot { - # We need a key. Do not set. [DscProperty(Key)] - [string]$SID + [string]$WinGetConfigRoot = "WinGetConfigRoot" [DscProperty()] - [bool]$Exists + [string]$Path = "" [DscProperty()] - [string]$Path + [bool]$Exist = $false [DscProperty()] - [Scope]$Scope = [Scope]::User - - hidden [string] $WinGetConfigRoot = 'WinGetConfigRoot' + [WinGetScope]$WinGetScope = [WinGetScope]::User [WinGetConfigRoot] Get() { $currentState = [WinGetConfigRoot]::new() - $currentState.SID = '' - $wingetConfigRootValue = [System.Environment]::GetEnvironmentVariable($this.WinGetConfigRoot, $this.Scope) + $wingetConfigRootValue = [System.Environment]::GetEnvironmentVariable($this.WinGetConfigRoot, $this.WinGetScope) - if ([string]::IsNullOrEmpty($wingetConfigRootValue)) - { - $currentState.Exists = $false - } - else + if (-not [string]::IsNullOrEmpty($wingetConfigRootValue)) { - $currentState.Exists = $true + $currentState.Exist = $true $currentState.Path = $wingetConfigRootValue } + $currentState.WinGetScope = $this.WinGetScope return $currentState } [bool] Test() { $currentState = $this.Get() - return $this.Exists -eq $currentState.Exists + 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 (-not $this.Test()) { - if ($this.Exists) + if ($this.Exist) { - [System.Environment]::SetEnvironmentVariable($this.WinGetConfigRoot, $this.Path, $this.Scope) + [System.Environment]::SetEnvironmentVariable($this.WinGetConfigRoot, $this.Path, $this.WinGetScope) } else { - [System.Environment]::SetEnvironmentVariable($this.WinGetConfigRoot, "", $this.Scope) + [System.Environment]::SetEnvironmentVariable($this.WinGetConfigRoot, "", $this.WinGetScope) } } } diff --git a/src/PowerShell/tests/Microsoft.WinGet.DSC.Tests.ps1 b/src/PowerShell/tests/Microsoft.WinGet.DSC.Tests.ps1 index 6a1104f5cf..037115d559 100644 --- a/src/PowerShell/tests/Microsoft.WinGet.DSC.Tests.ps1 +++ b/src/PowerShell/tests/Microsoft.WinGet.DSC.Tests.ps1 @@ -244,29 +244,66 @@ Describe 'WinGetPackageManager' { } Describe 'WinGetConfigRoot' { - It 'Get WinGetConfigRoot' { - $result = InvokeWinGetDSC -Name WinGetConfigRoot -Method Get -Property @{} - $result.Exists | Should -Be $false - $result.Path | Should -Be $null - $result.Scope | Should -Be 'User' - } + It 'WinGetConfigRoot User Variable' { + $get = InvokeWinGetDSC -Name WinGetConfigRoot -Method Get -Property @{} + $get.WinGetConfigRoot = "WinGetConfigRoot" + $get.Exist | Should -Be $false + $get.Path | Should -Be "" + $get.Scope | Should -Be 'User' - It 'Test WinGetConfigRoot' { - $result = InvokeWinGetDSC -Name WinGetConfigRoot -Method Test -Property @{ Exists = $true; Path = 'C:\Foo\Bar' } - $result.InDesiredState | Should -Be $false - } + $test = InvokeWinGetDSC -Name WinGetConfigRoot -Method Test -Property @{ Exist = $true; Path = 'C:\Foo\Bar' } + $test.InDesiredState | Should -Be $false - It 'Set WinGetConfigRoot' { - InvokeWinGetDSC -Name WinGetConfigRoot -Method Set -Property @{ Exists = $true; Path = 'C:\Foo\Bar' } + InvokeWinGetDSC -Name WinGetConfigRoot -Method Set -Property @{ Exist = $true; Path = 'C:\Foo\Bar' } - # Verify $winGetConfigRoot is set - $result = InvokeWinGetDSC -Name WinGetConfigRoot -Method Get -Property @{ Exists = $true; Path = 'C:\Foo\Bar' } + # Verify that $WinGetConfigRoot variable is set. + $result = InvokeWinGetDSC -Name WinGetConfigRoot -Method Get -Property @{} $result.Exists | Should -Be $true $result.Path | Should -Be 'C:\Foo\Bar' $result.Scope | Should -Be 'User' + + # Remove $WinGetConfigRoot variable. + $test2 = InvokeWinGetDSC -Name WinGetConfigRoot -Method Test -Property @{ Exist = $false } + $test2.InDesiredState | Should -Be $false + + InvokeWinGetDSC -Name WinGetConfigRoot -Method Set -Property @{ Exist = $false } + + $result2 = InvokeWinGetDSC -Name WinGetConfigRoot -Method Get -Property @{} + $result2.WinGetConfigRoot = "WinGetConfigRoot" + $result2.Exists | Should -Be $false + $result2.Path | Should -Be "" + $result2.Scope | Should -Be 'User' } - AfterAll { - InvokeWinGetDSC -Name WinGetPackage -Method Set -Property @{ Ensure = 'Absent' } + It 'WinGetConfigRoot Machine Variable' { + $get = InvokeWinGetDSC -Name WinGetConfigRoot -Method Get -Property @{ WinGetScope = 'Machine'} + $get.WinGetConfigRoot = "WinGetConfigRoot" + $get.Exist | Should -Be $false + $get.Path | Should -Be "" + $get.Scope | Should -Be 'Machine' + + $test = InvokeWinGetDSC -Name WinGetConfigRoot -Method Test -Property @{ Exist = $true; Path = 'C:\Hello\World'; WinGetScope = 'Machine' } + $test.InDesiredState | Should -Be $false + + InvokeWinGetDSC -Name WinGetConfigRoot -Method Set -Property @{ Exist = $true; Path = 'C:\Hello\World'; WinGetScope = 'Machine' } + + # Verify that $WinGetConfigRoot variable is set. + $result = InvokeWinGetDSC -Name WinGetConfigRoot -Method Get -Property @{ WinGetScope = 'Machine' } + $result.WinGetConfigRoot = "WinGetConfigRoot" + $result.Exists | Should -Be $true + $result.Path | Should -Be 'C:\Hello\World' + $result.Scope | Should -Be 'Machine' + + # Remove $WinGetConfigRoot variable. + $test2 = InvokeWinGetDSC -Name WinGetConfigRoot -Method Test -Property @{ Exist = $false; WinGetScope = 'Machine' } + $test2.InDesiredState | Should -Be $false + + InvokeWinGetDSC -Name WinGetConfigRoot -Method Set -Property @{ Exist = $false; WinGetScope = 'Machine' } + + $result2 = InvokeWinGetDSC -Name WinGetConfigRoot -Method Get -Property @{WinGetScope = 'Machine'} + $result2.WinGetConfigRoot = "WinGetConfigRoot" + $result2.Exists | Should -Be $false + $result2.Path | Should -Be "" + $result2.Scope | Should -Be 'Machine' } } From d79a85aee1961b5ea823394b72989f858e8af907 Mon Sep 17 00:00:00 2001 From: --global Date: Tue, 19 Nov 2024 12:11:44 -0800 Subject: [PATCH 3/4] fix tests --- .../tests/Microsoft.WinGet.DSC.Tests.ps1 | 54 ++++--------------- 1 file changed, 11 insertions(+), 43 deletions(-) diff --git a/src/PowerShell/tests/Microsoft.WinGet.DSC.Tests.ps1 b/src/PowerShell/tests/Microsoft.WinGet.DSC.Tests.ps1 index 037115d559..7765f6a354 100644 --- a/src/PowerShell/tests/Microsoft.WinGet.DSC.Tests.ps1 +++ b/src/PowerShell/tests/Microsoft.WinGet.DSC.Tests.ps1 @@ -244,66 +244,34 @@ Describe 'WinGetPackageManager' { } Describe 'WinGetConfigRoot' { - It 'WinGetConfigRoot User Variable' { - $get = InvokeWinGetDSC -Name WinGetConfigRoot -Method Get -Property @{} + 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.Scope | Should -Be 'User' + $get.WinGetScope | Should -Be $_ - $test = InvokeWinGetDSC -Name WinGetConfigRoot -Method Test -Property @{ Exist = $true; Path = 'C:\Foo\Bar' } + $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' } + 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 @{} + $result = InvokeWinGetDSC -Name WinGetConfigRoot -Method Get -Property @{WinGetScope = $_} $result.Exists | Should -Be $true $result.Path | Should -Be 'C:\Foo\Bar' - $result.Scope | Should -Be 'User' + $result.WinGetScope | Should -Be $_ # Remove $WinGetConfigRoot variable. - $test2 = InvokeWinGetDSC -Name WinGetConfigRoot -Method Test -Property @{ Exist = $false } + $test2 = InvokeWinGetDSC -Name WinGetConfigRoot -Method Test -Property @{ Exist = $false; WinGetScope = $_ } $test2.InDesiredState | Should -Be $false - InvokeWinGetDSC -Name WinGetConfigRoot -Method Set -Property @{ Exist = $false } + InvokeWinGetDSC -Name WinGetConfigRoot -Method Set -Property @{ Exist = $false; WinGetScope = $_ } - $result2 = InvokeWinGetDSC -Name WinGetConfigRoot -Method Get -Property @{} + $result2 = InvokeWinGetDSC -Name WinGetConfigRoot -Method Get -Property @{WinGetScope = $_} $result2.WinGetConfigRoot = "WinGetConfigRoot" $result2.Exists | Should -Be $false $result2.Path | Should -Be "" - $result2.Scope | Should -Be 'User' - } - - It 'WinGetConfigRoot Machine Variable' { - $get = InvokeWinGetDSC -Name WinGetConfigRoot -Method Get -Property @{ WinGetScope = 'Machine'} - $get.WinGetConfigRoot = "WinGetConfigRoot" - $get.Exist | Should -Be $false - $get.Path | Should -Be "" - $get.Scope | Should -Be 'Machine' - - $test = InvokeWinGetDSC -Name WinGetConfigRoot -Method Test -Property @{ Exist = $true; Path = 'C:\Hello\World'; WinGetScope = 'Machine' } - $test.InDesiredState | Should -Be $false - - InvokeWinGetDSC -Name WinGetConfigRoot -Method Set -Property @{ Exist = $true; Path = 'C:\Hello\World'; WinGetScope = 'Machine' } - - # Verify that $WinGetConfigRoot variable is set. - $result = InvokeWinGetDSC -Name WinGetConfigRoot -Method Get -Property @{ WinGetScope = 'Machine' } - $result.WinGetConfigRoot = "WinGetConfigRoot" - $result.Exists | Should -Be $true - $result.Path | Should -Be 'C:\Hello\World' - $result.Scope | Should -Be 'Machine' - - # Remove $WinGetConfigRoot variable. - $test2 = InvokeWinGetDSC -Name WinGetConfigRoot -Method Test -Property @{ Exist = $false; WinGetScope = 'Machine' } - $test2.InDesiredState | Should -Be $false - - InvokeWinGetDSC -Name WinGetConfigRoot -Method Set -Property @{ Exist = $false; WinGetScope = 'Machine' } - - $result2 = InvokeWinGetDSC -Name WinGetConfigRoot -Method Get -Property @{WinGetScope = 'Machine'} - $result2.WinGetConfigRoot = "WinGetConfigRoot" - $result2.Exists | Should -Be $false - $result2.Path | Should -Be "" - $result2.Scope | Should -Be 'Machine' + $result2.WinGetScope | Should -Be $_ } } From 2e4e68d513b24b1ae99ef21a3f8f2337f0a95d46 Mon Sep 17 00:00:00 2001 From: --global Date: Tue, 19 Nov 2024 13:12:44 -0800 Subject: [PATCH 4/4] fix tests --- src/PowerShell/tests/Microsoft.WinGet.DSC.Tests.ps1 | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/PowerShell/tests/Microsoft.WinGet.DSC.Tests.ps1 b/src/PowerShell/tests/Microsoft.WinGet.DSC.Tests.ps1 index 7765f6a354..b0796fe739 100644 --- a/src/PowerShell/tests/Microsoft.WinGet.DSC.Tests.ps1 +++ b/src/PowerShell/tests/Microsoft.WinGet.DSC.Tests.ps1 @@ -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 } } @@ -258,7 +258,7 @@ Describe 'WinGetConfigRoot' { # Verify that $WinGetConfigRoot variable is set. $result = InvokeWinGetDSC -Name WinGetConfigRoot -Method Get -Property @{WinGetScope = $_} - $result.Exists | Should -Be $true + $result.Exist | Should -Be $true $result.Path | Should -Be 'C:\Foo\Bar' $result.WinGetScope | Should -Be $_ @@ -270,7 +270,7 @@ Describe 'WinGetConfigRoot' { $result2 = InvokeWinGetDSC -Name WinGetConfigRoot -Method Get -Property @{WinGetScope = $_} $result2.WinGetConfigRoot = "WinGetConfigRoot" - $result2.Exists | Should -Be $false + $result2.Exist | Should -Be $false $result2.Path | Should -Be "" $result2.WinGetScope | Should -Be $_ }