From d20c8202be33e711ef5116f73bafcb328f5b0728 Mon Sep 17 00:00:00 2001 From: Jason Naylor Date: Fri, 5 Apr 2024 09:10:16 -0700 Subject: [PATCH 1/5] Fix Path.ChangeExtension behavior with empty extension (#100687) - When ChangeExtension is called with an empty string completely remove the extension instead of adding a '.' --- src/libraries/System.Private.CoreLib/src/System/IO/Path.cs | 5 +++++ .../System.Runtime.Extensions.Tests/System/IO/PathTests.cs | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/IO/Path.cs b/src/libraries/System.Private.CoreLib/src/System/IO/Path.cs index 9d1dced98c8c0b..3b398fb83fd9ed 100644 --- a/src/libraries/System.Private.CoreLib/src/System/IO/Path.cs +++ b/src/libraries/System.Private.CoreLib/src/System/IO/Path.cs @@ -47,6 +47,11 @@ public static partial class Path int subLength = path.Length; if (subLength == 0) return string.Empty; + + if(string.IsNullOrEmpty(extension)) + { + return GetFileNameWithoutExtension(path); + } for (int i = path.Length - 1; i >= 0; i--) { diff --git a/src/libraries/System.Runtime/tests/System.Runtime.Extensions.Tests/System/IO/PathTests.cs b/src/libraries/System.Runtime/tests/System.Runtime.Extensions.Tests/System/IO/PathTests.cs index b248728a79da0a..01dd8d74f0ab87 100644 --- a/src/libraries/System.Runtime/tests/System.Runtime.Extensions.Tests/System/IO/PathTests.cs +++ b/src/libraries/System.Runtime/tests/System.Runtime.Extensions.Tests/System/IO/PathTests.cs @@ -15,7 +15,7 @@ public class PathTests : PathTestsBase InlineData(null, "exe", null), InlineData("", "", ""), InlineData("file.exe", null, "file"), - InlineData("file.exe", "", "file."), + InlineData("file.exe", "", "file"), InlineData("file", "exe", "file.exe"), InlineData("file", ".exe", "file.exe"), InlineData("file.txt", "exe", "file.exe"), From e6e051bdad5c05257abd7473435768fdbd054970 Mon Sep 17 00:00:00 2001 From: Jason Naylor Date: Fri, 5 Apr 2024 10:01:50 -0700 Subject: [PATCH 2/5] Remove accidental trailing whitespace --- src/libraries/System.Private.CoreLib/src/System/IO/Path.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/IO/Path.cs b/src/libraries/System.Private.CoreLib/src/System/IO/Path.cs index 3b398fb83fd9ed..a3b531caeb8fdd 100644 --- a/src/libraries/System.Private.CoreLib/src/System/IO/Path.cs +++ b/src/libraries/System.Private.CoreLib/src/System/IO/Path.cs @@ -47,7 +47,7 @@ public static partial class Path int subLength = path.Length; if (subLength == 0) return string.Empty; - + if(string.IsNullOrEmpty(extension)) { return GetFileNameWithoutExtension(path); From c038dd55f5e8e98d790f8aec0c9092f4b2a54585 Mon Sep 17 00:00:00 2001 From: Jason Naylor Date: Fri, 5 Apr 2024 10:16:10 -0700 Subject: [PATCH 3/5] Add Path.ChangeExtension test cases for files with no extension --- .../System.Runtime.Extensions.Tests/System/IO/PathTests.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/libraries/System.Runtime/tests/System.Runtime.Extensions.Tests/System/IO/PathTests.cs b/src/libraries/System.Runtime/tests/System.Runtime.Extensions.Tests/System/IO/PathTests.cs index 01dd8d74f0ab87..043f26d2b34c96 100644 --- a/src/libraries/System.Runtime/tests/System.Runtime.Extensions.Tests/System/IO/PathTests.cs +++ b/src/libraries/System.Runtime/tests/System.Runtime.Extensions.Tests/System/IO/PathTests.cs @@ -16,6 +16,8 @@ public class PathTests : PathTestsBase InlineData("", "", ""), InlineData("file.exe", null, "file"), InlineData("file.exe", "", "file"), + InlineData("file", null, "file"), + InlineData("file", "", "file"), InlineData("file", "exe", "file.exe"), InlineData("file", ".exe", "file.exe"), InlineData("file.txt", "exe", "file.exe"), From 19bd4ddacd49c0e977f8bef7796d6bccf2374391 Mon Sep 17 00:00:00 2001 From: Jason Naylor Date: Fri, 5 Apr 2024 11:36:10 -0700 Subject: [PATCH 4/5] Adjust ChangeExtension test to match updated behavior --- .../tests/System.IO.FileSystem.Tests/File/ChangeExtension.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/File/ChangeExtension.cs b/src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/File/ChangeExtension.cs index cb4156cd44a764..e8a1e26bc5fe6f 100644 --- a/src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/File/ChangeExtension.cs +++ b/src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/File/ChangeExtension.cs @@ -11,7 +11,7 @@ public class File_ChangeExtension : FileSystemTest [InlineData("", ".tmp", "")] [InlineData(null, ".tmp", null)] [InlineData("filename", ".tmp", "filename.tmp")] - [InlineData("filename.tmp", "", "filename.")] + [InlineData("filename.tmp", "", "filename")] [InlineData("filename.tmp", "...", "filename...")] [InlineData("filename.tmp", null, "filename")] [InlineData("filename.tmp.doc", ".tmp", "filename.tmp.tmp")] From ac15e358f3cab4724de190bbc3cc76e819d343c6 Mon Sep 17 00:00:00 2001 From: Jason Naylor Date: Fri, 5 Apr 2024 11:55:36 -0700 Subject: [PATCH 5/5] Adjust comment and refactor to use the existing null test --- .../System.Private.CoreLib/src/System/IO/Path.cs | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/src/libraries/System.Private.CoreLib/src/System/IO/Path.cs b/src/libraries/System.Private.CoreLib/src/System/IO/Path.cs index a3b531caeb8fdd..6977753c099ec0 100644 --- a/src/libraries/System.Private.CoreLib/src/System/IO/Path.cs +++ b/src/libraries/System.Private.CoreLib/src/System/IO/Path.cs @@ -37,7 +37,7 @@ public static partial class Path // the specified extension. If path is null, the function // returns null. If path does not contain a file extension, // the new file extension is appended to the path. If extension - // is null, any existing extension is removed from path. + // is null or empty, any existing extension is removed from path. [return: NotNullIfNotNull(nameof(path))] public static string? ChangeExtension(string? path, string? extension) { @@ -48,11 +48,6 @@ public static partial class Path if (subLength == 0) return string.Empty; - if(string.IsNullOrEmpty(extension)) - { - return GetFileNameWithoutExtension(path); - } - for (int i = path.Length - 1; i >= 0; i--) { char ch = path[i]; @@ -69,7 +64,7 @@ public static partial class Path } } - if (extension == null) + if (string.IsNullOrEmpty(extension)) { return path.Substring(0, subLength); }