Skip to content

Fix WinPS adapter tests #1017

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Aug 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ param(
$env:RUSTC_LOG=$null
$env:RUSTFLAGS='-Dwarnings'

trap {
Write-Error "An error occurred: $($_ | Out-String)"
exit 1
}

if ($Verbose) {
$env:RUSTC_LOG='rustc_codegen_ssa::back::link=info'
}
Expand Down Expand Up @@ -569,11 +574,7 @@ if ($Test) {
(Get-Module -Name Pester -ListAvailable).Path
}

try {
Invoke-Pester -ErrorAction Stop
} catch {
throw "Pester had unexpected error: $($_.Exception.Message)"
}
Invoke-Pester -Output Detailed -ErrorAction Stop
}

function Find-MakeAppx() {
Expand Down
46 changes: 35 additions & 11 deletions dsc/tests/dsc_extension_discover.tests.ps1
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

BeforeDiscovery {
try {
$windowWidth = [Console]::WindowWidth
} catch {
$consoleUnavailable = $true
}
}

Describe 'Discover extension tests' {
BeforeAll {
$oldPath = $env:PATH
Expand All @@ -15,15 +23,31 @@ Describe 'Discover extension tests' {
It 'Discover extensions' {
$out = dsc extension list | ConvertFrom-Json
$LASTEXITCODE | Should -Be 0
$out.Count | Should -Be 2 -Because ($out | Out-String)
$out[0].type | Should -Be 'Microsoft.DSC.Extension/Bicep'
$out[0].version | Should -Be '0.1.0'
$out[0].capabilities | Should -BeExactly @('import')
$out[0].manifest | Should -Not -BeNullOrEmpty
$out[1].type | Should -BeExactly 'Test/Discover'
$out[1].version | Should -BeExactly '0.1.0'
$out[1].capabilities | Should -BeExactly @('discover')
$out[1].manifest | Should -Not -BeNullOrEmpty
if ($IsWindows) {
$out.Count | Should -Be 3 -Because ($out | Out-String)
$out[0].type | Should -Be 'Microsoft.DSC.Extension/Bicep'
$out[0].version | Should -Be '0.1.0'
$out[0].capabilities | Should -BeExactly @('import')
$out[0].manifest | Should -Not -BeNullOrEmpty
$out[1].type | Should -Be 'Microsoft.Windows.Appx/Discover'
$out[1].version | Should -Be '0.1.0'
$out[1].capabilities | Should -BeExactly @('discover')
$out[1].manifest | Should -Not -BeNullOrEmpty
$out[2].type | Should -BeExactly 'Test/Discover'
$out[2].version | Should -BeExactly '0.1.0'
$out[2].capabilities | Should -BeExactly @('discover')
$out[2].manifest | Should -Not -BeNullOrEmpty
} else {
$out.Count | Should -Be 2 -Because ($out | Out-String)
$out[0].type | Should -Be 'Microsoft.DSC.Extension/Bicep'
$out[0].version | Should -Be '0.1.0'
$out[0].capabilities | Should -BeExactly @('import')
$out[0].manifest | Should -Not -BeNullOrEmpty
$out[1].type | Should -BeExactly 'Test/Discover'
$out[1].version | Should -BeExactly '0.1.0'
$out[1].capabilities | Should -BeExactly @('discover')
$out[1].manifest | Should -Not -BeNullOrEmpty
}
}

It 'Filtering works for extension discovered resources' {
Expand Down Expand Up @@ -94,12 +118,12 @@ Describe 'Discover extension tests' {
}
}

It 'Table can be not truncated' {
It 'Table can be not truncated' -Skip:($consoleUnavailable) {
$output = dsc extension list --output-format table-no-truncate
$LASTEXITCODE | Should -Be 0
$foundWideLine = $false
foreach ($line in $output) {
if ($line.Length -gt [Console]::WindowWidth) {
if ($line.Length -gt $windowWidth) {
$foundWideLine = $true
}
}
Expand Down
12 changes: 10 additions & 2 deletions dsc/tests/dsc_resource_list.tests.ps1
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

BeforeDiscovery {
try {
$windowWidth = [Console]::WindowWidth
} catch {
$consoleUnavailable = $true
}
}

Describe 'Tests for listing resources' {
It 'dsc resource list' {
$resources = dsc resource list | ConvertFrom-Json -Depth 10
Expand Down Expand Up @@ -89,12 +97,12 @@ Describe 'Tests for listing resources' {
$out | Should -BeLike "*ERROR*Adapter not found: foo`*"
}

It 'Table is not truncated' {
It 'Table is not truncated' -Skip:($consoleUnavailable) {
$output = dsc resource list --output-format table-no-truncate
$LASTEXITCODE | Should -Be 0
$foundWideLine = $false
foreach ($line in $output) {
if ($line.Length -gt [Console]::WindowWidth) {
if ($line.Length -gt $windowWidth) {
$foundWideLine = $true
break
}
Expand Down
1 change: 1 addition & 0 deletions dsc_lib/locales/en-us.toml
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ processChildExit = "Process '%{executable}' id %{id} exited with code %{code}"
processChildTerminated = "Process '%{executable}' id %{id} terminated by signal"
processTerminated = "Process terminated by signal"
commandInvoke = "Invoking command '%{executable}' with args %{args}"
commandCwd = "Current working directory: %{cwd}"
noArgs = "No args to process"
parseAsEnvVars = "Parsing input as environment variables"
parseAsStdin = "Parsing input as stdin"
Expand Down
21 changes: 7 additions & 14 deletions dsc_lib/src/discovery/command_discovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,10 +286,10 @@ impl ResourceDiscovery for CommandDiscovery {
if manifest.kind == Some(Kind::Adapter) {
trace!("{}", t!("discovery.commandDiscovery.adapterFound", adapter = resource.type_name));
insert_resource(&mut adapters, &resource, true);
} else {
trace!("{}", t!("discovery.commandDiscovery.resourceFound", resource = resource.type_name));
insert_resource(&mut resources, &resource, true);
}
// also make sure to add adapters as a resource as well
trace!("{}", t!("discovery.commandDiscovery.resourceFound", resource = resource.type_name));
insert_resource(&mut resources, &resource, true);
}
}
}
Expand Down Expand Up @@ -563,27 +563,20 @@ impl ResourceDiscovery for CommandDiscovery {

// TODO: This should be a BTreeMap of the resource name and a BTreeMap of the version and DscResource, this keeps it version sorted more efficiently
fn insert_resource(resources: &mut BTreeMap<String, Vec<DscResource>>, resource: &DscResource, skip_duplicate_version: bool) {
if resources.contains_key(&resource.type_name) {
let Some(resource_versions) = resources.get_mut(&resource.type_name) else {
resources.insert(resource.type_name.clone(), vec![resource.clone()]);
return;
};
if let Some(resource_versions) = resources.get_mut(&resource.type_name) {
debug!("Resource '{}' already exists, checking versions", resource.type_name);
// compare the resource versions and insert newest to oldest using semver
let mut insert_index = resource_versions.len();
for (index, resource_instance) in resource_versions.iter().enumerate() {
let resource_instance_version = match Version::parse(&resource_instance.version) {
Ok(v) => v,
Err(err) => {
// write as info since PowerShell resources tend to have invalid semver
info!("Resource '{}' has invalid version: {err}", resource_instance.type_name);
Err(_err) => {
continue;
},
};
let resource_version = match Version::parse(&resource.version) {
Ok(v) => v,
Err(err) => {
// write as info since PowerShell resources tend to have invalid semver
info!("Resource '{}' has invalid version: {err}", resource.type_name);
Err(_err) => {
continue;
},
};
Expand Down
3 changes: 3 additions & 0 deletions dsc_lib/src/dscresources/command_resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -697,6 +697,9 @@ async fn run_process_async(executable: &str, args: Option<Vec<String>>, input: O
#[allow(clippy::implicit_hasher)]
pub fn invoke_command(executable: &str, args: Option<Vec<String>>, input: Option<&str>, cwd: Option<&str>, env: Option<HashMap<String, String>>, exit_codes: Option<&HashMap<i32, String>>) -> Result<(i32, String, String), DscError> {
debug!("{}", t!("dscresources.commandResource.commandInvoke", executable = executable, args = args : {:?}));
if let Some(cwd) = cwd {
debug!("{}", t!("dscresources.commandResource.commandCwd", cwd = cwd));
}

tokio::runtime::Builder::new_multi_thread()
.enable_all()
Expand Down
8 changes: 6 additions & 2 deletions extensions/appx/appx.tests.ps1
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

Describe 'Tests for Appx resource discovery' -Skip:(!$IsWindows){
BeforeDiscovery {
$runningInCI = $null -ne $env:GITHUB_RUN_ID
}

Describe 'Tests for Appx resource discovery' -Skip:(!$IsWindows -or $runningInCI) {
It 'Should find DSC appx resources' {
$out = dsc resource list | ConvertFrom-Json
$LASTEXITCODE | Should -Be 0
$found = $false
foreach ($resource in $out) {
if ($resource.directory.StartsWith("$env:ProgramFiles\WindowsApps\Microsoft.DesiredStateConfiguration-Private")) {
if ($resource.directory.StartsWith("$env:ProgramFiles\WindowsApps")) {
$found = $true
break
}
Expand Down
4 changes: 3 additions & 1 deletion extensions/bicep/bicep.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Describe 'Bicep extension tests' -Skip:(!$foundBicep) {
$out = dsc -l trace config get -f $bicepFile 2>$TestDrive/error.log | ConvertFrom-Json
$LASTEXITCODE | Should -Be 0 -Because (Get-Content -Path $TestDrive/error.log -Raw | Out-String)
$out.results[0].result.actualState.output | Should -BeExactly 'Hello, world!'
$bicepFile = $bicepFile.ToString().Replace('\', '\\')
(Get-Content -Path $TestDrive/error.log -Raw) | Should -Match "Importing file '$bicepFile' with extension 'Microsoft.DSC.Extension/Bicep'"
}

Expand All @@ -28,9 +29,10 @@ resource invalid 'Microsoft.DSC.Extension/Bicep:1.0' = {
properties: {
output: 'This is invalid'
"@
$out = dsc -l trace config get -f $bicepFile 2>$TestDrive/error.log | ConvertFrom-Json
dsc -l trace config get -f $bicepFile 2>$TestDrive/error.log | ConvertFrom-Json
$LASTEXITCODE | Should -Be 4 -Because (Get-Content -Path $TestDrive/error.log -Raw | Out-String)
$content = (Get-Content -Path $TestDrive/error.log -Raw)
$bicepFile = $bicepFile.ToString().Replace('\', '\\')
$content | Should -Match "Importing file '$bicepFile' with extension 'Microsoft.DSC.Extension/Bicep'"
$content | Should -Match "BCP033"
}
Expand Down
3 changes: 2 additions & 1 deletion powershell-adapter/Tests/powershellgroup.config.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ Describe 'PowerShell adapter resource tests' {
$cacheFilePath = Join-Path $env:LocalAppData "dsc" "PSAdapterCache.json"
}
}

AfterAll {
$env:PSModulePath = $OldPSModulePath
}

BeforeEach {
Remove-Item -Force -ea SilentlyContinue -Path $cacheFilePath
Remove-Item -Force -ErrorAction Ignore -Path $cacheFilePath
}

It 'Get works on config with class-based resources' {
Expand Down
5 changes: 3 additions & 2 deletions powershell-adapter/Tests/powershellgroup.resource.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ Describe 'PowerShell adapter resource tests' {
$cacheFilePath = Join-Path $env:LocalAppData "dsc" "PSAdapterCache.json"
}
}

AfterAll {
$env:PSModulePath = $OldPSModulePath
}

BeforeEach {
Remove-Item -Force -ea SilentlyContinue -Path $cacheFilePath
Remove-Item -Force -ErrorAction Ignore -Path $cacheFilePath
}

It 'Discovery includes class-based resources' {
Expand Down Expand Up @@ -173,7 +174,7 @@ Describe 'PowerShell adapter resource tests' {
# remove the module files
Remove-Item -Recurse -Force -Path "$TestDrive/TestClassResource"
# verify that cache rebuid happened
dsc -l trace resource list '*' -a Microsoft.DSC/PowerShell 2> $TestDrive/tracing.txt
$null = dsc -l trace resource list '*' -a Microsoft.DSC/PowerShell 2> $TestDrive/tracing.txt

$LASTEXITCODE | Should -Be 0
"$TestDrive/tracing.txt" | Should -FileContentMatchExactly 'Detected non-existent cache entry'
Expand Down
Loading
Loading