Skip to content

Commit e6725f4

Browse files
authored
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`.
1 parent 08ea796 commit e6725f4

File tree

2 files changed

+53
-3
lines changed
  • src/TestableIO.System.IO.Abstractions.TestingHelpers
  • tests/TestableIO.System.IO.Abstractions.TestingHelpers.Tests

2 files changed

+53
-3
lines changed

src/TestableIO.System.IO.Abstractions.TestingHelpers/MockPath.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ public override string GetRelativePath(string relativeTo, string path)
165165
throw new ArgumentNullException(nameof(relativeTo), StringResources.Manager.GetString("VALUE_CANNOT_BE_NULL"));
166166
}
167167

168-
if (relativeTo.Length == 0)
168+
if (string.IsNullOrWhiteSpace(relativeTo))
169169
{
170170
throw CommonExceptions.PathIsNotOfALegalForm(nameof(relativeTo));
171171
}

tests/TestableIO.System.IO.Abstractions.TestingHelpers.Tests/MockPathTests.cs

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -494,8 +494,6 @@ public void IsPathFullyQualified_WithRelativePathParts_ReturnsFalse()
494494
Assert.IsFalse(result);
495495
}
496496

497-
498-
499497
[Test]
500498
public void GetRelativePath_Works()
501499
{
@@ -508,6 +506,58 @@ public void GetRelativePath_Works()
508506
//Assert
509507
Assert.AreEqual(XFS.Path("e\\f.txt"), result);
510508
}
509+
510+
[Test]
511+
public void GetRelativePath_WhenPathIsNull_ShouldThrowArgumentNullException()
512+
{
513+
var mockPath = new MockFileSystem().Path;
514+
515+
var exception = Assert.Throws<ArgumentNullException>(() =>
516+
{
517+
mockPath.GetRelativePath("foo", null);
518+
});
519+
520+
Assert.AreEqual("path", exception.ParamName);
521+
}
522+
523+
[Test]
524+
public void GetRelativePath_WhenPathIsWhitespace_ShouldThrowArgumentException()
525+
{
526+
var mockPath = new MockFileSystem().Path;
527+
528+
var exception = Assert.Throws<ArgumentException>(() =>
529+
{
530+
mockPath.GetRelativePath("foo", " ");
531+
});
532+
533+
Assert.AreEqual("path", exception.ParamName);
534+
}
535+
536+
[Test]
537+
public void GetRelativePath_WhenRelativeToNull_ShouldThrowArgumentNullException()
538+
{
539+
var mockPath = new MockFileSystem().Path;
540+
541+
var exception = Assert.Throws<ArgumentNullException>(() =>
542+
{
543+
mockPath.GetRelativePath(null, "foo");
544+
});
545+
546+
Assert.AreEqual("relativeTo", exception.ParamName);
547+
}
548+
549+
[Test]
550+
public void GetRelativePath_WhenRelativeToIsWhitespace_ShouldThrowArgumentException()
551+
{
552+
var mockPath = new MockFileSystem().Path;
553+
554+
var exception = Assert.Throws<ArgumentException>(() =>
555+
{
556+
mockPath.GetRelativePath(" ", "foo");
557+
});
558+
559+
Assert.AreEqual("relativeTo", exception.ParamName);
560+
}
511561
#endif
512562

513563
#if FEATURE_PATH_EXISTS

0 commit comments

Comments
 (0)