From ecaa301a670086ef803bf6f5c97eeeb0e2f644fc Mon Sep 17 00:00:00 2001 From: Gijs Reijn Date: Thu, 7 Nov 2024 04:33:15 +0100 Subject: [PATCH 1/8] New module script --- CONTRIBUTING.md | 32 ++++-- utilities/scripts/New-DscResourceModule.ps1 | 106 ++++++++++++++++++++ 2 files changed, 131 insertions(+), 7 deletions(-) create mode 100644 utilities/scripts/New-DscResourceModule.ps1 diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 5a33cac4..932f3069 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -109,14 +109,31 @@ Once the team have approved an issue/spec, development can proceed. If no develo ### Fork, Clone, Branch and Create your PR -Once you've discussed your proposed feature/fix/etc. with a team member, and you've agreed an approach or a spec has been written and approved, it's time to start development: +Once you've discussed your proposed feature/fix/etc. with a team member, and you've agreed an approach or a spec has been written and approved, it's time to start development. There are two flows you can follow depending on the proposed feature or fix. + +If you're feature (or module) has not yet been created, follow these steps: + +1. Fork the repository if you haven't already. +2. Clone your fork locally. +3. Dot-source the `New-DscResourceModule.ps` in your PowerShell session. +4. Create a new module scaffolding by executing: `New-DscResourceModule -DscResourceModule '' -Description 'DSC Resource for '` +5. Work on your changes and write tests. +6. Build and test to see if it works. +7. Create & push a feature branch. +8. Create a [Draft Pull Request (PR)](https://github.blog/2019-02-14-introducing-draft-pull-requests/). +9. If you are finished with your changes and you want a review, change the state. + +> [!TIP] +> Don't forget to add the `DscResourcesToExport` and `Tags`. + +When you are working on a fix or you want to add additional features to an existing module, you can follow the below steps: 1. Fork the repository if you haven't already. -1. Clone your fork locally. -1. Create & push a feature branch. -1. Create a [Draft Pull Request (PR)](https://github.blog/2019-02-14-introducing-draft-pull-requests/). -1. Work on your changes. -1. Build and see if it works. +2. Clone your fork locally. +3. Work on your fix or feature, and _optionally_ write tests +4. Build and test to see if it works. +5. Create & push a feature branch. +6. Create a [Pull Request (PR)](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/creating-a-pull-request) when you are finished with your changes ### Testing @@ -126,7 +143,8 @@ Testing is a key component in the development workflow. When you'd like the team to take a look, (even if the work is not yet fully-complete), mark the Draft PR as 'Ready For Review' so that the team can review your work and provide comments, suggestions, and request changes. It may take several cycles, but the end result will be solid, testable, conformant code that is safe for us to merge. -> ⚠ Remember: **changes you make may affect both the Windows Package Manager and the schema support implemented in our validation pipelines!** Because of this, we will treat community PR's with the same level of scrutiny and rigor as commits submitted to the official Windows source by team members and partners. +> [!CAUTION] +> Remember: **changes you make may affect both the Windows Package Manager and the schema support implemented in our validation pipelines!** Because of this, we will treat community PR's with the same level of scrutiny and rigor as commits submitted to the official Windows source by team members and partners. ### Merge diff --git a/utilities/scripts/New-DscResourceModule.ps1 b/utilities/scripts/New-DscResourceModule.ps1 new file mode 100644 index 00000000..6e7c9511 --- /dev/null +++ b/utilities/scripts/New-DscResourceModule.ps1 @@ -0,0 +1,106 @@ +function New-DscResourceModule { + <# + .SYNOPSIS + Creates a new DSC (Desired State Configuration) resource module structure. + + .DESCRIPTION + The function New-DscResourceModule function creates a new DSC resource module structure with the specified name and description. + It sets up the necessary directory structure for resources and tests within the given base path. + + .PARAMETER DscResourceModule + The name of the DSC resource module to create. + + .PARAMETER Description + A description of the DSC resource module. + + .PARAMETER BasePath + The base path where the DSC resource module structure will be created. The default value is the parent directory of the script. + + .EXAMPLE + PS C:\> New-DscResourceModule -DscResourceModule 'Microsoft.Windows.Language' -Description 'DSC Resource for Windows Language' + + This command creates a new DSC resource module named 'Microsoft.Windows.Language' with the specified description in the default base path. + #> + [CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'Low')] + [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingPositionalParameters', '', Justification = 'Positional parameters are used for simplicity. Targeting PS 7+')] + param ( + [Parameter(Mandatory)] + [string]$DscResourceModule, + + [Parameter(Mandatory)] + [string]$Description, + + [Parameter()] + [string]$BasePath = (Join-Path $PSScriptRoot '..' '..'), + + [Parameter()] + [ValidateNotNullOrEmpty()] + [string[]] $DscResourceToExport + + ) + + $resourcePath = Join-Path $BasePath 'resources' $DscResourceModule + $testsPath = Join-Path $BasePath 'tests' $DscResourceModule + + # Create directories if they do not exist + if (-not (Test-Path -Path $resourcePath)) { + Write-Verbose -Message "Creating directory: $resourcePath" + $null = New-Item -ItemType Directory -Path $resourcePath -Force + } + + if (-not (Test-Path -Path $testsPath)) { + Write-Verbose -Message "Creating test directory: $testsPath" + $null = New-Item -ItemType Directory -Path $testsPath -Force + } + + $moduleManifestPath = (Join-Path $BasePath 'resources' $DscResourceModule "$DscResourceModule.psd1") + + $moduleManifestParams = @{ + Path = $moduleManifestPath + RootModule = "$DscResourceModule.psm1" + ModuleVersion = '0.1.0' + Author = 'Microsoft Corporation' + CompanyName = 'Microsoft Corporation' + Copyright = '(c) Microsoft Corporation. All rights reserved.' + Description = $Description + PowerShellVersion = '7.2' + FunctionsToExport = @('test') + CmdletsToExport = @() + VariablesToExport = @() + AliasesToExport = @() + } + + if ($DscResourceToExport) { + # New module manifest does not properly handle arrays of strings :( + $moduleManifestParams.Add('DscResourcesToExport', @($DscResourceToExport)) + } + + if (-not (Test-Path $moduleManifestPath)) { + if ($PSCmdlet.ShouldProcess($moduleManifestPath, 'Create module manifest')) { + Write-Verbose -Message ($moduleManifestParams | ConvertTo-Json -Depth 10 | Out-String) + New-ModuleManifest @moduleManifestParams + } + + # Workaround for issue: https://github.com/PowerShell/PowerShell/issues/5922 + $fileContent = Get-Content $moduleManifestPath + $newLicenseUri = "LicenseUri = 'https://github.com/microsoft/winget-dsc/blob/main/LICENSE'" + $fileContent = $fileContent -replace '# LicenseUri = ''''', $newLicenseUri + $newProjectUri = "ProjectUri = 'https://github.com/microsoft/winget-dsc'" + $fileContent = $fileContent -replace '# ProjectUri = ''''', $newProjectUri + $newPrerelease = "Prerelease = 'alpha'" + $fileContent = $fileContent -replace '# Prerelease = ''''', $newPrerelease + # TODO: Add tags + + Set-Content -Path $moduleManifestPath -Value $fileContent + } + + $psm1Path = Join-Path -Path $resourcePath -ChildPath "$DscResourceModule.psm1" + if (-not (Test-Path $psm1Path)) { + $null = New-Item -ItemType File -Path $psm1Path -Force + } + + $testsFilePath = Join-Path -Path $testsPath -ChildPath "$DscResourceModule.Tests.ps1" + if (-not (Test-Path $testsFilePath)) { + $null = New-Item -ItemType File -Path $testsFilePath -Force + } +} From c6a34d859819f0c7ff033f01a7b5f7d4977c0554 Mon Sep 17 00:00:00 2001 From: Gijs Reijn Date: Thu, 7 Nov 2024 04:37:46 +0100 Subject: [PATCH 2/8] Fix spelling --- .../actions/spelling/expect/generic_terms.txt | 1 + .../Microsoft.DotNet.Dsc.Tests.ps1 | 238 ---------- .../Microsoft.VSCode.Dsc.Tests.ps1 | 101 ----- .../Microsoft.Windows.Developer.Tests.ps1 | 176 -------- ...ft.Windows.Setting.Accessibility.Tests.ps1 | 406 ------------------ tests/PythonPip3Dsc/PythonPip3Dsc.Tests.ps1 | 106 ----- 6 files changed, 1 insertion(+), 1027 deletions(-) delete mode 100644 tests/Microsoft.DotNet.Dsc/Microsoft.DotNet.Dsc.Tests.ps1 delete mode 100644 tests/Microsoft.VSCode.Dsc/Microsoft.VSCode.Dsc.Tests.ps1 delete mode 100644 tests/Microsoft.Windows.Developer/Microsoft.Windows.Developer.Tests.ps1 delete mode 100644 tests/Microsoft.Windows.Setting.Accessibility/Microsoft.Windows.Setting.Accessibility.Tests.ps1 delete mode 100644 tests/PythonPip3Dsc/PythonPip3Dsc.Tests.ps1 diff --git a/.github/actions/spelling/expect/generic_terms.txt b/.github/actions/spelling/expect/generic_terms.txt index 5a88fd33..8a799bed 100644 --- a/.github/actions/spelling/expect/generic_terms.txt +++ b/.github/actions/spelling/expect/generic_terms.txt @@ -12,3 +12,4 @@ worktree sortby msft automerge +Workaround diff --git a/tests/Microsoft.DotNet.Dsc/Microsoft.DotNet.Dsc.Tests.ps1 b/tests/Microsoft.DotNet.Dsc/Microsoft.DotNet.Dsc.Tests.ps1 deleted file mode 100644 index 620dea67..00000000 --- a/tests/Microsoft.DotNet.Dsc/Microsoft.DotNet.Dsc.Tests.ps1 +++ /dev/null @@ -1,238 +0,0 @@ -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. -using module Microsoft.DotNet.Dsc - -$ErrorActionPreference = 'Stop' -Set-StrictMode -Version Latest - -<# -.Synopsis - Pester tests related to the Microsoft.DotNet.Dsc PowerShell module. -#> - -BeforeAll { - Install-Module -Name PSDesiredStateConfiguration -Force -SkipPublisherCheck - Import-Module Microsoft.DotNet.Dsc - - $script:toolsDir = Join-Path $env:USERPROFILE 'tools' - - if (-not (Test-Path $toolsDir)) { - $null = New-Item -ItemType Directory -Path $toolsDir -Force -ErrorAction SilentlyContinue - } -} - -Describe 'List available DSC resources' { - It 'Shows DSC Resources' { - $expectedDSCResources = 'DotNetToolPackage' - $availableDSCResources = (Get-DscResource -Module Microsoft.DotNet.Dsc).Name - $availableDSCResources.count | Should -Be 1 - $availableDSCResources | Where-Object { $expectedDSCResources -notcontains $_ } | Should -BeNullOrEmpty -ErrorAction Stop - } -} - -Describe 'DSC operation capabilities' { - It 'Sets desired package' -Skip:(!$IsWindows) { - $parameters = @{ - PackageId = 'gitversion.tool' - } - - Invoke-DscResource -Name DotNetToolPackage -ModuleName Microsoft.DotNet.Dsc -Method Set -Property $parameters - - $finalState = Invoke-DscResource -Name DotNetToolPackage -ModuleName Microsoft.DotNet.Dsc -Method Get -Property $parameters - $finalState.Exist | Should -BeTrue - $finalState.Version | Should -Not -BeNullOrEmpty - } - - It 'Sets desired package with prerelease' -Skip:(!$IsWindows) { - $parameters = @{ - PackageId = 'dotnet-ef' - PreRelease = $true - } - - Invoke-DscResource -Name DotNetToolPackage -ModuleName Microsoft.DotNet.Dsc -Method Set -Property $parameters - - $finalState = Invoke-DscResource -Name DotNetToolPackage -ModuleName Microsoft.DotNet.Dsc -Method Get -Property $parameters - $finalState.PackageId | Should -Be $parameters.PackageId - $finalState.PreRelease | Should -BeTrue - } - - It 'Sets desired package with version' -Skip:(!$IsWindows) { - $parameters = @{ - PackageId = 'dotnet-reportgenerator-globaltool' - Version = '5.3.9' - } - - Invoke-DscResource -Name DotNetToolPackage -ModuleName Microsoft.DotNet.Dsc -Method Set -Property $parameters - - $finalState = Invoke-DscResource -Name DotNetToolPackage -ModuleName Microsoft.DotNet.Dsc -Method Get -Property $parameters - $finalState.PackageId | Should -Be $parameters.PackageId - $finalState.Version | Should -Be $parameters.Version - } - - It 'Updates desired package with latest version' -Skip:(!$IsWindows) { - $parameters = @{ - PackageId = 'dotnet-reportgenerator-globaltool' - Version = '5.3.10' - } - - Invoke-DscResource -Name DotNetToolPackage -ModuleName Microsoft.DotNet.Dsc -Method Set -Property $parameters - - $finalState = Invoke-DscResource -Name DotNetToolPackage -ModuleName Microsoft.DotNet.Dsc -Method Get -Property $parameters - $finalState.PackageId | Should -Be $parameters.PackageId - $finalState.Version | Should -Be $parameters.Version - } - - It 'Sets desired package with prerelease version' -Skip:(!$IsWindows) { - $parameters = @{ - PackageId = 'PowerShell' - Version = '7.2.0-preview.5' - } - - Invoke-DscResource -Name DotNetToolPackage -ModuleName Microsoft.DotNet.Dsc -Method Set -Property $parameters - - $finalState = Invoke-DscResource -Name DotNetToolPackage -ModuleName Microsoft.DotNet.Dsc -Method Get -Property $parameters - $finalState.PackageId | Should -Be $parameters.PackageId - $finalState.Version | Should -Be $parameters.Version - $finalState.PreRelease | Should -BeTrue - } - - It 'Exports resources' -Skip:(!$IsWindows) { - $obj = [DotNetToolPackage]::Export() - - $obj.PackageId.Contains('dotnet-ef') | Should -Be $true - $obj.PackageId.Contains('dotnet-reportgenerator-globaltool') | Should -Be $true - } - - It 'Throws error when resource is not a tool' -Skip:(!$IsWindows) { - $parameters = @{ - PackageId = 'Azure-Core' # not a tool - } - - { Invoke-DscResource -Name DotNetToolPackage -ModuleName Microsoft.DotNet.Dsc -Method Set -Property $parameters } | Should -Throw -ExpectedMessage 'Executing dotnet.exe with {tool install Azure-Core --global --ignore-failed-sources} failed.' - } - - It 'Installs in tool path location with version' -Skip:(!$IsWindows) { - $parameters = @{ - PackageId = 'dotnet-dump' - ToolPathDirectory = $toolsDir - Version = '8.0.532401' - } - - Invoke-DscResource -Name DotNetToolPackage -ModuleName Microsoft.DotNet.Dsc -Method Set -Property $parameters - - $state = Invoke-DscResource -Name DotNetToolPackage -ModuleName Microsoft.DotNet.Dsc -Method Get -Property $parameters - $state.Exist | Should -BeTrue - $state.ToolPathDirectory | Should -Be $parameters.ToolPathDirectory - $state::InstalledPackages[$parameters.PackageId].ToolPathDirectory | Should -Be $parameters.ToolPathDirectory # It should reflect updated export() - } - - # TODO: Work on update scenario - It 'Update in tool path location' -Skip:(!$IsWindows) { - $parameters = @{ - PackageId = 'dotnet-dump' - ToolPathDirectory = $toolsDir - Version = '8.0.547301' - } - - Invoke-DscResource -Name DotNetToolPackage -ModuleName Microsoft.DotNet.Dsc -Method Set -Property $parameters - - $state = Invoke-DscResource -Name DotNetToolPackage -ModuleName Microsoft.DotNet.Dsc -Method Get -Property $parameters - $state.Exist | Should -BeTrue - $state.ToolPathDirectory | Should -Be $parameters.ToolPathDirectory - $state::InstalledPackages[$parameters.PackageId].ToolPathDirectory | Should -Be $parameters.ToolPathDirectory # It should reflect updated export() - } - - It 'Uninstall a tool' -Skip:(!$IsWindows) { - $parameters = @{ - PackageId = 'gitversion.tool' - Exist = $false - } - - Invoke-DscResource -Name DotNetToolPackage -ModuleName Microsoft.DotNet.Dsc -Method Set -Property $parameters - - $state = Invoke-DscResource -Name DotNetToolPackage -ModuleName Microsoft.DotNet.Dsc -Method Get -Property $parameters - $state.Exist | Should -BeFalse - } - - It 'Uninstall a tool from tool path location' -Skip:(!$IsWindows) { - $parameters = @{ - PackageId = 'dotnet-dump' - ToolPathDirectory = $toolsDir - Exist = $false - } - - Invoke-DscResource -Name DotNetToolPackage -ModuleName Microsoft.DotNet.Dsc -Method Set -Property $parameters - - $state = Invoke-DscResource -Name DotNetToolPackage -ModuleName Microsoft.DotNet.Dsc -Method Get -Property $parameters - $state.Exist | Should -BeFalse - } - - It 'Downgrades a tool' -Skip:(!$IsWindows) { - $parameters = @{ - PackageId = 'gitversion.tool' # should install latest version - } - - Invoke-DscResource -Name DotNetToolPackage -ModuleName Microsoft.DotNet.Dsc -Method Set -Property $parameters - - $parameters = @{ - PackageId = 'gitversion.tool' - Version = '6.0.2' - } - - Invoke-DscResource -Name DotNetToolPackage -ModuleName Microsoft.DotNet.Dsc -Method Set -Property $parameters - $state = Invoke-DscResource -Name DotNetToolPackage -ModuleName Microsoft.DotNet.Dsc -Method Get -Property $parameters - $state.Version | Should -Be $parameters.Version - } -} - -Describe 'DSC helper functions' { - Context 'Semantic Versioning' { - It 'Parses valid semantic version' { - $version = '1.2.3' - $result = Get-SemVer -version $version - $result.Major | Should -Be 1 - $result.Minor | Should -Be 2 - $result.Build | Should -Be 3 - } - - It 'Parses semantic version with alpha' { - $version = '1.2.3-alpha' - $result = Get-SemVer -version $version - $result.Major | Should -Be 1 - $result.Minor | Should -Be 2 - $result.Build | Should -Be 3 - $result.Revision | Should -Be 0 # Because pre-release is not a number according the handling - } - - It 'Parses semantic version with alpha tag and version' { - $version = '1.2.3-alpha.123' - $result = Get-SemVer -version $version - $result.Major | Should -Be 1 - $result.Minor | Should -Be 2 - $result.Build | Should -Be 3 - $result.Revision | Should -Be '123' - } - - It 'Parses semantic version with beta tag and version' { - $version = '1.2.3-beta.11' - $result = Get-SemVer -version $version - $result.Major | Should -Be 1 - $result.Minor | Should -Be 2 - $result.Build | Should -Be 3 - $result.Revision | Should -Be '11' - } - - It 'Parses semantic version with rc and version' { - $version = '1.2.3-rc.1' - $result = Get-SemVer -version $version - $result.Major | Should -Be 1 - $result.Minor | Should -Be 2 - $result.Build | Should -Be 3 - $result.Revision | Should -Be '1' - } - } -} - -AfterAll { - Remove-Item -Path $toolsDir -Recurse -Force -ErrorAction SilentlyContinue -} diff --git a/tests/Microsoft.VSCode.Dsc/Microsoft.VSCode.Dsc.Tests.ps1 b/tests/Microsoft.VSCode.Dsc/Microsoft.VSCode.Dsc.Tests.ps1 deleted file mode 100644 index 118cbe5a..00000000 --- a/tests/Microsoft.VSCode.Dsc/Microsoft.VSCode.Dsc.Tests.ps1 +++ /dev/null @@ -1,101 +0,0 @@ -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. -using module Microsoft.VSCode.Dsc - -$ErrorActionPreference = 'Stop' -Set-StrictMode -Version Latest - -<# -.Synopsis - Pester tests related to the Microsoft.VSCode.Dsc PowerShell module. -#> - -BeforeAll { - Install-Module -Name PSDesiredStateConfiguration -Force -SkipPublisherCheck - Import-Module Microsoft.VSCode.Dsc - - # Install VSCode - Invoke-WebRequest https://raw.githubusercontent.com/PowerShell/vscode-powershell/main/scripts/Install-VSCode.ps1 -UseBasicParsing -OutFile Install-VSCode.ps1 - .\Install-VSCode.ps1 -BuildEdition Stable-User -Confirm:$false - - # Install VSCode Insiders - .\Install-VSCode.ps1 -BuildEdition Insider-User -Confirm:$false -} - -Describe 'List available DSC resources' { - It 'Shows DSC Resources' { - $expectedDSCResources = 'VSCodeExtension' - $availableDSCResources = (Get-DscResource -Module Microsoft.VSCode.Dsc).Name - $availableDSCResources.count | Should -Be 1 - $availableDSCResources | Where-Object { $expectedDSCResources -notcontains $_ } | Should -BeNullOrEmpty -ErrorAction Stop - } -} - -Describe 'VSCodeExtension' { - It 'Keeps current extension.' -Skip:(!$IsWindows) { - $parameters = @{ - Name = 'ms-vscode.powershell' - } - $initialState = Invoke-DscResource -Name VSCodeExtension -ModuleName Microsoft.VSCode.Dsc -Method Get -Property $parameters - - $testResult = Invoke-DscResource -Name VSCodeExtension -ModuleName Microsoft.VSCode.Dsc -Method Test -Property $parameters - $testResult.InDesiredState | Should -Be $true - - # Invoking set should not change these values. - Invoke-DscResource -Name VSCodeExtension -ModuleName Microsoft.VSCode.Dsc -Method Set -Property $parameters - $finalState = Invoke-DscResource -Name VSCodeExtension -ModuleName Microsoft.VSCode.Dsc -Method Get -Property $parameters - $finalState.Name | Should -Be $initialState.Name - $finalState.Version | Should -Be $initialState.Version - $finalState.Exist | Should -Be $initialState.Exist - } - - It 'Sets desired extension' -Skip:(!$IsWindows) { - $desiredState = @{ - Name = 'ms-azure-devops.azure-pipelines' - } - - Invoke-DscResource -Name VSCodeExtension -ModuleName Microsoft.VSCode.Dsc -Method Set -Property $desiredState - - $finalState = Invoke-DscResource -Name VSCodeExtension -ModuleName Microsoft.VSCode.Dsc -Method Get -Property $desiredState - $finalState.Name | Should -Be $desiredState.Name - $finalState.Exist | Should -BeTrue - } -} - -Describe 'VSCodeExtension-Insiders' { - It 'Keeps current extension in Insiders edition.' -Skip:(!$IsWindows) { - $parameters = @{ - Name = 'ms-vscode.powershell' - Insiders = $true - } - - $initialState = Invoke-DscResource -Name VSCodeExtension -ModuleName Microsoft.VSCode.Dsc -Method Get -Property $parameters - - $testResult = Invoke-DscResource -Name VSCodeExtension -ModuleName Microsoft.VSCode.Dsc -Method Test -Property $parameters - $testResult.InDesiredState | Should -Be $true - - # Invoking set should not change these values. - Invoke-DscResource -Name VSCodeExtension -ModuleName Microsoft.VSCode.Dsc -Method Set -Property $parameters - $finalState = Invoke-DscResource -Name VSCodeExtension -ModuleName Microsoft.VSCode.Dsc -Method Get -Property $parameters - $finalState.Name | Should -Be $initialState.Name - $finalState.Version | Should -Be $initialState.Version - $finalState.Exist | Should -Be $initialState.Exist - } - - It 'Sets desired extension in Insiders edition' -Skip:(!$IsWindows) { - $desiredState = @{ - Name = 'ms-azure-devops.azure-pipelines' - Insiders = $true - } - - Invoke-DscResource -Name VSCodeExtension -ModuleName Microsoft.VSCode.Dsc -Method Set -Property $desiredState - - $finalState = Invoke-DscResource -Name VSCodeExtension -ModuleName Microsoft.VSCode.Dsc -Method Get -Property $desiredState - $finalState.Name | Should -Be $desiredState.Name - $finalState.Exist | Should -BeTrue - } -} - -AfterAll { - # Uninstall VSCode? -} diff --git a/tests/Microsoft.Windows.Developer/Microsoft.Windows.Developer.Tests.ps1 b/tests/Microsoft.Windows.Developer/Microsoft.Windows.Developer.Tests.ps1 deleted file mode 100644 index c72b4253..00000000 --- a/tests/Microsoft.Windows.Developer/Microsoft.Windows.Developer.Tests.ps1 +++ /dev/null @@ -1,176 +0,0 @@ -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. -using module Microsoft.Windows.Developer - -$ErrorActionPreference = "Stop" -Set-StrictMode -Version Latest - -<# -.Synopsis - Pester tests related to the Microsoft.WinGet.Developer PowerShell module. -#> - -BeforeAll { - Install-Module -Name PSDesiredStateConfiguration -Force -SkipPublisherCheck - Import-Module Microsoft.Windows.Developer - - # Create test registry path. - New-Item -Path TestRegistry:\ -Name TestKey - # Set-ItemProperty requires the PSDrive to be in the format 'HKCU:'. - $env:TestRegistryPath = ((Get-Item -Path TestRegistry:\).Name).replace("HKEY_CURRENT_USER", "HKCU:") -} - -Describe 'List available DSC resources' { - It 'Shows DSC Resources' { - $expectedDSCResources = "DeveloperMode", "OsVersion", "ShowSecondsInClock", "EnableDarkMode", "Taskbar", "UserAccessControl", "WindowsExplorer", "EnableRemoteDesktop" - $availableDSCResources = (Get-DscResource -Module Microsoft.Windows.Developer).Name - $availableDSCResources.length | Should -Be 8 - $availableDSCResources | Where-Object { $expectedDSCResources -notcontains $_ } | Should -BeNullOrEmpty -ErrorAction Stop - } -} - -Describe 'Taskbar' { - It 'Keeps current value.' { - $initialState = Invoke-DscResource -Name Taskbar -ModuleName Microsoft.Windows.Developer -Method Get -Property @{} - - $parameters = @{ - Alignment = 'KeepCurrentValue'; - HideLabelsMode = 'KeepCurrentValue'; - SearchboxMode = 'KeepCurrentValue'; - TaskViewButton = 'KeepCurrentValue'; - WidgetsButton = 'KeepCurrentValue' - } - - $testResult = Invoke-DscResource -Name Taskbar -ModuleName Microsoft.Windows.Developer -Method Test -Property $parameters - $testResult.InDesiredState | Should -Be $true - - # Invoking set should not change these values. - Invoke-DscResource -Name Taskbar -ModuleName Microsoft.Windows.Developer -Method Set -Property $parameters - $finalState = Invoke-DscResource -Name Taskbar -ModuleName Microsoft.Windows.Developer -Method Get -Property @{} - $finalState.Alignment | Should -Be $initialState.Alignment - $finalState.HideLabelsMode | Should -Be $initialState.HideLabelsMode - $finalState.SearchboxMode | Should -Be $initialState.SearchboxMode - $finalState.TaskViewButton | Should -Be $initialState.WidgetsButton - } - - It 'Sets desired value' { - # Randomly generate desired state. Minimum is set to 1 to avoid KeepCurrentValue - $desiredAlignment = [Alignment](Get-Random -Maximum 3 -Minimum 1) - $desiredHideLabelsMode = [HideTaskBarLabelsBehavior](Get-Random -Maximum 4 -Minimum 1) - $desiredSearchboxMode = [SearchBoxMode](Get-Random -Maximum 5 -Minimum 1) - $desiredTaskViewButton = [ShowHideFeature](Get-Random -Maximum 3 -Minimum 1) - $desiredWidgetsButton = [ShowHideFeature](Get-Random -Maximum 3 -Minimum 1) - - $desiredState = @{ Alignment = $desiredAlignment; - HideLabelsMode = $desiredHideLabelsMode; - SearchboxMode = $desiredSearchboxMode; - TaskViewButton = $desiredTaskViewButton; - WidgetsButton = $desiredWidgetsButton - } - - Invoke-DscResource -Name Taskbar -ModuleName Microsoft.Windows.Developer -Method Set -Property $desiredState - - $finalState = Invoke-DscResource -Name Taskbar -ModuleName Microsoft.Windows.Developer -Method Get -Property @{} - $finalState.Alignment | Should -Be $desiredAlignment - $finalState.HideLabelsMode | Should -Be $desiredHideLabelsMode - $finalState.SearchboxMode | Should -Be $desiredSearchboxMode - $finalState.TaskViewButton | Should -Be $desiredTaskViewButton - $finalState.WidgetsButton | Should -Be $desiredWidgetsButton - } -} - -Describe 'WindowsExplorer' { - It 'Keeps current value.' { - $initialState = Invoke-DscResource -Name WindowsExplorer -ModuleName Microsoft.Windows.Developer -Method Get -Property @{} - - $parameters = @{ - FileExtensions = 'KeepCurrentValue'; - HiddenFiles = 'KeepCurrentValue'; - ItemCheckBoxes = 'KeepCurrentValue' - } - - $testResult = Invoke-DscResource -Name WindowsExplorer -ModuleName Microsoft.Windows.Developer -Method Test -Property $parameters - $testResult.InDesiredState | Should -Be $true - - # Invoking set should not change these values. - Invoke-DscResource -Name WindowsExplorer -ModuleName Microsoft.Windows.Developer -Method Set -Property $parameters - $finalState = Invoke-DscResource -Name WindowsExplorer -ModuleName Microsoft.Windows.Developer -Method Get -Property @{} - $finalState.FileExtensions | Should -Be $initialState.FileExtensions - $finalState.HiddenFiles | Should -Be $initialState.HiddenFiles - $finalState.ItemCheckBoxes | Should -Be $initialState.ItemCheckBoxes - } - - It 'Sets desired value' { - # Randomly generate desired state. Minimum is set to 1 to avoid using KeepCurrentValue - $desiredFileExtensions = [ShowHideFeature](Get-Random -Maximum 3 -Minimum 1) - $desiredHiddenFiles = [ShowHideFeature](Get-Random -Maximum 3 -Minimum 1) - $desiredItemCheckBoxes = [ShowHideFeature](Get-Random -Maximum 3 -Minimum 1) - - $desiredState = @{ - FileExtensions = $desiredFileExtensions; - HiddenFiles = $desiredHiddenFiles; - ItemCheckBoxes = $desiredItemCheckBoxes - } - - Invoke-DscResource -Name WindowsExplorer -ModuleName Microsoft.Windows.Developer -Method Set -Property $desiredState - - $finalState = Invoke-DscResource -Name WindowsExplorer -ModuleName Microsoft.Windows.Developer -Method Get -Property @{} - $finalState.FileExtensions | Should -Be $desiredFileExtensions - $finalState.HiddenFiles | Should -Be $desiredHiddenFiles - $finalState.ItemCheckBoxes | Should -Be $desiredItemCheckBoxes - } -} - -Describe 'UserAccessControl' { - It 'Keeps current value.' { - $initialState = Invoke-DscResource -Name UserAccessControl -ModuleName Microsoft.Windows.Developer -Method Get -Property @{} - - $parameters = @{ AdminConsentPromptBehavior = 'KeepCurrentValue' } - - $testResult = Invoke-DscResource -Name UserAccessControl -ModuleName Microsoft.Windows.Developer -Method Test -Property $parameters - $testResult.InDesiredState | Should -Be $true - - # Invoking set should not change these values. - Invoke-DscResource -Name UserAccessControl -ModuleName Microsoft.Windows.Developer -Method Set -Property $parameters - $finalState = Invoke-DscResource -Name UserAccessControl -ModuleName Microsoft.Windows.Developer -Method Get -Property @{} - $finalState.AdminConsentPromptBehavior | Should -Be $initialState.AdminConsentPromptBehavior - } - - It 'Sets desired value.' { - # Randomly generate desired state. Minimum is set to 1 to avoid using KeepCurrentValue - $desiredAdminConsentPromptBehavior = [AdminConsentPromptBehavior](Get-Random -Maximum 6 -Minimum 1) - - $desiredState = @{ AdminConsentPromptBehavior = $desiredAdminConsentPromptBehavior } - - Invoke-DscResource -Name UserAccessControl -ModuleName Microsoft.Windows.Developer -Method Set -Property $desiredState - - $finalState = Invoke-DscResource -Name UserAccessControl -ModuleName Microsoft.Windows.Developer -Method Get -Property @{} - $finalState.AdminConsentPromptBehavior | Should -Be $desiredAdminConsentPromptBehavior - } -} - -Describe 'EnableRemoteDesktop' { - It 'Sets Enabled' { - $desiredRemoteDesktopBehavior = [Ensure]::Present - $desiredState = @{ Ensure = $desiredRemoteDesktopBehavior } - - Invoke-DscResource -Name EnableRemoteDesktop -ModuleName Microsoft.Windows.Developer -Method Set -Property $desiredState - - $finalState = Invoke-DscResource -Name EnableRemoteDesktop -ModuleName Microsoft.Windows.Developer -Method Get -Property @{} - $finalState.Ensure | Should -Be $desiredRemoteDesktopBehavior - } - - It 'Sets Disabled' { - $desiredRemoteDesktopBehavior = [Ensure]::Absent - $desiredState = @{ Ensure = $desiredRemoteDesktopBehavior } - - Invoke-DscResource -Name EnableRemoteDesktop -ModuleName Microsoft.Windows.Developer -Method Set -Property $desiredState - - $finalState = Invoke-DscResource -Name EnableRemoteDesktop -ModuleName Microsoft.Windows.Developer -Method Get -Property @{} - $finalState.Ensure | Should -Be $desiredRemoteDesktopBehavior - } -} - -AfterAll { - $env:TestRegistryPath = "" -} diff --git a/tests/Microsoft.Windows.Setting.Accessibility/Microsoft.Windows.Setting.Accessibility.Tests.ps1 b/tests/Microsoft.Windows.Setting.Accessibility/Microsoft.Windows.Setting.Accessibility.Tests.ps1 deleted file mode 100644 index dd22de6d..00000000 --- a/tests/Microsoft.Windows.Setting.Accessibility/Microsoft.Windows.Setting.Accessibility.Tests.ps1 +++ /dev/null @@ -1,406 +0,0 @@ - -# Copyright (c) Microsoft Corporation. All rights reserved. -# Licensed under the MIT License. -using module Microsoft.Windows.Setting.Accessibility - -$ErrorActionPreference = 'Stop' -Set-StrictMode -Version Latest - -<# -.Synopsis - Pester tests related to the Microsoft.Windows.Setting.Accessibility PowerShell module. -#> - -BeforeAll { - if ($null -eq (Get-Module -ListAvailable -Name PSDesiredStateConfiguration)) { - Install-Module -Name PSDesiredStateConfiguration -Force -SkipPublisherCheck - } - - Import-Module Microsoft.Windows.Setting.Accessibility - - # Create test registry path. - New-Item -Path TestRegistry:\ -Name TestKey - # Set-ItemProperty requires the PSDrive to be in the format 'HKCU:'. - $env:TestRegistryPath = ((Get-Item -Path TestRegistry:\).Name).replace('HKEY_CURRENT_USER', 'HKCU:') -} - -Describe 'List available DSC resources' { - It 'Shows DSC Resources' { - $expectedDSCResources = 'Text', 'Magnifier', 'MousePointer', 'VisualEffect', 'Audio', 'TextCursor', 'StickyKeys', 'ToggleKeys', 'FilterKeys' - $availableDSCResources = (Get-DscResource -Module Microsoft.Windows.Setting.Accessibility).Name - $availableDSCResources.length | Should -Be 9 - $availableDSCResources | Where-Object { $expectedDSCResources -notcontains $_ } | Should -BeNullOrEmpty -ErrorAction Stop - } -} - -Describe 'Text' { - It 'Keeps current value.' { - $initialState = Invoke-DscResource -Name Text -ModuleName Microsoft.Windows.Setting.Accessibility -Method Get -Property @{} - - $parameters = @{ Size = 'KeepCurrentValue' } - - $testResult = Invoke-DscResource -Name Text -ModuleName Microsoft.Windows.Setting.Accessibility -Method Test -Property $parameters - $testResult.InDesiredState | Should -Be $true - - # Invoking set should not change these values. - Invoke-DscResource -Name Text -ModuleName Microsoft.Windows.Setting.Accessibility -Method Set -Property $parameters - $finalState = Invoke-DscResource -Name Text -ModuleName Microsoft.Windows.Setting.Accessibility -Method Get -Property @{} - $finalState.Size | Should -Be $initialState.Size - } - - It 'Sets desired value' { - # Randomly generate desired state. Minimum is set to 1 to avoid KeepCurrentValue - $desiredTextSize = [TextSize](Get-Random -Maximum 4 -Minimum 1) - - $desiredState = @{ Size = $desiredTextSize } - - Invoke-DscResource -Name Text -ModuleName Microsoft.Windows.Setting.Accessibility -Method Set -Property $desiredState - - $finalState = Invoke-DscResource -Name Text -ModuleName Microsoft.Windows.Setting.Accessibility -Method Get -Property @{} - $finalState.Size | Should -Be $desiredTextSize - } -} - -Describe 'Magnifier' { - It 'Keeps current value.' { - $initialState = Invoke-DscResource -Name Magnifier -ModuleName Microsoft.Windows.Setting.Accessibility -Method Get -Property @{} - - $parameters = @{ Magnification = 'KeepCurrentValue' } - - $testResult = Invoke-DscResource -Name Magnifier -ModuleName Microsoft.Windows.Setting.Accessibility -Method Test -Property $parameters - $testResult.InDesiredState | Should -Be $true - - # Invoking set should not change these values. - Invoke-DscResource -Name Magnifier -ModuleName Microsoft.Windows.Setting.Accessibility -Method Set -Property $parameters - $finalState = Invoke-DscResource -Name Magnifier -ModuleName Microsoft.Windows.Setting.Accessibility -Method Get -Property @{} - $finalState.Magnification | Should -Be $initialState.Magnification - } - - It 'Sets desired value' { - # Randomly generate desired state. Minimum is set to 1 to avoid KeepCurrentValue - $desiredMagnification = [MagnificationValue](Get-Random -Maximum 4 -Minimum 1) - - $desiredState = @{ Magnification = $desiredMagnification } - - Invoke-DscResource -Name Magnifier -ModuleName Microsoft.Windows.Setting.Accessibility -Method Set -Property $desiredState - - $finalState = Invoke-DscResource -Name Magnifier -ModuleName Microsoft.Windows.Setting.Accessibility -Method Get -Property @{} - $finalState.Magnification | Should -Be $desiredMagnification - } -} - -Describe 'MousePointer' { - It 'Keeps current value.' { - $initialState = Invoke-DscResource -Name MousePointer -ModuleName Microsoft.Windows.Setting.Accessibility -Method Get -Property @{} - - $parameters = @{ PointerSize = 'KeepCurrentValue' } - - $testResult = Invoke-DscResource -Name MousePointer -ModuleName Microsoft.Windows.Setting.Accessibility -Method Test -Property $parameters - $testResult.InDesiredState | Should -Be $true - - # Invoking set should not change these values. - Invoke-DscResource -Name MousePointer -ModuleName Microsoft.Windows.Setting.Accessibility -Method Set -Property $parameters - $finalState = Invoke-DscResource -Name MousePointer -ModuleName Microsoft.Windows.Setting.Accessibility -Method Get -Property @{} - $finalState.PointerSize | Should -Be $initialState.PointerSize - } - - It 'Sets desired value' { - # Randomly generate desired state. Minimum is set to 1 to avoid KeepCurrentValue - $desiredPointerSize = [PointerSize](Get-Random -Maximum 4 -Minimum 1) - - $desiredState = @{ PointerSize = $desiredPointerSize } - - Invoke-DscResource -Name MousePointer -ModuleName Microsoft.Windows.Setting.Accessibility -Method Set -Property $desiredState - - $finalState = Invoke-DscResource -Name MousePointer -ModuleName Microsoft.Windows.Setting.Accessibility -Method Get -Property @{} - $finalState.PointerSize | Should -Be $desiredPointerSize - } -} - -Describe 'VisualEffect' { - It 'AlwaysShowScrollbars.' { - Invoke-DscResource -Name VisualEffect -ModuleName Microsoft.Windows.Setting.Accessibility -Method Set -Property @{ AlwaysShowScrollbars = $false } - - $initialState = Invoke-DscResource -Name VisualEffect -ModuleName Microsoft.Windows.Setting.Accessibility -Method Get -Property @{} - $initialState.AlwaysShowScrollbars | Should -Be $false - - # Set 'AlwaysShowScrollbars' to true. - $parameters = @{ AlwaysShowScrollbars = $true } - $testResult = Invoke-DscResource -Name VisualEffect -ModuleName Microsoft.Windows.Setting.Accessibility -Method Test -Property $parameters - $testResult.InDesiredState | Should -Be $false - - # Verify the changes are correct. - Invoke-DscResource -Name VisualEffect -ModuleName Microsoft.Windows.Setting.Accessibility -Method Set -Property $parameters - $finalState = Invoke-DscResource -Name VisualEffect -ModuleName Microsoft.Windows.Setting.Accessibility -Method Get -Property @{} - $finalState.AlwaysShowScrollbars | Should -Be $true - - $testResult2 = Invoke-DscResource -Name VisualEffect -ModuleName Microsoft.Windows.Setting.Accessibility -Method Test -Property $parameters - $testResult2.InDesiredState | Should -Be $true - } - It 'TransparencyEffects.' { - Invoke-DscResource -Name VisualEffect -ModuleName Microsoft.Windows.Setting.Accessibility -Method Set -Property @{ TransparencyEffects = $false } - - $initialState = Invoke-DscResource -Name VisualEffect -ModuleName Microsoft.Windows.Setting.Accessibility -Method Get -Property @{} - $initialState.TransparencyEffects | Should -Be $false - - # Set 'TransparencyEffects' to true. - $parameters = @{ TransparencyEffects = $true } - $testResult = Invoke-DscResource -Name VisualEffect -ModuleName Microsoft.Windows.Setting.Accessibility -Method Test -Property $parameters - $testResult.InDesiredState | Should -Be $false - - # Verify the changes are correct. - Invoke-DscResource -Name VisualEffect -ModuleName Microsoft.Windows.Setting.Accessibility -Method Set -Property $parameters - $finalState = Invoke-DscResource -Name VisualEffect -ModuleName Microsoft.Windows.Setting.Accessibility -Method Get -Property @{} - $finalState.TransparencyEffects | Should -Be $true - - $testResult2 = Invoke-DscResource -Name VisualEffect -ModuleName Microsoft.Windows.Setting.Accessibility -Method Test -Property $parameters - $testResult2.InDesiredState | Should -Be $true - } - It 'MessageDuration' { - $firstValue = 5 #Key is missing by default, and default value is 5 when not specified. - $secondValue = 10 - - $initialState = Invoke-DscResource -Name VisualEffect -ModuleName Microsoft.Windows.Setting.Accessibility -Method Get -Property @{} - $initialState.MessageDurationInSeconds | Should -Be $firstValue - - # Set 'MessageDurationInSeconds' to 10. - $parameters = @{ MessageDurationInSeconds = $secondValue } - $testResult = Invoke-DscResource -Name VisualEffect -ModuleName Microsoft.Windows.Setting.Accessibility -Method Test -Property $parameters - $testResult.InDesiredState | Should -Be $false - - # Verify the changes are correct. - Invoke-DscResource -Name VisualEffect -ModuleName Microsoft.Windows.Setting.Accessibility -Method Set -Property $parameters - $finalState = Invoke-DscResource -Name VisualEffect -ModuleName Microsoft.Windows.Setting.Accessibility -Method Get -Property @{} - $finalState.MessageDurationInSeconds | Should -Be $secondValue - - $testResult2 = Invoke-DscResource -Name VisualEffect -ModuleName Microsoft.Windows.Setting.Accessibility -Method Test -Property $parameters - $testResult2.InDesiredState | Should -Be $true - } -} - -Describe 'Audio' { - It 'EnableMonoAudio.' { - Invoke-DscResource -Name Audio -ModuleName Microsoft.Windows.Setting.Accessibility -Method Set -Property @{ EnableMonoAudio = $false } - - $initialState = Invoke-DscResource -Name Audio -ModuleName Microsoft.Windows.Setting.Accessibility -Method Get -Property @{} - $initialState.EnableMonoAudio | Should -Be $false - - # Set 'EnableMonoAudio' to true. - $parameters = @{ EnableMonoAudio = $true } - $testResult = Invoke-DscResource -Name Audio -ModuleName Microsoft.Windows.Setting.Accessibility -Method Test -Property $parameters - $testResult.InDesiredState | Should -Be $false - - # Verify the changes are correct. - Invoke-DscResource -Name Audio -ModuleName Microsoft.Windows.Setting.Accessibility -Method Set -Property $parameters - $finalState = Invoke-DscResource -Name Audio -ModuleName Microsoft.Windows.Setting.Accessibility -Method Get -Property @{} - $finalState.EnableMonoAudio | Should -Be $true - - $testResult2 = Invoke-DscResource -Name Audio -ModuleName Microsoft.Windows.Setting.Accessibility -Method Test -Property $parameters - $testResult2.InDesiredState | Should -Be $true - } -} - -Describe 'TextCursor' { - It 'IndicatorStatus.' { - $initialState = Invoke-DscResource -Name TextCursor -ModuleName Microsoft.Windows.Setting.Accessibility -Method Get -Property @{} - $initialState.IndicatorStatus # Should -Be $false - - # Set 'IndicatorStatus' to true. - $parameters = @{ IndicatorStatus = $true } - $testResult = Invoke-DscResource -Name TextCursor -ModuleName Microsoft.Windows.Setting.Accessibility -Method Test -Property $parameters - $testResult.InDesiredState | Should -Be $false - - # Verify the changes are correct. - Invoke-DscResource -Name TextCursor -ModuleName Microsoft.Windows.Setting.Accessibility -Method Set -Property $parameters - $finalState = Invoke-DscResource -Name TextCursor -ModuleName Microsoft.Windows.Setting.Accessibility -Method Get -Property @{} - $finalState.IndicatorStatus | Should -Be $true - - $testResult2 = Invoke-DscResource -Name TextCursor -ModuleName Microsoft.Windows.Setting.Accessibility -Method Test -Property $parameters - $testResult2.InDesiredState | Should -Be $true - } - It 'IndicatorSize.' { - Invoke-DscResource -Name TextCursor -ModuleName Microsoft.Windows.Setting.Accessibility -Method Set -Property @{ IndicatorSize = 1 } - - $initialState = Invoke-DscResource -Name TextCursor -ModuleName Microsoft.Windows.Setting.Accessibility -Method Get -Property @{} - $initialState.IndicatorSize | Should -Be 1 - - # Set 'IndicatorSize' to 2. - $parameters = @{ IndicatorSize = 2 } - $testResult = Invoke-DscResource -Name TextCursor -ModuleName Microsoft.Windows.Setting.Accessibility -Method Test -Property $parameters - $testResult.InDesiredState | Should -Be $false - - # Verify the changes are correct. - Invoke-DscResource -Name TextCursor -ModuleName Microsoft.Windows.Setting.Accessibility -Method Set -Property $parameters - $finalState = Invoke-DscResource -Name TextCursor -ModuleName Microsoft.Windows.Setting.Accessibility -Method Get -Property @{} - $finalState.IndicatorSize | Should -Be 2 - - $testResult2 = Invoke-DscResource -Name TextCursor -ModuleName Microsoft.Windows.Setting.Accessibility -Method Test -Property $parameters - $testResult2.InDesiredState | Should -Be $true - } - It 'IndicatorColor.' { - Invoke-DscResource -Name TextCursor -ModuleName Microsoft.Windows.Setting.Accessibility -Method Set -Property @{ IndicatorColor = 16711871 } - - $initialState = Invoke-DscResource -Name TextCursor -ModuleName Microsoft.Windows.Setting.Accessibility -Method Get -Property @{} - $initialState.IndicatorColor | Should -Be 16711871 - - # Set 'IndicatorColor' to true. - $parameters = @{ IndicatorColor = 16711872 } #Increment default by 1 - $testResult = Invoke-DscResource -Name TextCursor -ModuleName Microsoft.Windows.Setting.Accessibility -Method Test -Property $parameters - $testResult.InDesiredState | Should -Be $false - - # Verify the changes are correct. - Invoke-DscResource -Name TextCursor -ModuleName Microsoft.Windows.Setting.Accessibility -Method Set -Property $parameters - $finalState = Invoke-DscResource -Name TextCursor -ModuleName Microsoft.Windows.Setting.Accessibility -Method Get -Property @{} - $finalState.IndicatorColor | Should -Be 16711872 - - $testResult2 = Invoke-DscResource -Name TextCursor -ModuleName Microsoft.Windows.Setting.Accessibility -Method Test -Property $parameters - $testResult2.InDesiredState | Should -Be $true - } - It 'Thickness' { #int - $firstValue = 1 #Key is missing by default, and default value is 5 when not specified. - $secondValue = 2 - - $initialState = Invoke-DscResource -Name TextCursor -ModuleName Microsoft.Windows.Setting.Accessibility -Method Get -Property @{} - $initialState.Thickness | Should -Be $firstValue - - # Set 'Thickness' to 2. - $parameters = @{ Thickness = $secondValue } - $testResult = Invoke-DscResource -Name TextCursor -ModuleName Microsoft.Windows.Setting.Accessibility -Method Test -Property $parameters - $testResult.InDesiredState | Should -Be $false - - # Verify the changes are correct. - Invoke-DscResource -Name TextCursor -ModuleName Microsoft.Windows.Setting.Accessibility -Method Set -Property $parameters - $finalState = Invoke-DscResource -Name TextCursor -ModuleName Microsoft.Windows.Setting.Accessibility -Method Get -Property @{} - $finalState.Thickness | Should -Be $secondValue - - $testResult2 = Invoke-DscResource -Name TextCursor -ModuleName Microsoft.Windows.Setting.Accessibility -Method Test -Property $parameters - $testResult2.InDesiredState | Should -Be $true - } -} - -Describe 'StickyKeys' { - It 'Each property can be set' { - # Get a snapshot of the current state - $baselineState = Invoke-DscResource -Name StickyKeys -ModuleName Microsoft.Windows.Setting.Accessibility -Method Get -Property @{} - # Get a list of all the properties that can be changed - $propertyList = $baselineState.PSObject.Properties.Name | Where-Object { $_ -ne 'SID' } - - # Change each property individually and check each time - $propertyList | ForEach-Object { - $property = $_ # This is just for code readability - - # Set the desired state of the property to the opposite of whatever it currently is - $desiredPropertyState = !($baselineState | Select-Object -ExpandProperty $property) - - # Test the desired state against the current state. Since nothing has changed, the system should never be in the desired state - $parameters = @{ $property = $desiredPropertyState } - $testResult = Invoke-DscResource -Name StickyKeys -ModuleName Microsoft.Windows.Setting.Accessibility -Method Test -Property $parameters - $testResult.InDesiredState | Should -Be $false - - # Set the new state and check that it applied - Invoke-DscResource -Name StickyKeys -ModuleName Microsoft.Windows.Setting.Accessibility -Method Set -Property $parameters - $finalState = Invoke-DscResource -Name StickyKeys -ModuleName Microsoft.Windows.Setting.Accessibility -Method Get -Property @{} - $($finalState | Select-Object -ExpandProperty $property) | Should -Be $desiredPropertyState - - # Test the desired state against the current state. Now that the change has been applied, it should always be in the desired state - $testResult2 = Invoke-DscResource -Name StickyKeys -ModuleName Microsoft.Windows.Setting.Accessibility -Method Test -Property $parameters - $testResult2.InDesiredState | Should -Be $true - } - - # Set all properties back to the initial state at once - $parameterList = $baselineState | Select-Object -Property $propertyList - $parameters = @{} # Needs to be a hashtable for setting them all at once - $parameterList.PSObject.Properties | ForEach-Object { $parameters[$_.Name] = $_.Value } - $testResult = Invoke-DscResource -Name StickyKeys -ModuleName Microsoft.Windows.Setting.Accessibility -Method Test -Property $parameters - $testResult.InDesiredState | Should -Be $false # Everything should still be opposite from when each property was changed individually - Invoke-DscResource -Name StickyKeys -ModuleName Microsoft.Windows.Setting.Accessibility -Method Set -Property $parameters - $testResult2 = Invoke-DscResource -Name StickyKeys -ModuleName Microsoft.Windows.Setting.Accessibility -Method Test -Property $parameters - $testResult2.InDesiredState | Should -Be $true - } -} - -Describe 'ToggleKeys' { - It 'Each property can be set' { - # Get a snapshot of the current state - $baselineState = Invoke-DscResource -Name ToggleKeys -ModuleName Microsoft.Windows.Setting.Accessibility -Method Get -Property @{} - # Get a list of all the properties that can be changed - $propertyList = $baselineState.PSObject.Properties.Name | Where-Object { $_ -ne 'SID' } - - # Change each property individually and check each time - $propertyList | ForEach-Object { - $property = $_ # This is just for code readability - - # Set the desired state of the property to the opposite of whatever it currently is - $desiredPropertyState = !($baselineState | Select-Object -ExpandProperty $property) - - # Test the desired state against the current state. Since nothing has changed, the system should never be in the desired state - $parameters = @{ $property = $desiredPropertyState } - $testResult = Invoke-DscResource -Name ToggleKeys -ModuleName Microsoft.Windows.Setting.Accessibility -Method Test -Property $parameters - $testResult.InDesiredState | Should -Be $false - - # Set the new state and check that it applied - Invoke-DscResource -Name ToggleKeys -ModuleName Microsoft.Windows.Setting.Accessibility -Method Set -Property $parameters - $finalState = Invoke-DscResource -Name ToggleKeys -ModuleName Microsoft.Windows.Setting.Accessibility -Method Get -Property @{} - $($finalState | Select-Object -ExpandProperty $property) | Should -Be $desiredPropertyState - - # Test the desired state against the current state. Now that the change has been applied, it should always be in the desired state - $testResult2 = Invoke-DscResource -Name ToggleKeys -ModuleName Microsoft.Windows.Setting.Accessibility -Method Test -Property $parameters - $testResult2.InDesiredState | Should -Be $true - } - - # Set all properties back to the initial state at once - $parameterList = $baselineState | Select-Object -Property $propertyList - $parameters = @{} # Needs to be a hashtable for setting them all at once - $parameterList.PSObject.Properties | ForEach-Object { $parameters[$_.Name] = $_.Value } - $testResult = Invoke-DscResource -Name ToggleKeys -ModuleName Microsoft.Windows.Setting.Accessibility -Method Test -Property $parameters - $testResult.InDesiredState | Should -Be $false # Everything should still be opposite from when each property was changed individually - Invoke-DscResource -Name ToggleKeys -ModuleName Microsoft.Windows.Setting.Accessibility -Method Set -Property $parameters - $testResult2 = Invoke-DscResource -Name ToggleKeys -ModuleName Microsoft.Windows.Setting.Accessibility -Method Test -Property $parameters - $testResult2.InDesiredState | Should -Be $true - } -} - -Describe 'FilterKeys' { - It 'Each property can be set' { - # Get a snapshot of the current state - $baselineState = Invoke-DscResource -Name FilterKeys -ModuleName Microsoft.Windows.Setting.Accessibility -Method Get -Property @{} - # Get a list of all the properties that can be changed - $propertyList = $baselineState.PSObject.Properties.Name | Where-Object { $_ -ne 'SID' } - - # Change each property individually and check each time - $propertyList | ForEach-Object { - $property = $_ # This is just for code readability - - # Set the desired state of the property to the opposite of whatever it currently is - $desiredPropertyState = !($baselineState | Select-Object -ExpandProperty $property) - - # Test the desired state against the current state. Since nothing has changed, the system should never be in the desired state - $parameters = @{ $property = $desiredPropertyState } - $testResult = Invoke-DscResource -Name FilterKeys -ModuleName Microsoft.Windows.Setting.Accessibility -Method Test -Property $parameters - $testResult.InDesiredState | Should -Be $false - - # Set the new state and check that it applied - Invoke-DscResource -Name FilterKeys -ModuleName Microsoft.Windows.Setting.Accessibility -Method Set -Property $parameters - $finalState = Invoke-DscResource -Name FilterKeys -ModuleName Microsoft.Windows.Setting.Accessibility -Method Get -Property @{} - $($finalState | Select-Object -ExpandProperty $property) | Should -Be $desiredPropertyState - - # Test the desired state against the current state. Now that the change has been applied, it should always be in the desired state - $testResult2 = Invoke-DscResource -Name FilterKeys -ModuleName Microsoft.Windows.Setting.Accessibility -Method Test -Property $parameters - $testResult2.InDesiredState | Should -Be $true - } - - # Set all properties back to the initial state at once - $parameterList = $baselineState | Select-Object -Property $propertyList - $parameters = @{} # Needs to be a hashtable for setting them all at once - $parameterList.PSObject.Properties | ForEach-Object { $parameters[$_.Name] = $_.Value } - $testResult = Invoke-DscResource -Name FilterKeys -ModuleName Microsoft.Windows.Setting.Accessibility -Method Test -Property $parameters - $testResult.InDesiredState | Should -Be $false # Everything should still be opposite from when each property was changed individually - Invoke-DscResource -Name FilterKeys -ModuleName Microsoft.Windows.Setting.Accessibility -Method Set -Property $parameters - $testResult2 = Invoke-DscResource -Name FilterKeys -ModuleName Microsoft.Windows.Setting.Accessibility -Method Test -Property $parameters - $testResult2.InDesiredState | Should -Be $true - } -} - -AfterAll { - $env:TestRegistryPath = '' -} diff --git a/tests/PythonPip3Dsc/PythonPip3Dsc.Tests.ps1 b/tests/PythonPip3Dsc/PythonPip3Dsc.Tests.ps1 deleted file mode 100644 index 9d1d33ee..00000000 --- a/tests/PythonPip3Dsc/PythonPip3Dsc.Tests.ps1 +++ /dev/null @@ -1,106 +0,0 @@ -using module PythonPip3Dsc - -$ErrorActionPreference = 'Stop' -Set-StrictMode -Version Latest - -<# -.Synopsis - Pester tests related to the PythonPip3Dsc PowerShell module. -#> - -BeforeAll { - # Before import module make sure Python is installed - if ($env:TF_BUILD) { - $outFile = Join-Path $env:TEMP 'python.exe' - Invoke-WebRequest -Uri 'https://www.python.org/ftp/python/3.14.0/python-3.14.0a1-amd64.exe' -UseBasicParsing -OutFile $outFile - & $outFile /quiet InstallAllUsers=1 PrependPath=1 Include_test=0 - } - - Import-Module PythonPip3Dsc -Force -ErrorAction SilentlyContinue -} - -Describe 'List available DSC resources' { - It 'Shows DSC Resources' { - $expectedDSCResources = 'Pip3Package' - $availableDSCResources = (Get-DscResource -Module PythonPip3Dsc).Name - $availableDSCResources.count | Should -Be 1 - $availableDSCResources | Where-Object { $expectedDSCResources -notcontains $_ } | Should -BeNullOrEmpty -ErrorAction Stop - } -} - -Describe 'Pip3Package' { - It 'Sets desired package' -Skip:(!$IsWindows) { - $desiredState = @{ - PackageName = 'django' - } - - Invoke-DscResource -Name Pip3Package -ModuleName PythonPip3Dsc -Method Set -Property $desiredState - - $finalState = Invoke-DscResource -Name Pip3Package -ModuleName PythonPip3Dsc -Method Get -Property $desiredState - $finalState.PackageName | Should -Be $desiredState.PackageName - $finalState.Exist | Should -BeTrue - } - - It 'Sets desired package with version' -Skip:(!$IsWindows) { - $desiredState = @{ - PackageName = 'flask' - Version = '3.0.3' - } - - Invoke-DscResource -Name Pip3Package -ModuleName PythonPip3Dsc -Method Set -Property $desiredState - - $finalState = Invoke-DscResource -Name Pip3Package -ModuleName PythonPip3Dsc -Method Get -Property $desiredState - $finalState.PackageName | Should -Be $desiredState.PackageName - $finalState.Exist | Should -BeTrue - $finalState.Version | Should -Be $desiredState.Version - } - - It 'Updates with specific version' -Skip:(!$IsWindows) { - $desiredState = @{ - PackageName = 'requests' - Version = '2.32.2' - } - - Invoke-DscResource -Name Pip3Package -ModuleName PythonPip3Dsc -Method Set -Property $desiredState - - # Now update the package to a newer version - $desiredState.Version = '2.32.3' - Invoke-DscResource -Name Pip3Package -ModuleName PythonPip3Dsc -Method Set -Property $desiredState - - $finalState = Invoke-DscResource -Name Pip3Package -ModuleName PythonPip3Dsc -Method Get -Property $desiredState - $finalState.PackageName | Should -Be $desiredState.PackageName - $finalState.Exist | Should -BeTrue - $finalState.Version | Should -Be $desiredState.Version - } - - It 'Handles non-existent package gracefully' -Skip:(!$IsWindows) { - $desiredState = @{ - PackageName = 'nonexistentpackage' - } - - Invoke-DscResource -Name Pip3Package -ModuleName PythonPip3Dsc -Method Set -Property $desiredState - - $finalState = Invoke-DscResource -Name Pip3Package -ModuleName PythonPip3Dsc -Method Test -Property $desiredState - $finalState.InDesiredState | Should -Be $false - } - - It 'Removes package if not desired' -Skip:(!$IsWindows) { - $desiredState = @{ - PackageName = 'numpy' - } - - # Ensure the package is installed first - Invoke-DscResource -Name Pip3Package -ModuleName PythonPip3Dsc -Method Set -Property $desiredState - - # Now remove the package - $desiredState = @{ - PackageName = 'numpy' - Exist = $false - } - - Invoke-DscResource -Name Pip3Package -ModuleName PythonPip3Dsc -Method Set -Property $desiredState - - $finalState = Invoke-DscResource -Name Pip3Package -ModuleName PythonPip3Dsc -Method Get -Property $desiredState - $finalState.Exist | Should -BeFalse - } -} From 3285c108b6283e12c45d4323474f9ba026b41dfe Mon Sep 17 00:00:00 2001 From: Gijs Reijn Date: Thu, 7 Nov 2024 04:47:04 +0100 Subject: [PATCH 3/8] Fix spelling --- .github/actions/spelling/expect/generic_terms.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/actions/spelling/expect/generic_terms.txt b/.github/actions/spelling/expect/generic_terms.txt index 5a88fd33..8a799bed 100644 --- a/.github/actions/spelling/expect/generic_terms.txt +++ b/.github/actions/spelling/expect/generic_terms.txt @@ -12,3 +12,4 @@ worktree sortby msft automerge +Workaround From 067e5796fca8fdbea8055dd55bfc249a9deb097f Mon Sep 17 00:00:00 2001 From: Gijs Reijn Date: Thu, 7 Nov 2024 04:50:46 +0100 Subject: [PATCH 4/8] Re-add test on screw up --- .../Microsoft.DotNet.Dsc.Tests.ps1 | 238 ++++++++++ .../Microsoft.VSCode.Dsc.Tests.ps1 | 101 +++++ .../Microsoft.Windows.Developer.Tests.ps1 | 176 ++++++++ ...ft.Windows.Setting.Accessibility.Tests.ps1 | 406 ++++++++++++++++++ tests/PythonPip3Dsc/PythonPip3Dsc.Tests.ps1 | 106 +++++ 5 files changed, 1027 insertions(+) create mode 100644 tests/Microsoft.DotNet.Dsc/Microsoft.DotNet.Dsc.Tests.ps1 create mode 100644 tests/Microsoft.VSCode.Dsc/Microsoft.VSCode.Dsc.Tests.ps1 create mode 100644 tests/Microsoft.Windows.Developer/Microsoft.Windows.Developer.Tests.ps1 create mode 100644 tests/Microsoft.Windows.Setting.Accessibility/Microsoft.Windows.Setting.Accessibility.Tests.ps1 create mode 100644 tests/PythonPip3Dsc/PythonPip3Dsc.Tests.ps1 diff --git a/tests/Microsoft.DotNet.Dsc/Microsoft.DotNet.Dsc.Tests.ps1 b/tests/Microsoft.DotNet.Dsc/Microsoft.DotNet.Dsc.Tests.ps1 new file mode 100644 index 00000000..620dea67 --- /dev/null +++ b/tests/Microsoft.DotNet.Dsc/Microsoft.DotNet.Dsc.Tests.ps1 @@ -0,0 +1,238 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. +using module Microsoft.DotNet.Dsc + +$ErrorActionPreference = 'Stop' +Set-StrictMode -Version Latest + +<# +.Synopsis + Pester tests related to the Microsoft.DotNet.Dsc PowerShell module. +#> + +BeforeAll { + Install-Module -Name PSDesiredStateConfiguration -Force -SkipPublisherCheck + Import-Module Microsoft.DotNet.Dsc + + $script:toolsDir = Join-Path $env:USERPROFILE 'tools' + + if (-not (Test-Path $toolsDir)) { + $null = New-Item -ItemType Directory -Path $toolsDir -Force -ErrorAction SilentlyContinue + } +} + +Describe 'List available DSC resources' { + It 'Shows DSC Resources' { + $expectedDSCResources = 'DotNetToolPackage' + $availableDSCResources = (Get-DscResource -Module Microsoft.DotNet.Dsc).Name + $availableDSCResources.count | Should -Be 1 + $availableDSCResources | Where-Object { $expectedDSCResources -notcontains $_ } | Should -BeNullOrEmpty -ErrorAction Stop + } +} + +Describe 'DSC operation capabilities' { + It 'Sets desired package' -Skip:(!$IsWindows) { + $parameters = @{ + PackageId = 'gitversion.tool' + } + + Invoke-DscResource -Name DotNetToolPackage -ModuleName Microsoft.DotNet.Dsc -Method Set -Property $parameters + + $finalState = Invoke-DscResource -Name DotNetToolPackage -ModuleName Microsoft.DotNet.Dsc -Method Get -Property $parameters + $finalState.Exist | Should -BeTrue + $finalState.Version | Should -Not -BeNullOrEmpty + } + + It 'Sets desired package with prerelease' -Skip:(!$IsWindows) { + $parameters = @{ + PackageId = 'dotnet-ef' + PreRelease = $true + } + + Invoke-DscResource -Name DotNetToolPackage -ModuleName Microsoft.DotNet.Dsc -Method Set -Property $parameters + + $finalState = Invoke-DscResource -Name DotNetToolPackage -ModuleName Microsoft.DotNet.Dsc -Method Get -Property $parameters + $finalState.PackageId | Should -Be $parameters.PackageId + $finalState.PreRelease | Should -BeTrue + } + + It 'Sets desired package with version' -Skip:(!$IsWindows) { + $parameters = @{ + PackageId = 'dotnet-reportgenerator-globaltool' + Version = '5.3.9' + } + + Invoke-DscResource -Name DotNetToolPackage -ModuleName Microsoft.DotNet.Dsc -Method Set -Property $parameters + + $finalState = Invoke-DscResource -Name DotNetToolPackage -ModuleName Microsoft.DotNet.Dsc -Method Get -Property $parameters + $finalState.PackageId | Should -Be $parameters.PackageId + $finalState.Version | Should -Be $parameters.Version + } + + It 'Updates desired package with latest version' -Skip:(!$IsWindows) { + $parameters = @{ + PackageId = 'dotnet-reportgenerator-globaltool' + Version = '5.3.10' + } + + Invoke-DscResource -Name DotNetToolPackage -ModuleName Microsoft.DotNet.Dsc -Method Set -Property $parameters + + $finalState = Invoke-DscResource -Name DotNetToolPackage -ModuleName Microsoft.DotNet.Dsc -Method Get -Property $parameters + $finalState.PackageId | Should -Be $parameters.PackageId + $finalState.Version | Should -Be $parameters.Version + } + + It 'Sets desired package with prerelease version' -Skip:(!$IsWindows) { + $parameters = @{ + PackageId = 'PowerShell' + Version = '7.2.0-preview.5' + } + + Invoke-DscResource -Name DotNetToolPackage -ModuleName Microsoft.DotNet.Dsc -Method Set -Property $parameters + + $finalState = Invoke-DscResource -Name DotNetToolPackage -ModuleName Microsoft.DotNet.Dsc -Method Get -Property $parameters + $finalState.PackageId | Should -Be $parameters.PackageId + $finalState.Version | Should -Be $parameters.Version + $finalState.PreRelease | Should -BeTrue + } + + It 'Exports resources' -Skip:(!$IsWindows) { + $obj = [DotNetToolPackage]::Export() + + $obj.PackageId.Contains('dotnet-ef') | Should -Be $true + $obj.PackageId.Contains('dotnet-reportgenerator-globaltool') | Should -Be $true + } + + It 'Throws error when resource is not a tool' -Skip:(!$IsWindows) { + $parameters = @{ + PackageId = 'Azure-Core' # not a tool + } + + { Invoke-DscResource -Name DotNetToolPackage -ModuleName Microsoft.DotNet.Dsc -Method Set -Property $parameters } | Should -Throw -ExpectedMessage 'Executing dotnet.exe with {tool install Azure-Core --global --ignore-failed-sources} failed.' + } + + It 'Installs in tool path location with version' -Skip:(!$IsWindows) { + $parameters = @{ + PackageId = 'dotnet-dump' + ToolPathDirectory = $toolsDir + Version = '8.0.532401' + } + + Invoke-DscResource -Name DotNetToolPackage -ModuleName Microsoft.DotNet.Dsc -Method Set -Property $parameters + + $state = Invoke-DscResource -Name DotNetToolPackage -ModuleName Microsoft.DotNet.Dsc -Method Get -Property $parameters + $state.Exist | Should -BeTrue + $state.ToolPathDirectory | Should -Be $parameters.ToolPathDirectory + $state::InstalledPackages[$parameters.PackageId].ToolPathDirectory | Should -Be $parameters.ToolPathDirectory # It should reflect updated export() + } + + # TODO: Work on update scenario + It 'Update in tool path location' -Skip:(!$IsWindows) { + $parameters = @{ + PackageId = 'dotnet-dump' + ToolPathDirectory = $toolsDir + Version = '8.0.547301' + } + + Invoke-DscResource -Name DotNetToolPackage -ModuleName Microsoft.DotNet.Dsc -Method Set -Property $parameters + + $state = Invoke-DscResource -Name DotNetToolPackage -ModuleName Microsoft.DotNet.Dsc -Method Get -Property $parameters + $state.Exist | Should -BeTrue + $state.ToolPathDirectory | Should -Be $parameters.ToolPathDirectory + $state::InstalledPackages[$parameters.PackageId].ToolPathDirectory | Should -Be $parameters.ToolPathDirectory # It should reflect updated export() + } + + It 'Uninstall a tool' -Skip:(!$IsWindows) { + $parameters = @{ + PackageId = 'gitversion.tool' + Exist = $false + } + + Invoke-DscResource -Name DotNetToolPackage -ModuleName Microsoft.DotNet.Dsc -Method Set -Property $parameters + + $state = Invoke-DscResource -Name DotNetToolPackage -ModuleName Microsoft.DotNet.Dsc -Method Get -Property $parameters + $state.Exist | Should -BeFalse + } + + It 'Uninstall a tool from tool path location' -Skip:(!$IsWindows) { + $parameters = @{ + PackageId = 'dotnet-dump' + ToolPathDirectory = $toolsDir + Exist = $false + } + + Invoke-DscResource -Name DotNetToolPackage -ModuleName Microsoft.DotNet.Dsc -Method Set -Property $parameters + + $state = Invoke-DscResource -Name DotNetToolPackage -ModuleName Microsoft.DotNet.Dsc -Method Get -Property $parameters + $state.Exist | Should -BeFalse + } + + It 'Downgrades a tool' -Skip:(!$IsWindows) { + $parameters = @{ + PackageId = 'gitversion.tool' # should install latest version + } + + Invoke-DscResource -Name DotNetToolPackage -ModuleName Microsoft.DotNet.Dsc -Method Set -Property $parameters + + $parameters = @{ + PackageId = 'gitversion.tool' + Version = '6.0.2' + } + + Invoke-DscResource -Name DotNetToolPackage -ModuleName Microsoft.DotNet.Dsc -Method Set -Property $parameters + $state = Invoke-DscResource -Name DotNetToolPackage -ModuleName Microsoft.DotNet.Dsc -Method Get -Property $parameters + $state.Version | Should -Be $parameters.Version + } +} + +Describe 'DSC helper functions' { + Context 'Semantic Versioning' { + It 'Parses valid semantic version' { + $version = '1.2.3' + $result = Get-SemVer -version $version + $result.Major | Should -Be 1 + $result.Minor | Should -Be 2 + $result.Build | Should -Be 3 + } + + It 'Parses semantic version with alpha' { + $version = '1.2.3-alpha' + $result = Get-SemVer -version $version + $result.Major | Should -Be 1 + $result.Minor | Should -Be 2 + $result.Build | Should -Be 3 + $result.Revision | Should -Be 0 # Because pre-release is not a number according the handling + } + + It 'Parses semantic version with alpha tag and version' { + $version = '1.2.3-alpha.123' + $result = Get-SemVer -version $version + $result.Major | Should -Be 1 + $result.Minor | Should -Be 2 + $result.Build | Should -Be 3 + $result.Revision | Should -Be '123' + } + + It 'Parses semantic version with beta tag and version' { + $version = '1.2.3-beta.11' + $result = Get-SemVer -version $version + $result.Major | Should -Be 1 + $result.Minor | Should -Be 2 + $result.Build | Should -Be 3 + $result.Revision | Should -Be '11' + } + + It 'Parses semantic version with rc and version' { + $version = '1.2.3-rc.1' + $result = Get-SemVer -version $version + $result.Major | Should -Be 1 + $result.Minor | Should -Be 2 + $result.Build | Should -Be 3 + $result.Revision | Should -Be '1' + } + } +} + +AfterAll { + Remove-Item -Path $toolsDir -Recurse -Force -ErrorAction SilentlyContinue +} diff --git a/tests/Microsoft.VSCode.Dsc/Microsoft.VSCode.Dsc.Tests.ps1 b/tests/Microsoft.VSCode.Dsc/Microsoft.VSCode.Dsc.Tests.ps1 new file mode 100644 index 00000000..118cbe5a --- /dev/null +++ b/tests/Microsoft.VSCode.Dsc/Microsoft.VSCode.Dsc.Tests.ps1 @@ -0,0 +1,101 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. +using module Microsoft.VSCode.Dsc + +$ErrorActionPreference = 'Stop' +Set-StrictMode -Version Latest + +<# +.Synopsis + Pester tests related to the Microsoft.VSCode.Dsc PowerShell module. +#> + +BeforeAll { + Install-Module -Name PSDesiredStateConfiguration -Force -SkipPublisherCheck + Import-Module Microsoft.VSCode.Dsc + + # Install VSCode + Invoke-WebRequest https://raw.githubusercontent.com/PowerShell/vscode-powershell/main/scripts/Install-VSCode.ps1 -UseBasicParsing -OutFile Install-VSCode.ps1 + .\Install-VSCode.ps1 -BuildEdition Stable-User -Confirm:$false + + # Install VSCode Insiders + .\Install-VSCode.ps1 -BuildEdition Insider-User -Confirm:$false +} + +Describe 'List available DSC resources' { + It 'Shows DSC Resources' { + $expectedDSCResources = 'VSCodeExtension' + $availableDSCResources = (Get-DscResource -Module Microsoft.VSCode.Dsc).Name + $availableDSCResources.count | Should -Be 1 + $availableDSCResources | Where-Object { $expectedDSCResources -notcontains $_ } | Should -BeNullOrEmpty -ErrorAction Stop + } +} + +Describe 'VSCodeExtension' { + It 'Keeps current extension.' -Skip:(!$IsWindows) { + $parameters = @{ + Name = 'ms-vscode.powershell' + } + $initialState = Invoke-DscResource -Name VSCodeExtension -ModuleName Microsoft.VSCode.Dsc -Method Get -Property $parameters + + $testResult = Invoke-DscResource -Name VSCodeExtension -ModuleName Microsoft.VSCode.Dsc -Method Test -Property $parameters + $testResult.InDesiredState | Should -Be $true + + # Invoking set should not change these values. + Invoke-DscResource -Name VSCodeExtension -ModuleName Microsoft.VSCode.Dsc -Method Set -Property $parameters + $finalState = Invoke-DscResource -Name VSCodeExtension -ModuleName Microsoft.VSCode.Dsc -Method Get -Property $parameters + $finalState.Name | Should -Be $initialState.Name + $finalState.Version | Should -Be $initialState.Version + $finalState.Exist | Should -Be $initialState.Exist + } + + It 'Sets desired extension' -Skip:(!$IsWindows) { + $desiredState = @{ + Name = 'ms-azure-devops.azure-pipelines' + } + + Invoke-DscResource -Name VSCodeExtension -ModuleName Microsoft.VSCode.Dsc -Method Set -Property $desiredState + + $finalState = Invoke-DscResource -Name VSCodeExtension -ModuleName Microsoft.VSCode.Dsc -Method Get -Property $desiredState + $finalState.Name | Should -Be $desiredState.Name + $finalState.Exist | Should -BeTrue + } +} + +Describe 'VSCodeExtension-Insiders' { + It 'Keeps current extension in Insiders edition.' -Skip:(!$IsWindows) { + $parameters = @{ + Name = 'ms-vscode.powershell' + Insiders = $true + } + + $initialState = Invoke-DscResource -Name VSCodeExtension -ModuleName Microsoft.VSCode.Dsc -Method Get -Property $parameters + + $testResult = Invoke-DscResource -Name VSCodeExtension -ModuleName Microsoft.VSCode.Dsc -Method Test -Property $parameters + $testResult.InDesiredState | Should -Be $true + + # Invoking set should not change these values. + Invoke-DscResource -Name VSCodeExtension -ModuleName Microsoft.VSCode.Dsc -Method Set -Property $parameters + $finalState = Invoke-DscResource -Name VSCodeExtension -ModuleName Microsoft.VSCode.Dsc -Method Get -Property $parameters + $finalState.Name | Should -Be $initialState.Name + $finalState.Version | Should -Be $initialState.Version + $finalState.Exist | Should -Be $initialState.Exist + } + + It 'Sets desired extension in Insiders edition' -Skip:(!$IsWindows) { + $desiredState = @{ + Name = 'ms-azure-devops.azure-pipelines' + Insiders = $true + } + + Invoke-DscResource -Name VSCodeExtension -ModuleName Microsoft.VSCode.Dsc -Method Set -Property $desiredState + + $finalState = Invoke-DscResource -Name VSCodeExtension -ModuleName Microsoft.VSCode.Dsc -Method Get -Property $desiredState + $finalState.Name | Should -Be $desiredState.Name + $finalState.Exist | Should -BeTrue + } +} + +AfterAll { + # Uninstall VSCode? +} diff --git a/tests/Microsoft.Windows.Developer/Microsoft.Windows.Developer.Tests.ps1 b/tests/Microsoft.Windows.Developer/Microsoft.Windows.Developer.Tests.ps1 new file mode 100644 index 00000000..c72b4253 --- /dev/null +++ b/tests/Microsoft.Windows.Developer/Microsoft.Windows.Developer.Tests.ps1 @@ -0,0 +1,176 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. +using module Microsoft.Windows.Developer + +$ErrorActionPreference = "Stop" +Set-StrictMode -Version Latest + +<# +.Synopsis + Pester tests related to the Microsoft.WinGet.Developer PowerShell module. +#> + +BeforeAll { + Install-Module -Name PSDesiredStateConfiguration -Force -SkipPublisherCheck + Import-Module Microsoft.Windows.Developer + + # Create test registry path. + New-Item -Path TestRegistry:\ -Name TestKey + # Set-ItemProperty requires the PSDrive to be in the format 'HKCU:'. + $env:TestRegistryPath = ((Get-Item -Path TestRegistry:\).Name).replace("HKEY_CURRENT_USER", "HKCU:") +} + +Describe 'List available DSC resources' { + It 'Shows DSC Resources' { + $expectedDSCResources = "DeveloperMode", "OsVersion", "ShowSecondsInClock", "EnableDarkMode", "Taskbar", "UserAccessControl", "WindowsExplorer", "EnableRemoteDesktop" + $availableDSCResources = (Get-DscResource -Module Microsoft.Windows.Developer).Name + $availableDSCResources.length | Should -Be 8 + $availableDSCResources | Where-Object { $expectedDSCResources -notcontains $_ } | Should -BeNullOrEmpty -ErrorAction Stop + } +} + +Describe 'Taskbar' { + It 'Keeps current value.' { + $initialState = Invoke-DscResource -Name Taskbar -ModuleName Microsoft.Windows.Developer -Method Get -Property @{} + + $parameters = @{ + Alignment = 'KeepCurrentValue'; + HideLabelsMode = 'KeepCurrentValue'; + SearchboxMode = 'KeepCurrentValue'; + TaskViewButton = 'KeepCurrentValue'; + WidgetsButton = 'KeepCurrentValue' + } + + $testResult = Invoke-DscResource -Name Taskbar -ModuleName Microsoft.Windows.Developer -Method Test -Property $parameters + $testResult.InDesiredState | Should -Be $true + + # Invoking set should not change these values. + Invoke-DscResource -Name Taskbar -ModuleName Microsoft.Windows.Developer -Method Set -Property $parameters + $finalState = Invoke-DscResource -Name Taskbar -ModuleName Microsoft.Windows.Developer -Method Get -Property @{} + $finalState.Alignment | Should -Be $initialState.Alignment + $finalState.HideLabelsMode | Should -Be $initialState.HideLabelsMode + $finalState.SearchboxMode | Should -Be $initialState.SearchboxMode + $finalState.TaskViewButton | Should -Be $initialState.WidgetsButton + } + + It 'Sets desired value' { + # Randomly generate desired state. Minimum is set to 1 to avoid KeepCurrentValue + $desiredAlignment = [Alignment](Get-Random -Maximum 3 -Minimum 1) + $desiredHideLabelsMode = [HideTaskBarLabelsBehavior](Get-Random -Maximum 4 -Minimum 1) + $desiredSearchboxMode = [SearchBoxMode](Get-Random -Maximum 5 -Minimum 1) + $desiredTaskViewButton = [ShowHideFeature](Get-Random -Maximum 3 -Minimum 1) + $desiredWidgetsButton = [ShowHideFeature](Get-Random -Maximum 3 -Minimum 1) + + $desiredState = @{ Alignment = $desiredAlignment; + HideLabelsMode = $desiredHideLabelsMode; + SearchboxMode = $desiredSearchboxMode; + TaskViewButton = $desiredTaskViewButton; + WidgetsButton = $desiredWidgetsButton + } + + Invoke-DscResource -Name Taskbar -ModuleName Microsoft.Windows.Developer -Method Set -Property $desiredState + + $finalState = Invoke-DscResource -Name Taskbar -ModuleName Microsoft.Windows.Developer -Method Get -Property @{} + $finalState.Alignment | Should -Be $desiredAlignment + $finalState.HideLabelsMode | Should -Be $desiredHideLabelsMode + $finalState.SearchboxMode | Should -Be $desiredSearchboxMode + $finalState.TaskViewButton | Should -Be $desiredTaskViewButton + $finalState.WidgetsButton | Should -Be $desiredWidgetsButton + } +} + +Describe 'WindowsExplorer' { + It 'Keeps current value.' { + $initialState = Invoke-DscResource -Name WindowsExplorer -ModuleName Microsoft.Windows.Developer -Method Get -Property @{} + + $parameters = @{ + FileExtensions = 'KeepCurrentValue'; + HiddenFiles = 'KeepCurrentValue'; + ItemCheckBoxes = 'KeepCurrentValue' + } + + $testResult = Invoke-DscResource -Name WindowsExplorer -ModuleName Microsoft.Windows.Developer -Method Test -Property $parameters + $testResult.InDesiredState | Should -Be $true + + # Invoking set should not change these values. + Invoke-DscResource -Name WindowsExplorer -ModuleName Microsoft.Windows.Developer -Method Set -Property $parameters + $finalState = Invoke-DscResource -Name WindowsExplorer -ModuleName Microsoft.Windows.Developer -Method Get -Property @{} + $finalState.FileExtensions | Should -Be $initialState.FileExtensions + $finalState.HiddenFiles | Should -Be $initialState.HiddenFiles + $finalState.ItemCheckBoxes | Should -Be $initialState.ItemCheckBoxes + } + + It 'Sets desired value' { + # Randomly generate desired state. Minimum is set to 1 to avoid using KeepCurrentValue + $desiredFileExtensions = [ShowHideFeature](Get-Random -Maximum 3 -Minimum 1) + $desiredHiddenFiles = [ShowHideFeature](Get-Random -Maximum 3 -Minimum 1) + $desiredItemCheckBoxes = [ShowHideFeature](Get-Random -Maximum 3 -Minimum 1) + + $desiredState = @{ + FileExtensions = $desiredFileExtensions; + HiddenFiles = $desiredHiddenFiles; + ItemCheckBoxes = $desiredItemCheckBoxes + } + + Invoke-DscResource -Name WindowsExplorer -ModuleName Microsoft.Windows.Developer -Method Set -Property $desiredState + + $finalState = Invoke-DscResource -Name WindowsExplorer -ModuleName Microsoft.Windows.Developer -Method Get -Property @{} + $finalState.FileExtensions | Should -Be $desiredFileExtensions + $finalState.HiddenFiles | Should -Be $desiredHiddenFiles + $finalState.ItemCheckBoxes | Should -Be $desiredItemCheckBoxes + } +} + +Describe 'UserAccessControl' { + It 'Keeps current value.' { + $initialState = Invoke-DscResource -Name UserAccessControl -ModuleName Microsoft.Windows.Developer -Method Get -Property @{} + + $parameters = @{ AdminConsentPromptBehavior = 'KeepCurrentValue' } + + $testResult = Invoke-DscResource -Name UserAccessControl -ModuleName Microsoft.Windows.Developer -Method Test -Property $parameters + $testResult.InDesiredState | Should -Be $true + + # Invoking set should not change these values. + Invoke-DscResource -Name UserAccessControl -ModuleName Microsoft.Windows.Developer -Method Set -Property $parameters + $finalState = Invoke-DscResource -Name UserAccessControl -ModuleName Microsoft.Windows.Developer -Method Get -Property @{} + $finalState.AdminConsentPromptBehavior | Should -Be $initialState.AdminConsentPromptBehavior + } + + It 'Sets desired value.' { + # Randomly generate desired state. Minimum is set to 1 to avoid using KeepCurrentValue + $desiredAdminConsentPromptBehavior = [AdminConsentPromptBehavior](Get-Random -Maximum 6 -Minimum 1) + + $desiredState = @{ AdminConsentPromptBehavior = $desiredAdminConsentPromptBehavior } + + Invoke-DscResource -Name UserAccessControl -ModuleName Microsoft.Windows.Developer -Method Set -Property $desiredState + + $finalState = Invoke-DscResource -Name UserAccessControl -ModuleName Microsoft.Windows.Developer -Method Get -Property @{} + $finalState.AdminConsentPromptBehavior | Should -Be $desiredAdminConsentPromptBehavior + } +} + +Describe 'EnableRemoteDesktop' { + It 'Sets Enabled' { + $desiredRemoteDesktopBehavior = [Ensure]::Present + $desiredState = @{ Ensure = $desiredRemoteDesktopBehavior } + + Invoke-DscResource -Name EnableRemoteDesktop -ModuleName Microsoft.Windows.Developer -Method Set -Property $desiredState + + $finalState = Invoke-DscResource -Name EnableRemoteDesktop -ModuleName Microsoft.Windows.Developer -Method Get -Property @{} + $finalState.Ensure | Should -Be $desiredRemoteDesktopBehavior + } + + It 'Sets Disabled' { + $desiredRemoteDesktopBehavior = [Ensure]::Absent + $desiredState = @{ Ensure = $desiredRemoteDesktopBehavior } + + Invoke-DscResource -Name EnableRemoteDesktop -ModuleName Microsoft.Windows.Developer -Method Set -Property $desiredState + + $finalState = Invoke-DscResource -Name EnableRemoteDesktop -ModuleName Microsoft.Windows.Developer -Method Get -Property @{} + $finalState.Ensure | Should -Be $desiredRemoteDesktopBehavior + } +} + +AfterAll { + $env:TestRegistryPath = "" +} diff --git a/tests/Microsoft.Windows.Setting.Accessibility/Microsoft.Windows.Setting.Accessibility.Tests.ps1 b/tests/Microsoft.Windows.Setting.Accessibility/Microsoft.Windows.Setting.Accessibility.Tests.ps1 new file mode 100644 index 00000000..dd22de6d --- /dev/null +++ b/tests/Microsoft.Windows.Setting.Accessibility/Microsoft.Windows.Setting.Accessibility.Tests.ps1 @@ -0,0 +1,406 @@ + +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. +using module Microsoft.Windows.Setting.Accessibility + +$ErrorActionPreference = 'Stop' +Set-StrictMode -Version Latest + +<# +.Synopsis + Pester tests related to the Microsoft.Windows.Setting.Accessibility PowerShell module. +#> + +BeforeAll { + if ($null -eq (Get-Module -ListAvailable -Name PSDesiredStateConfiguration)) { + Install-Module -Name PSDesiredStateConfiguration -Force -SkipPublisherCheck + } + + Import-Module Microsoft.Windows.Setting.Accessibility + + # Create test registry path. + New-Item -Path TestRegistry:\ -Name TestKey + # Set-ItemProperty requires the PSDrive to be in the format 'HKCU:'. + $env:TestRegistryPath = ((Get-Item -Path TestRegistry:\).Name).replace('HKEY_CURRENT_USER', 'HKCU:') +} + +Describe 'List available DSC resources' { + It 'Shows DSC Resources' { + $expectedDSCResources = 'Text', 'Magnifier', 'MousePointer', 'VisualEffect', 'Audio', 'TextCursor', 'StickyKeys', 'ToggleKeys', 'FilterKeys' + $availableDSCResources = (Get-DscResource -Module Microsoft.Windows.Setting.Accessibility).Name + $availableDSCResources.length | Should -Be 9 + $availableDSCResources | Where-Object { $expectedDSCResources -notcontains $_ } | Should -BeNullOrEmpty -ErrorAction Stop + } +} + +Describe 'Text' { + It 'Keeps current value.' { + $initialState = Invoke-DscResource -Name Text -ModuleName Microsoft.Windows.Setting.Accessibility -Method Get -Property @{} + + $parameters = @{ Size = 'KeepCurrentValue' } + + $testResult = Invoke-DscResource -Name Text -ModuleName Microsoft.Windows.Setting.Accessibility -Method Test -Property $parameters + $testResult.InDesiredState | Should -Be $true + + # Invoking set should not change these values. + Invoke-DscResource -Name Text -ModuleName Microsoft.Windows.Setting.Accessibility -Method Set -Property $parameters + $finalState = Invoke-DscResource -Name Text -ModuleName Microsoft.Windows.Setting.Accessibility -Method Get -Property @{} + $finalState.Size | Should -Be $initialState.Size + } + + It 'Sets desired value' { + # Randomly generate desired state. Minimum is set to 1 to avoid KeepCurrentValue + $desiredTextSize = [TextSize](Get-Random -Maximum 4 -Minimum 1) + + $desiredState = @{ Size = $desiredTextSize } + + Invoke-DscResource -Name Text -ModuleName Microsoft.Windows.Setting.Accessibility -Method Set -Property $desiredState + + $finalState = Invoke-DscResource -Name Text -ModuleName Microsoft.Windows.Setting.Accessibility -Method Get -Property @{} + $finalState.Size | Should -Be $desiredTextSize + } +} + +Describe 'Magnifier' { + It 'Keeps current value.' { + $initialState = Invoke-DscResource -Name Magnifier -ModuleName Microsoft.Windows.Setting.Accessibility -Method Get -Property @{} + + $parameters = @{ Magnification = 'KeepCurrentValue' } + + $testResult = Invoke-DscResource -Name Magnifier -ModuleName Microsoft.Windows.Setting.Accessibility -Method Test -Property $parameters + $testResult.InDesiredState | Should -Be $true + + # Invoking set should not change these values. + Invoke-DscResource -Name Magnifier -ModuleName Microsoft.Windows.Setting.Accessibility -Method Set -Property $parameters + $finalState = Invoke-DscResource -Name Magnifier -ModuleName Microsoft.Windows.Setting.Accessibility -Method Get -Property @{} + $finalState.Magnification | Should -Be $initialState.Magnification + } + + It 'Sets desired value' { + # Randomly generate desired state. Minimum is set to 1 to avoid KeepCurrentValue + $desiredMagnification = [MagnificationValue](Get-Random -Maximum 4 -Minimum 1) + + $desiredState = @{ Magnification = $desiredMagnification } + + Invoke-DscResource -Name Magnifier -ModuleName Microsoft.Windows.Setting.Accessibility -Method Set -Property $desiredState + + $finalState = Invoke-DscResource -Name Magnifier -ModuleName Microsoft.Windows.Setting.Accessibility -Method Get -Property @{} + $finalState.Magnification | Should -Be $desiredMagnification + } +} + +Describe 'MousePointer' { + It 'Keeps current value.' { + $initialState = Invoke-DscResource -Name MousePointer -ModuleName Microsoft.Windows.Setting.Accessibility -Method Get -Property @{} + + $parameters = @{ PointerSize = 'KeepCurrentValue' } + + $testResult = Invoke-DscResource -Name MousePointer -ModuleName Microsoft.Windows.Setting.Accessibility -Method Test -Property $parameters + $testResult.InDesiredState | Should -Be $true + + # Invoking set should not change these values. + Invoke-DscResource -Name MousePointer -ModuleName Microsoft.Windows.Setting.Accessibility -Method Set -Property $parameters + $finalState = Invoke-DscResource -Name MousePointer -ModuleName Microsoft.Windows.Setting.Accessibility -Method Get -Property @{} + $finalState.PointerSize | Should -Be $initialState.PointerSize + } + + It 'Sets desired value' { + # Randomly generate desired state. Minimum is set to 1 to avoid KeepCurrentValue + $desiredPointerSize = [PointerSize](Get-Random -Maximum 4 -Minimum 1) + + $desiredState = @{ PointerSize = $desiredPointerSize } + + Invoke-DscResource -Name MousePointer -ModuleName Microsoft.Windows.Setting.Accessibility -Method Set -Property $desiredState + + $finalState = Invoke-DscResource -Name MousePointer -ModuleName Microsoft.Windows.Setting.Accessibility -Method Get -Property @{} + $finalState.PointerSize | Should -Be $desiredPointerSize + } +} + +Describe 'VisualEffect' { + It 'AlwaysShowScrollbars.' { + Invoke-DscResource -Name VisualEffect -ModuleName Microsoft.Windows.Setting.Accessibility -Method Set -Property @{ AlwaysShowScrollbars = $false } + + $initialState = Invoke-DscResource -Name VisualEffect -ModuleName Microsoft.Windows.Setting.Accessibility -Method Get -Property @{} + $initialState.AlwaysShowScrollbars | Should -Be $false + + # Set 'AlwaysShowScrollbars' to true. + $parameters = @{ AlwaysShowScrollbars = $true } + $testResult = Invoke-DscResource -Name VisualEffect -ModuleName Microsoft.Windows.Setting.Accessibility -Method Test -Property $parameters + $testResult.InDesiredState | Should -Be $false + + # Verify the changes are correct. + Invoke-DscResource -Name VisualEffect -ModuleName Microsoft.Windows.Setting.Accessibility -Method Set -Property $parameters + $finalState = Invoke-DscResource -Name VisualEffect -ModuleName Microsoft.Windows.Setting.Accessibility -Method Get -Property @{} + $finalState.AlwaysShowScrollbars | Should -Be $true + + $testResult2 = Invoke-DscResource -Name VisualEffect -ModuleName Microsoft.Windows.Setting.Accessibility -Method Test -Property $parameters + $testResult2.InDesiredState | Should -Be $true + } + It 'TransparencyEffects.' { + Invoke-DscResource -Name VisualEffect -ModuleName Microsoft.Windows.Setting.Accessibility -Method Set -Property @{ TransparencyEffects = $false } + + $initialState = Invoke-DscResource -Name VisualEffect -ModuleName Microsoft.Windows.Setting.Accessibility -Method Get -Property @{} + $initialState.TransparencyEffects | Should -Be $false + + # Set 'TransparencyEffects' to true. + $parameters = @{ TransparencyEffects = $true } + $testResult = Invoke-DscResource -Name VisualEffect -ModuleName Microsoft.Windows.Setting.Accessibility -Method Test -Property $parameters + $testResult.InDesiredState | Should -Be $false + + # Verify the changes are correct. + Invoke-DscResource -Name VisualEffect -ModuleName Microsoft.Windows.Setting.Accessibility -Method Set -Property $parameters + $finalState = Invoke-DscResource -Name VisualEffect -ModuleName Microsoft.Windows.Setting.Accessibility -Method Get -Property @{} + $finalState.TransparencyEffects | Should -Be $true + + $testResult2 = Invoke-DscResource -Name VisualEffect -ModuleName Microsoft.Windows.Setting.Accessibility -Method Test -Property $parameters + $testResult2.InDesiredState | Should -Be $true + } + It 'MessageDuration' { + $firstValue = 5 #Key is missing by default, and default value is 5 when not specified. + $secondValue = 10 + + $initialState = Invoke-DscResource -Name VisualEffect -ModuleName Microsoft.Windows.Setting.Accessibility -Method Get -Property @{} + $initialState.MessageDurationInSeconds | Should -Be $firstValue + + # Set 'MessageDurationInSeconds' to 10. + $parameters = @{ MessageDurationInSeconds = $secondValue } + $testResult = Invoke-DscResource -Name VisualEffect -ModuleName Microsoft.Windows.Setting.Accessibility -Method Test -Property $parameters + $testResult.InDesiredState | Should -Be $false + + # Verify the changes are correct. + Invoke-DscResource -Name VisualEffect -ModuleName Microsoft.Windows.Setting.Accessibility -Method Set -Property $parameters + $finalState = Invoke-DscResource -Name VisualEffect -ModuleName Microsoft.Windows.Setting.Accessibility -Method Get -Property @{} + $finalState.MessageDurationInSeconds | Should -Be $secondValue + + $testResult2 = Invoke-DscResource -Name VisualEffect -ModuleName Microsoft.Windows.Setting.Accessibility -Method Test -Property $parameters + $testResult2.InDesiredState | Should -Be $true + } +} + +Describe 'Audio' { + It 'EnableMonoAudio.' { + Invoke-DscResource -Name Audio -ModuleName Microsoft.Windows.Setting.Accessibility -Method Set -Property @{ EnableMonoAudio = $false } + + $initialState = Invoke-DscResource -Name Audio -ModuleName Microsoft.Windows.Setting.Accessibility -Method Get -Property @{} + $initialState.EnableMonoAudio | Should -Be $false + + # Set 'EnableMonoAudio' to true. + $parameters = @{ EnableMonoAudio = $true } + $testResult = Invoke-DscResource -Name Audio -ModuleName Microsoft.Windows.Setting.Accessibility -Method Test -Property $parameters + $testResult.InDesiredState | Should -Be $false + + # Verify the changes are correct. + Invoke-DscResource -Name Audio -ModuleName Microsoft.Windows.Setting.Accessibility -Method Set -Property $parameters + $finalState = Invoke-DscResource -Name Audio -ModuleName Microsoft.Windows.Setting.Accessibility -Method Get -Property @{} + $finalState.EnableMonoAudio | Should -Be $true + + $testResult2 = Invoke-DscResource -Name Audio -ModuleName Microsoft.Windows.Setting.Accessibility -Method Test -Property $parameters + $testResult2.InDesiredState | Should -Be $true + } +} + +Describe 'TextCursor' { + It 'IndicatorStatus.' { + $initialState = Invoke-DscResource -Name TextCursor -ModuleName Microsoft.Windows.Setting.Accessibility -Method Get -Property @{} + $initialState.IndicatorStatus # Should -Be $false + + # Set 'IndicatorStatus' to true. + $parameters = @{ IndicatorStatus = $true } + $testResult = Invoke-DscResource -Name TextCursor -ModuleName Microsoft.Windows.Setting.Accessibility -Method Test -Property $parameters + $testResult.InDesiredState | Should -Be $false + + # Verify the changes are correct. + Invoke-DscResource -Name TextCursor -ModuleName Microsoft.Windows.Setting.Accessibility -Method Set -Property $parameters + $finalState = Invoke-DscResource -Name TextCursor -ModuleName Microsoft.Windows.Setting.Accessibility -Method Get -Property @{} + $finalState.IndicatorStatus | Should -Be $true + + $testResult2 = Invoke-DscResource -Name TextCursor -ModuleName Microsoft.Windows.Setting.Accessibility -Method Test -Property $parameters + $testResult2.InDesiredState | Should -Be $true + } + It 'IndicatorSize.' { + Invoke-DscResource -Name TextCursor -ModuleName Microsoft.Windows.Setting.Accessibility -Method Set -Property @{ IndicatorSize = 1 } + + $initialState = Invoke-DscResource -Name TextCursor -ModuleName Microsoft.Windows.Setting.Accessibility -Method Get -Property @{} + $initialState.IndicatorSize | Should -Be 1 + + # Set 'IndicatorSize' to 2. + $parameters = @{ IndicatorSize = 2 } + $testResult = Invoke-DscResource -Name TextCursor -ModuleName Microsoft.Windows.Setting.Accessibility -Method Test -Property $parameters + $testResult.InDesiredState | Should -Be $false + + # Verify the changes are correct. + Invoke-DscResource -Name TextCursor -ModuleName Microsoft.Windows.Setting.Accessibility -Method Set -Property $parameters + $finalState = Invoke-DscResource -Name TextCursor -ModuleName Microsoft.Windows.Setting.Accessibility -Method Get -Property @{} + $finalState.IndicatorSize | Should -Be 2 + + $testResult2 = Invoke-DscResource -Name TextCursor -ModuleName Microsoft.Windows.Setting.Accessibility -Method Test -Property $parameters + $testResult2.InDesiredState | Should -Be $true + } + It 'IndicatorColor.' { + Invoke-DscResource -Name TextCursor -ModuleName Microsoft.Windows.Setting.Accessibility -Method Set -Property @{ IndicatorColor = 16711871 } + + $initialState = Invoke-DscResource -Name TextCursor -ModuleName Microsoft.Windows.Setting.Accessibility -Method Get -Property @{} + $initialState.IndicatorColor | Should -Be 16711871 + + # Set 'IndicatorColor' to true. + $parameters = @{ IndicatorColor = 16711872 } #Increment default by 1 + $testResult = Invoke-DscResource -Name TextCursor -ModuleName Microsoft.Windows.Setting.Accessibility -Method Test -Property $parameters + $testResult.InDesiredState | Should -Be $false + + # Verify the changes are correct. + Invoke-DscResource -Name TextCursor -ModuleName Microsoft.Windows.Setting.Accessibility -Method Set -Property $parameters + $finalState = Invoke-DscResource -Name TextCursor -ModuleName Microsoft.Windows.Setting.Accessibility -Method Get -Property @{} + $finalState.IndicatorColor | Should -Be 16711872 + + $testResult2 = Invoke-DscResource -Name TextCursor -ModuleName Microsoft.Windows.Setting.Accessibility -Method Test -Property $parameters + $testResult2.InDesiredState | Should -Be $true + } + It 'Thickness' { #int + $firstValue = 1 #Key is missing by default, and default value is 5 when not specified. + $secondValue = 2 + + $initialState = Invoke-DscResource -Name TextCursor -ModuleName Microsoft.Windows.Setting.Accessibility -Method Get -Property @{} + $initialState.Thickness | Should -Be $firstValue + + # Set 'Thickness' to 2. + $parameters = @{ Thickness = $secondValue } + $testResult = Invoke-DscResource -Name TextCursor -ModuleName Microsoft.Windows.Setting.Accessibility -Method Test -Property $parameters + $testResult.InDesiredState | Should -Be $false + + # Verify the changes are correct. + Invoke-DscResource -Name TextCursor -ModuleName Microsoft.Windows.Setting.Accessibility -Method Set -Property $parameters + $finalState = Invoke-DscResource -Name TextCursor -ModuleName Microsoft.Windows.Setting.Accessibility -Method Get -Property @{} + $finalState.Thickness | Should -Be $secondValue + + $testResult2 = Invoke-DscResource -Name TextCursor -ModuleName Microsoft.Windows.Setting.Accessibility -Method Test -Property $parameters + $testResult2.InDesiredState | Should -Be $true + } +} + +Describe 'StickyKeys' { + It 'Each property can be set' { + # Get a snapshot of the current state + $baselineState = Invoke-DscResource -Name StickyKeys -ModuleName Microsoft.Windows.Setting.Accessibility -Method Get -Property @{} + # Get a list of all the properties that can be changed + $propertyList = $baselineState.PSObject.Properties.Name | Where-Object { $_ -ne 'SID' } + + # Change each property individually and check each time + $propertyList | ForEach-Object { + $property = $_ # This is just for code readability + + # Set the desired state of the property to the opposite of whatever it currently is + $desiredPropertyState = !($baselineState | Select-Object -ExpandProperty $property) + + # Test the desired state against the current state. Since nothing has changed, the system should never be in the desired state + $parameters = @{ $property = $desiredPropertyState } + $testResult = Invoke-DscResource -Name StickyKeys -ModuleName Microsoft.Windows.Setting.Accessibility -Method Test -Property $parameters + $testResult.InDesiredState | Should -Be $false + + # Set the new state and check that it applied + Invoke-DscResource -Name StickyKeys -ModuleName Microsoft.Windows.Setting.Accessibility -Method Set -Property $parameters + $finalState = Invoke-DscResource -Name StickyKeys -ModuleName Microsoft.Windows.Setting.Accessibility -Method Get -Property @{} + $($finalState | Select-Object -ExpandProperty $property) | Should -Be $desiredPropertyState + + # Test the desired state against the current state. Now that the change has been applied, it should always be in the desired state + $testResult2 = Invoke-DscResource -Name StickyKeys -ModuleName Microsoft.Windows.Setting.Accessibility -Method Test -Property $parameters + $testResult2.InDesiredState | Should -Be $true + } + + # Set all properties back to the initial state at once + $parameterList = $baselineState | Select-Object -Property $propertyList + $parameters = @{} # Needs to be a hashtable for setting them all at once + $parameterList.PSObject.Properties | ForEach-Object { $parameters[$_.Name] = $_.Value } + $testResult = Invoke-DscResource -Name StickyKeys -ModuleName Microsoft.Windows.Setting.Accessibility -Method Test -Property $parameters + $testResult.InDesiredState | Should -Be $false # Everything should still be opposite from when each property was changed individually + Invoke-DscResource -Name StickyKeys -ModuleName Microsoft.Windows.Setting.Accessibility -Method Set -Property $parameters + $testResult2 = Invoke-DscResource -Name StickyKeys -ModuleName Microsoft.Windows.Setting.Accessibility -Method Test -Property $parameters + $testResult2.InDesiredState | Should -Be $true + } +} + +Describe 'ToggleKeys' { + It 'Each property can be set' { + # Get a snapshot of the current state + $baselineState = Invoke-DscResource -Name ToggleKeys -ModuleName Microsoft.Windows.Setting.Accessibility -Method Get -Property @{} + # Get a list of all the properties that can be changed + $propertyList = $baselineState.PSObject.Properties.Name | Where-Object { $_ -ne 'SID' } + + # Change each property individually and check each time + $propertyList | ForEach-Object { + $property = $_ # This is just for code readability + + # Set the desired state of the property to the opposite of whatever it currently is + $desiredPropertyState = !($baselineState | Select-Object -ExpandProperty $property) + + # Test the desired state against the current state. Since nothing has changed, the system should never be in the desired state + $parameters = @{ $property = $desiredPropertyState } + $testResult = Invoke-DscResource -Name ToggleKeys -ModuleName Microsoft.Windows.Setting.Accessibility -Method Test -Property $parameters + $testResult.InDesiredState | Should -Be $false + + # Set the new state and check that it applied + Invoke-DscResource -Name ToggleKeys -ModuleName Microsoft.Windows.Setting.Accessibility -Method Set -Property $parameters + $finalState = Invoke-DscResource -Name ToggleKeys -ModuleName Microsoft.Windows.Setting.Accessibility -Method Get -Property @{} + $($finalState | Select-Object -ExpandProperty $property) | Should -Be $desiredPropertyState + + # Test the desired state against the current state. Now that the change has been applied, it should always be in the desired state + $testResult2 = Invoke-DscResource -Name ToggleKeys -ModuleName Microsoft.Windows.Setting.Accessibility -Method Test -Property $parameters + $testResult2.InDesiredState | Should -Be $true + } + + # Set all properties back to the initial state at once + $parameterList = $baselineState | Select-Object -Property $propertyList + $parameters = @{} # Needs to be a hashtable for setting them all at once + $parameterList.PSObject.Properties | ForEach-Object { $parameters[$_.Name] = $_.Value } + $testResult = Invoke-DscResource -Name ToggleKeys -ModuleName Microsoft.Windows.Setting.Accessibility -Method Test -Property $parameters + $testResult.InDesiredState | Should -Be $false # Everything should still be opposite from when each property was changed individually + Invoke-DscResource -Name ToggleKeys -ModuleName Microsoft.Windows.Setting.Accessibility -Method Set -Property $parameters + $testResult2 = Invoke-DscResource -Name ToggleKeys -ModuleName Microsoft.Windows.Setting.Accessibility -Method Test -Property $parameters + $testResult2.InDesiredState | Should -Be $true + } +} + +Describe 'FilterKeys' { + It 'Each property can be set' { + # Get a snapshot of the current state + $baselineState = Invoke-DscResource -Name FilterKeys -ModuleName Microsoft.Windows.Setting.Accessibility -Method Get -Property @{} + # Get a list of all the properties that can be changed + $propertyList = $baselineState.PSObject.Properties.Name | Where-Object { $_ -ne 'SID' } + + # Change each property individually and check each time + $propertyList | ForEach-Object { + $property = $_ # This is just for code readability + + # Set the desired state of the property to the opposite of whatever it currently is + $desiredPropertyState = !($baselineState | Select-Object -ExpandProperty $property) + + # Test the desired state against the current state. Since nothing has changed, the system should never be in the desired state + $parameters = @{ $property = $desiredPropertyState } + $testResult = Invoke-DscResource -Name FilterKeys -ModuleName Microsoft.Windows.Setting.Accessibility -Method Test -Property $parameters + $testResult.InDesiredState | Should -Be $false + + # Set the new state and check that it applied + Invoke-DscResource -Name FilterKeys -ModuleName Microsoft.Windows.Setting.Accessibility -Method Set -Property $parameters + $finalState = Invoke-DscResource -Name FilterKeys -ModuleName Microsoft.Windows.Setting.Accessibility -Method Get -Property @{} + $($finalState | Select-Object -ExpandProperty $property) | Should -Be $desiredPropertyState + + # Test the desired state against the current state. Now that the change has been applied, it should always be in the desired state + $testResult2 = Invoke-DscResource -Name FilterKeys -ModuleName Microsoft.Windows.Setting.Accessibility -Method Test -Property $parameters + $testResult2.InDesiredState | Should -Be $true + } + + # Set all properties back to the initial state at once + $parameterList = $baselineState | Select-Object -Property $propertyList + $parameters = @{} # Needs to be a hashtable for setting them all at once + $parameterList.PSObject.Properties | ForEach-Object { $parameters[$_.Name] = $_.Value } + $testResult = Invoke-DscResource -Name FilterKeys -ModuleName Microsoft.Windows.Setting.Accessibility -Method Test -Property $parameters + $testResult.InDesiredState | Should -Be $false # Everything should still be opposite from when each property was changed individually + Invoke-DscResource -Name FilterKeys -ModuleName Microsoft.Windows.Setting.Accessibility -Method Set -Property $parameters + $testResult2 = Invoke-DscResource -Name FilterKeys -ModuleName Microsoft.Windows.Setting.Accessibility -Method Test -Property $parameters + $testResult2.InDesiredState | Should -Be $true + } +} + +AfterAll { + $env:TestRegistryPath = '' +} diff --git a/tests/PythonPip3Dsc/PythonPip3Dsc.Tests.ps1 b/tests/PythonPip3Dsc/PythonPip3Dsc.Tests.ps1 new file mode 100644 index 00000000..9d1d33ee --- /dev/null +++ b/tests/PythonPip3Dsc/PythonPip3Dsc.Tests.ps1 @@ -0,0 +1,106 @@ +using module PythonPip3Dsc + +$ErrorActionPreference = 'Stop' +Set-StrictMode -Version Latest + +<# +.Synopsis + Pester tests related to the PythonPip3Dsc PowerShell module. +#> + +BeforeAll { + # Before import module make sure Python is installed + if ($env:TF_BUILD) { + $outFile = Join-Path $env:TEMP 'python.exe' + Invoke-WebRequest -Uri 'https://www.python.org/ftp/python/3.14.0/python-3.14.0a1-amd64.exe' -UseBasicParsing -OutFile $outFile + & $outFile /quiet InstallAllUsers=1 PrependPath=1 Include_test=0 + } + + Import-Module PythonPip3Dsc -Force -ErrorAction SilentlyContinue +} + +Describe 'List available DSC resources' { + It 'Shows DSC Resources' { + $expectedDSCResources = 'Pip3Package' + $availableDSCResources = (Get-DscResource -Module PythonPip3Dsc).Name + $availableDSCResources.count | Should -Be 1 + $availableDSCResources | Where-Object { $expectedDSCResources -notcontains $_ } | Should -BeNullOrEmpty -ErrorAction Stop + } +} + +Describe 'Pip3Package' { + It 'Sets desired package' -Skip:(!$IsWindows) { + $desiredState = @{ + PackageName = 'django' + } + + Invoke-DscResource -Name Pip3Package -ModuleName PythonPip3Dsc -Method Set -Property $desiredState + + $finalState = Invoke-DscResource -Name Pip3Package -ModuleName PythonPip3Dsc -Method Get -Property $desiredState + $finalState.PackageName | Should -Be $desiredState.PackageName + $finalState.Exist | Should -BeTrue + } + + It 'Sets desired package with version' -Skip:(!$IsWindows) { + $desiredState = @{ + PackageName = 'flask' + Version = '3.0.3' + } + + Invoke-DscResource -Name Pip3Package -ModuleName PythonPip3Dsc -Method Set -Property $desiredState + + $finalState = Invoke-DscResource -Name Pip3Package -ModuleName PythonPip3Dsc -Method Get -Property $desiredState + $finalState.PackageName | Should -Be $desiredState.PackageName + $finalState.Exist | Should -BeTrue + $finalState.Version | Should -Be $desiredState.Version + } + + It 'Updates with specific version' -Skip:(!$IsWindows) { + $desiredState = @{ + PackageName = 'requests' + Version = '2.32.2' + } + + Invoke-DscResource -Name Pip3Package -ModuleName PythonPip3Dsc -Method Set -Property $desiredState + + # Now update the package to a newer version + $desiredState.Version = '2.32.3' + Invoke-DscResource -Name Pip3Package -ModuleName PythonPip3Dsc -Method Set -Property $desiredState + + $finalState = Invoke-DscResource -Name Pip3Package -ModuleName PythonPip3Dsc -Method Get -Property $desiredState + $finalState.PackageName | Should -Be $desiredState.PackageName + $finalState.Exist | Should -BeTrue + $finalState.Version | Should -Be $desiredState.Version + } + + It 'Handles non-existent package gracefully' -Skip:(!$IsWindows) { + $desiredState = @{ + PackageName = 'nonexistentpackage' + } + + Invoke-DscResource -Name Pip3Package -ModuleName PythonPip3Dsc -Method Set -Property $desiredState + + $finalState = Invoke-DscResource -Name Pip3Package -ModuleName PythonPip3Dsc -Method Test -Property $desiredState + $finalState.InDesiredState | Should -Be $false + } + + It 'Removes package if not desired' -Skip:(!$IsWindows) { + $desiredState = @{ + PackageName = 'numpy' + } + + # Ensure the package is installed first + Invoke-DscResource -Name Pip3Package -ModuleName PythonPip3Dsc -Method Set -Property $desiredState + + # Now remove the package + $desiredState = @{ + PackageName = 'numpy' + Exist = $false + } + + Invoke-DscResource -Name Pip3Package -ModuleName PythonPip3Dsc -Method Set -Property $desiredState + + $finalState = Invoke-DscResource -Name Pip3Package -ModuleName PythonPip3Dsc -Method Get -Property $desiredState + $finalState.Exist | Should -BeFalse + } +} From 196f4df1efe7dccf7d4e4c0fd5ef5a339b403c5b Mon Sep 17 00:00:00 2001 From: Gijs Reijn Date: Thu, 7 Nov 2024 11:33:40 +0100 Subject: [PATCH 5/8] Rewrite to tools directory --- utilities/scripts/New-DscResourceModule.ps1 | 106 ------------------- utilities/tools/New-DscResourceModule.ps1 | 107 ++++++++++++++++++++ 2 files changed, 107 insertions(+), 106 deletions(-) delete mode 100644 utilities/scripts/New-DscResourceModule.ps1 create mode 100644 utilities/tools/New-DscResourceModule.ps1 diff --git a/utilities/scripts/New-DscResourceModule.ps1 b/utilities/scripts/New-DscResourceModule.ps1 deleted file mode 100644 index 6e7c9511..00000000 --- a/utilities/scripts/New-DscResourceModule.ps1 +++ /dev/null @@ -1,106 +0,0 @@ -function New-DscResourceModule { - <# - .SYNOPSIS - Creates a new DSC (Desired State Configuration) resource module structure. - - .DESCRIPTION - The function New-DscResourceModule function creates a new DSC resource module structure with the specified name and description. - It sets up the necessary directory structure for resources and tests within the given base path. - - .PARAMETER DscResourceModule - The name of the DSC resource module to create. - - .PARAMETER Description - A description of the DSC resource module. - - .PARAMETER BasePath - The base path where the DSC resource module structure will be created. The default value is the parent directory of the script. - - .EXAMPLE - PS C:\> New-DscResourceModule -DscResourceModule 'Microsoft.Windows.Language' -Description 'DSC Resource for Windows Language' - - This command creates a new DSC resource module named 'Microsoft.Windows.Language' with the specified description in the default base path. - #> - [CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'Low')] - [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingPositionalParameters', '', Justification = 'Positional parameters are used for simplicity. Targeting PS 7+')] - param ( - [Parameter(Mandatory)] - [string]$DscResourceModule, - - [Parameter(Mandatory)] - [string]$Description, - - [Parameter()] - [string]$BasePath = (Join-Path $PSScriptRoot '..' '..'), - - [Parameter()] - [ValidateNotNullOrEmpty()] - [string[]] $DscResourceToExport - - ) - - $resourcePath = Join-Path $BasePath 'resources' $DscResourceModule - $testsPath = Join-Path $BasePath 'tests' $DscResourceModule - - # Create directories if they do not exist - if (-not (Test-Path -Path $resourcePath)) { - Write-Verbose -Message "Creating directory: $resourcePath" - $null = New-Item -ItemType Directory -Path $resourcePath -Force - } - - if (-not (Test-Path -Path $testsPath)) { - Write-Verbose -Message "Creating test directory: $testsPath" - $null = New-Item -ItemType Directory -Path $testsPath -Force - } - - $moduleManifestPath = (Join-Path $BasePath 'resources' $DscResourceModule "$DscResourceModule.psd1") - - $moduleManifestParams = @{ - Path = $moduleManifestPath - RootModule = "$DscResourceModule.psm1" - ModuleVersion = '0.1.0' - Author = 'Microsoft Corporation' - CompanyName = 'Microsoft Corporation' - Copyright = '(c) Microsoft Corporation. All rights reserved.' - Description = $Description - PowerShellVersion = '7.2' - FunctionsToExport = @('test') - CmdletsToExport = @() - VariablesToExport = @() - AliasesToExport = @() - } - - if ($DscResourceToExport) { - # New module manifest does not properly handle arrays of strings :( - $moduleManifestParams.Add('DscResourcesToExport', @($DscResourceToExport)) - } - - if (-not (Test-Path $moduleManifestPath)) { - if ($PSCmdlet.ShouldProcess($moduleManifestPath, 'Create module manifest')) { - Write-Verbose -Message ($moduleManifestParams | ConvertTo-Json -Depth 10 | Out-String) - New-ModuleManifest @moduleManifestParams - } - - # Workaround for issue: https://github.com/PowerShell/PowerShell/issues/5922 - $fileContent = Get-Content $moduleManifestPath - $newLicenseUri = "LicenseUri = 'https://github.com/microsoft/winget-dsc/blob/main/LICENSE'" - $fileContent = $fileContent -replace '# LicenseUri = ''''', $newLicenseUri - $newProjectUri = "ProjectUri = 'https://github.com/microsoft/winget-dsc'" - $fileContent = $fileContent -replace '# ProjectUri = ''''', $newProjectUri - $newPrerelease = "Prerelease = 'alpha'" - $fileContent = $fileContent -replace '# Prerelease = ''''', $newPrerelease - # TODO: Add tags - - Set-Content -Path $moduleManifestPath -Value $fileContent - } - - $psm1Path = Join-Path -Path $resourcePath -ChildPath "$DscResourceModule.psm1" - if (-not (Test-Path $psm1Path)) { - $null = New-Item -ItemType File -Path $psm1Path -Force - } - - $testsFilePath = Join-Path -Path $testsPath -ChildPath "$DscResourceModule.Tests.ps1" - if (-not (Test-Path $testsFilePath)) { - $null = New-Item -ItemType File -Path $testsFilePath -Force - } -} diff --git a/utilities/tools/New-DscResourceModule.ps1 b/utilities/tools/New-DscResourceModule.ps1 new file mode 100644 index 00000000..639261b7 --- /dev/null +++ b/utilities/tools/New-DscResourceModule.ps1 @@ -0,0 +1,107 @@ +<# +.SYNOPSIS + Creates a new DSC (Desired State Configuration) resource module structure. + +.DESCRIPTION + The function New-DscResourceModule function creates a new DSC resource module structure with the specified name and description. + It sets up the necessary directory structure for resources and tests within the given base path. + +.PARAMETER DscResourceModule + The name of the DSC resource module to create. + +.PARAMETER Description + A description of the DSC resource module. + +.PARAMETER BasePath + The base path where the DSC resource module structure will be created. The default value is the parent directory of the script. + +.EXAMPLE + PS C:\> New-DscResourceModule -DscResourceModule 'Microsoft.Windows.Language' -Description 'DSC Resource for Windows Language' + + This command creates a new DSC resource module named 'Microsoft.Windows.Language' with the specified description in the default base path. +#> +#Requires -Version 7 +[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingPositionalParameters', '', Justification = 'Positional parameters are used for simplicity. Targeting PS 7+')] +param ( + [Parameter(Mandatory)] + [string]$DscResourceModule, + + [Parameter(Mandatory)] + [string]$Description, + + [Parameter()] + [ValidateNotNullOrEmpty()] + [string[]] $DscResourceToExport +) + +$basePath = "$PSScriptRoot\..\.." + +if (Test-Path $basePath) { + $resourcePath = Join-Path $basePath 'resources' $DscResourceModule + $testsPath = Join-Path $basePath 'tests' $DscResourceModule +} else { + $basePath = $resourcePath = $testsPath = (Resolve-Path '.\').Path +} + +# Create directories if they do not exist +if (-not (Test-Path -Path $resourcePath)) { + Write-Verbose -Message "Creating directory: $resourcePath" + New-Item -ItemType Directory -Path $resourcePath -Force | Out-Null +} + +if (-not (Test-Path -Path $testsPath)) { + Write-Verbose -Message "Creating test directory: $testsPath" + New-Item -ItemType Directory -Path $testsPath -Force | Out-Null +} + +$moduleManifestPath = (Join-Path $basePath 'resources' $DscResourceModule "$DscResourceModule.psd1") + +$moduleManifestParams = @{ + Path = $moduleManifestPath + RootModule = "$DscResourceModule.psm1" + ModuleVersion = '0.1.0' + Author = 'Microsoft Corporation' + CompanyName = 'Microsoft Corporation' + Copyright = '(c) Microsoft Corporation. All rights reserved.' + Description = $Description + PowerShellVersion = '7.2' + FunctionsToExport = @('test') + CmdletsToExport = @() + VariablesToExport = @() + AliasesToExport = @() +} + +if ($DscResourceToExport) { + # New module manifest does not properly handle arrays of strings :( + $moduleManifestParams.Add('DscResourcesToExport', @($DscResourceToExport)) +} + +if (-not (Test-Path $moduleManifestPath)) { + if ($PSCmdlet.ShouldProcess($moduleManifestPath, 'Create module manifest')) { + Write-Verbose -Message ($moduleManifestParams | ConvertTo-Json -Depth 10 | Out-String) + New-ModuleManifest @moduleManifestParams + } + + # Workaround for issue: https://github.com/PowerShell/PowerShell/issues/5922 + $fileContent = Get-Content $moduleManifestPath + $newLicenseUri = "LicenseUri = 'https://github.com/microsoft/winget-dsc/blob/main/LICENSE'" + $fileContent = $fileContent -replace '# LicenseUri = ''''', $newLicenseUri + $newProjectUri = "ProjectUri = 'https://github.com/microsoft/winget-dsc'" + $fileContent = $fileContent -replace '# ProjectUri = ''''', $newProjectUri + $newPrerelease = "Prerelease = 'alpha'" + $fileContent = $fileContent -replace '# Prerelease = ''''', $newPrerelease + # TODO: Add tags + + Set-Content -Path $moduleManifestPath -Value $fileContent +} + +$psm1Path = Join-Path -Path $resourcePath -ChildPath "$DscResourceModule.psm1" +if (-not (Test-Path $psm1Path)) { + New-Item -ItemType File -Path $psm1Path -Force | Out-Null +} + +$testsFilePath = Join-Path -Path $testsPath -ChildPath "$DscResourceModule.Tests.ps1" +if (-not (Test-Path $testsFilePath)) { + New-Item -ItemType File -Path $testsFilePath -Force | Out-Null +} + From 219b45444db5e3f3a322c4265b96f8b13852bddb Mon Sep 17 00:00:00 2001 From: Gijs Reijn Date: Thu, 7 Nov 2024 11:34:31 +0100 Subject: [PATCH 6/8] Forgot to remove test --- utilities/tools/New-DscResourceModule.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utilities/tools/New-DscResourceModule.ps1 b/utilities/tools/New-DscResourceModule.ps1 index 639261b7..7969eec4 100644 --- a/utilities/tools/New-DscResourceModule.ps1 +++ b/utilities/tools/New-DscResourceModule.ps1 @@ -65,7 +65,7 @@ $moduleManifestParams = @{ Copyright = '(c) Microsoft Corporation. All rights reserved.' Description = $Description PowerShellVersion = '7.2' - FunctionsToExport = @('test') + FunctionsToExport = @() CmdletsToExport = @() VariablesToExport = @() AliasesToExport = @() From a0b4ac095f71bd54d1d03092c8d4d2b1b87dc6c1 Mon Sep 17 00:00:00 2001 From: Gijs Reijn Date: Thu, 7 Nov 2024 11:35:40 +0100 Subject: [PATCH 7/8] Rewrite contributing --- CONTRIBUTING.md | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 932f3069..4181bac0 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -115,13 +115,12 @@ If you're feature (or module) has not yet been created, follow these steps: 1. Fork the repository if you haven't already. 2. Clone your fork locally. -3. Dot-source the `New-DscResourceModule.ps` in your PowerShell session. -4. Create a new module scaffolding by executing: `New-DscResourceModule -DscResourceModule '' -Description 'DSC Resource for '` -5. Work on your changes and write tests. -6. Build and test to see if it works. -7. Create & push a feature branch. -8. Create a [Draft Pull Request (PR)](https://github.blog/2019-02-14-introducing-draft-pull-requests/). -9. If you are finished with your changes and you want a review, change the state. +3. Open a PowerShell terminal session and execute: `.\utilities\tools\New-DscResourceModule.ps1 -DscResourceModule '' -Description 'DSC Resource for '` +4. Work on your changes and write tests. +5. Build and test to see if it works. +6. Create & push a feature branch. +7. Create a [Draft Pull Request (PR)](https://github.blog/2019-02-14-introducing-draft-pull-requests/). +8. If you are finished with your changes and you want a review, change the state. > [!TIP] > Don't forget to add the `DscResourcesToExport` and `Tags`. From 358eaec9f527edd8bdc67893ecac40543494aed8 Mon Sep 17 00:00:00 2001 From: Gijs Reijn Date: Thu, 7 Nov 2024 15:14:39 +0100 Subject: [PATCH 8/8] Fix PSData --- utilities/tools/New-DscResourceModule.ps1 | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) diff --git a/utilities/tools/New-DscResourceModule.ps1 b/utilities/tools/New-DscResourceModule.ps1 index 7969eec4..5d2e9222 100644 --- a/utilities/tools/New-DscResourceModule.ps1 +++ b/utilities/tools/New-DscResourceModule.ps1 @@ -69,6 +69,9 @@ $moduleManifestParams = @{ CmdletsToExport = @() VariablesToExport = @() AliasesToExport = @() + LicenseUri = 'https://github.com/microsoft/winget-dsc/blob/main/LICENSE' + ProjectUri = 'https://github.com/microsoft/winget-dsc' + Prerelease = 'alpha' } if ($DscResourceToExport) { @@ -81,18 +84,6 @@ if (-not (Test-Path $moduleManifestPath)) { Write-Verbose -Message ($moduleManifestParams | ConvertTo-Json -Depth 10 | Out-String) New-ModuleManifest @moduleManifestParams } - - # Workaround for issue: https://github.com/PowerShell/PowerShell/issues/5922 - $fileContent = Get-Content $moduleManifestPath - $newLicenseUri = "LicenseUri = 'https://github.com/microsoft/winget-dsc/blob/main/LICENSE'" - $fileContent = $fileContent -replace '# LicenseUri = ''''', $newLicenseUri - $newProjectUri = "ProjectUri = 'https://github.com/microsoft/winget-dsc'" - $fileContent = $fileContent -replace '# ProjectUri = ''''', $newProjectUri - $newPrerelease = "Prerelease = 'alpha'" - $fileContent = $fileContent -replace '# Prerelease = ''''', $newPrerelease - # TODO: Add tags - - Set-Content -Path $moduleManifestPath -Value $fileContent } $psm1Path = Join-Path -Path $resourcePath -ChildPath "$DscResourceModule.psm1"