Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public MockDirectoryInfo(IMockFileDataAccessor mockFileDataAccessor, string dire
{
throw new ArgumentNullException("path", StringResources.Manager.GetString("VALUE_CANNOT_BE_NULL"));
}
if (directoryPath.Trim() == string.Empty)
if (PathVerifier.IsEffectivelyEmpty(directoryPath))
{
throw CommonExceptions.PathIsNotOfALegalForm("path");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ public override bool Exists(string path)
return false;
}

if (path.Trim() == string.Empty)
if (PathVerifier.IsEffectivelyEmpty(path))
{
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public override string GetFullPath(string path)
throw new ArgumentNullException(nameof(path), StringResources.Manager.GetString("VALUE_CANNOT_BE_NULL"));
}

if (string.IsNullOrWhiteSpace(path))
if (PathVerifier.IsEffectivelyEmpty(path))
{
throw CommonExceptions.PathIsNotOfALegalForm(nameof(path));
}
Expand Down Expand Up @@ -167,7 +167,7 @@ public override string GetRelativePath(string relativeTo, string path)
throw new ArgumentNullException(nameof(relativeTo), StringResources.Manager.GetString("VALUE_CANNOT_BE_NULL"));
}

if (string.IsNullOrWhiteSpace(relativeTo))
if (PathVerifier.IsEffectivelyEmpty(relativeTo))
{
throw CommonExceptions.PathIsNotOfALegalForm(nameof(relativeTo));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public void IsLegalAbsoluteOrRelative(string path, string paramName)
throw new ArgumentException("Empty file name is not legal.", paramName);
}

if (path.Trim() == string.Empty)
if (IsEffectivelyEmpty(path))
{
throw CommonExceptions.PathIsNotOfALegalForm(paramName);
}
Expand All @@ -67,6 +67,15 @@ public void IsLegalAbsoluteOrRelative(string path, string paramName)
}
}

/// <summary>
/// Determines whether the given path is effectively empty: empty on every platform,
/// and also whitespace-only on Windows. Other platforms accept whitespace-only names.
/// </summary>
internal static bool IsEffectivelyEmpty(string path)
{
return string.IsNullOrEmpty(path) || (XFS.IsWindowsPlatform() && string.IsNullOrWhiteSpace(path));
}

private static bool IsValidUseOfVolumeSeparatorChar(string path)
{
if (XFS.IsWindowsPlatform() && path.StartsWith(WINDOWS_EXTENDED_LENGTH_PATH_PREFIX))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,22 @@ public async Task MockDirectoryInfo_Constructor_ShouldThrowArgumentException_IfA
await That(exception.Message).StartsWith("The path is not of a legal form.");
}

[Test]
[UnixOnly(UnixSpecifics.WhitespaceOnlyNames)]
public async Task MockDirectoryInfo_Constructor_ShouldAcceptWhitespaceOnlyNameOnUnix()
{
// Arrange
var fileSystem = new MockFileSystem();
var directoryInfo = fileSystem.DirectoryInfo.New(" ");

// Act
directoryInfo.Create();

// Assert
await That(directoryInfo.FullName).IsEqualTo(fileSystem.Path.Combine(fileSystem.Directory.GetCurrentDirectory(), " "));
await That(fileSystem.Directory.Exists(" ")).IsTrue();
}

[TestCase(@"c:\temp\folder\folder")]
[TestCase(@"..\..\..\Desktop")]
public async Task MockDirectoryInfo_ToString_ShouldReturnDirectoryName(string directoryName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ public async Task MockDirectory_CreateSymbolicLink_ShouldFailWithEmptyTarget()
}

[Test]
[WindowsOnly(WindowsSpecifics.StrictPathRules)]
public async Task MockDirectory_CreateSymbolicLink_ShouldFailWithIllegalPath()
{
// Arrange
Expand All @@ -121,6 +122,7 @@ public async Task MockDirectory_CreateSymbolicLink_ShouldFailWithIllegalPath()
}

[Test]
[WindowsOnly(WindowsSpecifics.StrictPathRules)]
public async Task MockDirectory_CreateSymbolicLink_ShouldFailWithIllegalTarget()
{
// Arrange
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public async Task MockFile_AppendAllLines_ShouldThrowArgumentExceptionIfPathIsZe

[TestCase(" ")]
[TestCase(" ")]
[WindowsOnly(WindowsSpecifics.StrictPathRules)]
public async Task MockFile_AppendAllLines_ShouldThrowArgumentExceptionIfPathContainsOnlyWhitespaces(string path)
{
// Arrange
Expand Down Expand Up @@ -196,6 +197,7 @@ public async Task MockFile_AppendAllLinesAsync_ShouldThrowArgumentExceptionIfPat

[TestCase(" ")]
[TestCase(" ")]
[WindowsOnly(WindowsSpecifics.StrictPathRules)]
public async Task MockFile_AppendAllLinesAsync_ShouldThrowArgumentExceptionIfPathContainsOnlyWhitespaces(string path)
{
// Arrange
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,7 @@ public async Task MockFile_Copy_ShouldThrowArgumentExceptionWhenSourceIsEmpty_Pa
}

[Test]
[WindowsOnly(WindowsSpecifics.StrictPathRules)]
public async Task MockFile_Copy_ShouldThrowArgumentExceptionWhenSourceIsStringOfBlanks()
{
string sourceFilePath = " ";
Expand Down Expand Up @@ -358,6 +359,7 @@ public async Task MockFile_Copy_ShouldThrowArgumentNullExceptionWhenTargetIsNull
}

[Test]
[WindowsOnly(WindowsSpecifics.StrictPathRules)]
public async Task MockFile_Copy_ShouldThrowArgumentExceptionWhenTargetIsStringOfBlanks()
{
string sourceFilePath = XFS.Path(@"c:\something\demo.txt");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ public async Task MockFile_Create_ShouldThrowArgumentNullExceptionIfPathIsNull1(

[TestCase(" ")]
[TestCase(" ")]
[WindowsOnly(WindowsSpecifics.StrictPathRules)]
public async Task MockFile_Create_ShouldThrowArgumentExceptionIfPathContainsOnlyWhitespaces(string path)
{
// Arrange
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public async Task MockFile_Delete_ShouldDeleteFile()

[TestCase(" ")]
[TestCase(" ")]
[WindowsOnly(WindowsSpecifics.StrictPathRules)]
public async Task MockFile_Delete_ShouldThrowArgumentExceptionIfPathContainsOnlyWhitespaces(string path)
{
// Arrange
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public class MockFileGetCreationTimeTests
{
[TestCase(" ")]
[TestCase(" ")]
[WindowsOnly(WindowsSpecifics.StrictPathRules)]
public async Task MockFile_GetCreationTime_ShouldThrowArgumentExceptionIfPathContainsOnlyWhitespaces(string path)
{
// Arrange
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public class MockFileGetCreationTimeUtcTests
{
[TestCase(" ")]
[TestCase(" ")]
[WindowsOnly(WindowsSpecifics.StrictPathRules)]
public async Task MockFile_GetCreationTimeUtc_ShouldThrowArgumentExceptionIfPathContainsOnlyWhitespaces(string path)
{
// Arrange
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public class MockFileGetLastAccessTimeTests
{
[TestCase(" ")]
[TestCase(" ")]
[WindowsOnly(WindowsSpecifics.StrictPathRules)]
public async Task MockFile_GetLastAccessTime_ShouldThrowArgumentExceptionIfPathContainsOnlyWhitespaces(string path)
{
// Arrange
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public class MockFileGetLastAccessTimeUtcTests
{
[TestCase(" ")]
[TestCase(" ")]
[WindowsOnly(WindowsSpecifics.StrictPathRules)]
public async Task MockFile_GetLastAccessTimeUtc_ShouldThrowArgumentExceptionIfPathContainsOnlyWhitespaces(string path)
{
// Arrange
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public class MockFileGetLastWriteTimeTests
{
[TestCase(" ")]
[TestCase(" ")]
[WindowsOnly(WindowsSpecifics.StrictPathRules)]
public async Task MockFile_GetLastWriteTime_ShouldThrowArgumentExceptionIfPathContainsOnlyWhitespaces(string path)
{
// Arrange
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ public class MockFileGetLastWriteTimeUtcTests
{
[TestCase(" ")]
[TestCase(" ")]
[WindowsOnly(WindowsSpecifics.StrictPathRules)]
public async Task MockFile_GetLastWriteTimeUtc_ShouldThrowArgumentExceptionIfPathContainsOnlyWhitespaces(string path)
{
// Arrange
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,35 @@ public async Task MockFileInfo_NullPath_ThrowArgumentNullException()

}

[Test]
[UnixOnly(UnixSpecifics.WhitespaceOnlyNames)]
public async Task MockFileInfo_WhitespaceOnlyName_ShouldBeLegalOnUnix()
{
var fileSystem = new MockFileSystem();
var currentDirectory = fileSystem.Directory.GetCurrentDirectory();
var fileInfo = fileSystem.FileInfo.New(" ");

fileSystem.File.WriteAllText(" ", "Demo text content");

await That(fileInfo.FullName).IsEqualTo(fileSystem.Path.Combine(currentDirectory, " "));
await That(fileInfo.Exists).IsTrue();
await That(fileSystem.File.Exists(" ")).IsTrue();
await That(fileSystem.File.ReadAllText(" ")).IsEqualTo("Demo text content");
await That(fileSystem.Directory.GetFiles(currentDirectory)).Contains(fileInfo.FullName);
}

[Test]
[WindowsOnly(WindowsSpecifics.StrictPathRules)]
public async Task MockFileInfo_WhitespaceOnlyName_ShouldThrowArgumentExceptionOnWindows()
{
var fileSystem = new MockFileSystem();

Action action = () => fileSystem.FileInfo.New(" ");

var exception = await That(action).Throws<ArgumentException>();
await That(exception.Message).StartsWith("The path is not of a legal form.");
}

[Test]
public async Task MockFileInfo_Exists_ShouldReturnTrueIfFileExistsInMemoryFileSystem()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ public async Task MockFile_Move_ShouldThrowArgumentExceptionWhenSourceIsEmpty_Pa
}

[Test]
[WindowsOnly(WindowsSpecifics.StrictPathRules)]
public async Task MockFile_Move_ShouldThrowArgumentExceptionWhenSourceIsStringOfBlanks()
{
string sourceFilePath = " ";
Expand Down Expand Up @@ -322,6 +323,7 @@ public async Task MockFile_Move_ShouldThrowArgumentNullExceptionWhenTargetIsNull
}

[Test]
[WindowsOnly(WindowsSpecifics.StrictPathRules)]
public async Task MockFile_Move_ShouldThrowArgumentExceptionWhenTargetIsStringOfBlanks()
{
string sourceFilePath = XFS.Path(@"c:\something\demo.txt");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,18 @@ public async Task MockFile_SetAttributes_ShouldSetAttributesOnDirectory()
}

[Test]
[TestCase("", FileAttributes.Normal)]
public async Task MockFile_SetAttributes_ShouldThrowArgumentExceptionIfPathIsEmpty()
{
var fileSystem = new MockFileSystem();

Action action = () => fileSystem.File.SetAttributes("", FileAttributes.Normal);

await That(action).Throws<ArgumentException>();
}

[Test]
[TestCase(" ", FileAttributes.Normal)]
[WindowsOnly(WindowsSpecifics.StrictPathRules)]
public async Task MockFile_SetAttributes_ShouldThrowArgumentExceptionIfPathContainsOnlyWhitespaces(string path, FileAttributes attributes)
{
var fileSystem = new MockFileSystem();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ public async Task MockFile_CreateSymbolicLink_ShouldFailWithEmptyTarget()
}

[Test]
[WindowsOnly(WindowsSpecifics.StrictPathRules)]
public async Task MockFile_CreateSymbolicLink_ShouldFailWithIllegalPath()
{
// Arrange
Expand All @@ -128,6 +129,7 @@ public async Task MockFile_CreateSymbolicLink_ShouldFailWithIllegalPath()
}

[Test]
[WindowsOnly(WindowsSpecifics.StrictPathRules)]
public async Task MockFile_CreateSymbolicLink_ShouldFailWithIllegalTarget()
{
// Arrange
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ public async Task GetFullPath_EmptyValue_ShouldThrowArgumentException()
}

[Test]
[WindowsOnly(WindowsSpecifics.StrictPathRules)]
public async Task GetFullPath_WithWhiteSpace_ShouldThrowArgumentException()
{
var mockFileSystem = new MockFileSystem();
Expand Down Expand Up @@ -535,6 +536,7 @@ public async Task GetRelativePath_WhenPathIsNull_ShouldThrowArgumentNullExceptio
}

[Test]
[WindowsOnly(WindowsSpecifics.StrictPathRules)]
public async Task GetRelativePath_WhenPathIsWhitespace_ShouldThrowArgumentException()
{
var mockPath = new MockFileSystem().Path;
Expand All @@ -561,6 +563,7 @@ public async Task GetRelativePath_WhenRelativeToNull_ShouldThrowArgumentNullExce
}

[Test]
[WindowsOnly(WindowsSpecifics.StrictPathRules)]
public async Task GetRelativePath_WhenRelativeToIsWhitespace_ShouldThrowArgumentException()
{
var mockPath = new MockFileSystem().Path;
Expand All @@ -572,6 +575,18 @@ public async Task GetRelativePath_WhenRelativeToIsWhitespace_ShouldThrowArgument

await That(exception.ParamName).IsEqualTo("relativeTo");
}

[TestCase(" ", "foo", "../foo")]
[TestCase("foo", " ", "../ ")]
[UnixOnly(UnixSpecifics.WhitespaceOnlyNames)]
public async Task GetRelativePath_WithWhitespaceOnlyName_ShouldWorkOnUnix(string relativeTo, string path, string expected)
{
var mockPath = new MockFileSystem().Path;

var result = mockPath.GetRelativePath(relativeTo, path);

await That(result).IsEqualTo(expected);
}
#endif

#if FEATURE_PATH_EXISTS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ internal static class UnixSpecifics
public const string SlashRoot = "Filesystem root is just '/' on Unix";

public const string CaseSensitivity = "Paths are case-sensitive on Unix";

public const string WhitespaceOnlyNames = "Whitespace-only names are legal on Unix";
}
Loading