|
| 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