Skip to content

Commit 9b084f2

Browse files
committed
Simplify Extension parameter handling and add edge case tests
- Simplify extension logic: use ternary operator to pass null when empty - Remove TrimEnd('.'): fixes issue where dots in base name were removed - Add test cases for preserving dots in base name: * When removing extension (file...txt -> file..) * When changing extension (file...txt -> file...md)
1 parent 1b02f52 commit 9b084f2

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

src/Microsoft.PowerShell.Commands.Management/commands/management/CombinePathCommand.cs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -144,13 +144,7 @@ protected override void ProcessRecord()
144144
// Change extension if specified
145145
if (Extension != null && joinedPath != null)
146146
{
147-
joinedPath = System.IO.Path.ChangeExtension(joinedPath, Extension);
148-
149-
// Remove trailing dot when extension is empty string
150-
if (Extension.Length == 0)
151-
{
152-
joinedPath = joinedPath.TrimEnd('.');
153-
}
147+
joinedPath = System.IO.Path.ChangeExtension(joinedPath, Extension.Length == 0 ? null : Extension);
154148
}
155149

156150
if (Resolve)

test/powershell/Modules/Microsoft.PowerShell.Management/Join-Path.Tests.ps1

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@ Describe "Join-Path cmdlet tests" -Tags "CI" {
9696
$result = Join-Path -Path "folder" -ChildPath "file.txt" -Extension ""
9797
$result | Should -BeExactly "folder${SepChar}file"
9898
}
99+
It "should preserve dots in base name when removing extension with empty string" {
100+
$result = Join-Path -Path "folder" -ChildPath "file...txt" -Extension ""
101+
$result | Should -BeExactly "folder${SepChar}file.."
102+
}
99103
It "should change extension for multiple paths" {
100104
$result = Join-Path -Path "folder1", "folder2" -ChildPath "file.txt" -Extension ".log"
101105
$result.Count | Should -Be 2
@@ -112,6 +116,10 @@ Describe "Join-Path cmdlet tests" -Tags "CI" {
112116
$result = Join-Path -Path "folder" -ChildPath "file.backup.txt" -Extension ".log"
113117
$result | Should -BeExactly "folder${SepChar}file.backup.log"
114118
}
119+
It "should preserve dots in base name when changing extension" {
120+
$result = Join-Path -Path "folder" -ChildPath "file...txt" -Extension ".md"
121+
$result | Should -BeExactly "folder${SepChar}file...md"
122+
}
115123
It "should add extension to directory-like path" {
116124
$result = Join-Path -Path "folder" -ChildPath "subfolder" -Extension ".log"
117125
$result | Should -BeExactly "folder${SepChar}subfolder.log"

0 commit comments

Comments
 (0)