Skip to content

Commit

Permalink
Merge pull request #363 from mgreenegit/powershell_group
Browse files Browse the repository at this point in the history
PowerShell adapter PR
  • Loading branch information
SteveL-MSFT authored Apr 12, 2024
2 parents ef36815 + 98f4126 commit 079a90f
Show file tree
Hide file tree
Showing 27 changed files with 1,382 additions and 465 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ node_modules/
tree-sitter-dscexpression/bindings/
tree-sitter-dscexpression/src/
tree-sitter-dscexpression/parser.*
tree-sitter-dscexpression/binding.gyp
tree-sitter-dscexpression/binding.gyp
23 changes: 17 additions & 6 deletions build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,18 @@ $skip_test_projects_on_windows = @("tree-sitter-dscexpression")

if (Test-Path "./copy_files.txt") {
Get-Content "./copy_files.txt" | ForEach-Object {
Copy-Item $_ $target -Force -ErrorAction Ignore
# if the line contains a '\' character, throw an error
if ($_ -match '\\') {
throw "copy_files.txt should use '/' as the path separator"
}
# copy the file to the target directory, creating the directory path if needed
$fileCopyPath = $_.split('/')
if ($fileCopyPath.Length -gt 1) {
$fileCopyPath = $fileCopyPath[0..($fileCopyPath.Length - 2)]
$fileCopyPath = $fileCopyPath -join '/'
New-Item -ItemType Directory -Path "$target/$fileCopyPath" -Force -ErrorAction Ignore | Out-Null
}
Copy-Item $_ "$target/$_" -Force -ErrorAction Ignore
}
}

Expand All @@ -199,6 +210,8 @@ $skip_test_projects_on_windows = @("tree-sitter-dscexpression")
}
}

Save-PSResource -Path $target -Name 'PSDesiredStateConfiguration' -Version '2.0.7' -Repository PSGallery -TrustRepository

if ($failed) {
Write-Host -ForegroundColor Red "Build failed"
exit 1
Expand Down Expand Up @@ -243,14 +256,12 @@ if ($Test) {
$FullyQualifiedName = @{ModuleName="PSDesiredStateConfiguration";ModuleVersion="2.0.7"}
if (-not(Get-Module -ListAvailable -FullyQualifiedName $FullyQualifiedName))
{ "Installing module PSDesiredStateConfiguration 2.0.7"
Set-PSRepository -Name 'PSGallery' -InstallationPolicy Trusted
Install-Module PSDesiredStateConfiguration -RequiredVersion 2.0.7
Install-PSResource -Name PSDesiredStateConfiguration -Version 2.0.7 -Repository PSGallery -TrustRepository
}

if (-not(Get-Module -ListAvailable -Name Pester))
{ "Installing module Pester"
Set-PSRepository -Name 'PSGallery' -InstallationPolicy Trusted
Install-Module Pester -WarningAction Ignore
Install-PSResource Pester -WarningAction Ignore -Repository PSGallery -TrustRepository
}

foreach ($project in $projects) {
Expand Down Expand Up @@ -295,7 +306,7 @@ if ($Test) {
if (-not(Get-Module -ListAvailable -Name Pester))
{ "Installing module Pester"
$InstallTargetDir = ($env:PSModulePath -split ";")[0]
Find-Module -Name 'Pester' -Repository 'PSGallery' | Save-Module -Path $InstallTargetDir
Find-PSResource -Name 'Pester' -Repository 'PSGallery' | Save-PSResource -Path $InstallTargetDir -TrustRepository
}

"Updated Pester module location:"
Expand Down
3 changes: 2 additions & 1 deletion dsc/tests/dsc_config_get.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ Describe 'dsc config get tests' {
It 'will fail if resource schema does not match' -Skip:(!$IsWindows) {
$jsonPath = Join-Path $PSScriptRoot '../examples/invalid_schema.dsc.yaml'
$config = Get-Content $jsonPath -Raw
$null = $config | dsc config get | ConvertFrom-Json
$testError = & {$config | dsc config get get 2>&1}
$testError[0] | Should -match 'error:'
$LASTEXITCODE | Should -Be 2
}

Expand Down
3 changes: 2 additions & 1 deletion dsc/tests/dsc_get.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ Describe 'resource get tests' {
"Name": "ProductName"
}
'@
$json | dsc resource get -r Microsoft.Windows/registry
$testError = & {$json | dsc resource get -r Microsoft.Windows/registry get 2>&1}
$testError[0] | SHould -match 'error:'
$LASTEXITCODE | Should -Be 2
}
}
23 changes: 14 additions & 9 deletions dsc/tests/dsc_parameters.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ Describe 'Parameters tests' {
"@
$params_json = @{ parameters = @{ param1 = $value }} | ConvertTo-Json

$null = $config_yaml | dsc config -p $params_json get
$testError = & {$config_yaml | dsc config -p $params_json get 2>&1}
$testError | Should -match 'Parameter input failure:'
$LASTEXITCODE | Should -Be 4
}

Expand All @@ -108,8 +109,9 @@ Describe 'Parameters tests' {
"@
$params_json = @{ parameters = @{ param1 = $value }} | ConvertTo-Json

$null = $config_yaml | dsc config -p $params_json get
$LASTEXITCODE | Should -Be 4
$testError = & {$config_yaml | dsc config -p $params_json get get 2>&1}
$testError[0] | Should -match 'error'
$LASTEXITCODE | Should -Be 2
}

It 'Input number value is out of range for <min> and <max>' -TestCases @(
Expand All @@ -134,8 +136,9 @@ Describe 'Parameters tests' {
"@
$params_json = @{ parameters = @{ param1 = $value }} | ConvertTo-Json

$null = $config_yaml | dsc config -p $params_json get
$LASTEXITCODE | Should -Be 4
$testError = & {$config_yaml | dsc config -p $params_json get get 2>&1}
$testError[0] | Should -match 'error'
$LASTEXITCODE | Should -Be 2
}

It 'Input is not in the allowed value list for <type>' -TestCases @(
Expand All @@ -158,8 +161,9 @@ Describe 'Parameters tests' {
"@
$params_json = @{ parameters = @{ param1 = $value }} | ConvertTo-Json

$null = $config_yaml | dsc config -p $params_json get
$LASTEXITCODE | Should -Be 4
$testError = & {$config_yaml | dsc config -p $params_json get get 2>&1}
$testError[0] | Should -match 'error'
$LASTEXITCODE | Should -Be 2
}

It 'Length constraint is incorrectly applied to <type> with <constraint>' -TestCases @(
Expand All @@ -184,8 +188,9 @@ Describe 'Parameters tests' {
"@
$params_json = @{ parameters = @{ param1 = $value }} | ConvertTo-Json

$null = $config_yaml | dsc config -p $params_json get | ConvertFrom-Json
$LASTEXITCODE | Should -Be 4
$testError = & {$config_yaml | dsc config -p $params_json get get 2>&1}
$testError[0] | Should -match 'error'
$LASTEXITCODE | Should -Be 2
}

It 'Default value is used when not provided' {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

enum EnsureEnumeration {
Absent
Present
Expand All @@ -14,7 +17,7 @@ function Get-TargetResource {
)

$returnValue = @{
Ensure = [EnsureEnumeration]::Absent
Ensure = ([EnsureEnumeration]::Absent).ToString()
Name = $Name
SourceLocation = $null
ScriptSourceLocation = $null
Expand All @@ -27,7 +30,7 @@ function Get-TargetResource {
}

if ($Name -eq "TestPSRepository1") {
$returnValue.Ensure = [EnsureEnumeration]::Present
$returnValue.Ensure = ([EnsureEnumeration]::Present).ToString()
$returnValue.SourceLocation = 'https://www.powershellgallery.com/api/v2'
$returnValue.ScriptSourceLocation = 'https://www.powershellgallery.com/api/v2/items/psscript'
$returnValue.PublishLocation = 'https://www.powershellgallery.com/api/v2/package/'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
@{
RootModule = 'TestClassResource.psm1'
ModuleVersion = '1.0.0'
GUID = '5d73a601-4a6c-43c5-ba3f-619b18bbb404'
Author = 'Microsoft Corporation'
CompanyName = 'Microsoft Corporation'
Copyright = '(c) Microsoft Corporation. All rights reserved.'
Description = 'PowerShell module for testing DSCv3'
PowerShellVersion = '5.0'
DscResourcesToExport = 'TestClassResource'
PowerShellVersion = '5.1'
DscResourcesToExport = 'TestPSRepository'
FunctionsToExport = @(
'Test-World')
VariablesToExport = '@()'
VariablesToExport = @()
AliasesToExport = @()
PrivateData = @{
PSData = @{
Expand All @@ -20,6 +21,7 @@
'PSEdition_Core',
'Linux',
'Mac')
DscCapabilities = @('Get', 'Test')
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

@{

# Script module or binary module file associated with this manifest.
RootModule = 'TestClassResource.psm1'

# Version number of this module.
ModuleVersion = '0.0.1'

# ID used to uniquely identify this module
GUID = 'b267fa32-e77d-48e6-9248-676cc6f2327f'

# 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 = 'TestClassResource'

# 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'
)
}
}

}

Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

using namespace System.Collections.Generic

enum EnumPropEnumeration {
Expand All @@ -15,7 +18,7 @@ class TestClassResource
[string] $Prop1

[DscProperty()]
[EnumPropEnumeration] $EnumProp
[string] $EnumProp

[void] Set()
{
Expand Down Expand Up @@ -43,7 +46,7 @@ class TestClassResource
{
$this.Prop1 = $env:DSC_CONFIG_ROOT
}
$this.EnumProp = [EnumPropEnumeration]::Expected
$this.EnumProp = ([EnumPropEnumeration]::Expected).ToString()
return $this
}

Expand Down
7 changes: 6 additions & 1 deletion powershell-adapter/Tests/class_ps_resources.dsc.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

$schema: https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/2023/10/config/document.json
resources:
- name: Working with classic DSC resources
Expand All @@ -9,6 +12,8 @@ resources:
properties:
Name: TestPSRepository1
- name: Class-resource Info
type: PSTestModule/TestClassResource
type: TestClassResource/TestClassResource
properties:
Name: TestClassResource1
Prop1: ValueForProp1
EnumProp: Expected
5 changes: 4 additions & 1 deletion powershell-adapter/Tests/native_and_powershell.dsc.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

# Example configuration mixing native app resources with classic PS resources
$schema: https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/2023/10/config/document.json
resources:
Expand All @@ -20,4 +23,4 @@ resources:
properties:
keyPath: HKLM\Software\Microsoft\Windows NT\CurrentVersion
valueName: ProductName
_ensure: Present
_ensure: Present
Loading

0 comments on commit 079a90f

Please sign in to comment.