Skip to content

Commit c485260

Browse files
committed
Re-Wrote tests to improve coverage and match expected outputs
This update introduces a full suite of focused, line‑based Pester tests for the scaffolding system. Each test validates only the logic unique to its corresponding function, avoiding redundancy and ensuring long‑term maintainability.
1 parent fddcb50 commit c485260

13 files changed

Lines changed: 585 additions & 98 deletions
Lines changed: 62 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,69 @@
11
# Requires -Module Pester
22
Describe "Initialize-GitRepository" {
33

4-
It "Initializes a git repository" {
5-
$temp = Join-Path $env:TEMP ([guid]::NewGuid().ToString())
6-
New-Item -ItemType Directory -Path $temp | Out-Null
4+
BeforeAll {
5+
# Import the module under test
6+
$moduleRoot = Join-Path $PSScriptRoot '..' | Resolve-Path
7+
$manifest = Join-Path $moduleRoot 'NewModuleTemplate.psd1'
8+
Import-Module $manifest -Force
79

8-
Initialize-GitRepository -ModulePath $temp
10+
# Detect Git availability once
11+
$global:GitAvailable = [bool](Get-Command git -ErrorAction SilentlyContinue)
12+
}
13+
14+
BeforeEach {
15+
# Fresh temp directory for each test
16+
$guid = [guid]::NewGuid().ToString()
17+
$root = Join-Path $env:TEMP $guid
18+
19+
New-Item -ItemType Directory -Path $root | Out-Null
20+
21+
Set-Variable -Name TempRoot -Value $root -Scope Local
22+
}
23+
24+
It "Creates .gitignore and initializes a repo when Git is available" -Skip:(-not $GitAvailable) {
25+
$result = Initialize-GitRepository -ModulePath $TempRoot
26+
27+
$gitignorePath = Join-Path $TempRoot ".gitignore"
28+
29+
# .gitignore exists
30+
Test-Path $gitignorePath | Should -Be $true
31+
32+
# Function returns correct path
33+
$result | Should -Be $gitignorePath
34+
35+
# .git folder exists
36+
Test-Path (Join-Path $TempRoot ".git") | Should -Be $true
37+
38+
# Initial commit exists
39+
$log = git -C $TempRoot log --oneline
40+
$log | Should -Not -BeNullOrEmpty
41+
}
42+
43+
It "Does not create a repo when Git is NOT available" -Skip:$GitAvailable {
44+
$result = Initialize-GitRepository -ModulePath $TempRoot
45+
46+
# Should return nothing
47+
$result | Should -BeNullOrEmpty
48+
49+
# No .gitignore
50+
Test-Path (Join-Path $TempRoot ".gitignore") | Should -Be $false
51+
52+
# No .git folder
53+
Test-Path (Join-Path $TempRoot ".git") | Should -Be $false
54+
}
55+
56+
It "Is idempotent when Git is available" -Skip:(-not $GitAvailable) {
57+
Initialize-GitRepository -ModulePath $TempRoot
58+
59+
{
60+
Initialize-GitRepository -ModulePath $TempRoot
61+
} | Should -Not -Throw
62+
63+
# Repo still exists
64+
Test-Path (Join-Path $TempRoot ".git") | Should -Be $true
965

10-
Test-Path "$temp\.git" | Should Be $true
66+
# .gitignore still exists
67+
Test-Path (Join-Path $TempRoot ".gitignore") | Should -Be $true
1168
}
1269
}
Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,54 @@
11
# Requires -Module Pester
22
Describe "New-AnalyzerSettings" {
33

4-
It "Creates analyzer settings file" {
5-
$temp = Join-Path $env:TEMP ([guid]::NewGuid().ToString()) "TestModule"
6-
New-Item -ItemType Directory -Path $temp | Out-Null
4+
BeforeAll {
5+
$moduleRoot = Join-Path $PSScriptRoot '..' | Resolve-Path
6+
$manifest = Join-Path $moduleRoot 'NewModuleTemplate.psd1'
7+
Import-Module $manifest -Force
8+
}
9+
10+
BeforeEach {
11+
$guid = [guid]::NewGuid().ToString()
12+
$root = Join-Path $env:TEMP $guid
13+
14+
New-Item -ItemType Directory -Path $root | Out-Null
15+
16+
Set-Variable -Name TempRoot -Value $root -Scope Local
17+
}
18+
19+
It "Creates the Analyzer directory and settings file" {
20+
$result = New-AnalyzerSettings -ModulePath $TempRoot
21+
22+
$settingsDir = Join-Path $TempRoot "Analyzer"
23+
$expectedPath = Join-Path $settingsDir "PSScriptAnalyzerSettings.psd1"
24+
25+
Test-Path $settingsDir | Should -Be $true
26+
Test-Path $expectedPath | Should -Be $true
27+
$result | Should -Be $expectedPath
28+
}
29+
30+
It "Contains expected rule definitions" {
31+
$path = New-AnalyzerSettings -ModulePath $TempRoot
32+
$lines = Get-Content $path | ForEach-Object { $_.Trim() }
33+
34+
$lines | Should -Contain "PSAvoidUsingWriteHost = @{"
35+
$lines | Should -Contain "PSUseDeclaredVarsMoreThanAssignments = @{"
36+
$lines | Should -Contain "PSUseCorrectCasing = @{"
37+
$lines | Should -Contain "PSProvideCommentHelp = @{"
38+
$lines | Should -Contain "PSAvoidUsingCmdletAliases = @{"
39+
$lines | Should -Contain "PSAvoidTrailingWhitespace = @{"
40+
}
41+
42+
It "Is idempotent and overwrites the settings cleanly" {
43+
New-AnalyzerSettings -ModulePath $TempRoot
44+
45+
{
46+
New-AnalyzerSettings -ModulePath $TempRoot
47+
} | Should -Not -Throw
748

8-
New-AnalyzerSettings -ModulePath $temp
49+
$lines = Get-Content (Join-Path $TempRoot "Analyzer\PSScriptAnalyzerSettings.psd1") |
50+
ForEach-Object { $_.Trim() }
951

10-
Test-Path "$temp\Analyzer\PSScriptAnalyzerSettings.psd1" | Should Be $true
52+
$lines | Should -Contain "PSUseConsistentIndentation = @{"
1153
}
1254
}

‎Tests/New-BuildScript.Tests.ps1‎

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Requires -Module Pester
2+
Describe "New-BuildScript" {
3+
4+
BeforeAll {
5+
# Import the module under test
6+
$moduleRoot = Join-Path $PSScriptRoot '..' | Resolve-Path
7+
$manifest = Join-Path $moduleRoot 'NewModuleTemplate.psd1'
8+
Import-Module $manifest -Force
9+
}
10+
11+
BeforeEach {
12+
# Fresh temp directory for each test
13+
$guid = [guid]::NewGuid().ToString()
14+
$root = Join-Path $env:TEMP $guid
15+
16+
New-Item -ItemType Directory -Path $root | Out-Null
17+
18+
Set-Variable -Name TempRoot -Value $root -Scope Local
19+
Set-Variable -Name ScriptsDir -Value (Join-Path $root "Scripts") -Scope Local
20+
Set-Variable -Name BuildScriptPath -Value (Join-Path $ScriptsDir "build.ps1") -Scope Local
21+
}
22+
23+
It "Creates the Scripts directory and build.ps1" {
24+
New-BuildScript -ModulePath $TempRoot -Name "TestModule"
25+
26+
Test-Path $ScriptsDir | Should -Be $true
27+
Test-Path $BuildScriptPath | Should -Be $true
28+
}
29+
30+
It "Replaces __MODULE__ correctly inside the script" {
31+
New-BuildScript -ModulePath $TempRoot -Name "TestModule"
32+
33+
$lines = Get-Content $BuildScriptPath | ForEach-Object { $_.Trim() }
34+
35+
$lines | Should -Contain 'Write-Information "Building TestModule..."'
36+
$lines | Should -Contain 'Import-Module "$PSScriptRoot\..\" -Force'
37+
$lines | Should -Contain 'New-MarkdownHelp -Module TestModule -OutputFolder "$PSScriptRoot\..\Docs\" -WithModulePage -Force'
38+
}
39+
40+
It "Contains expected build pipeline sections" {
41+
New-BuildScript -ModulePath $TempRoot -Name "TestModule"
42+
43+
$lines = Get-Content $BuildScriptPath | ForEach-Object { $_.Trim() }
44+
45+
# Analyzer section
46+
$lines | Should -Contain 'Invoke-ScriptAnalyzer -Path $PSScriptRoot -Settings "$PSScriptRoot\..\Analyzer\PSScriptAnalyzerSettings.psd1"'
47+
48+
# Pester section
49+
$lines | Should -Contain 'Invoke-Pester -Path "$PSScriptRoot\..\Tests"'
50+
51+
# Docs section
52+
$lines | Should -Contain 'Import-Module PlatyPS -ErrorAction Stop'
53+
}
54+
55+
It "Is idempotent and overwrites the script cleanly" {
56+
New-BuildScript -ModulePath $TempRoot -Name "TestModule"
57+
58+
{
59+
New-BuildScript -ModulePath $TempRoot -Name "TestModule"
60+
} | Should -Not -Throw
61+
62+
$lines = Get-Content $BuildScriptPath | ForEach-Object { $_.Trim() }
63+
$lines | Should -Contain 'Write-Information "Building TestModule..."'
64+
}
65+
}
Lines changed: 57 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,64 @@
11
# Requires -Module Pester
22
Describe "New-DocumentationScripts" {
33

4-
It "Creates documentation scripts" {
5-
$temp = Join-Path $env:TEMP ([guid]::NewGuid().ToString()) "TestModule"
6-
New-Item -ItemType Directory -Path $temp | Out-Null
4+
BeforeAll {
5+
# Import the module under test
6+
$moduleRoot = Join-Path $PSScriptRoot '..' | Resolve-Path
7+
$manifest = Join-Path $moduleRoot 'NewModuleTemplate.psd1'
8+
Import-Module $manifest -Force
9+
}
10+
11+
BeforeEach {
12+
# Fresh temp directory for each test
13+
$guid = [guid]::NewGuid().ToString()
14+
$root = Join-Path $env:TEMP $guid
15+
16+
New-Item -ItemType Directory -Path $root | Out-Null
17+
18+
Set-Variable -Name TempRoot -Value $root -Scope Local
19+
}
20+
21+
It "Creates the Scripts directory and documentation update script" {
22+
$result = New-DocumentationScripts -ModulePath $TempRoot -Name "TestModule"
23+
24+
$scriptsDir = Join-Path $TempRoot "Scripts"
25+
$expectedPath = Join-Path $scriptsDir "Update-ModuleDocumentation.ps1"
26+
27+
# Directory exists
28+
Test-Path $scriptsDir | Should -Be $true
29+
30+
# Script exists
31+
Test-Path $expectedPath | Should -Be $true
32+
33+
# Function returns correct path
34+
$result | Should -Be $expectedPath
35+
}
36+
37+
It "Replaces __MODULE_NAME__ correctly inside the script" {
38+
$path = New-DocumentationScripts -ModulePath $TempRoot -Name "TestModule"
39+
$lines = Get-Content $path
40+
41+
# Script should import PlatyPS
42+
$lines | Should -Contain "Import-Module PlatyPS -ErrorAction Stop"
43+
44+
# Script should import the module manifest using the correct name
45+
$lines | Should -Contain 'Import-Module "$PSScriptRoot\..\TestModule.psd1" -Force'
46+
47+
# Script should call New-MarkdownHelp with the correct module name
48+
$lines | Should -Contain 'New-MarkdownHelp -Module TestModule -OutputFolder "$PSScriptRoot\..\Docs\" -WithModulePage -Force'
49+
}
50+
51+
It "Is idempotent and overwrites the script cleanly" {
52+
# First write
53+
New-DocumentationScripts -ModulePath $TempRoot -Name "TestModule"
754

8-
New-DocumentationScripts -Name "TestModule" -ModulePath $temp
55+
# Second write should not throw
56+
{
57+
New-DocumentationScripts -ModulePath $TempRoot -Name "TestModule"
58+
} | Should -Not -Throw
959

10-
Test-Path "$temp\Scripts\Update-ModuleDocumentation.ps1" | Should Be $true
60+
# Script still contains correct module name
61+
$lines = Get-Content (Join-Path $TempRoot "Scripts\Update-ModuleDocumentation.ps1")
62+
$lines | Should -Contain 'New-MarkdownHelp -Module TestModule -OutputFolder "$PSScriptRoot\..\Docs\" -WithModulePage -Force'
1163
}
1264
}
Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,46 @@
11
# Requires -Module Pester
22
Describe "New-ModuleChangelog" {
33

4-
It "Creates a CHANGELOG.md file" {
5-
$temp = Join-Path $env:TEMP ([guid]::NewGuid().ToString())
6-
New-Item -ItemType Directory -Path $temp | Out-Null
4+
BeforeAll {
5+
$moduleRoot = Join-Path $PSScriptRoot '..' | Resolve-Path
6+
$manifest = Join-Path $moduleRoot 'NewModuleTemplate.psd1'
7+
Import-Module $manifest -Force
8+
}
9+
10+
BeforeEach {
11+
$guid = [guid]::NewGuid().ToString()
12+
$root = Join-Path $env:TEMP $guid
713

8-
$result = New-ModuleChangelog -ModulePath $temp -Version "1.0.0"
14+
New-Item -ItemType Directory -Path $root | Out-Null
915

10-
Test-Path $result | Should Be $true
16+
Set-Variable -Name TempRoot -Value $root -Scope Local
1117
}
1218

13-
It "Populates the changelog with correct version" {
14-
$temp = Join-Path $env:TEMP ([guid]::NewGuid().ToString())
15-
New-Item -ItemType Directory -Path $temp | Out-Null
19+
It "Includes the correct version and notes" {
20+
$version = "2.5.0"
21+
$notes = "Added new API endpoints."
1622

17-
$result = New-ModuleChangelog -ModulePath $temp -Version "0.1.0"
23+
$path = New-ModuleChangelog -ModulePath $TempRoot -Version $version -Notes $notes
24+
$lines = Get-Content $path
1825

19-
$content = Get-Content $result -Raw
20-
$content | Should Match "0.1.0"
26+
$date = (Get-Date).ToString("yyyy-MM-dd")
27+
28+
$lines | Should -Contain "## [$version] - $date"
29+
$lines | Should -Contain "### Added"
30+
$lines | Should -Contain "- $notes"
2131
}
2232

23-
It "Includes the provided notes" {
24-
$temp = Join-Path $env:TEMP ([guid]::NewGuid().ToString())
25-
New-Item -ItemType Directory -Path $temp | Out-Null
33+
It "Is idempotent and overwrites the file cleanly" {
34+
New-ModuleChangelog -ModulePath $TempRoot -Version "1.0.0" -Notes "Initial"
35+
36+
{
37+
New-ModuleChangelog -ModulePath $TempRoot -Version "1.0.1" -Notes "Second release"
38+
} | Should -Not -Throw
2639

27-
$result = New-ModuleChangelog -ModulePath $temp -Version "1.0.0" -Notes "Testing notes"
40+
$lines = Get-Content (Join-Path $TempRoot "CHANGELOG.md")
41+
$date = (Get-Date).ToString("yyyy-MM-dd")
2842

29-
$content = Get-Content $result -Raw
30-
$content | Should Match "Testing notes"
43+
$lines | Should -Contain "## [1.0.1] - $date"
44+
$lines | Should -Contain "- Second release"
3145
}
3246
}

0 commit comments

Comments
 (0)