Team Ai
Datasetpublic

MegaBites-AI/Windows-powershell

sourceHugging Facemitupdated 6mo agoView on Hugging Face
0likes372downloads
dsc.profileresource.Tests.ps1215 linesDownload Raw Back to dsc
1# Copyright (c) Microsoft Corporation.2# Licensed under the MIT License.3 4Describe "DSC PowerShell Profile Resource Tests" -Tag "CI" {5    BeforeAll {6        $DSC_ROOT = $env:DSC_ROOT7        $skipCleanup = $false8 9        if (-not (Test-Path -Path $DSC_ROOT)) {10            $skipCleanup = $true11            throw "DSC_ROOT environment variable is not set or path does not exist."12        }13 14        Write-Verbose "DSC_ROOT is set to $DSC_ROOT" -Verbose15 16        $originalPath = $env:PATH17 18        $pathSeparator = [System.IO.Path]::PathSeparator19        $env:PATH += "$pathSeparator$DSC_ROOT"20        $env:PATH += "$pathSeparator$PSHome"21 22        Write-Verbose "Updated PATH to include DSC_ROOT: $env:PATH" -Verbose23 24        # Ensure DSC v3 is available25        if (-not (Get-Command -name dsc -CommandType Application -ErrorAction SilentlyContinue)) {26            Get-ChildItem $DSC_ROOT -Recurse 'dsc' | ForEach-Object {27                Write-Verbose "Found DSC executable at $($_.FullName)" -Verbose28            }29            throw "DSC v3 is not installed"30        }31 32        $dscExe = Get-Command -name dsc -CommandType Application | Select-Object -First 133 34        $testProfileContent = "# Test profile content currentuser currenthost"35        $testProfilePathCurrentUserCurrentHost = $PROFILE.CurrentUserCurrentHost36        Copy-Item -Path $testProfilePathCurrentUserCurrentHost -Destination "$TestDrive/currentuser-currenthost-profile.bak" -Force -ErrorAction SilentlyContinue37        New-Item -Path $testProfilePathCurrentUserCurrentHost -Value $testProfileContent -Force -ItemType File38 39        $testProfileContent = "# Test profile content currentuser allhosts"40        $testProfilePathCurrentUserAllHosts = $PROFILE.CurrentUserAllHosts41        Copy-Item -Path $testProfilePathCurrentUserAllHosts -Destination "$TestDrive/currentuser-allhosts-profile.bak" -Force -ErrorAction SilentlyContinue42        New-Item -Path $testProfilePathCurrentUserAllHosts -Value $testProfileContent -Force -ItemType File43    }44    AfterAll {45        if ($skipCleanup) {46            return47        }48 49        # Restore original profile50        $testProfilePathCurrentUserCurrentHost = $PROFILE.CurrentUserCurrentHost51        if (Test-Path "$TestDrive/currentuser-currenthost-profile.bak") {52            Copy-Item -Path "$TestDrive/currentuser-currenthost-profile.bak" -Destination $testProfilePathCurrentUserCurrentHost -Force -ErrorAction SilentlyContinue53        }54        else {55            Remove-Item $testProfilePathCurrentUserCurrentHost -Force -ErrorAction SilentlyContinue56        }57 58        $testProfilePathCurrentUserAllHosts = $PROFILE.CurrentUserAllHosts59        if (Test-Path "$TestDrive/currentuser-allhosts-profile.bak") {60            Copy-Item -Path "$TestDrive/currentuser-allhosts-profile.bak" -Destination $testProfilePathCurrentUserAllHosts -Force -ErrorAction SilentlyContinue61        }62        else {63            Remove-Item $testProfilePathCurrentUserAllHosts -Force -ErrorAction SilentlyContinue64        }65 66        $env:PATH = $originalPath67        Remove-Item -Path "$TestDrive/currentuser-currenthost-profile.bak" -Force -ErrorAction SilentlyContinue68        Remove-Item -Path "$TestDrive/currentuser-allhosts-profile.bak" -Force -ErrorAction SilentlyContinue69    }70 71    It 'DSC resource is located at $PSHome' {72        $resourceFile = Join-Path -Path $PSHome -ChildPath 'pwsh.profile.resource.ps1'73        $resourceFile | Should -Exist74 75        $resourceManifest = Join-Path -Path $PSHome -ChildPath 'pwsh.profile.dsc.resource.json'76        $resourceManifest | Should -Exist77    }78 79    It 'DSC resource can be found' {80        (& $dscExe resource list -o json | ConvertFrom-Json  | Select-Object -Property type).type | Should -Contain 'Microsoft.PowerShell/Profile'81    }82 83    It 'DSC resource can set current user current host profile' {84        $setOutput = (& $dscExe config set --file $PSScriptRoot/psprofile_currentuser_currenthost.dsc.yaml -o json) | ConvertFrom-Json85        $expectedContent = "Write-Host 'Welcome to your PowerShell profile - CurrentUserCurrentHost!'"86        $setOutput.results.result.afterState.content | Should -BeExactly $expectedContent87    }88 89    It 'DSC resource can get current user current host profile' {90        $getOutput = (& $dscExe config get --file $PSScriptRoot/psprofile_currentuser_currenthost.dsc.yaml -o json) | ConvertFrom-Json91        $expectedContent = "Write-Host 'Welcome to your PowerShell profile - CurrentUserCurrentHost!'"92        $getOutput.results.result.actualState.content | Should -BeExactly $expectedContent93    }94 95    It 'DSC resource can set content as empty for current user current host profile' -Pending {96        $setOutput = (& $dscExe config set --file $PSScriptRoot/psprofile_currentuser_currenthost_emptycontent.dsc.yaml -o json) | ConvertFrom-Json97        $setOutput.results.result.afterState.content | Should -BeExactly ''98    }99 100    It 'DSC resource can set current user all hosts profile' {101        $setOutput = (& $dscExe config set --file $PSScriptRoot/psprofile_currentuser_allhosts.dsc.yaml -o json) | ConvertFrom-Json102        $expectedContent = "Write-Host 'Welcome to your PowerShell profile - CurrentUserAllHosts!'"103        $setOutput.results.result.afterState.content | Should -BeExactly $expectedContent104    }105 106    It 'DSC resource can get current user all hosts profile' {107        $getOutput = (& $dscExe config get --file $PSScriptRoot/psprofile_currentuser_allhosts.dsc.yaml -o json) | ConvertFrom-Json108        $expectedContent = "Write-Host 'Welcome to your PowerShell profile - CurrentUserAllHosts!'"109        $getOutput.results.result.actualState.content | Should -BeExactly $expectedContent110    }111 112    It 'DSC resource can export all profiles' {113        $exportOutput = (& $dscExe config export --file $PSScriptRoot/psprofile_export.dsc.yaml -o json) | ConvertFrom-Json114 115        $exportOutput.resources | Should -HaveCount 4116 117        $exportOutput.resources | ForEach-Object {118            $_.type | Should -Be 'Microsoft.PowerShell/Profile'119            $_.name | Should -BeIn @('AllUsersCurrentHost', 'AllUsersAllHosts', 'CurrentUserCurrentHost', 'CurrentUserAllHosts')120        }121    }122}123 124Describe "DSC PowerShell Profile resource elevated tests" -Tag "CI", 'RequireAdminOnWindows', 'RequireSudoOnUnix' {125    BeforeAll {126        $DSC_ROOT = $env:DSC_ROOT127        $skipCleanup = $false128 129        if (-not (Test-Path -Path $DSC_ROOT)) {130            $skipCleanup = $true131            throw "DSC_ROOT environment variable is not set or path does not exist."132        }133 134        Write-Verbose "DSC_ROOT is set to $DSC_ROOT" -Verbose135 136        $originalPath = $env:PATH137 138        $pathSeparator = [System.IO.Path]::PathSeparator139        $env:PATH += "$pathSeparator$DSC_ROOT"140        $env:PATH += "$pathSeparator$PSHome"141 142        Write-Verbose "Updated PATH to include DSC_ROOT: $env:PATH" -Verbose143 144        # Ensure DSC v3 is available145        if (-not (Get-Command -name dsc -CommandType Application -ErrorAction SilentlyContinue)) {146            Get-ChildItem $DSC_ROOT -Recurse 'dsc' | ForEach-Object {147                Write-Verbose "Found DSC executable at $($_.FullName)" -Verbose148            }149            throw "DSC v3 is not installed"150        }151 152        $dscExe = Get-Command -name dsc -CommandType Application | Select-Object -First 1153 154        $testProfileContent = "# Test profile content allusers currenthost"155        $testProfilePathAllUsersCurrentHost = $PROFILE.AllUsersCurrentHost156        Copy-Item -Path $testProfilePathAllUsersCurrentHost -Destination "$TestDrive/allusers-currenthost-profile.bak" -Force -ErrorAction SilentlyContinue157        New-Item -Path $testProfilePathAllUsersCurrentHost -Value $testProfileContent -Force -ItemType File158 159        $testProfileContent = "# Test profile content allusers allhosts"160        $testProfilePathAllUsersAllHosts = $PROFILE.AllUsersAllHosts161        Copy-Item -Path $testProfilePathAllUsersAllHosts -Destination "$TestDrive/allusers-allhosts-profile.bak" -Force -ErrorAction SilentlyContinue162        New-Item -Path $testProfilePathAllUsersAllHosts -Value $testProfileContent -Force -ItemType File163    }164    AfterAll {165        if ($skipCleanup) {166            return167        }168 169        $env:PATH = $originalPath170 171        $testProfilePathAllUsersCurrentHost = $PROFILE.AllUsersCurrentHost172        if (Test-Path "$TestDrive/allusers-currenthost-profile.bak") {173            Copy-Item -Path "$TestDrive/allusers-currenthost-profile.bak" -Destination $testProfilePathAllUsersCurrentHost -Force -ErrorAction SilentlyContinue174        }175        else {176            Remove-Item $testProfilePathAllUsersCurrentHost -Force -ErrorAction SilentlyContinue177        }178 179        $testProfilePathAllUsersAllHosts = $PROFILE.AllUsersAllHosts180        if (Test-Path "$TestDrive/allusers-allhosts-profile.bak") {181            Copy-Item -Path "$TestDrive/allusers-allhosts-profile.bak" -Destination $testProfilePathAllUsersAllHosts -Force -ErrorAction SilentlyContinue182        }183        else {184            Remove-Item $testProfilePathAllUsersAllHosts -Force -ErrorAction SilentlyContinue185        }186 187        Remove-Item -Path "$TestDrive/currentuser-allhosts-profile.bak" -Force -ErrorAction SilentlyContinue188        Remove-Item -Path "$TestDrive/allusers-allhosts-profile.bak" -Force -ErrorAction SilentlyContinue189    }190 191    It 'DSC resource can set all users all hosts profile' {192        $setOutput = (& $dscExe config set --file $PSScriptRoot/psprofile_alluser_allhost.dsc.yaml -o json) | ConvertFrom-Json193        $expectedContent = "Write-Host 'Welcome to your PowerShell profile - AllUsersAllHosts!'"194        $setOutput.results.result.afterState.content | Should -BeExactly $expectedContent195    }196 197    It 'DSC resource can get all users all hosts profile' {198        $getOutput = (& $dscExe config get --file $PSScriptRoot/psprofile_alluser_allhost.dsc.yaml -o json) | ConvertFrom-Json199        $expectedContent = "Write-Host 'Welcome to your PowerShell profile - AllUsersAllHosts!'"200        $getOutput.results.result.actualState.content | Should -BeExactly $expectedContent201    }202 203    It 'DSC resource can set all users current hosts profile' {204        $setOutput = (& $dscExe config set --file $PSScriptRoot/psprofile_allusers_currenthost.dsc.yaml -o json) | ConvertFrom-Json205        $expectedContent = "Write-Host 'Welcome to your PowerShell profile - AllUsersCurrentHost!'"206        $setOutput.results.result.afterState.content | Should -BeExactly $expectedContent207    }208 209    It 'DSC resource can get all users current hosts profile' {210        $getOutput = (& $dscExe config get --file $PSScriptRoot/psprofile_allusers_currenthost.dsc.yaml -o json) | ConvertFrom-Json211        $expectedContent = "Write-Host 'Welcome to your PowerShell profile - AllUsersCurrentHost!'"212        $getOutput.results.result.actualState.content | Should -BeExactly $expectedContent213    }214}215