Skip to content

Commit

Permalink
fix: use correct paramName in Path.GetRelativePath (#1035)
Browse files Browse the repository at this point in the history
When calling `Path.GetRelativePath` with an incorrect `relativeTo` parameter (with whitespace), the ParamName of the exception was incorrectly set to `path` instead of `relativeTo`.
  • Loading branch information
vbreuss authored Aug 29, 2023
1 parent 08ea796 commit e6725f4
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -494,8 +494,6 @@ public void IsPathFullyQualified_WithRelativePathParts_ReturnsFalse()
Assert.IsFalse(result);
}



[Test]
public void GetRelativePath_Works()
{
Expand All @@ -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<ArgumentNullException>(() =>
{
mockPath.GetRelativePath("foo", null);
});

Assert.AreEqual("path", exception.ParamName);
}

[Test]
public void GetRelativePath_WhenPathIsWhitespace_ShouldThrowArgumentException()
{
var mockPath = new MockFileSystem().Path;

var exception = Assert.Throws<ArgumentException>(() =>
{
mockPath.GetRelativePath("foo", " ");
});

Assert.AreEqual("path", exception.ParamName);
}

[Test]
public void GetRelativePath_WhenRelativeToNull_ShouldThrowArgumentNullException()
{
var mockPath = new MockFileSystem().Path;

var exception = Assert.Throws<ArgumentNullException>(() =>
{
mockPath.GetRelativePath(null, "foo");
});

Assert.AreEqual("relativeTo", exception.ParamName);
}

[Test]
public void GetRelativePath_WhenRelativeToIsWhitespace_ShouldThrowArgumentException()
{
var mockPath = new MockFileSystem().Path;

var exception = Assert.Throws<ArgumentException>(() =>
{
mockPath.GetRelativePath(" ", "foo");
});

Assert.AreEqual("relativeTo", exception.ParamName);
}
#endif

#if FEATURE_PATH_EXISTS
Expand Down

0 comments on commit e6725f4

Please sign in to comment.