From da38fd597f173e0fc888dbb65a57daf44697c2bf Mon Sep 17 00:00:00 2001 From: Robert Dailey Date: Sun, 29 May 2022 16:10:52 -0500 Subject: [PATCH] refactor: Move NormalizePath logic to TestLibrary --- .../Extensions/FileSystemExtensionsTest.cs | 26 +++++-------------- src/TestLibrary/FileUtils.cs | 20 ++++++++++++++ src/TestLibrary/MockFileSystemExtensions.cs | 16 ++++++++++++ src/TestLibrary/TestLibrary.csproj | 1 + 4 files changed, 44 insertions(+), 19 deletions(-) create mode 100644 src/TestLibrary/FileUtils.cs create mode 100644 src/TestLibrary/MockFileSystemExtensions.cs diff --git a/src/Common.Tests/Extensions/FileSystemExtensionsTest.cs b/src/Common.Tests/Extensions/FileSystemExtensionsTest.cs index 9a093a61..8baadce7 100644 --- a/src/Common.Tests/Extensions/FileSystemExtensionsTest.cs +++ b/src/Common.Tests/Extensions/FileSystemExtensionsTest.cs @@ -4,6 +4,7 @@ using System.Text.RegularExpressions; using Common.Extensions; using FluentAssertions; using NUnit.Framework; +using TestLibrary; namespace Common.Tests.Extensions; @@ -21,19 +22,6 @@ public class FileSystemExtensionsTest }); } - private static ICollection NormalizePaths(IEnumerable paths) - => paths.Select(NormalizePath).ToList(); - - private static string NormalizePath(string path) - { - if (MockUnixSupport.IsUnixPlatform()) - { - return Regex.Replace(path, @"^C:\\", "/").Replace("\\", "/"); - } - - return Regex.Replace(path, @"^/", @"C:\").Replace("/", "\\"); - } - private static MockFileSystem NewMockFileSystem(IEnumerable files, string cwd) { return NewMockFileSystem(files, Array.Empty(), cwd); @@ -45,13 +33,13 @@ public class FileSystemExtensionsTest var fileData = files.Select(x => (x, new MockFileData(""))); return new MockFileSystem(fileData.Concat(dirData) - .ToDictionary(x => x.Item1, y => y.Item2), NormalizePath(cwd)); + .ToDictionary(x => x.Item1, y => y.Item2), FileUtils.NormalizePath(cwd)); } [Test] public void Merge_directories_works() { - var files = NormalizePaths(new[] + var files = FileUtils.NormalizePaths(new[] { @"path1\1\file1.txt", @"path1\1\file2.txt", @@ -59,7 +47,7 @@ public class FileSystemExtensionsTest @"path1\file4.txt" }); - var dirs = NormalizePaths(new[] + var dirs = FileUtils.NormalizePaths(new[] { @"path1\empty1", @"path1\empty2", @@ -80,7 +68,7 @@ public class FileSystemExtensionsTest [Test] public void Fail_if_file_already_exists() { - var files = NormalizePaths(new[] + var files = FileUtils.NormalizePaths(new[] { @"path1\1\file1.txt", @"path1\1\file2.txt", @@ -97,12 +85,12 @@ public class FileSystemExtensionsTest [Test] public void Fail_if_directory_exists_where_file_goes() { - var files = NormalizePaths(new[] + var files = FileUtils.NormalizePaths(new[] { @"path1\1\file1" }); - var dirs = NormalizePaths(new[] + var dirs = FileUtils.NormalizePaths(new[] { @"path2\1\file1" }); diff --git a/src/TestLibrary/FileUtils.cs b/src/TestLibrary/FileUtils.cs new file mode 100644 index 00000000..a198fbb4 --- /dev/null +++ b/src/TestLibrary/FileUtils.cs @@ -0,0 +1,20 @@ +using System.IO.Abstractions.TestingHelpers; +using System.Text.RegularExpressions; + +namespace TestLibrary; + +public static class FileUtils +{ + public static ICollection NormalizePaths(IEnumerable paths) + => paths.Select(NormalizePath).ToList(); + + public static string NormalizePath(string path) + { + if (MockUnixSupport.IsUnixPlatform()) + { + return Regex.Replace(path, @"^C:\\", "/").Replace("\\", "/"); + } + + return Regex.Replace(path, @"^/", @"C:\").Replace("/", "\\"); + } +} diff --git a/src/TestLibrary/MockFileSystemExtensions.cs b/src/TestLibrary/MockFileSystemExtensions.cs new file mode 100644 index 00000000..5d5d13b0 --- /dev/null +++ b/src/TestLibrary/MockFileSystemExtensions.cs @@ -0,0 +1,16 @@ +using System.IO.Abstractions.TestingHelpers; + +namespace TestLibrary; + +public static class MockFileSystemExtensions +{ + public static void AddFileNoData(this MockFileSystem fs, string path) + { + fs.AddFile(FileUtils.NormalizePath(path), new MockFileData("")); + } + + public static void AddDirectory2(this MockFileSystem fs, string path) + { + fs.AddDirectory(FileUtils.NormalizePath(path)); + } +} diff --git a/src/TestLibrary/TestLibrary.csproj b/src/TestLibrary/TestLibrary.csproj index 77f9e8b4..303fe95f 100644 --- a/src/TestLibrary/TestLibrary.csproj +++ b/src/TestLibrary/TestLibrary.csproj @@ -9,6 +9,7 @@ +