Skip to content

Commit 4711ee2

Browse files
authored
test: add pester tests (#6)
1 parent 31fc36a commit 4711ee2

File tree

7 files changed

+161
-3
lines changed

7 files changed

+161
-3
lines changed

.github/workflows/analyze-pwsh.yml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,16 @@ jobs:
4545
Import-Module -Name ConvertToSARIF
4646
4747
$injectionHunterRules = (Get-Module -list InjectionHunter).Path
48-
Invoke-ScriptAnalyzer -Settings PSScriptAnalyzerSettings.psd1 -CustomRulePath $injectionHunterRules -Path . -Recurse -OutVariable issues | ConvertTo-SARIF -FilePath results.sarif
48+
49+
$params = @{
50+
Settings = 'PSScriptAnalyzerSettings.psd1'
51+
CustomRulePath = $injectionHunterRules
52+
Path = '.'
53+
Recurse = $true
54+
OutVariable = 'issues'
55+
}
56+
57+
Invoke-ScriptAnalyzer @params | ConvertTo-SARIF -FilePath results.sarif
4958
5059
$errors = $issues.Where({$_.Severity -eq 'Error'})
5160
$warnings = $issues.Where({$_.Severity -eq 'Warning'})

.github/workflows/test.yml

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@ jobs:
3232
run: |
3333
$expected = (Join-Path $pwd test stubs expected replaced.tpl)
3434
$subject = (Join-Path $pwd test subject-${{ matrix.style }}.tpl)
35-
$prestine = (Join-Path $pwd test stubs prestine ${{ matrix.style }}.tpl)
35+
$pristine = (Join-Path $pwd test stubs pristine ${{ matrix.style }}.tpl)
3636
3737
$untouched = (Join-Path $pwd test stubs untouched no-matches.tpl)
3838
$untouchedHash = (Get-FileHash -Path $untouched -Algorithm SHA256).Hash
3939
40-
Copy-Item -Path $prestine -Destination $subject
40+
Copy-Item -Path $pristine -Destination $subject
4141
4242
Write-Output "expected-path=$expected" | Out-File -FilePath $env:GITHUB_OUTPUT -Encoding utf8NoBOM -Append
4343
Write-Output "subject-path=$subject" | Out-File -FilePath $env:GITHUB_OUTPUT -Encoding utf8NoBOM -Append
@@ -104,3 +104,14 @@ jobs:
104104
} else {
105105
echo "✓ Token untouched test passed"
106106
}
107+
108+
pester:
109+
runs-on: ubuntu-latest
110+
name: Pester tests
111+
steps:
112+
- name: Checkout repository
113+
uses: actions/checkout@main
114+
115+
- name: Run Pester Tests
116+
shell: pwsh
117+
run: ./test/ReplaceTokens.Tests.ps1

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,3 +118,5 @@ scratch.*
118118
# !.vscode/launch.json
119119
# !.vscode/settings.json
120120
# !.vscode/tasks.json
121+
122+
test/TokenReplaceTest/

test/ReplaceTokens.Tests.ps1

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
# Usage: Invoke-Pester -Path ReplaceTokens.Tests.ps1 -Output Detailed
2+
3+
# Check if Pester is installed, if not, install it
4+
if (-not (Get-Module -Name Pester -ListAvailable))
5+
{
6+
Install-Module -Name Pester -Force -SkipPublisherCheck
7+
}
8+
9+
# Import the script being tested
10+
$scriptPath = Join-Path -Path (Get-Item -Path $PSScriptRoot).Parent.FullName -ChildPath 'action.ps1'
11+
12+
Describe 'ReplaceTokens Function' {
13+
14+
BeforeAll {
15+
# Set up a temporary test directory
16+
$testDir = Join-Path -Path $PSScriptRoot -ChildPath 'TokenReplaceTest'
17+
New-Item -Path $testDir -ItemType Directory -Force | Out-Null
18+
}
19+
20+
AfterAll {
21+
# Cleanup test directory
22+
Remove-Item -Path $testDir -Recurse -Force
23+
}
24+
25+
It 'Replaces tokens when environment variables exist' {
26+
# Arrange
27+
$testFile = Join-Path -Path $testDir -ChildPath 'test1.txt'
28+
Set-Content -Path $testFile -Value 'Hello, {{NAME}}!' -Encoding utf8NoBOM -NoNewline
29+
30+
$env:NAME = 'Alice'
31+
32+
# Act
33+
& $scriptPath -Path $testFile -Style 'mustache' -Encoding 'utf8NoBOM' -NoNewline
34+
$result = Get-Content -Path $testFile -Raw
35+
36+
# Assert
37+
$result | Should -Be 'Hello, Alice!'
38+
}
39+
40+
It 'Does not replace tokens if no matching environment variable exists' {
41+
# Arrange
42+
$testFile = Join-Path -Path $testDir -ChildPath 'test2.txt'
43+
Set-Content -Path $testFile -Value 'Welcome, {{REPLACE_TOKENS_ACTION}}!' -Encoding utf8NoBOM -NoNewline
44+
45+
# Act
46+
& $scriptPath -Path $testFile -Style 'mustache' -Encoding 'utf8NoBOM' -NoNewline
47+
$result = Get-Content -Path $testFile -Raw
48+
49+
# Assert
50+
$result | Should -Be 'Welcome, {{REPLACE_TOKENS_ACTION}}!' # Token remains unchanged
51+
}
52+
53+
It 'Handles empty environment variable values correctly' {
54+
# Arrange
55+
$testFile = Join-Path -Path $testDir -ChildPath 'test3.txt'
56+
Set-Content -Path $testFile -Value 'Your ID: {{ID}}' -Encoding utf8NoBOM -NoNewline
57+
58+
$env:ID = ''
59+
60+
# Act
61+
& $scriptPath -Path $testFile -Style 'mustache' -Encoding 'utf8NoBOM' -NoNewline
62+
$result = Get-Content -Path $testFile -Raw
63+
64+
# Assert
65+
$result | Should -Be 'Your ID: {{ID}}' # Should remain unchanged with a warning
66+
}
67+
68+
It 'Applies correct encoding options' {
69+
# Arrange
70+
$testFile = Join-Path -Path $testDir -ChildPath 'test5.txt'
71+
Set-Content -Path $testFile -Value 'Encoding Test' -Encoding ascii -NoNewline
72+
73+
# Act
74+
$result = Get-Content -Path $testFile -Raw -Encoding ascii
75+
76+
# Assert
77+
$result | Should -Be 'Encoding Test'
78+
}
79+
80+
It 'Replaces tokens with envsubst style' {
81+
# Arrange
82+
$testFile = Join-Path -Path $testDir -ChildPath 'test6.txt'
83+
Set-Content -Path $testFile -Value 'Hello, ${NAME}!' -Encoding utf8NoBOM -NoNewline
84+
85+
$env:NAME = 'Bob'
86+
87+
# Act
88+
& $scriptPath -Path $testFile -Style 'envsubst' -Encoding 'utf8NoBOM' -NoNewline
89+
$result = Get-Content -Path $testFile -Raw
90+
91+
# Assert
92+
$result | Should -Be 'Hello, Bob!'
93+
}
94+
95+
It 'Replaces tokens with make style' {
96+
# Arrange
97+
$testFile = Join-Path -Path $testDir -ChildPath 'test7.txt'
98+
Set-Content -Path $testFile -Value 'Hello, $(NAME)!' -Encoding utf8NoBOM -NoNewline
99+
100+
$env:NAME = 'Charlie'
101+
102+
# Act
103+
& $scriptPath -Path $testFile -Style 'make' -Encoding 'utf8NoBOM' -NoNewline
104+
$result = Get-Content -Path $testFile -Raw
105+
106+
# Assert
107+
$result | Should -Be 'Hello, Charlie!'
108+
}
109+
110+
It 'Does not replace tokens if file is excluded' {
111+
# Arrange
112+
$testFile = Join-Path -Path $testDir -ChildPath 'test8.txt'
113+
Set-Content -Path $testFile -Value 'Hello, {{NAME}}!' -Encoding utf8NoBOM -NoNewline
114+
115+
$env:NAME = 'Dave'
116+
117+
# Act
118+
& $scriptPath -Path $testFile -Style 'mustache' -Encoding 'utf8NoBOM' -NoNewline -Exclude 'test8.txt'
119+
$result = Get-Content -Path $testFile -Raw
120+
121+
# Assert
122+
$result | Should -Be 'Hello, {{NAME}}!' # Token remains unchanged
123+
}
124+
125+
It 'Fails the step if no tokens were replaced and fail is true' {
126+
# Arrange
127+
$testFile = Join-Path -Path $testDir -ChildPath 'test9.txt'
128+
Set-Content -Path $testFile -Value 'No tokens here!' -Encoding utf8NoBOM -NoNewline
129+
130+
# Act
131+
$result = & $scriptPath -Path $testFile -Style 'mustache' -Encoding 'utf8NoBOM' -NoNewline
132+
133+
# Assert
134+
$result.Count | Should -Be 0 # No tokens were replaced
135+
}
136+
}

0 commit comments

Comments
 (0)