Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ jobs:
run: |
$expected = (Join-Path $pwd test stubs expected replaced.tpl)
$subject = (Join-Path $pwd test subject-${{ matrix.style }}.tpl)
$prestine = (Join-Path $pwd test stubs prestine ${{ matrix.style }}.tpl)
$pristine = (Join-Path $pwd test stubs pristine ${{ matrix.style }}.tpl)

$untouched = (Join-Path $pwd test stubs untouched no-matches.tpl)
$untouchedHash = (Get-FileHash -Path $untouched -Algorithm SHA256).Hash

Copy-Item -Path $prestine -Destination $subject
Copy-Item -Path $pristine -Destination $subject

Write-Output "expected-path=$expected" | Out-File -FilePath $env:GITHUB_OUTPUT -Encoding utf8NoBOM -Append
Write-Output "subject-path=$subject" | Out-File -FilePath $env:GITHUB_OUTPUT -Encoding utf8NoBOM -Append
Expand Down Expand Up @@ -104,3 +104,15 @@ jobs:
} else {
echo "✓ Token untouched test passed"
}

pester:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@main

- name: Run Pester Tests
shell: pwsh
run: |
$testPath = (Join-Path -Path "$env:GITHUB_WORKSPACE" -ChildPath 'test')
Invoke-Pester -Path (Join-Path -Path $testPath -ChildPath 'ReplaceTokens.Tests.ps1') -Output Detailed
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,5 @@ scratch.*
# !.vscode/launch.json
# !.vscode/settings.json
# !.vscode/tasks.json

test/TokenReplaceTest/
88 changes: 88 additions & 0 deletions test/ReplaceTokens.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# Usage: Invoke-Pester -Path ReplaceTokens.Tests.ps1 -Output Detailed

# Check if Pester is installed, if not, install it
if (-not (Get-Module -Name Pester -ListAvailable))
{
Install-Module -Name Pester -Force -SkipPublisherCheck
}

# Import the script being tested
$scriptPath = Join-Path -Path (Get-Item -Path $PSScriptRoot).Parent.FullName -ChildPath 'action.ps1'

Describe 'ReplaceTokens Function' {

BeforeAll {
# Set up a temporary test directory
$testDir = Join-Path -Path $PSScriptRoot -ChildPath 'TokenReplaceTest'
New-Item -Path $testDir -ItemType Directory -Force | Out-Null
}

AfterAll {
# Cleanup test directory
Remove-Item -Path $testDir -Recurse -Force
}

It 'Checks the script path' {
# Act
$scriptPath = Join-Path -Path (Get-Item -Path $PSScriptRoot).Parent.FullName -ChildPath 'action.ps1'

# Assert
$scriptPath | Should -Not -BeNullOrEmpty
$scriptPath | Should -BeExactly (Join-Path -Path (Get-Item -Path $PSScriptRoot).Parent.FullName -ChildPath 'action.ps1')
}

It 'Replaces tokens when environment variables exist' {
# Arrange
$testFile = Join-Path -Path $testDir -ChildPath 'test1.txt'
Set-Content -Path $testFile -Value 'Hello, {{NAME}}!' -Encoding utf8NoBOM -NoNewline

$env:NAME = 'Alice'

# Act
& $scriptPath -Path $testFile -Style 'mustache' -Encoding 'utf8NoBOM' -NoNewline
$result = Get-Content -Path $testFile -Raw

# Assert
$result | Should -Be 'Hello, Alice!'
}

It 'Does not replace tokens if no matching environment variable exists' {
# Arrange
$testFile = Join-Path -Path $testDir -ChildPath 'test2.txt'
Set-Content -Path $testFile -Value 'Welcome, {{REPLACE_TOKENS_ACTION}}!' -Encoding utf8NoBOM -NoNewline

# Act
& $scriptPath -Path $testFile -Style 'mustache' -Encoding 'utf8NoBOM' -NoNewline
$result = Get-Content -Path $testFile -Raw

# Assert
$result | Should -Be 'Welcome, {{REPLACE_TOKENS_ACTION}}!' # Token remains unchanged
}

It 'Handles empty environment variable values correctly' {
# Arrange
$testFile = Join-Path -Path $testDir -ChildPath 'test3.txt'
Set-Content -Path $testFile -Value 'Your ID: {{ID}}' -Encoding utf8NoBOM -NoNewline

$env:ID = ''

# Act
& $scriptPath -Path $testFile -Style 'mustache' -Encoding 'utf8NoBOM' -NoNewline
$result = Get-Content -Path $testFile -Raw

# Assert
$result | Should -Be 'Your ID: {{ID}}' # Should remain unchanged with a warning
}

It 'Applies correct encoding options' {
# Arrange
$testFile = Join-Path -Path $testDir -ChildPath 'test5.txt'
Set-Content -Path $testFile -Value 'Encoding Test' -Encoding ascii -NoNewline

# Act
$result = Get-Content -Path $testFile -Raw -Encoding ascii

# Assert
$result | Should -Be 'Encoding Test'
}
}
Loading