From e6725f42c45369196b8079944426af423bccd3c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Valentin=20Breu=C3=9F?= Date: Tue, 29 Aug 2023 16:11:45 +0200 Subject: [PATCH] fix: use correct paramName in Path.GetRelativePath (#1035) When calling `Path.GetRelativePath` with an incorrect `relativeTo` parameter (with whitespace), the ParamName of the exception was incorrectly set to `path` instead of `relativeTo`. --- .../MockPath.cs | 2 +- .../MockPathTests.cs | 54 ++++++++++++++++++- 2 files changed, 53 insertions(+), 3 deletions(-) diff --git a/src/TestableIO.System.IO.Abstractions.TestingHelpers/MockPath.cs b/src/TestableIO.System.IO.Abstractions.TestingHelpers/MockPath.cs index 1ed5c01ab..b79f7c623 100644 --- a/src/TestableIO.System.IO.Abstractions.TestingHelpers/MockPath.cs +++ b/src/TestableIO.System.IO.Abstractions.TestingHelpers/MockPath.cs @@ -165,7 +165,7 @@ public override string GetRelativePath(string relativeTo, string path) throw new ArgumentNullException(nameof(relativeTo), StringResources.Manager.GetString("VALUE_CANNOT_BE_NULL")); } - if (relativeTo.Length == 0) + if (string.IsNullOrWhiteSpace(relativeTo)) { throw CommonExceptions.PathIsNotOfALegalForm(nameof(relativeTo)); } diff --git a/tests/TestableIO.System.IO.Abstractions.TestingHelpers.Tests/MockPathTests.cs b/tests/TestableIO.System.IO.Abstractions.TestingHelpers.Tests/MockPathTests.cs index 69e8b80bb..0cdcaab1f 100644 --- a/tests/TestableIO.System.IO.Abstractions.TestingHelpers.Tests/MockPathTests.cs +++ b/tests/TestableIO.System.IO.Abstractions.TestingHelpers.Tests/MockPathTests.cs @@ -494,8 +494,6 @@ public void IsPathFullyQualified_WithRelativePathParts_ReturnsFalse() Assert.IsFalse(result); } - - [Test] public void GetRelativePath_Works() { @@ -508,6 +506,58 @@ public void GetRelativePath_Works() //Assert Assert.AreEqual(XFS.Path("e\\f.txt"), result); } + + [Test] + public void GetRelativePath_WhenPathIsNull_ShouldThrowArgumentNullException() + { + var mockPath = new MockFileSystem().Path; + + var exception = Assert.Throws(() => + { + mockPath.GetRelativePath("foo", null); + }); + + Assert.AreEqual("path", exception.ParamName); + } + + [Test] + public void GetRelativePath_WhenPathIsWhitespace_ShouldThrowArgumentException() + { + var mockPath = new MockFileSystem().Path; + + var exception = Assert.Throws(() => + { + mockPath.GetRelativePath("foo", " "); + }); + + Assert.AreEqual("path", exception.ParamName); + } + + [Test] + public void GetRelativePath_WhenRelativeToNull_ShouldThrowArgumentNullException() + { + var mockPath = new MockFileSystem().Path; + + var exception = Assert.Throws(() => + { + mockPath.GetRelativePath(null, "foo"); + }); + + Assert.AreEqual("relativeTo", exception.ParamName); + } + + [Test] + public void GetRelativePath_WhenRelativeToIsWhitespace_ShouldThrowArgumentException() + { + var mockPath = new MockFileSystem().Path; + + var exception = Assert.Throws(() => + { + mockPath.GetRelativePath(" ", "foo"); + }); + + Assert.AreEqual("relativeTo", exception.ParamName); + } #endif #if FEATURE_PATH_EXISTS