Skip to content

Commit 6c8f31e

Browse files
author
Adrian Wells
committedAug 31, 2018
Expanded testing
1 parent ead9f25 commit 6c8f31e

File tree

1 file changed

+67
-8
lines changed

1 file changed

+67
-8
lines changed
 

‎ITGlueAPI/Tests/ITGlueAPI.Tests.ps1

+67-8
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
1-
# Tested with Pester module version 4.4.0
1+
#Requires -Modules Pester
22

3-
# Name of this module found by parsing name of test file (ITGlueAPI\Tests\ITGlueAPI.Tests.ps1)
3+
# Obtain name of this module by parsing name of test file (ITGlueAPI\Tests\ITGlueAPI.Tests.ps1)
44
$ThisModule = $PSCommandPath -replace '\.Tests\.ps1$'
55
$ThisModuleName = $ThisModule | Split-Path -Leaf
66

7-
# Path of the module based on location of test file (ITGlueAPI\Tests\ITGlueAPI.Tests.ps1)
7+
# Obtain path of the module based on location of test file (ITGlueAPI\Tests\ITGlueAPI.Tests.ps1)
88
$ThisModulePath = Split-Path (Split-Path -Parent $PSCommandPath) -Parent
99

1010
# Make sure one or multiple versions of the module are not loaded
1111
Get-Module -Name $ThisModuleName | Remove-Module
1212

13+
# Credit - borrowed with care from http://www.lazywinadmin.com/2016/05/using-pester-to-test-your-manifest-file.html and modified as needed
14+
# Manifest file path
15+
$ManifestFile = "$ThisModulePath\$ThisModuleName.psd1"
16+
17+
# Import the module and store the information about the module
18+
$ModuleInformation = Import-module -Name $ManifestFile -PassThru
19+
1320
# Internal Files
1421
$InternalDirectoryFiles = (
1522
'APIKey.ps1',
@@ -46,17 +53,42 @@ $ResourceDirectoryFiles = (
4653
'Users.ps1'
4754
)
4855

49-
Describe '$ThisModuleName Module Tests' {
56+
# Manifest Elements
57+
$ManifestFileElements = (
58+
'RootModule',
59+
'Author',
60+
'CompanyName',
61+
'Description',
62+
'Copyright',
63+
'PowerShellVersion',
64+
'NestedModules',
65+
'HelpInfoURI'
66+
)
67+
68+
69+
Test-ModuleManifest -Path .\ITGlueAPI.psd1
70+
write-host $ModuleInformation.ModuleVersion -ForegroundColor Cyan
71+
72+
Describe "$ThisModuleName Module Tests" {
5073

5174
Context 'Test Module' {
5275
It "has the root module $ThisModuleName.psm1" {
5376
"$ThisModulePath\$ThisModuleName.psm1" | Should Exist
5477
}
5578

56-
It "has a manifest file of $ThisModuleName.psd1" {
57-
"$ThisModulePath\$ThisModuleName.psd1" | Should Exist
58-
# TODO - This test is currently failing...
59-
#"$ThisModulePath\$ThisModuleName.psd1" | Should Contain "ITGlueAPI.psm1"
79+
Context "Test Manifest File (.psd1)"{
80+
It "Should pass Test-ModuleManifest" {
81+
$errors = $null
82+
$errors = Test-ModuleManifest -Path $ThisModulePath\$ThisModuleName.psd1 -ErrorAction Stop
83+
$errors.Count | Should Be 1
84+
}
85+
86+
# Credit - borrowed with care from http://www.lazywinadmin.com/2016/05/using-pester-to-test-your-manifest-file.html and modified as needed
87+
ForEach ($ManifestFileElement in $ManifestFileElements) {
88+
It "Should contains $ManifestFileElement"{
89+
$ModuleInformation.$ManifestFileElement | Should not BeNullOrEmpty
90+
}
91+
}
6092
}
6193

6294
It "$ThisModuleName\Resources directory has functions" {
@@ -78,7 +110,19 @@ Describe '$ThisModuleName Module Tests' {
78110
It "$InternalFile should exist" {
79111
"$ThisModulePath\Internal\$InternalFile" | Should Exist
80112
}
113+
It "$InternalFile is valid PowerShell code" {
114+
$psFile = Get-Content -Path "$ThisModulePath\Internal\$InternalFile" -ErrorAction Stop
115+
$errors = $null
116+
$null = [System.Management.Automation.PSParser]::Tokenize($psfile, [ref]$errors)
117+
$errors.Count | Should Be 0
118+
}
81119
}
120+
#Context "$InternalFile has tests" {
121+
$InternalFileTest = $InternalFile -replace '\.ps1$'
122+
It "$InternalFileTest.Tests.ps1 should exist" {
123+
$InternalFileTest.Tests.ps1 | Should Exist
124+
}
125+
#}
82126
}
83127

84128
# Check that Resource files exist
@@ -87,6 +131,21 @@ Describe '$ThisModuleName Module Tests' {
87131
It "$ResourceFile should exist" {
88132
"$ThisModulePath\Resources\$ResourceFile" | Should Exist
89133
}
134+
It "$ResourceFile is valid PowerShell code" {
135+
$psFile = Get-Content -Path "$ThisModulePath\Resources\$ResourceFile" -ErrorAction Stop
136+
$errors = $null
137+
$null = [System.Management.Automation.PSParser]::Tokenize($psfile, [ref]$errors)
138+
$errors.Count | Should Be 0
139+
}
140+
}
141+
# TODO - add tests to check for tests files
142+
}
143+
144+
Context "PowerShell $ThisModuleName Import Test" {
145+
# Credit - borrowed with care from https://github.com/TheMattCollins0/MattTools/blob/master/Tests/ModuleImport.Tests.ps1 and modified as needed
146+
It "Should import PowerShell $ThisModuleName succesfully" {
147+
Import-Module -Name $ThisModulePath -ErrorVariable ImportError
148+
$ImportError | Should Be $null
90149
}
91150
}
92151
}

0 commit comments

Comments
 (0)
Please sign in to comment.