-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPathFinder.Tests.ps1
140 lines (124 loc) · 5.87 KB
/
PathFinder.Tests.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#Requires -Module @{ ModuleName = 'Pester'; ModuleVersion = '5.0.2' }
#Requires -Module @{ ModuleName = 'PSScriptAnalyzer'; ModuleVersion = '1.19.0' }
[string]$psTestDataPath = [System.IO.Path]::ChangeExtension($PSCommandPath, 'psd1')
if (-not [System.IO.File]::Exists($psTestDataPath)) {
Write-Warning -Message "PowerShell data file not found: $psTestDataPath"
return
}
$psTestData = Import-PowerShellDataFile -Path $psTestDataPath
# Constants
## The module root folder:
$psTestData.psModuleRoot = $PSScriptRoot
## The module name:
#[string]$psModuleName = [System.IO.Path]::GetFileNameWithoutExtension($PSScriptRoot)
$psTestData.psModuleName = [System.IO.Path]::GetFileNameWithoutExtension($PSScriptRoot)
## The expected manifest name:
#[string]$psManifestName = "$($psModuleName).psd1"
$psTestData.psManifestName = "$($psModuleName).psd1"
## The expected module manifest path:
#[string]$psManifestPath = [System.IO.Path]::Combine($PSScriptRoot, $psManifestName)
$psTestData.psManifestPath = [System.IO.Path]::Combine($PSScriptRoot, $psManifestName)
## The expected root module names:
$psTestData.psRootNames = $psTestData.psModuleExtensions.ForEach({
"$($psTestData.psModuleName)$($_)"
})
## The expected directory structure:
$psTestData.psDirsShouldPresent = $psTestData.psDirsShouldPresent.ForEach({
[System.IO.Path]::Combine($PSScriptRoot, $_)
})
Describe "General tests for the module: $($psTestData.psModuleName)" {
Context "Inventory: $($psTestData.psModuleName)" {
It "Subfolders should be present" -TestCases $psTestData {
param(
$psDirsShouldPresent
)
$psDirsShouldPresent.ForEach({
Write-Verbose "Subfolder `"$_`" should exist"
[bool]$folderExists = [System.IO.Directory]::Exists($_)
$folderExists | Should -BeTrue
})
}
It "Subfolders should contain scripts" -TestCases $psTestData {
param(
$psDirsShouldPresent
)
$psDirsShouldPresent.ForEach({
Write-Verbose "Subfolder `"$_`" should contain scripts"
[System.IO.Directory]::EnumerateFiles($_, '*.ps1') | Should -BeGreaterThan 0
})
}
It "The module file should be present" -TestCases $psTestData {
param(
$psRootNames
)
[string[]]$rootFilesPresent = $psRootNames.ForEach({
[System.IO.Directory]::EnumerateFiles($PSScriptRoot, $_)
})
$rootFilesPresent.Count | Should -BeGreaterThan 0
}
}
Context "General tests of the scripts" {
It "Invoke PSScriptAnalyzer for scripts" -TestCases $psTestData {
param (
$psDirsShouldPresent,
$psScriptAnalyzerRules
)
[string[]]$scriptsAll = $psDirsShouldPresent.ForEach({
[System.IO.Directory]::EnumerateFiles($_, '*.ps1')
}) -notmatch '\.tests\.ps1$'
$scriptsAll.ForEach({
Invoke-ScriptAnalyzer -Path $_ `
-ExcludeRule $psScriptAnalyzerRules.ExcludeRule `
-Severity $psScriptAnalyzerRules.Severity `
| Should -BeNullOrEmpty
})
}
}
if ([System.IO.File]::Exists($psTestData.psManifestPath)) {
Context "Testing the manifest $($psTestData.psManifestName)" {
It "Invoke PSScriptAnalyzer for the module manifest: $($psTestData.psManifestName)" -TestCases $psTestData {
param(
$psManifestPath,
$psScriptAnalyzerRules
)
Invoke-ScriptAnalyzer -Path $psManifestPath `
-ExcludeRule $psScriptAnalyzerRules.Manifest.ExcludeRule `
-Severity $psScriptAnalyzerRules.Manifest.Severity `
| Should -BeNullOrEmpty
}
It "Read the manifest: $($psTestData.psManifestPath)" -TestCases $psTestData {
param(
$psManifestPath
)
$psManifestData = Import-PowerShellDataFile -Path $psManifestPath
$psManifestData | Should -BeOfType 'System.Collections.Hashtable'
[string]$psManifestRootModule = $psManifestData.RootModule
if ($psManifestRootModule)
{
[System.IO.Path]::GetExtension($psManifestRootModule) | Should -Not -BeIn @('.ps1', '.psd1')
}
}
}
}
Context "Test load: $($psTestData.psModuleName)" {
It "Get-Module $($psTestData.psModuleName)" -TestCases $psTestData {
param(
$psModuleRoot
)
$psModuleInfo = Get-Module -Name $psModuleRoot -ListAvailable
$psModuleInfo | Should -BeOfType 'System.Management.Automation.PSModuleInfo'
}
It "Import and remove module $($psTestData.psModuleName)" -TestCases $psTestData {
param(
$psModuleRoot,
$psModuleName
)
$psImportResult = Import-Module -Name $psModuleRoot
$psImportResult | Should -BeNullOrEmpty
$psModuleInfo = Get-Module -Name $psModuleName
$psModuleInfo | Should -BeOfType 'System.Management.Automation.PSModuleInfo'
$psModuleInfo.Name | Should -BeExactly $psModuleName
$psModuleInfo.ModuleBase | Should -BeExactly $PSScriptRoot
}
}
}