Skip to content

Commit

Permalink
Allow Windows paths on non Windows-systems
Browse files Browse the repository at this point in the history
This makes it possible to test code containing Windows paths
on non-Windows systems. After all, paths themselves do not
interact with the actual file system in any way.
  • Loading branch information
patriksvensson committed Sep 21, 2021
1 parent 9cd6291 commit bd37574
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 122 deletions.
37 changes: 18 additions & 19 deletions src/Spectre.IO.Tests/Unit/DirectoryPathTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using NSubstitute;
using Shouldly;
using Spectre.IO.Testing;
using Spectre.IO.Testing.Xunit;
using Xunit;

namespace Spectre.IO.Tests.Unit.IO
Expand All @@ -12,7 +11,7 @@ public sealed class DirectoryPathTests
{
public sealed class TheGetDirectoryNameMethod
{
[WindowsTheory]
[Theory]
[InlineData("C:/Data", "Data")]
[InlineData("C:/Data/Work", "Work")]
[InlineData("C:/Data/Work/file.txt", "file.txt")]
Expand Down Expand Up @@ -108,7 +107,7 @@ public void Should_Combine_Paths(string first, string second, string expected)
result.FullPath.ShouldBe(expected);
}

[WindowsTheory]
[Theory]
[InlineData("c:/assets/shaders/", "simple.frag", "c:/assets/shaders/simple.frag")]
[InlineData("c:/", "simple.frag", "c:/simple.frag")]
[InlineData("c:/assets/shaders/", "test/simple.frag", "c:/assets/shaders/test/simple.frag")]
Expand Down Expand Up @@ -143,7 +142,7 @@ public void Can_Not_Combine_Directory_Path_With_Absolute_File_Path()
.And().Message.ShouldBe("Cannot combine a directory path with an absolute file path.");
}

[WindowsFact]
[Fact]
public void Can_Not_Combine_Directory_Path_With_Absolute_UNC_File_Path()
{
// Given
Expand Down Expand Up @@ -176,7 +175,7 @@ public void Should_Combine_Paths(string first, string second, string expected)
result.FullPath.ShouldBe(expected);
}

[WindowsTheory]
[Theory]
[InlineData("c:/assets/shaders/", "simple", "c:/assets/shaders/simple")]
[InlineData("c:/", "simple", "c:/simple")]
[InlineData(@"\\", "foo", @"\\foo")]
Expand Down Expand Up @@ -225,7 +224,7 @@ public void Can_Not_Combine_Directory_Path_With_Absolute_Directory_Path()
.And().Message.ShouldBe("Cannot combine a directory path with an absolute directory path.");
}

[WindowsTheory]
[Theory]
[InlineData("C:/foo/bar")]
[InlineData(@"\\foo\bar")]
public void Can_Not_Combine_Directory_Path_With_Absolute_Windows_Directory_Path(string input)
Expand Down Expand Up @@ -290,7 +289,7 @@ public void Should_Create_New_Absolute_Path_Identical_To_The_Path()
result.FullPath.ShouldBe("/assets");
}

[WindowsTheory]
[Theory]
[InlineData("C:/foo")]
[InlineData(@"\\foo")]
public void Should_Create_New_Absolute_Windows_Path_Identical_To_The_Path(string fullPath)
Expand Down Expand Up @@ -403,7 +402,7 @@ public void Should_Collapse_Path_With_Separated_Ellipsis()
path.FullPath.ShouldBe("hello/world");
}

[WindowsFact]
[Fact]
public void Should_Collapse_Path_With_Windows_Root()
{
// Given, When
Expand All @@ -423,7 +422,7 @@ public void Should_Collapse_Path_With_Non_Windows_Root()
path.FullPath.ShouldBe("/hello/world");
}

[WindowsFact]
[Fact]
public void Should_Stop_Collapsing_When_Windows_Root_Is_Reached()
{
// Given, When
Expand Down Expand Up @@ -514,7 +513,7 @@ public sealed class WithDirectoryPath
{
public sealed class InWindowsFormat
{
[WindowsTheory]
[Theory]
[InlineData("C:/A/B/C", "C:/A/B/C", ".")]
[InlineData("C:/", "C:/", ".")]
[InlineData("C:/A/B/C", "C:/A/D/E", "../../D/E")]
Expand All @@ -539,7 +538,7 @@ public void Should_Returns_Relative_Path_Between_Paths(string from, string to, s
relativePath.FullPath.ShouldBe(expected);
}

[WindowsTheory]
[Theory]
[InlineData("C:/A/B/C", "D:/A/B/C")]
[InlineData("C:/A/B", "D:/E/")]
[InlineData("C:/", "B:/")]
Expand All @@ -559,7 +558,7 @@ public void Should_Throw_If_No_Relative_Path_Can_Be_Found(string from, string to
.And().Message.ShouldBe("Paths must share a common prefix.");
}

[WindowsFact]
[Fact]
public void Should_Throw_If_Target_DirectoryPath_Is_Null()
{
// Given
Expand All @@ -573,7 +572,7 @@ public void Should_Throw_If_Target_DirectoryPath_Is_Null()
.And().ParamName.ShouldBe("to");
}

[WindowsFact]
[Fact]
public void Should_Throw_If_Source_DirectoryPath_Is_Relative()
{
// Given
Expand All @@ -587,7 +586,7 @@ public void Should_Throw_If_Source_DirectoryPath_Is_Relative()
.And().Message.ShouldBe("Source path must be an absolute path.");
}

[WindowsTheory]
[Theory]
[InlineData("C:/A/B/C")]
[InlineData(@"\\A\B\C")]
public void Should_Throw_If_Target_DirectoryPath_Is_Relative(string input)
Expand Down Expand Up @@ -690,7 +689,7 @@ public sealed class WithFilePath
{
public sealed class InWindowsFormat
{
[WindowsTheory]
[Theory]
[InlineData("C:/A/B/C", "C:/A/B/C/hello.txt", "hello.txt")]
[InlineData("C:/", "C:/hello.txt", "hello.txt")]
[InlineData("C:/A/B/C", "C:/A/D/E/hello.txt", "../../D/E/hello.txt")]
Expand All @@ -714,7 +713,7 @@ public void Should_Returns_Relative_Path_Between_Paths(string from, string to, s
relativePath.FullPath.ShouldBe(expected);
}

[WindowsTheory]
[Theory]
[InlineData("C:/A/B/C", "D:/A/B/C/hello.txt")]
[InlineData("C:/A/B", "D:/E/hello.txt")]
[InlineData("C:/", "B:/hello.txt")]
Expand All @@ -734,7 +733,7 @@ public void Should_Throw_If_No_Relative_Path_Can_Be_Found(string from, string to
.And().Message.ShouldBe("Paths must share a common prefix.");
}

[WindowsFact]
[Fact]
public void Should_Throw_If_Target_FilePath_Is_Null()
{
// Given
Expand All @@ -748,7 +747,7 @@ public void Should_Throw_If_Target_FilePath_Is_Null()
.And().ParamName.ShouldBe("to");
}

[WindowsFact]
[Fact]
public void Should_Throw_If_Source_DirectoryPath_Is_Relative()
{
// Given
Expand All @@ -762,7 +761,7 @@ public void Should_Throw_If_Source_DirectoryPath_Is_Relative()
.And().Message.ShouldBe("Source path must be an absolute path.");
}

[WindowsTheory]
[Theory]
[InlineData("C:/A/B/C")]
[InlineData(@"\\A\B\C")]
public void Should_Throw_If_Target_FilePath_Is_Relative(string input)
Expand Down
45 changes: 22 additions & 23 deletions src/Spectre.IO.Tests/Unit/FilePathTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using NSubstitute;
using Shouldly;
using Spectre.IO.Testing;
using Spectre.IO.Testing.Xunit;
using Xunit;

namespace Spectre.IO.Tests.Unit.IO
Expand All @@ -26,7 +25,7 @@ public void Can_See_If_A_Path_Has_An_Extension(string fullPath, bool expected)
path.HasExtension.ShouldBe(expected);
}

[WindowsTheory]
[Theory]
[InlineData("C:/foo/bar/baz.txt", true)]
[InlineData("C:/foo/bar/baz", false)]
[InlineData("C:/foo/bar.baz/qux", false)]
Expand Down Expand Up @@ -65,7 +64,7 @@ public void Can_Get_Extension(string fullPath, string expected)
result.ShouldBe(expected);
}

[WindowsTheory]
[Theory]
[InlineData("C:/foo/bar/baz.txt", ".txt")]
[InlineData("C:/foo/bar/baz.txt/qux.md", ".md")]
[InlineData("C:/foo/bar/baz.txt/qux.md.rs", ".rs")]
Expand Down Expand Up @@ -109,7 +108,7 @@ public void Should_Remove_Extension(string fullPath, string expected)
result.FullPath.ShouldBe(expected);
}

[WindowsTheory]
[Theory]
[InlineData("C:/foo/bar/baz.txt", "C:/foo/bar/baz")]
[InlineData("C:/foo/bar/baz.txt/qux.md", "C:/foo/bar/baz.txt/qux")]
[InlineData("C:/foo/bar/baz.txt/qux.md.rs", "C:/foo/bar/baz.txt/qux.md")]
Expand Down Expand Up @@ -149,7 +148,7 @@ public void Can_Get_Directory_For_File_Path(string input, string expected)
result.FullPath.ShouldBe(expected);
}

[WindowsTheory]
[Theory]
[InlineData("C:/temp/hello.txt", "C:/temp")]
[InlineData(@"\\temp\hello.txt", @"\\temp")]
public void Can_Get_Directory_For_Windows_File_Path(string fullPath, string expected)
Expand Down Expand Up @@ -190,7 +189,7 @@ public void Can_Get_Directory_For_Absolute_File_Path_In_Root()
result.FullPath.ShouldBe("/");
}

[WindowsTheory]
[Theory]
[InlineData("C:/hello.txt", "C:/")]
[InlineData(@"\\hello.txt", @"\\")]
public void Can_Get_Directory_For_Absolute_File_Path_In_Windows_Root(string fullPath, string expected)
Expand Down Expand Up @@ -227,7 +226,7 @@ public void Can_Change_Extension_Of_Path(string input, string extension, string
path.ToString().ShouldBe(expected);
}

[WindowsTheory]
[Theory]
[InlineData("C:/temp/hello.txt", ".dat", "C:/temp/hello.dat")]
[InlineData("C:/temp/hello", ".dat", "C:/temp/hello.dat")]
[InlineData("C:/", ".dat", "C:/.dat")]
Expand Down Expand Up @@ -278,7 +277,7 @@ public void Can_Append_Extension_To_Path(string extension, string expected)
path.ToString().ShouldBe(expected);
}

[WindowsTheory]
[Theory]
[InlineData("C:/temp/hello.txt", ".dat", "C:/temp/hello.txt.dat")]
[InlineData(@"\\temp\hello.txt", ".dat", @"\\temp\hello.txt.dat")]
public void Can_Append_Extension_To_Windows_Path(string fullPath, string extension, string expected)
Expand Down Expand Up @@ -318,7 +317,7 @@ public void Can_Get_Filename_From_Path(string input, string expected)
result.FullPath.ShouldBe(expected);
}

[WindowsTheory]
[Theory]
[InlineData("C:/input/test.txt", "test.txt")]
[InlineData("C:/input/test.foo.txt", "test.foo.txt")]
[InlineData("C:/input/test", "test")]
Expand Down Expand Up @@ -367,7 +366,7 @@ public void Should_Return_Filename_Without_Extension_From_Path(string fullPath,
result.FullPath.ShouldBe(expected);
}

[WindowsTheory]
[Theory]
[InlineData("C:/input/test.txt", "test")]
[InlineData("C:/input/test.foo.txt", "test.foo")]
[InlineData("C:/input/test", "test")]
Expand Down Expand Up @@ -442,7 +441,7 @@ public void Should_Return_Same_File_Path_If_File_Path_Is_Absolute(string expecte
result.FullPath.ShouldBe(expected);
}

[WindowsTheory]
[Theory]
[InlineData("C:/foo/bar.txt")]
[InlineData(@"\\foo\bar.txt")]
public void Should_Create_New_Absolute_Windows_Path_Identical_To_The_Path(string expected)
Expand Down Expand Up @@ -558,7 +557,7 @@ public void Should_Collapse_Path_With_Separated_Ellipsis()
path.FullPath.ShouldBe("hello/world/foo.txt");
}

[WindowsFact]
[Fact]
public void Should_Collapse_Path_With_Windows_Root()
{
// Given, When
Expand All @@ -578,7 +577,7 @@ public void Should_Collapse_Path_With_Non_Windows_Root()
path.FullPath.ShouldBe("/hello/world/foo.txt");
}

[WindowsFact]
[Fact]
public void Should_Stop_Collapsing_When_Windows_Root_Is_Reached()
{
// Given, When
Expand Down Expand Up @@ -669,7 +668,7 @@ public sealed class WithDirectoryPath
{
public sealed class InWindowsFormat
{
[WindowsTheory]
[Theory]
[InlineData("C:/A/B/C/hello.txt", "C:/A/B/C", ".")]
[InlineData("C:/hello.txt", "C:/", ".")]
[InlineData("C:/A/B/C/hello.txt", "C:/A/D/E", "../../D/E")]
Expand All @@ -694,7 +693,7 @@ public void Should_Returns_Relative_Path_Between_Paths(string from, string to, s
relativePath.FullPath.ShouldBe(expected);
}

[WindowsTheory]
[Theory]
[InlineData("C:/A/B/C/hello.txt", "D:/A/B/C")]
[InlineData("C:/A/B/hello.txt", "D:/E/")]
[InlineData("C:/hello.txt", "B:/")]
Expand All @@ -714,7 +713,7 @@ public void Should_Throw_If_No_Relative_Path_Can_Be_Found(string from, string to
.And().Message.ShouldBe("Paths must share a common prefix.");
}

[WindowsFact]
[Fact]
public void Should_Throw_If_Target_DirectoryPath_Is_Null()
{
// Given
Expand All @@ -728,7 +727,7 @@ public void Should_Throw_If_Target_DirectoryPath_Is_Null()
.And().ParamName.ShouldBe("to");
}

[WindowsFact]
[Fact]
public void Should_Throw_If_Source_DirectoryPath_Is_Relative()
{
// Given
Expand All @@ -742,7 +741,7 @@ public void Should_Throw_If_Source_DirectoryPath_Is_Relative()
.And().Message.ShouldBe("Source path must be an absolute path.");
}

[WindowsTheory]
[Theory]
[InlineData("C:/A/B/C/hello.txt")]
[InlineData(@"\\A\B\C\hello.txt")]
public void Should_Throw_If_Target_DirectoryPath_Is_Relative(string input)
Expand Down Expand Up @@ -845,7 +844,7 @@ public sealed class WithFilePath
{
public sealed class InWindowsFormat
{
[WindowsTheory]
[Theory]
[InlineData("C:/A/B/C/hello.txt", "C:/A/B/C/hello.txt", "hello.txt")]
[InlineData("C:/hello.txt", "C:/hello.txt", "hello.txt")]
[InlineData("C:/hello.txt", "C:/world.txt", "world.txt")]
Expand All @@ -872,7 +871,7 @@ public void Should_Returns_Relative_Path_Between_Paths(string from, string to, s
relativePath.FullPath.ShouldBe(expected);
}

[WindowsTheory]
[Theory]
[InlineData("C:/A/B/C/hello.txt", "D:/A/B/C/hello.txt")]
[InlineData("C:/A/B/hello.txt", "D:/E/hello.txt")]
[InlineData("C:/hello.txt", "B:/hello.txt")]
Expand All @@ -892,7 +891,7 @@ public void Should_Throw_If_No_Relative_Path_Can_Be_Found(string from, string to
.And().Message.ShouldBe("Paths must share a common prefix.");
}

[WindowsFact]
[Fact]
public void Should_Throw_If_Target_FilePath_Is_Null()
{
// Given
Expand All @@ -906,7 +905,7 @@ public void Should_Throw_If_Target_FilePath_Is_Null()
.And().ParamName.ShouldBe("to");
}

[WindowsFact]
[Fact]
public void Should_Throw_If_Source_DirectoryPath_Is_Relative()
{
// Given
Expand All @@ -920,7 +919,7 @@ public void Should_Throw_If_Source_DirectoryPath_Is_Relative()
.And().Message.ShouldBe("Source path must be an absolute path.");
}

[WindowsTheory]
[Theory]
[InlineData("C:/A/B/C/hello.txt")]
[InlineData(@"\\A\B\C\hello.txt")]
public void Should_Throw_If_Target_FilePath_Is_Relative(string input)
Expand Down
Loading

0 comments on commit bd37574

Please sign in to comment.