Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit70a6a80

Browse files
authored
Add -Extension parameter to Join-Path cmdlet (#26482)
1 parent8948c41 commit70a6a80

File tree

2 files changed

+119
-0
lines changed

2 files changed

+119
-0
lines changed

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,21 @@ public class JoinPathCommand : CoreCommandWithCredentialsBase
5151
[Parameter]
5252
publicSwitchParameterResolve{get;set;}
5353

54+
/// <summary>
55+
/// Gets or sets the extension to use for the resulting path.
56+
/// If not specified, the original extension (if any) is preserved.
57+
/// <para>
58+
/// Behavior:
59+
/// - If the path has an existing extension, it will be replaced with the specified extension.
60+
/// - If the path does not have an extension, the specified extension will be added.
61+
/// - If an empty string is provided, any existing extension will be removed.
62+
/// - A leading dot in the extension is optional; if omitted, one will be added automatically.
63+
/// </para>
64+
/// </summary>
65+
[Parameter(ValueFromPipelineByPropertyName=true)]
66+
[ValidateNotNull]
67+
publicstringExtension{get;set;}
68+
5469
#endregion Parameters
5570

5671
#region Command code
@@ -128,6 +143,12 @@ protected override void ProcessRecord()
128143
continue;
129144
}
130145

146+
// If Extension parameter is present it is not null due to [ValidateNotNull].
147+
if(Extensionis notnull)
148+
{
149+
joinedPath=System.IO.Path.ChangeExtension(joinedPath,Extension.Length==0?null:Extension);
150+
}
151+
131152
if(Resolve)
132153
{
133154
// Resolve the paths. The default API (GetResolvedPSPathFromPSPath)

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

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp