1- # Update-MatchInFile.Tests.ps1
1+ # Update-MatchInFile.Tests.ps1
22# Update-MatchInFile コマンドレットの統合テスト
33
44# Requires -Modules @ { ModuleName = " Pester" ; ModuleVersion = " 5.0.0" }
@@ -31,26 +31,26 @@ Describe "Update-MatchInFile Integration Tests" {
3131
3232 Context " テキストマッチによる置換(Contains)" {
3333 It " 指定したテキストを含む部分を置換できる" {
34- Update-MatchInFile - Path $script :testFile -Contains " localhost" - Replacement " production.example.com"
34+ Update-MatchInFile - Path $script :testFile - OldText " localhost" - Replacement " production.example.com"
3535 $result = Get-Content $script :testFile
3636 $result [0 ] | Should - Be " Server: production.example.com"
3737 }
3838
3939 It " 複数行にマッチする場合、すべて置換される" {
40- Update-MatchInFile - Path $script :testFile -Contains " true" - Replacement " false"
40+ Update-MatchInFile - Path $script :testFile - OldText " true" - Replacement " false"
4141 $result = Get-Content $script :testFile
4242 $result [4 ] | Should - Be " Debug: false"
4343 }
4444
4545 It " マッチしない場合、何も変更されない" {
4646 $originalContent = Get-Content $script :testFile
47- Update-MatchInFile - Path $script :testFile -Contains " NonExistentText" - Replacement " NewValue"
47+ Update-MatchInFile - Path $script :testFile - OldText " NonExistentText" - Replacement " NewValue"
4848 $result = Get-Content $script :testFile
4949 $result | Should - Be $originalContent
5050 }
5151
5252 It " 大文字小文字を区別する(case-sensitive)" {
53- Update-MatchInFile - Path $script :testFile -Contains " LOCALHOST" - Replacement " server.local"
53+ Update-MatchInFile - Path $script :testFile - OldText " LOCALHOST" - Replacement " server.local"
5454 $result = Get-Content $script :testFile
5555 $result [0 ] | Should - Be " Server: localhost" # 大文字とマッチしないため変更されない
5656 }
@@ -94,7 +94,7 @@ Describe "Update-MatchInFile Integration Tests" {
9494
9595 Context " 範囲内での置換" {
9696 It " LineRange と Contains を組み合わせられる" {
97- Update-MatchInFile - Path $script :testFile - LineRange 1 , 3 -Contains " admin" - Replacement " superuser"
97+ Update-MatchInFile - Path $script :testFile - LineRange 1 , 3 - OldText " admin" - Replacement " superuser"
9898 $result = Get-Content $script :testFile
9999 $result [2 ] | Should - Be " Username: superuser"
100100 # 範囲外は変更されない
@@ -141,7 +141,7 @@ Describe "Update-MatchInFile Integration Tests" {
141141 Context " 特殊文字の処理" {
142142 It " 特殊文字を含むテキストを置換できる" {
143143 Set-Content - Path $script :testFile - Value @ (" Price: $100 " , " Discount: 10%" ) - Encoding UTF8
144- Update-MatchInFile - Path $script :testFile -Contains ' $100' - Replacement ' $200'
144+ Update-MatchInFile - Path $script :testFile - OldText ' $100' - Replacement ' $200'
145145 $result = Get-Content $script :testFile
146146 $result [0 ] | Should - Be " Price: $200 "
147147 }
@@ -158,7 +158,7 @@ Describe "Update-MatchInFile Integration Tests" {
158158 It " UTF-8ファイルを正しく処理できる" {
159159 $content = " 設定: 日本語"
160160 Set-Content - Path $script :testFile - Value $content - Encoding UTF8
161- Update-MatchInFile - Path $script :testFile -Contains " 日本語" - Replacement " English" - Encoding UTF8
161+ Update-MatchInFile - Path $script :testFile - OldText " 日本語" - Replacement " English" - Encoding UTF8
162162 $result = Get-Content $script :testFile - Encoding UTF8
163163 $result | Should - Be " 設定: English"
164164 }
@@ -173,14 +173,14 @@ Describe "Update-MatchInFile Integration Tests" {
173173
174174 Context " バックアップ機能" {
175175 It " -Backup を指定するとバックアップファイルが作成される" {
176- Update-MatchInFile - Path $script :testFile -Contains " localhost" - Replacement " newhost" - Backup
176+ Update-MatchInFile - Path $script :testFile - OldText " localhost" - Replacement " newhost" - Backup
177177 $backupFiles = Get-ChildItem - Path (Split-Path $script :testFile ) - Filter " $ ( [System.IO.Path ]::GetFileName($script :testFile )) *.bak"
178178 $backupFiles.Count | Should - BeGreaterThan 0
179179 }
180180
181181 It " バックアップファイルに元の内容が保存される" {
182182 $originalContent = Get-Content $script :testFile
183- Update-MatchInFile - Path $script :testFile -Contains " localhost" - Replacement " newhost" - Backup
183+ Update-MatchInFile - Path $script :testFile - OldText " localhost" - Replacement " newhost" - Backup
184184 $backupFile = Get-ChildItem - Path (Split-Path $script :testFile ) - Filter " $ ( [System.IO.Path ]::GetFileName($script :testFile )) *.bak" | Select-Object - First 1
185185 $backupContent = Get-Content $backupFile.FullName
186186 $backupContent | Should - Be $originalContent
@@ -190,25 +190,25 @@ Describe "Update-MatchInFile Integration Tests" {
190190 Context " WhatIf と Confirm" {
191191 It " -WhatIf を指定すると実際には変更しない" {
192192 $originalContent = Get-Content $script :testFile
193- Update-MatchInFile - Path $script :testFile -Contains " localhost" - Replacement " newhost" - WhatIf
193+ Update-MatchInFile - Path $script :testFile - OldText " localhost" - Replacement " newhost" - WhatIf
194194 $result = Get-Content $script :testFile
195195 $result | Should - Be $originalContent
196196 }
197197 }
198198
199199 Context " エラーハンドリング" {
200200 It " 存在しないファイルでエラーになる" {
201- { Update-MatchInFile - Path " C:\NonExistent\file.txt" -Contains " test" - Replacement " new" - ErrorAction Stop } |
201+ { Update-MatchInFile - Path " C:\NonExistent\file.txt" - OldText " test" - Replacement " new" - ErrorAction Stop } |
202202 Should - Throw
203203 }
204204
205205 It " Contains と Pattern を同時に指定するとエラーになる" {
206- { Update-MatchInFile - Path $script :testFile -Contains " test" - Pattern " test" - Replacement " new" - ErrorAction Stop } |
206+ { Update-MatchInFile - Path $script :testFile - OldText " test" - Pattern " test" - Replacement " new" - ErrorAction Stop } |
207207 Should - Throw
208208 }
209209
210210 It " Replacement を指定しないとエラーになる" {
211- { Update-MatchInFile - Path $script :testFile -Contains " test" - ErrorAction Stop } |
211+ { Update-MatchInFile - Path $script :testFile - OldText " test" - ErrorAction Stop } |
212212 Should - Throw
213213 }
214214
@@ -224,7 +224,7 @@ Describe "Update-MatchInFile Integration Tests" {
224224 Set-Content - Path $file2 - Value " Server: localhost"
225225
226226 try {
227- Get-Item @ ($script :testFile , $file2 ) | Update-MatchInFile -Contains " localhost" - Replacement " production"
227+ Get-Item @ ($script :testFile , $file2 ) | Update-MatchInFile - OldText " localhost" - Replacement " production"
228228 $result1 = Get-Content $script :testFile
229229 $result2 = Get-Content $file2
230230 $result1 [0 ] | Should - Be " Server: production"
@@ -238,7 +238,7 @@ Describe "Update-MatchInFile Integration Tests" {
238238 Context " 空文字列による削除(Empty Replacement)" {
239239 It " Contains + 空文字列でマッチしたテキストを削除できる" {
240240 Set-Content - Path $script :testFile - Value " Server=localhost:8080" - Encoding UTF8
241- Update-MatchInFile - Path $script :testFile -Contains " :8080" - Replacement " "
241+ Update-MatchInFile - Path $script :testFile - OldText " :8080" - Replacement " "
242242 $result = Get-Content $script :testFile - Raw
243243 $result.Trim () | Should - Be " Server=localhost"
244244 }
@@ -275,15 +275,15 @@ Describe "Update-MatchInFile Integration Tests" {
275275 It " 空文字列削除後もエンコーディングが保持される" {
276276 $content = " 日本語テキスト:削除対象"
277277 Set-Content - Path $script :testFile - Value $content - Encoding UTF8
278- Update-MatchInFile - Path $script :testFile -Contains " :削除対象" - Replacement " "
278+ Update-MatchInFile - Path $script :testFile - OldText " :削除対象" - Replacement " "
279279 $result = Get-Content $script :testFile - Raw - Encoding UTF8
280280 $result.Trim () | Should - Be " 日本語テキスト"
281281 }
282282 }
283283
284284 Context " Replacement パラメータの検証" {
285285 It " Replacement を指定しない(null)場合、Contains でエラーになる" {
286- { Update-MatchInFile - Path $script :testFile -Contains " test" } | Should - Throw
286+ { Update-MatchInFile - Path $script :testFile - OldText " test" } | Should - Throw
287287 }
288288
289289 It " Replacement を指定しない(null)場合、Pattern でエラーになる" {
@@ -292,7 +292,7 @@ Describe "Update-MatchInFile Integration Tests" {
292292
293293 It " Replacement に空文字列を指定した場合はエラーにならない(削除として動作)" {
294294 Set-Content - Path $script :testFile - Value " Test123" - Encoding UTF8
295- { Update-MatchInFile - Path $script :testFile -Contains " 123" - Replacement " " } |
295+ { Update-MatchInFile - Path $script :testFile - OldText " 123" - Replacement " " } |
296296 Should -Not - Throw
297297 $result = Get-Content $script :testFile - Raw
298298 $result.Trim () | Should - Be " Test"
0 commit comments