Skip to content

Commit d1293c1

Browse files
author
Kapil Borle
committed
Add tests for ModuleDependencyHandler
1 parent 9411a82 commit d1293c1

File tree

2 files changed

+119
-0
lines changed

2 files changed

+119
-0
lines changed

Tests/Engine/MissingDSCResource.ps1

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Configuration SomeConfiguration
2+
{
3+
Import-DscResource -ModuleName MyDSCResource
4+
}
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
if (!(Get-Module PSScriptAnalyzer) -and !$testingLibraryUsage)
2+
{
3+
Import-Module PSScriptAnalyzer
4+
}
5+
6+
if ($testingLibraryUsage)
7+
{
8+
return
9+
}
10+
11+
$directory = Split-Path -Parent $MyInvocation.MyCommand.Path
12+
$violationFileName = 'MissingDSCResource.ps1'
13+
$violationFilePath = Join-Path $directory $violationFileName
14+
15+
Describe "Resolve DSC Resource Dependency" {
16+
17+
Function Test-EnvironmentVariables($oldEnv)
18+
{
19+
$newEnv = Get-Item Env:\* | Sort-Object -Property Key
20+
$newEnv.Count | Should Be $oldEnv.Count
21+
foreach ($index in 1..$newEnv.Count)
22+
{
23+
$newEnv[$index].Key | Should Be $oldEnv[$index].Key
24+
$newEnv[$index].Value | Should Be $oldEnv[$index].Value
25+
}
26+
}
27+
28+
Context "Module handler class" {
29+
$oldEnvVars = Get-Item Env:\* | Sort-Object -Property Key
30+
$oldPSModulePath = $env:PSModulePath
31+
It "Sets defaults correctly" {
32+
$depHandler = [Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.ModuleDependencyHandler]::new()
33+
34+
$expectedPath = [System.Environment]::GetEnvironmentVariable("TEMP");
35+
$depHandler.TempPath | Should Be $expectedPath
36+
37+
$expectedLocalAppDataPath = [System.Environment]::GetEnvironmentVariable("LOCALAPPDATA");
38+
$depHandler.LocalAppDataPath | Should Be $expectedLocalAppDataPath
39+
40+
$expectedModuleRepository = "PSGallery"
41+
$depHandler.ModuleRepository | Should Be $expectedModuleRepository
42+
43+
$expectedPssaAppDataPath = Join-Path $depHandler.LocalAppDataPath "PSScriptAnalyzer"
44+
$depHandler.PSSAAppDataPath | Should Be $expectedPssaAppDataPath
45+
46+
$expectedPSModulePath = $oldPSModulePath + [System.IO.Path]::PathSeparator + $depHandler.TempModulePath
47+
$env:PSModulePath | Should Be $expectedPSModulePath
48+
49+
$depHandler.Dispose()
50+
}
51+
52+
It "Keeps the environment variables unchanged" {
53+
Test-EnvironmentVariables($oldEnvVars)
54+
}
55+
}
56+
57+
Context "Invoke-ScriptAnalyzer without switch" {
58+
It "Has parse errors" {
59+
$dr = Invoke-ScriptAnalyzer -Path $violationFilePath -ErrorVariable parseErrors -ErrorAction SilentlyContinue
60+
$parseErrors.Count | Should Be 1
61+
}
62+
}
63+
64+
Context "Invoke-ScriptAnalyzer with switch" {
65+
$oldEnvVars = Get-Item Env:\* | Sort-Object -Property Key
66+
$moduleName = "MyDscResource"
67+
$modulePath = Join-Path (Join-Path (Join-Path (Split-Path $directory) "Rules") "DSCResources") $moduleName
68+
# Save the current environment variables
69+
$oldLocalAppDataPath = $env:LOCALAPPDATA
70+
$oldTempPath = $env:TEMP
71+
$oldPSModulePath = $env:PSModulePath
72+
73+
# set the environment variables
74+
$tempPath = Join-Path $oldTempPath ([guid]::NewGUID()).ToString()
75+
$newLocalAppDataPath = Join-Path $tempPath "LocalAppData"
76+
$newTempPath = Join-Path $tempPath "Temp"
77+
$env:LOCALAPPDATA = $newLocalAppDataPath
78+
$env:TEMP = $newTempPath
79+
80+
# create the temporary directories
81+
New-Item -Type Directory -Path $newLocalAppDataPath
82+
New-Item -Type Directory -Path $newTempPath
83+
84+
# create and dispose module dependency handler object
85+
# to setup the temporary module location
86+
$depHandler = [Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.ModuleDependencyHandler]::new()
87+
$pssaAppDataPath = $depHandler.PSSAAppDataPath
88+
$tempModulePath = $depHandler.TempModulePath
89+
$depHandler.Dispose()
90+
91+
# copy myresource module to the temporary location
92+
# we could let the module dependency handler download it from psgallery
93+
Copy-Item -Recurse -Path $modulePath -Destination $tempModulePath
94+
95+
It "Doesn't have parse errors" {
96+
# invoke script analyzer
97+
$dr = Invoke-ScriptAnalyzer -Path $violationFilePath -ErrorVariable parseErrors -ErrorAction SilentlyContinue
98+
$dr.Count | Should Be 0
99+
}
100+
101+
It "Keeps PSModulePath unchanged before and after invocation" {
102+
$dr = Invoke-ScriptAnalyzer -Path $violationFilePath -ErrorVariable parseErrors -ErrorAction SilentlyContinue
103+
$env:PSModulePath | Should Be $oldPSModulePath
104+
}
105+
#restore environment variables and clean up temporary location
106+
$env:LOCALAPPDATA = $oldLocalAppDataPath
107+
$env:TEMP = $oldTempPath
108+
Remove-Item -Recurse -Path $tempModulePath
109+
Remove-Item -Recurse -Path $tempPath
110+
111+
It "Keeps the environment variables unchanged" {
112+
Test-EnvironmentVariables($oldEnvVars)
113+
}
114+
}
115+
}

0 commit comments

Comments
 (0)