@@ -84,4 +84,102 @@ Describe "Join-Path cmdlet tests" -Tags "CI" {
8484$result = Join-Path - Path$Path - ChildPath$ChildPath
8585$result | Should- BeExactly$ExpectedResult
8686 }
87+ It" should handle extension parameter: <TestName>" - TestCases@ (
88+ @ {
89+ TestName = " change extension"
90+ ChildPath = " file.txt"
91+ Extension = " .log"
92+ ExpectedChildPath = " file.log"
93+ }
94+ @ {
95+ TestName = " add extension to file without extension"
96+ ChildPath = " file"
97+ Extension = " .txt"
98+ ExpectedChildPath = " file.txt"
99+ }
100+ @ {
101+ TestName = " extension without leading dot"
102+ ChildPath = " file.txt"
103+ Extension = " log"
104+ ExpectedChildPath = " file.log"
105+ }
106+ @ {
107+ TestName = " double extension with dot"
108+ ChildPath = " file.txt"
109+ Extension = " .tar.gz"
110+ ExpectedChildPath = " file.tar.gz"
111+ }
112+ @ {
113+ TestName = " double extension without dot"
114+ ChildPath = " file.txt"
115+ Extension = " tar.gz"
116+ ExpectedChildPath = " file.tar.gz"
117+ }
118+ @ {
119+ TestName = " remove extension with empty string"
120+ ChildPath = " file.txt"
121+ Extension = " "
122+ ExpectedChildPath = " file"
123+ }
124+ @ {
125+ TestName = " preserve dots in base name when removing extension with empty string"
126+ ChildPath = " file...txt"
127+ Extension = " "
128+ ExpectedChildPath = " file.."
129+ }
130+ @ {
131+ TestName = " replace only the last extension for files with multiple dots"
132+ ChildPath = " file.backup.txt"
133+ Extension = " .log"
134+ ExpectedChildPath = " file.backup.log"
135+ }
136+ @ {
137+ TestName = " preserve dots in base name when changing extension"
138+ ChildPath = " file...txt"
139+ Extension = " .md"
140+ ExpectedChildPath = " file...md"
141+ }
142+ @ {
143+ TestName = " add extension to directory-like path"
144+ ChildPath = " subfolder"
145+ Extension = " .log"
146+ ExpectedChildPath = " subfolder.log"
147+ }
148+ ) {
149+ param ($TestName , $ChildPath , $Extension , $ExpectedChildPath )
150+ $result = Join-Path - Path" folder" - ChildPath$ChildPath - Extension$Extension
151+ $result | Should- BeExactly" folder${SepChar}${ExpectedChildPath} "
152+ }
153+ It" should handle extension parameter with multiple child path segments: <TestName>" - TestCases@ (
154+ @ {
155+ TestName = " change extension when joining multiple child path segments"
156+ ChildPaths = @ (" subfolder" , " file.txt" )
157+ Extension = " .log"
158+ ExpectedPath = " folder${SepChar} subfolder${SepChar} file.log"
159+ }
160+ ) {
161+ param ($TestName , $ChildPaths , $Extension , $ExpectedPath )
162+ $result = Join-Path - Path" folder" - ChildPath$ChildPaths - Extension$Extension
163+ $result | Should- BeExactly$ExpectedPath
164+ }
165+ It" should change extension for multiple paths" {
166+ $result = Join-Path - Path" folder1" , " folder2" - ChildPath" file.txt" - Extension" .log"
167+ $result.Count | Should- Be2
168+ $result [0 ]| Should- BeExactly" folder1${SepChar} file.log"
169+ $result [1 ]| Should- BeExactly" folder2${SepChar} file.log"
170+ }
171+ It" should resolve path when -Extension changes to existing file" {
172+ New-Item - Path TestDrive:\testfile.log- ItemType File- Force| Out-Null
173+ $result = Join-Path - Path TestDrive:- ChildPath" testfile.txt" - Extension" .log" - Resolve
174+ $result | Should- BeLike" *testfile.log"
175+ }
176+ It" should throw error when -Extension changes to non-existing file with -Resolve" {
177+ {Join-Path - Path TestDrive:- ChildPath" testfile.txt" - Extension" .nonexistent" - Resolve- ErrorAction Stop;Throw " Previous statement unexpectedly succeeded..." }|
178+ Should- Throw- ErrorId" PathNotFound,Microsoft.PowerShell.Commands.JoinPathCommand"
179+ }
180+ It" should accept Extension from pipeline by property name" {
181+ $obj = [PSCustomObject ]@ {Path = " folder" ;ChildPath = " file.txt" ;Extension = " .log" }
182+ $result = $obj | Join-Path
183+ $result | Should- BeExactly" folder${SepChar} file.log"
184+ }
87185}