From e8e1180cc00a55318aa8320ecb89b6e56ee06b3d Mon Sep 17 00:00:00 2001 From: Gijs Reijn Date: Tue, 19 Nov 2024 05:36:53 +0100 Subject: [PATCH 01/13] Allow modules to be added to PSModulePath without version --- .../psDscAdapter/psDscAdapter.psm1 | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/powershell-adapter/psDscAdapter/psDscAdapter.psm1 b/powershell-adapter/psDscAdapter/psDscAdapter.psm1 index 2aad54cc..fd35745d 100644 --- a/powershell-adapter/psDscAdapter/psDscAdapter.psm1 +++ b/powershell-adapter/psDscAdapter/psDscAdapter.psm1 @@ -33,14 +33,15 @@ function Get-DSCResourceModules continue } - foreach($moduleFolder in Get-ChildItem $folder -Directory) - { - $addModule = $false - foreach($psd1 in Get-ChildItem -Recurse -Filter "$($moduleFolder.Name).psd1" -Path $moduleFolder.fullname -Depth 2) - { - $containsDSCResource = select-string -LiteralPath $psd1 -pattern '^[^#]*\bDscResourcesToExport\b.*' - if($null -ne $containsDSCResource) - { + $moduleFolders = Get-ChildItem $folder -Directory + if (-not $moduleFolders) { + $moduleFolders = Get-ChildItem (Split-Path $folder -Parent) | Where-Object { $_.Name -eq (Split-Path $folder -Leaf) } + } + + foreach ($moduleFolder in $moduleFolders) { + foreach ($psd1 in Get-ChildItem -Recurse -Filter "$($moduleFolder.Name).psd1" -Path $moduleFolder.fullname -Depth 2) { + $containsDSCResource = Select-String -LiteralPath $psd1 -Pattern '^[^#]*\bDscResourcesToExport\b.*' + if ($null -ne $containsDSCResource) { $dscModulePsd1List.Add($psd1) | Out-Null } } From ee039473e4f061fc58f3d1ee2e5ce349805a75b6 Mon Sep 17 00:00:00 2001 From: Gijs Reijn Date: Wed, 27 Nov 2024 08:50:58 +0100 Subject: [PATCH 02/13] Add tests --- .../TestClassNoVersion.psd1 | 52 +++++++++++++++++++ .../TestClassNoVersion.psm1 | 20 +++++++ .../Tests/powershellgroup.resource.tests.ps1 | 27 +++++++++- 3 files changed, 98 insertions(+), 1 deletion(-) create mode 100644 powershell-adapter/Tests/TestClassNoVersion/TestClassNoVersion.psd1 create mode 100644 powershell-adapter/Tests/TestClassNoVersion/TestClassNoVersion.psm1 diff --git a/powershell-adapter/Tests/TestClassNoVersion/TestClassNoVersion.psd1 b/powershell-adapter/Tests/TestClassNoVersion/TestClassNoVersion.psd1 new file mode 100644 index 00000000..7defc6db --- /dev/null +++ b/powershell-adapter/Tests/TestClassNoVersion/TestClassNoVersion.psd1 @@ -0,0 +1,52 @@ +# Copyright (c) Microsoft Corporation. +# Licensed under the MIT License. + +@{ + + # Script module or binary module file associated with this manifest. + RootModule = 'TestClassNoVersion.psm1' + + # Version number of this module. + ModuleVersion = '0.0.1' + + # Supported PSEditions + # CompatiblePSEditions = @() + + # ID used to uniquely identify this module + GUID = 'ec985d60-82f4-4d45-83e0-b6f935654350' + + # Author of this module + Author = 'Microsoft' + + # Company or vendor of this module + CompanyName = 'Microsoft Corporation' + + # Copyright statement for this module + Copyright = '(c) Microsoft. All rights reserved.' + + # Functions to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no functions to export. + FunctionsToExport = '*' + + # Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export. + CmdletsToExport = '*' + + # Variables to export from this module + VariablesToExport = '*' + + # Aliases to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no aliases to export. + AliasesToExport = '*' + + # DSC resources to export from this module + DscResourcesToExport = 'TestClassNoVersion' + + # Private data to pass to the module specified in RootModule/ModuleToProcess. This may also contain a PSData hashtable with additional module metadata used by PowerShell. + PrivateData = @{ + + PSData = @{ + DscCapabilities = @('Get', 'Test', 'Set') + + } # End of PSData hashtable + + } # End of PrivateData hashtable +} + diff --git a/powershell-adapter/Tests/TestClassNoVersion/TestClassNoVersion.psm1 b/powershell-adapter/Tests/TestClassNoVersion/TestClassNoVersion.psm1 new file mode 100644 index 00000000..5b40b337 --- /dev/null +++ b/powershell-adapter/Tests/TestClassNoVersion/TestClassNoVersion.psm1 @@ -0,0 +1,20 @@ +[DscResource()] +class TestClassNoVersion +{ + [DscProperty(Key)] + [string] $Name + + [void] Set() + { + } + + [bool] Test() + { + return $true + } + + [TestClassNoVersion] Get() + { + return $this + } +} \ No newline at end of file diff --git a/powershell-adapter/Tests/powershellgroup.resource.tests.ps1 b/powershell-adapter/Tests/powershellgroup.resource.tests.ps1 index fed67da1..82f4253d 100644 --- a/powershell-adapter/Tests/powershellgroup.resource.tests.ps1 +++ b/powershell-adapter/Tests/powershellgroup.resource.tests.ps1 @@ -28,7 +28,7 @@ Describe 'PowerShell adapter resource tests' { $r = dsc resource list '*' -a Microsoft.DSC/PowerShell $LASTEXITCODE | Should -Be 0 $resources = $r | ConvertFrom-Json - ($resources | ? {$_.Type -eq 'TestClassResource/TestClassResource'}).Count | Should -Be 1 + ($resources | ? {$_.Type -eq 'TestClassResource/TestClassResource' -and $_.Type -eq 'TestClassNoVersion/TestClassNoVersion'}).Count | Should -Be 2 } It 'Get works on class-based resource' { @@ -44,6 +44,14 @@ Describe 'PowerShell adapter resource tests' { $propertiesNames | Should -Not -Contain 'HiddenNonDscProperty' } + It 'Get works on class-based resource without sub-directory version' { + + $r = "{'Name':'TestClassNoVersion'}" | dsc resource get -r 'TestClassNoVersion/TestClassNoVersion' + $LASTEXITCODE | Should -Be 0 + $res = $r | ConvertFrom-Json + $res.actualState.result.properties.Name | Should -BeExactly 'TestClassNoVersion' + } + It 'Get uses enum names on class-based resource' { $r = "{'Name':'TestClassResource1'}" | dsc resource get -r 'TestClassResource/TestClassResource' -f - @@ -66,6 +74,15 @@ Describe 'PowerShell adapter resource tests' { $propertiesNames | Should -Not -Contain 'HiddenNonDscProperty' } + It 'Test works on class-based resource without sub-directory version' { + + $r = "{'Name':'TestClassNoVersion'}" | dsc resource test -r 'TestClassNoVersion/TestClassNoVersion' + $LASTEXITCODE | Should -Be 0 + $res = $r | ConvertFrom-Json + $res.actualState.result.properties.InDesiredState | Should -Be $True + $res.actualState.result.properties.InDesiredState.GetType().Name | Should -Be "Boolean" + } + It 'Set works on class-based resource' { $r = "{'Name':'TestClassResource1','Prop1':'ValueForProp1'}" | dsc resource set -r 'TestClassResource/TestClassResource' -f - @@ -74,6 +91,14 @@ Describe 'PowerShell adapter resource tests' { $res.afterState.result | Should -Not -BeNull } + It 'Set works on class-based resource without sub-directory version' { + + $r = "{'Name':'TestClassNoVersion'}" | dsc resource set -r 'TestClassNoVersion/TestClassNoVersion' + $LASTEXITCODE | Should -Be 0 + $res = $r | ConvertFrom-Json + $res.afterState.result | Should -Not -BeNull + } + It 'Export works on PS class-based resource' { $r = dsc resource export -r TestClassResource/TestClassResource From ced2421f38d190c960415ee9c8da45a274e29de5 Mon Sep 17 00:00:00 2001 From: Gijs Reijn Date: Wed, 27 Nov 2024 10:19:00 +0100 Subject: [PATCH 03/13] Fix incorrect discovery on include --- powershell-adapter/Tests/powershellgroup.resource.tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/powershell-adapter/Tests/powershellgroup.resource.tests.ps1 b/powershell-adapter/Tests/powershellgroup.resource.tests.ps1 index 82f4253d..86c65bf5 100644 --- a/powershell-adapter/Tests/powershellgroup.resource.tests.ps1 +++ b/powershell-adapter/Tests/powershellgroup.resource.tests.ps1 @@ -28,7 +28,7 @@ Describe 'PowerShell adapter resource tests' { $r = dsc resource list '*' -a Microsoft.DSC/PowerShell $LASTEXITCODE | Should -Be 0 $resources = $r | ConvertFrom-Json - ($resources | ? {$_.Type -eq 'TestClassResource/TestClassResource' -and $_.Type -eq 'TestClassNoVersion/TestClassNoVersion'}).Count | Should -Be 2 + ($resources | ? {$_.Type -in @('TestClassResource/TestClassResource', 'TestClassNoVersion/TestClassNoVersion')}).Count | Should -Be 2 } It 'Get works on class-based resource' { From 8edeb3223de2e4f03e10aa5ad301ee8e944b2752 Mon Sep 17 00:00:00 2001 From: Gijs Reijn Date: Sat, 7 Dec 2024 21:05:47 +0100 Subject: [PATCH 04/13] Added description from Andrew --- powershell-adapter/psDscAdapter/psDscAdapter.psm1 | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/powershell-adapter/psDscAdapter/psDscAdapter.psm1 b/powershell-adapter/psDscAdapter/psDscAdapter.psm1 index fd35745d..8df88cc5 100644 --- a/powershell-adapter/psDscAdapter/psDscAdapter.psm1 +++ b/powershell-adapter/psDscAdapter/psDscAdapter.psm1 @@ -32,7 +32,8 @@ function Get-DSCResourceModules { continue } - + # Partially re-used code from the original DSCv2 Get-DSCResource code + # Get-Module -ListAvailable is slow and has a bug causing incorrect reporting $moduleFolders = Get-ChildItem $folder -Directory if (-not $moduleFolders) { $moduleFolders = Get-ChildItem (Split-Path $folder -Parent) | Where-Object { $_.Name -eq (Split-Path $folder -Leaf) } From b7e94a81d664cec4bcfc4df751b265b7eedd1759 Mon Sep 17 00:00:00 2001 From: "G.Reijn" Date: Sat, 7 Dec 2024 21:19:15 +0100 Subject: [PATCH 05/13] Fix tests from rebase --- .../Tests/powershellgroup.resource.tests.ps1 | 93 ++++++++++++------- 1 file changed, 59 insertions(+), 34 deletions(-) diff --git a/powershell-adapter/Tests/powershellgroup.resource.tests.ps1 b/powershell-adapter/Tests/powershellgroup.resource.tests.ps1 index 86c65bf5..21eeec90 100644 --- a/powershell-adapter/Tests/powershellgroup.resource.tests.ps1 +++ b/powershell-adapter/Tests/powershellgroup.resource.tests.ps1 @@ -4,10 +4,11 @@ Describe 'PowerShell adapter resource tests' { BeforeAll { - $OldPSModulePath = $env:PSModulePath + $OldPSModulePath = $env:PSModulePath $env:PSModulePath += [System.IO.Path]::PathSeparator + $PSScriptRoot - if ($IsLinux -or $IsMacOS) { + if ($IsLinux -or $IsMacOS) + { $cacheFilePath = Join-Path $env:HOME ".dsc" "PSAdapterCache.json" } else @@ -28,7 +29,7 @@ Describe 'PowerShell adapter resource tests' { $r = dsc resource list '*' -a Microsoft.DSC/PowerShell $LASTEXITCODE | Should -Be 0 $resources = $r | ConvertFrom-Json - ($resources | ? {$_.Type -in @('TestClassResource/TestClassResource', 'TestClassNoVersion/TestClassNoVersion')}).Count | Should -Be 2 + ($resources | ? { $_.Type -in @('TestClassResource/TestClassResource', 'TestClassNoVersion/TestClassNoVersion') }).Count | Should -Be 2 } It 'Get works on class-based resource' { @@ -46,7 +47,7 @@ Describe 'PowerShell adapter resource tests' { It 'Get works on class-based resource without sub-directory version' { - $r = "{'Name':'TestClassNoVersion'}" | dsc resource get -r 'TestClassNoVersion/TestClassNoVersion' + $r = "{'Name':'TestClassNoVersion'}" | dsc resource get -r 'TestClassNoVersion/TestClassNoVersion' -f - $LASTEXITCODE | Should -Be 0 $res = $r | ConvertFrom-Json $res.actualState.result.properties.Name | Should -BeExactly 'TestClassNoVersion' @@ -76,7 +77,7 @@ Describe 'PowerShell adapter resource tests' { It 'Test works on class-based resource without sub-directory version' { - $r = "{'Name':'TestClassNoVersion'}" | dsc resource test -r 'TestClassNoVersion/TestClassNoVersion' + $r = "{'Name':'TestClassNoVersion'}" | dsc resource test -r 'TestClassNoVersion/TestClassNoVersion' -f - $LASTEXITCODE | Should -Be 0 $res = $r | ConvertFrom-Json $res.actualState.result.properties.InDesiredState | Should -Be $True @@ -93,7 +94,7 @@ Describe 'PowerShell adapter resource tests' { It 'Set works on class-based resource without sub-directory version' { - $r = "{'Name':'TestClassNoVersion'}" | dsc resource set -r 'TestClassNoVersion/TestClassNoVersion' + $r = "{'Name':'TestClassNoVersion'}" | dsc resource set -r 'TestClassNoVersion/TestClassNoVersion' -f - $LASTEXITCODE | Should -Be 0 $res = $r | ConvertFrom-Json $res.afterState.result | Should -Not -BeNull @@ -109,7 +110,7 @@ Describe 'PowerShell adapter resource tests' { $res.resources[0].properties.result[0].Prop1 | Should -Be "Property of object1" # verify that only properties with DscProperty attribute are returned - $res.resources[0].properties.result | %{ + $res.resources[0].properties.result | % { $propertiesNames = $_ | Get-Member -MemberType NoteProperty | % Name $propertiesNames | Should -Not -Contain 'NonDscProperty' $propertiesNames | Should -Not -Contain 'HiddenNonDscProperty' @@ -122,7 +123,7 @@ Describe 'PowerShell adapter resource tests' { $LASTEXITCODE | Should -Be 0 $res = $r | ConvertFrom-Json $res.actualState.result.count | Should -Be 5 - $res.actualState.result| % {$_.Name | Should -Not -BeNullOrEmpty} + $res.actualState.result | % { $_.Name | Should -Not -BeNullOrEmpty } } It 'Verify that ClearCache works in PSAdapter' { @@ -141,14 +142,20 @@ Describe 'PowerShell adapter resource tests' { # generate the cache $null = dsc resource list '*' -a Microsoft.DSC/PowerShell # update the version in the cache file - $cacheFilePath = if ($IsWindows) { + $cacheFilePath = if ($IsWindows) + { # PS 6+ on Windows Join-Path $env:LocalAppData "dsc\PSAdapterCache.json" - } else { + } + else + { # either WinPS or PS 6+ on Linux/Mac - if ($PSVersionTable.PSVersion.Major -le 5) { + if ($PSVersionTable.PSVersion.Major -le 5) + { Join-Path $env:LocalAppData "dsc\WindowsPSAdapterCache.json" - } else { + } + else + { Join-Path $env:HOME ".dsc" "PSAdapterCache.json" } } @@ -170,7 +177,8 @@ Describe 'PowerShell adapter resource tests' { Remove-Item -Recurse -Force -Path "$PSScriptRoot/TestClassResource" $oldPath = $env:PSModulePath - try { + try + { $env:PSModulePath += [System.IO.Path]::PathSeparator + $TestDrive # generate the cache @@ -184,7 +192,8 @@ Describe 'PowerShell adapter resource tests' { "$TestDrive/tracing.txt" | Should -FileContentMatchExactly 'Detected non-existent cache entry' "$TestDrive/tracing.txt" | Should -FileContentMatchExactly 'Constructing Get-DscResource cache' } - finally { + finally + { $env:PSModulePath = $oldPath Copy-Item -Recurse -Force -Path "$PSScriptRoot/Backup/TestClassResource" -Destination "$PSScriptRoot" Remove-Item -Recurse -Force -Path "$PSScriptRoot/Backup" @@ -196,7 +205,7 @@ Describe 'PowerShell adapter resource tests' { $r = dsc resource list '*' -a Microsoft.DSC/PowerShell $LASTEXITCODE | Should -Be 0 $resources = $r | ConvertFrom-Json - $t = $resources | ? {$_.Type -eq 'TestClassResource/TestClassResource'} + $t = $resources | ? { $_.Type -eq 'TestClassResource/TestClassResource' } $t.properties | Should -Contain "BaseProperty" } @@ -232,74 +241,83 @@ Describe 'PowerShell adapter resource tests' { $oldPath = $env:PSModulePath - try { + try + { $env:PSModulePath += [System.IO.Path]::PathSeparator + $pathRoot1 $env:PSModulePath += [System.IO.Path]::PathSeparator + $pathRoot2 $r = dsc resource list '*' -a Microsoft.DSC/PowerShell $LASTEXITCODE | Should -Be 0 $resources = $r | ConvertFrom-Json - $r = @($resources | ? {$_.Type -eq 'TestClassResource/TestClassResource'}) + $r = @($resources | ? { $_.Type -eq 'TestClassResource/TestClassResource' }) $r.Count | Should -Be 1 $r[0].Version | Should -Be '2.0.1' } - finally { + finally + { $env:PSModulePath = $oldPath } } It 'Verify adapted_dsc_type field in Get' { $oldPath = $env:PATH - try { + try + { $adapterPath = Join-Path $PSScriptRoot 'TestAdapter' $env:PATH += [System.IO.Path]::PathSeparator + $adapterPath - $r = '{TestCaseId: 1}'| dsc resource get -r 'Test/TestCase' -f - + $r = '{TestCaseId: 1}' | dsc resource get -r 'Test/TestCase' -f - $LASTEXITCODE | Should -Be 0 $resources = $r | ConvertFrom-Json $resources.actualState.result | Should -Be $True } - finally { + finally + { $env:PATH = $oldPath } } It 'Verify adapted_dsc_type field in Set' { $oldPath = $env:PATH - try { + try + { $adapterPath = Join-Path $PSScriptRoot 'TestAdapter' $env:PATH += [System.IO.Path]::PathSeparator + $adapterPath - $r = '{TestCaseId: 1}'| dsc resource set -r 'Test/TestCase' -f - + $r = '{TestCaseId: 1}' | dsc resource set -r 'Test/TestCase' -f - $LASTEXITCODE | Should -Be 0 $resources = $r | ConvertFrom-Json $resources.beforeState.result | Should -Be $True $resources.afterState.result | Should -Be $True } - finally { + finally + { $env:PATH = $oldPath } } It 'Verify adapted_dsc_type field in Test' { $oldPath = $env:PATH - try { + try + { $adapterPath = Join-Path $PSScriptRoot 'TestAdapter' $env:PATH += [System.IO.Path]::PathSeparator + $adapterPath - $r = '{TestCaseId: 1}'| dsc resource test -r 'Test/TestCase' -f - + $r = '{TestCaseId: 1}' | dsc resource test -r 'Test/TestCase' -f - $LASTEXITCODE | Should -Be 0 $resources = $r | ConvertFrom-Json $resources.actualState.result | Should -Be $True } - finally { + finally + { $env:PATH = $oldPath } } It 'Verify adapted_dsc_type field in Export' { $oldPath = $env:PATH - try { + try + { $adapterPath = Join-Path $PSScriptRoot 'TestAdapter' $env:PATH += [System.IO.Path]::PathSeparator + $adapterPath @@ -308,7 +326,8 @@ Describe 'PowerShell adapter resource tests' { $resources = $r | ConvertFrom-Json $resources.resources[0].properties.result | Should -Be $True } - finally { + finally + { $env:PATH = $oldPath } } @@ -328,14 +347,20 @@ Describe 'PowerShell adapter resource tests' { It 'Verify that there are no cache rebuilds for several sequential executions' { # remove cache file - $cacheFilePath = if ($IsWindows) { + $cacheFilePath = if ($IsWindows) + { # PS 6+ on Windows Join-Path $env:LocalAppData "dsc\PSAdapterCache.json" - } else { + } + else + { # either WinPS or PS 6+ on Linux/Mac - if ($PSVersionTable.PSVersion.Major -le 5) { + if ($PSVersionTable.PSVersion.Major -le 5) + { Join-Path $env:LocalAppData "dsc\WindowsPSAdapterCache.json" - } else { + } + else + { Join-Path $env:HOME ".dsc" "PSAdapterCache.json" } } @@ -346,7 +371,7 @@ Describe 'PowerShell adapter resource tests' { "$TestDrive/tracing.txt" | Should -FileContentMatchExactly 'Constructing Get-DscResource cache' # next executions following shortly after should Not rebuild the cache - 1..3 | %{ + 1..3 | % { dsc -l trace resource list -a Microsoft.DSC/PowerShell 2> $TestDrive/tracing.txt "$TestDrive/tracing.txt" | Should -Not -FileContentMatchExactly 'Constructing Get-DscResource cache' } From 1a488e32e34cecdf70bcbe3bb91cf7e145eac77a Mon Sep 17 00:00:00 2001 From: "G.Reijn" Date: Mon, 9 Dec 2024 19:11:51 +0100 Subject: [PATCH 06/13] Resolve remarks --- .vscode/settings.json | 1 + .../TestClassNoVersion.psd1 | 6 +- .../Tests/powershellgroup.resource.tests.ps1 | 77 ++++++------------- 3 files changed, 26 insertions(+), 58 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 24bf5814..93224251 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -12,6 +12,7 @@ "./y2j/Cargo.toml" ], "rust-analyzer.showUnlinkedFileNotification": true, + "powershell.codeFormatting.preset": "OTBS", "json.schemas": [ { "fileMatch": ["**/*.dsc.resource.json"], diff --git a/powershell-adapter/Tests/TestClassNoVersion/TestClassNoVersion.psd1 b/powershell-adapter/Tests/TestClassNoVersion/TestClassNoVersion.psd1 index 7defc6db..659605dd 100644 --- a/powershell-adapter/Tests/TestClassNoVersion/TestClassNoVersion.psd1 +++ b/powershell-adapter/Tests/TestClassNoVersion/TestClassNoVersion.psd1 @@ -25,16 +25,16 @@ Copyright = '(c) Microsoft. All rights reserved.' # Functions to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no functions to export. - FunctionsToExport = '*' + FunctionsToExport = @() # Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export. CmdletsToExport = '*' # Variables to export from this module - VariablesToExport = '*' + VariablesToExport = @() # Aliases to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no aliases to export. - AliasesToExport = '*' + AliasesToExport = @() # DSC resources to export from this module DscResourcesToExport = 'TestClassNoVersion' diff --git a/powershell-adapter/Tests/powershellgroup.resource.tests.ps1 b/powershell-adapter/Tests/powershellgroup.resource.tests.ps1 index 21eeec90..75f01969 100644 --- a/powershell-adapter/Tests/powershellgroup.resource.tests.ps1 +++ b/powershell-adapter/Tests/powershellgroup.resource.tests.ps1 @@ -7,12 +7,9 @@ Describe 'PowerShell adapter resource tests' { $OldPSModulePath = $env:PSModulePath $env:PSModulePath += [System.IO.Path]::PathSeparator + $PSScriptRoot - if ($IsLinux -or $IsMacOS) - { + if ($IsLinux -or $IsMacOS) { $cacheFilePath = Join-Path $env:HOME ".dsc" "PSAdapterCache.json" - } - else - { + } else { $cacheFilePath = Join-Path $env:LocalAppData "dsc" "PSAdapterCache.json" } } @@ -142,20 +139,14 @@ Describe 'PowerShell adapter resource tests' { # generate the cache $null = dsc resource list '*' -a Microsoft.DSC/PowerShell # update the version in the cache file - $cacheFilePath = if ($IsWindows) - { + $cacheFilePath = if ($IsWindows) { # PS 6+ on Windows Join-Path $env:LocalAppData "dsc\PSAdapterCache.json" - } - else - { + } else { # either WinPS or PS 6+ on Linux/Mac - if ($PSVersionTable.PSVersion.Major -le 5) - { + if ($PSVersionTable.PSVersion.Major -le 5) { Join-Path $env:LocalAppData "dsc\WindowsPSAdapterCache.json" - } - else - { + } else { Join-Path $env:HOME ".dsc" "PSAdapterCache.json" } } @@ -177,8 +168,7 @@ Describe 'PowerShell adapter resource tests' { Remove-Item -Recurse -Force -Path "$PSScriptRoot/TestClassResource" $oldPath = $env:PSModulePath - try - { + try { $env:PSModulePath += [System.IO.Path]::PathSeparator + $TestDrive # generate the cache @@ -191,9 +181,7 @@ Describe 'PowerShell adapter resource tests' { $LASTEXITCODE | Should -Be 0 "$TestDrive/tracing.txt" | Should -FileContentMatchExactly 'Detected non-existent cache entry' "$TestDrive/tracing.txt" | Should -FileContentMatchExactly 'Constructing Get-DscResource cache' - } - finally - { + } finally { $env:PSModulePath = $oldPath Copy-Item -Recurse -Force -Path "$PSScriptRoot/Backup/TestClassResource" -Destination "$PSScriptRoot" Remove-Item -Recurse -Force -Path "$PSScriptRoot/Backup" @@ -241,8 +229,7 @@ Describe 'PowerShell adapter resource tests' { $oldPath = $env:PSModulePath - try - { + try { $env:PSModulePath += [System.IO.Path]::PathSeparator + $pathRoot1 $env:PSModulePath += [System.IO.Path]::PathSeparator + $pathRoot2 @@ -252,17 +239,14 @@ Describe 'PowerShell adapter resource tests' { $r = @($resources | ? { $_.Type -eq 'TestClassResource/TestClassResource' }) $r.Count | Should -Be 1 $r[0].Version | Should -Be '2.0.1' - } - finally - { + } finally { $env:PSModulePath = $oldPath } } It 'Verify adapted_dsc_type field in Get' { $oldPath = $env:PATH - try - { + try { $adapterPath = Join-Path $PSScriptRoot 'TestAdapter' $env:PATH += [System.IO.Path]::PathSeparator + $adapterPath @@ -270,17 +254,14 @@ Describe 'PowerShell adapter resource tests' { $LASTEXITCODE | Should -Be 0 $resources = $r | ConvertFrom-Json $resources.actualState.result | Should -Be $True - } - finally - { + } finally { $env:PATH = $oldPath } } It 'Verify adapted_dsc_type field in Set' { $oldPath = $env:PATH - try - { + try { $adapterPath = Join-Path $PSScriptRoot 'TestAdapter' $env:PATH += [System.IO.Path]::PathSeparator + $adapterPath @@ -289,17 +270,14 @@ Describe 'PowerShell adapter resource tests' { $resources = $r | ConvertFrom-Json $resources.beforeState.result | Should -Be $True $resources.afterState.result | Should -Be $True - } - finally - { + } finally { $env:PATH = $oldPath } } It 'Verify adapted_dsc_type field in Test' { $oldPath = $env:PATH - try - { + try { $adapterPath = Join-Path $PSScriptRoot 'TestAdapter' $env:PATH += [System.IO.Path]::PathSeparator + $adapterPath @@ -307,17 +285,14 @@ Describe 'PowerShell adapter resource tests' { $LASTEXITCODE | Should -Be 0 $resources = $r | ConvertFrom-Json $resources.actualState.result | Should -Be $True - } - finally - { + } finally { $env:PATH = $oldPath } } It 'Verify adapted_dsc_type field in Export' { $oldPath = $env:PATH - try - { + try { $adapterPath = Join-Path $PSScriptRoot 'TestAdapter' $env:PATH += [System.IO.Path]::PathSeparator + $adapterPath @@ -325,9 +300,7 @@ Describe 'PowerShell adapter resource tests' { $LASTEXITCODE | Should -Be 0 $resources = $r | ConvertFrom-Json $resources.resources[0].properties.result | Should -Be $True - } - finally - { + } finally { $env:PATH = $oldPath } } @@ -347,20 +320,14 @@ Describe 'PowerShell adapter resource tests' { It 'Verify that there are no cache rebuilds for several sequential executions' { # remove cache file - $cacheFilePath = if ($IsWindows) - { + $cacheFilePath = if ($IsWindows) { # PS 6+ on Windows Join-Path $env:LocalAppData "dsc\PSAdapterCache.json" - } - else - { + } else { # either WinPS or PS 6+ on Linux/Mac - if ($PSVersionTable.PSVersion.Major -le 5) - { + if ($PSVersionTable.PSVersion.Major -le 5) { Join-Path $env:LocalAppData "dsc\WindowsPSAdapterCache.json" - } - else - { + } else { Join-Path $env:HOME ".dsc" "PSAdapterCache.json" } } From 704186d885d773d7cd2efa29a66d8176cb50d1d6 Mon Sep 17 00:00:00 2001 From: Gijs Reijn Date: Tue, 19 Nov 2024 05:36:53 +0100 Subject: [PATCH 07/13] Allow modules to be added to PSModulePath without version --- .../psDscAdapter/psDscAdapter.psm1 | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/powershell-adapter/psDscAdapter/psDscAdapter.psm1 b/powershell-adapter/psDscAdapter/psDscAdapter.psm1 index 2aad54cc..fd35745d 100644 --- a/powershell-adapter/psDscAdapter/psDscAdapter.psm1 +++ b/powershell-adapter/psDscAdapter/psDscAdapter.psm1 @@ -33,14 +33,15 @@ function Get-DSCResourceModules continue } - foreach($moduleFolder in Get-ChildItem $folder -Directory) - { - $addModule = $false - foreach($psd1 in Get-ChildItem -Recurse -Filter "$($moduleFolder.Name).psd1" -Path $moduleFolder.fullname -Depth 2) - { - $containsDSCResource = select-string -LiteralPath $psd1 -pattern '^[^#]*\bDscResourcesToExport\b.*' - if($null -ne $containsDSCResource) - { + $moduleFolders = Get-ChildItem $folder -Directory + if (-not $moduleFolders) { + $moduleFolders = Get-ChildItem (Split-Path $folder -Parent) | Where-Object { $_.Name -eq (Split-Path $folder -Leaf) } + } + + foreach ($moduleFolder in $moduleFolders) { + foreach ($psd1 in Get-ChildItem -Recurse -Filter "$($moduleFolder.Name).psd1" -Path $moduleFolder.fullname -Depth 2) { + $containsDSCResource = Select-String -LiteralPath $psd1 -Pattern '^[^#]*\bDscResourcesToExport\b.*' + if ($null -ne $containsDSCResource) { $dscModulePsd1List.Add($psd1) | Out-Null } } From db4268c19a26ce4a09f4ae42c833482e6abd3e51 Mon Sep 17 00:00:00 2001 From: Gijs Reijn Date: Wed, 27 Nov 2024 08:50:58 +0100 Subject: [PATCH 08/13] Add tests --- .../TestClassNoVersion.psd1 | 52 +++++++++++++++++++ .../TestClassNoVersion.psm1 | 20 +++++++ .../Tests/powershellgroup.resource.tests.ps1 | 27 +++++++++- 3 files changed, 98 insertions(+), 1 deletion(-) create mode 100644 powershell-adapter/Tests/TestClassNoVersion/TestClassNoVersion.psd1 create mode 100644 powershell-adapter/Tests/TestClassNoVersion/TestClassNoVersion.psm1 diff --git a/powershell-adapter/Tests/TestClassNoVersion/TestClassNoVersion.psd1 b/powershell-adapter/Tests/TestClassNoVersion/TestClassNoVersion.psd1 new file mode 100644 index 00000000..7defc6db --- /dev/null +++ b/powershell-adapter/Tests/TestClassNoVersion/TestClassNoVersion.psd1 @@ -0,0 +1,52 @@ +# Copyright (c) Microsoft Corporation. +# Licensed under the MIT License. + +@{ + + # Script module or binary module file associated with this manifest. + RootModule = 'TestClassNoVersion.psm1' + + # Version number of this module. + ModuleVersion = '0.0.1' + + # Supported PSEditions + # CompatiblePSEditions = @() + + # ID used to uniquely identify this module + GUID = 'ec985d60-82f4-4d45-83e0-b6f935654350' + + # Author of this module + Author = 'Microsoft' + + # Company or vendor of this module + CompanyName = 'Microsoft Corporation' + + # Copyright statement for this module + Copyright = '(c) Microsoft. All rights reserved.' + + # Functions to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no functions to export. + FunctionsToExport = '*' + + # Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export. + CmdletsToExport = '*' + + # Variables to export from this module + VariablesToExport = '*' + + # Aliases to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no aliases to export. + AliasesToExport = '*' + + # DSC resources to export from this module + DscResourcesToExport = 'TestClassNoVersion' + + # Private data to pass to the module specified in RootModule/ModuleToProcess. This may also contain a PSData hashtable with additional module metadata used by PowerShell. + PrivateData = @{ + + PSData = @{ + DscCapabilities = @('Get', 'Test', 'Set') + + } # End of PSData hashtable + + } # End of PrivateData hashtable +} + diff --git a/powershell-adapter/Tests/TestClassNoVersion/TestClassNoVersion.psm1 b/powershell-adapter/Tests/TestClassNoVersion/TestClassNoVersion.psm1 new file mode 100644 index 00000000..5b40b337 --- /dev/null +++ b/powershell-adapter/Tests/TestClassNoVersion/TestClassNoVersion.psm1 @@ -0,0 +1,20 @@ +[DscResource()] +class TestClassNoVersion +{ + [DscProperty(Key)] + [string] $Name + + [void] Set() + { + } + + [bool] Test() + { + return $true + } + + [TestClassNoVersion] Get() + { + return $this + } +} \ No newline at end of file diff --git a/powershell-adapter/Tests/powershellgroup.resource.tests.ps1 b/powershell-adapter/Tests/powershellgroup.resource.tests.ps1 index fed67da1..82f4253d 100644 --- a/powershell-adapter/Tests/powershellgroup.resource.tests.ps1 +++ b/powershell-adapter/Tests/powershellgroup.resource.tests.ps1 @@ -28,7 +28,7 @@ Describe 'PowerShell adapter resource tests' { $r = dsc resource list '*' -a Microsoft.DSC/PowerShell $LASTEXITCODE | Should -Be 0 $resources = $r | ConvertFrom-Json - ($resources | ? {$_.Type -eq 'TestClassResource/TestClassResource'}).Count | Should -Be 1 + ($resources | ? {$_.Type -eq 'TestClassResource/TestClassResource' -and $_.Type -eq 'TestClassNoVersion/TestClassNoVersion'}).Count | Should -Be 2 } It 'Get works on class-based resource' { @@ -44,6 +44,14 @@ Describe 'PowerShell adapter resource tests' { $propertiesNames | Should -Not -Contain 'HiddenNonDscProperty' } + It 'Get works on class-based resource without sub-directory version' { + + $r = "{'Name':'TestClassNoVersion'}" | dsc resource get -r 'TestClassNoVersion/TestClassNoVersion' + $LASTEXITCODE | Should -Be 0 + $res = $r | ConvertFrom-Json + $res.actualState.result.properties.Name | Should -BeExactly 'TestClassNoVersion' + } + It 'Get uses enum names on class-based resource' { $r = "{'Name':'TestClassResource1'}" | dsc resource get -r 'TestClassResource/TestClassResource' -f - @@ -66,6 +74,15 @@ Describe 'PowerShell adapter resource tests' { $propertiesNames | Should -Not -Contain 'HiddenNonDscProperty' } + It 'Test works on class-based resource without sub-directory version' { + + $r = "{'Name':'TestClassNoVersion'}" | dsc resource test -r 'TestClassNoVersion/TestClassNoVersion' + $LASTEXITCODE | Should -Be 0 + $res = $r | ConvertFrom-Json + $res.actualState.result.properties.InDesiredState | Should -Be $True + $res.actualState.result.properties.InDesiredState.GetType().Name | Should -Be "Boolean" + } + It 'Set works on class-based resource' { $r = "{'Name':'TestClassResource1','Prop1':'ValueForProp1'}" | dsc resource set -r 'TestClassResource/TestClassResource' -f - @@ -74,6 +91,14 @@ Describe 'PowerShell adapter resource tests' { $res.afterState.result | Should -Not -BeNull } + It 'Set works on class-based resource without sub-directory version' { + + $r = "{'Name':'TestClassNoVersion'}" | dsc resource set -r 'TestClassNoVersion/TestClassNoVersion' + $LASTEXITCODE | Should -Be 0 + $res = $r | ConvertFrom-Json + $res.afterState.result | Should -Not -BeNull + } + It 'Export works on PS class-based resource' { $r = dsc resource export -r TestClassResource/TestClassResource From 64c8ea958ff7ff5521fa973ca9186aaf6884bdaf Mon Sep 17 00:00:00 2001 From: Gijs Reijn Date: Wed, 27 Nov 2024 10:19:00 +0100 Subject: [PATCH 09/13] Fix incorrect discovery on include --- powershell-adapter/Tests/powershellgroup.resource.tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/powershell-adapter/Tests/powershellgroup.resource.tests.ps1 b/powershell-adapter/Tests/powershellgroup.resource.tests.ps1 index 82f4253d..86c65bf5 100644 --- a/powershell-adapter/Tests/powershellgroup.resource.tests.ps1 +++ b/powershell-adapter/Tests/powershellgroup.resource.tests.ps1 @@ -28,7 +28,7 @@ Describe 'PowerShell adapter resource tests' { $r = dsc resource list '*' -a Microsoft.DSC/PowerShell $LASTEXITCODE | Should -Be 0 $resources = $r | ConvertFrom-Json - ($resources | ? {$_.Type -eq 'TestClassResource/TestClassResource' -and $_.Type -eq 'TestClassNoVersion/TestClassNoVersion'}).Count | Should -Be 2 + ($resources | ? {$_.Type -in @('TestClassResource/TestClassResource', 'TestClassNoVersion/TestClassNoVersion')}).Count | Should -Be 2 } It 'Get works on class-based resource' { From c09075488e25d9ea986af4af887302ea2070d29b Mon Sep 17 00:00:00 2001 From: Gijs Reijn Date: Sat, 7 Dec 2024 21:05:47 +0100 Subject: [PATCH 10/13] Added description from Andrew --- powershell-adapter/psDscAdapter/psDscAdapter.psm1 | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/powershell-adapter/psDscAdapter/psDscAdapter.psm1 b/powershell-adapter/psDscAdapter/psDscAdapter.psm1 index fd35745d..8df88cc5 100644 --- a/powershell-adapter/psDscAdapter/psDscAdapter.psm1 +++ b/powershell-adapter/psDscAdapter/psDscAdapter.psm1 @@ -32,7 +32,8 @@ function Get-DSCResourceModules { continue } - + # Partially re-used code from the original DSCv2 Get-DSCResource code + # Get-Module -ListAvailable is slow and has a bug causing incorrect reporting $moduleFolders = Get-ChildItem $folder -Directory if (-not $moduleFolders) { $moduleFolders = Get-ChildItem (Split-Path $folder -Parent) | Where-Object { $_.Name -eq (Split-Path $folder -Leaf) } From b5135483ade841824aee159176f34858d6005df0 Mon Sep 17 00:00:00 2001 From: "G.Reijn" Date: Sat, 7 Dec 2024 21:19:15 +0100 Subject: [PATCH 11/13] Fix tests from rebase --- .../Tests/powershellgroup.resource.tests.ps1 | 93 ++++++++++++------- 1 file changed, 59 insertions(+), 34 deletions(-) diff --git a/powershell-adapter/Tests/powershellgroup.resource.tests.ps1 b/powershell-adapter/Tests/powershellgroup.resource.tests.ps1 index 86c65bf5..21eeec90 100644 --- a/powershell-adapter/Tests/powershellgroup.resource.tests.ps1 +++ b/powershell-adapter/Tests/powershellgroup.resource.tests.ps1 @@ -4,10 +4,11 @@ Describe 'PowerShell adapter resource tests' { BeforeAll { - $OldPSModulePath = $env:PSModulePath + $OldPSModulePath = $env:PSModulePath $env:PSModulePath += [System.IO.Path]::PathSeparator + $PSScriptRoot - if ($IsLinux -or $IsMacOS) { + if ($IsLinux -or $IsMacOS) + { $cacheFilePath = Join-Path $env:HOME ".dsc" "PSAdapterCache.json" } else @@ -28,7 +29,7 @@ Describe 'PowerShell adapter resource tests' { $r = dsc resource list '*' -a Microsoft.DSC/PowerShell $LASTEXITCODE | Should -Be 0 $resources = $r | ConvertFrom-Json - ($resources | ? {$_.Type -in @('TestClassResource/TestClassResource', 'TestClassNoVersion/TestClassNoVersion')}).Count | Should -Be 2 + ($resources | ? { $_.Type -in @('TestClassResource/TestClassResource', 'TestClassNoVersion/TestClassNoVersion') }).Count | Should -Be 2 } It 'Get works on class-based resource' { @@ -46,7 +47,7 @@ Describe 'PowerShell adapter resource tests' { It 'Get works on class-based resource without sub-directory version' { - $r = "{'Name':'TestClassNoVersion'}" | dsc resource get -r 'TestClassNoVersion/TestClassNoVersion' + $r = "{'Name':'TestClassNoVersion'}" | dsc resource get -r 'TestClassNoVersion/TestClassNoVersion' -f - $LASTEXITCODE | Should -Be 0 $res = $r | ConvertFrom-Json $res.actualState.result.properties.Name | Should -BeExactly 'TestClassNoVersion' @@ -76,7 +77,7 @@ Describe 'PowerShell adapter resource tests' { It 'Test works on class-based resource without sub-directory version' { - $r = "{'Name':'TestClassNoVersion'}" | dsc resource test -r 'TestClassNoVersion/TestClassNoVersion' + $r = "{'Name':'TestClassNoVersion'}" | dsc resource test -r 'TestClassNoVersion/TestClassNoVersion' -f - $LASTEXITCODE | Should -Be 0 $res = $r | ConvertFrom-Json $res.actualState.result.properties.InDesiredState | Should -Be $True @@ -93,7 +94,7 @@ Describe 'PowerShell adapter resource tests' { It 'Set works on class-based resource without sub-directory version' { - $r = "{'Name':'TestClassNoVersion'}" | dsc resource set -r 'TestClassNoVersion/TestClassNoVersion' + $r = "{'Name':'TestClassNoVersion'}" | dsc resource set -r 'TestClassNoVersion/TestClassNoVersion' -f - $LASTEXITCODE | Should -Be 0 $res = $r | ConvertFrom-Json $res.afterState.result | Should -Not -BeNull @@ -109,7 +110,7 @@ Describe 'PowerShell adapter resource tests' { $res.resources[0].properties.result[0].Prop1 | Should -Be "Property of object1" # verify that only properties with DscProperty attribute are returned - $res.resources[0].properties.result | %{ + $res.resources[0].properties.result | % { $propertiesNames = $_ | Get-Member -MemberType NoteProperty | % Name $propertiesNames | Should -Not -Contain 'NonDscProperty' $propertiesNames | Should -Not -Contain 'HiddenNonDscProperty' @@ -122,7 +123,7 @@ Describe 'PowerShell adapter resource tests' { $LASTEXITCODE | Should -Be 0 $res = $r | ConvertFrom-Json $res.actualState.result.count | Should -Be 5 - $res.actualState.result| % {$_.Name | Should -Not -BeNullOrEmpty} + $res.actualState.result | % { $_.Name | Should -Not -BeNullOrEmpty } } It 'Verify that ClearCache works in PSAdapter' { @@ -141,14 +142,20 @@ Describe 'PowerShell adapter resource tests' { # generate the cache $null = dsc resource list '*' -a Microsoft.DSC/PowerShell # update the version in the cache file - $cacheFilePath = if ($IsWindows) { + $cacheFilePath = if ($IsWindows) + { # PS 6+ on Windows Join-Path $env:LocalAppData "dsc\PSAdapterCache.json" - } else { + } + else + { # either WinPS or PS 6+ on Linux/Mac - if ($PSVersionTable.PSVersion.Major -le 5) { + if ($PSVersionTable.PSVersion.Major -le 5) + { Join-Path $env:LocalAppData "dsc\WindowsPSAdapterCache.json" - } else { + } + else + { Join-Path $env:HOME ".dsc" "PSAdapterCache.json" } } @@ -170,7 +177,8 @@ Describe 'PowerShell adapter resource tests' { Remove-Item -Recurse -Force -Path "$PSScriptRoot/TestClassResource" $oldPath = $env:PSModulePath - try { + try + { $env:PSModulePath += [System.IO.Path]::PathSeparator + $TestDrive # generate the cache @@ -184,7 +192,8 @@ Describe 'PowerShell adapter resource tests' { "$TestDrive/tracing.txt" | Should -FileContentMatchExactly 'Detected non-existent cache entry' "$TestDrive/tracing.txt" | Should -FileContentMatchExactly 'Constructing Get-DscResource cache' } - finally { + finally + { $env:PSModulePath = $oldPath Copy-Item -Recurse -Force -Path "$PSScriptRoot/Backup/TestClassResource" -Destination "$PSScriptRoot" Remove-Item -Recurse -Force -Path "$PSScriptRoot/Backup" @@ -196,7 +205,7 @@ Describe 'PowerShell adapter resource tests' { $r = dsc resource list '*' -a Microsoft.DSC/PowerShell $LASTEXITCODE | Should -Be 0 $resources = $r | ConvertFrom-Json - $t = $resources | ? {$_.Type -eq 'TestClassResource/TestClassResource'} + $t = $resources | ? { $_.Type -eq 'TestClassResource/TestClassResource' } $t.properties | Should -Contain "BaseProperty" } @@ -232,74 +241,83 @@ Describe 'PowerShell adapter resource tests' { $oldPath = $env:PSModulePath - try { + try + { $env:PSModulePath += [System.IO.Path]::PathSeparator + $pathRoot1 $env:PSModulePath += [System.IO.Path]::PathSeparator + $pathRoot2 $r = dsc resource list '*' -a Microsoft.DSC/PowerShell $LASTEXITCODE | Should -Be 0 $resources = $r | ConvertFrom-Json - $r = @($resources | ? {$_.Type -eq 'TestClassResource/TestClassResource'}) + $r = @($resources | ? { $_.Type -eq 'TestClassResource/TestClassResource' }) $r.Count | Should -Be 1 $r[0].Version | Should -Be '2.0.1' } - finally { + finally + { $env:PSModulePath = $oldPath } } It 'Verify adapted_dsc_type field in Get' { $oldPath = $env:PATH - try { + try + { $adapterPath = Join-Path $PSScriptRoot 'TestAdapter' $env:PATH += [System.IO.Path]::PathSeparator + $adapterPath - $r = '{TestCaseId: 1}'| dsc resource get -r 'Test/TestCase' -f - + $r = '{TestCaseId: 1}' | dsc resource get -r 'Test/TestCase' -f - $LASTEXITCODE | Should -Be 0 $resources = $r | ConvertFrom-Json $resources.actualState.result | Should -Be $True } - finally { + finally + { $env:PATH = $oldPath } } It 'Verify adapted_dsc_type field in Set' { $oldPath = $env:PATH - try { + try + { $adapterPath = Join-Path $PSScriptRoot 'TestAdapter' $env:PATH += [System.IO.Path]::PathSeparator + $adapterPath - $r = '{TestCaseId: 1}'| dsc resource set -r 'Test/TestCase' -f - + $r = '{TestCaseId: 1}' | dsc resource set -r 'Test/TestCase' -f - $LASTEXITCODE | Should -Be 0 $resources = $r | ConvertFrom-Json $resources.beforeState.result | Should -Be $True $resources.afterState.result | Should -Be $True } - finally { + finally + { $env:PATH = $oldPath } } It 'Verify adapted_dsc_type field in Test' { $oldPath = $env:PATH - try { + try + { $adapterPath = Join-Path $PSScriptRoot 'TestAdapter' $env:PATH += [System.IO.Path]::PathSeparator + $adapterPath - $r = '{TestCaseId: 1}'| dsc resource test -r 'Test/TestCase' -f - + $r = '{TestCaseId: 1}' | dsc resource test -r 'Test/TestCase' -f - $LASTEXITCODE | Should -Be 0 $resources = $r | ConvertFrom-Json $resources.actualState.result | Should -Be $True } - finally { + finally + { $env:PATH = $oldPath } } It 'Verify adapted_dsc_type field in Export' { $oldPath = $env:PATH - try { + try + { $adapterPath = Join-Path $PSScriptRoot 'TestAdapter' $env:PATH += [System.IO.Path]::PathSeparator + $adapterPath @@ -308,7 +326,8 @@ Describe 'PowerShell adapter resource tests' { $resources = $r | ConvertFrom-Json $resources.resources[0].properties.result | Should -Be $True } - finally { + finally + { $env:PATH = $oldPath } } @@ -328,14 +347,20 @@ Describe 'PowerShell adapter resource tests' { It 'Verify that there are no cache rebuilds for several sequential executions' { # remove cache file - $cacheFilePath = if ($IsWindows) { + $cacheFilePath = if ($IsWindows) + { # PS 6+ on Windows Join-Path $env:LocalAppData "dsc\PSAdapterCache.json" - } else { + } + else + { # either WinPS or PS 6+ on Linux/Mac - if ($PSVersionTable.PSVersion.Major -le 5) { + if ($PSVersionTable.PSVersion.Major -le 5) + { Join-Path $env:LocalAppData "dsc\WindowsPSAdapterCache.json" - } else { + } + else + { Join-Path $env:HOME ".dsc" "PSAdapterCache.json" } } @@ -346,7 +371,7 @@ Describe 'PowerShell adapter resource tests' { "$TestDrive/tracing.txt" | Should -FileContentMatchExactly 'Constructing Get-DscResource cache' # next executions following shortly after should Not rebuild the cache - 1..3 | %{ + 1..3 | % { dsc -l trace resource list -a Microsoft.DSC/PowerShell 2> $TestDrive/tracing.txt "$TestDrive/tracing.txt" | Should -Not -FileContentMatchExactly 'Constructing Get-DscResource cache' } From 58b9d3d24ee28b1732d0cbfd1ff95bc11bda428a Mon Sep 17 00:00:00 2001 From: "G.Reijn" Date: Mon, 9 Dec 2024 19:11:51 +0100 Subject: [PATCH 12/13] Resolve remarks --- .vscode/settings.json | 1 + .../TestClassNoVersion.psd1 | 6 +- .../Tests/powershellgroup.resource.tests.ps1 | 77 ++++++------------- 3 files changed, 26 insertions(+), 58 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 24bf5814..93224251 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -12,6 +12,7 @@ "./y2j/Cargo.toml" ], "rust-analyzer.showUnlinkedFileNotification": true, + "powershell.codeFormatting.preset": "OTBS", "json.schemas": [ { "fileMatch": ["**/*.dsc.resource.json"], diff --git a/powershell-adapter/Tests/TestClassNoVersion/TestClassNoVersion.psd1 b/powershell-adapter/Tests/TestClassNoVersion/TestClassNoVersion.psd1 index 7defc6db..659605dd 100644 --- a/powershell-adapter/Tests/TestClassNoVersion/TestClassNoVersion.psd1 +++ b/powershell-adapter/Tests/TestClassNoVersion/TestClassNoVersion.psd1 @@ -25,16 +25,16 @@ Copyright = '(c) Microsoft. All rights reserved.' # Functions to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no functions to export. - FunctionsToExport = '*' + FunctionsToExport = @() # Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export. CmdletsToExport = '*' # Variables to export from this module - VariablesToExport = '*' + VariablesToExport = @() # Aliases to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no aliases to export. - AliasesToExport = '*' + AliasesToExport = @() # DSC resources to export from this module DscResourcesToExport = 'TestClassNoVersion' diff --git a/powershell-adapter/Tests/powershellgroup.resource.tests.ps1 b/powershell-adapter/Tests/powershellgroup.resource.tests.ps1 index 21eeec90..75f01969 100644 --- a/powershell-adapter/Tests/powershellgroup.resource.tests.ps1 +++ b/powershell-adapter/Tests/powershellgroup.resource.tests.ps1 @@ -7,12 +7,9 @@ Describe 'PowerShell adapter resource tests' { $OldPSModulePath = $env:PSModulePath $env:PSModulePath += [System.IO.Path]::PathSeparator + $PSScriptRoot - if ($IsLinux -or $IsMacOS) - { + if ($IsLinux -or $IsMacOS) { $cacheFilePath = Join-Path $env:HOME ".dsc" "PSAdapterCache.json" - } - else - { + } else { $cacheFilePath = Join-Path $env:LocalAppData "dsc" "PSAdapterCache.json" } } @@ -142,20 +139,14 @@ Describe 'PowerShell adapter resource tests' { # generate the cache $null = dsc resource list '*' -a Microsoft.DSC/PowerShell # update the version in the cache file - $cacheFilePath = if ($IsWindows) - { + $cacheFilePath = if ($IsWindows) { # PS 6+ on Windows Join-Path $env:LocalAppData "dsc\PSAdapterCache.json" - } - else - { + } else { # either WinPS or PS 6+ on Linux/Mac - if ($PSVersionTable.PSVersion.Major -le 5) - { + if ($PSVersionTable.PSVersion.Major -le 5) { Join-Path $env:LocalAppData "dsc\WindowsPSAdapterCache.json" - } - else - { + } else { Join-Path $env:HOME ".dsc" "PSAdapterCache.json" } } @@ -177,8 +168,7 @@ Describe 'PowerShell adapter resource tests' { Remove-Item -Recurse -Force -Path "$PSScriptRoot/TestClassResource" $oldPath = $env:PSModulePath - try - { + try { $env:PSModulePath += [System.IO.Path]::PathSeparator + $TestDrive # generate the cache @@ -191,9 +181,7 @@ Describe 'PowerShell adapter resource tests' { $LASTEXITCODE | Should -Be 0 "$TestDrive/tracing.txt" | Should -FileContentMatchExactly 'Detected non-existent cache entry' "$TestDrive/tracing.txt" | Should -FileContentMatchExactly 'Constructing Get-DscResource cache' - } - finally - { + } finally { $env:PSModulePath = $oldPath Copy-Item -Recurse -Force -Path "$PSScriptRoot/Backup/TestClassResource" -Destination "$PSScriptRoot" Remove-Item -Recurse -Force -Path "$PSScriptRoot/Backup" @@ -241,8 +229,7 @@ Describe 'PowerShell adapter resource tests' { $oldPath = $env:PSModulePath - try - { + try { $env:PSModulePath += [System.IO.Path]::PathSeparator + $pathRoot1 $env:PSModulePath += [System.IO.Path]::PathSeparator + $pathRoot2 @@ -252,17 +239,14 @@ Describe 'PowerShell adapter resource tests' { $r = @($resources | ? { $_.Type -eq 'TestClassResource/TestClassResource' }) $r.Count | Should -Be 1 $r[0].Version | Should -Be '2.0.1' - } - finally - { + } finally { $env:PSModulePath = $oldPath } } It 'Verify adapted_dsc_type field in Get' { $oldPath = $env:PATH - try - { + try { $adapterPath = Join-Path $PSScriptRoot 'TestAdapter' $env:PATH += [System.IO.Path]::PathSeparator + $adapterPath @@ -270,17 +254,14 @@ Describe 'PowerShell adapter resource tests' { $LASTEXITCODE | Should -Be 0 $resources = $r | ConvertFrom-Json $resources.actualState.result | Should -Be $True - } - finally - { + } finally { $env:PATH = $oldPath } } It 'Verify adapted_dsc_type field in Set' { $oldPath = $env:PATH - try - { + try { $adapterPath = Join-Path $PSScriptRoot 'TestAdapter' $env:PATH += [System.IO.Path]::PathSeparator + $adapterPath @@ -289,17 +270,14 @@ Describe 'PowerShell adapter resource tests' { $resources = $r | ConvertFrom-Json $resources.beforeState.result | Should -Be $True $resources.afterState.result | Should -Be $True - } - finally - { + } finally { $env:PATH = $oldPath } } It 'Verify adapted_dsc_type field in Test' { $oldPath = $env:PATH - try - { + try { $adapterPath = Join-Path $PSScriptRoot 'TestAdapter' $env:PATH += [System.IO.Path]::PathSeparator + $adapterPath @@ -307,17 +285,14 @@ Describe 'PowerShell adapter resource tests' { $LASTEXITCODE | Should -Be 0 $resources = $r | ConvertFrom-Json $resources.actualState.result | Should -Be $True - } - finally - { + } finally { $env:PATH = $oldPath } } It 'Verify adapted_dsc_type field in Export' { $oldPath = $env:PATH - try - { + try { $adapterPath = Join-Path $PSScriptRoot 'TestAdapter' $env:PATH += [System.IO.Path]::PathSeparator + $adapterPath @@ -325,9 +300,7 @@ Describe 'PowerShell adapter resource tests' { $LASTEXITCODE | Should -Be 0 $resources = $r | ConvertFrom-Json $resources.resources[0].properties.result | Should -Be $True - } - finally - { + } finally { $env:PATH = $oldPath } } @@ -347,20 +320,14 @@ Describe 'PowerShell adapter resource tests' { It 'Verify that there are no cache rebuilds for several sequential executions' { # remove cache file - $cacheFilePath = if ($IsWindows) - { + $cacheFilePath = if ($IsWindows) { # PS 6+ on Windows Join-Path $env:LocalAppData "dsc\PSAdapterCache.json" - } - else - { + } else { # either WinPS or PS 6+ on Linux/Mac - if ($PSVersionTable.PSVersion.Major -le 5) - { + if ($PSVersionTable.PSVersion.Major -le 5) { Join-Path $env:LocalAppData "dsc\WindowsPSAdapterCache.json" - } - else - { + } else { Join-Path $env:HOME ".dsc" "PSAdapterCache.json" } } From dd460eb4f10a69916cea865b20b83aff32d8c7fe Mon Sep 17 00:00:00 2001 From: "G.Reijn" Date: Mon, 9 Dec 2024 19:35:44 +0100 Subject: [PATCH 13/13] no cmdlets --- .../Tests/TestClassNoVersion/TestClassNoVersion.psd1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/powershell-adapter/Tests/TestClassNoVersion/TestClassNoVersion.psd1 b/powershell-adapter/Tests/TestClassNoVersion/TestClassNoVersion.psd1 index 659605dd..cbe8e12b 100644 --- a/powershell-adapter/Tests/TestClassNoVersion/TestClassNoVersion.psd1 +++ b/powershell-adapter/Tests/TestClassNoVersion/TestClassNoVersion.psd1 @@ -28,7 +28,7 @@ FunctionsToExport = @() # Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export. - CmdletsToExport = '*' + CmdletsToExport = @() # Variables to export from this module VariablesToExport = @()