From 53e462f990e5a2f26b1a34e8c0a714d89e845296 Mon Sep 17 00:00:00 2001 From: Simon Cropp Date: Fri, 13 Sep 2024 21:51:14 +1000 Subject: [PATCH] Fix alt dir separator issue on mac (#1292) --- docs/serializer-settings.md | 50 +++++++++---------- ...nTests.ScrubProjectDirectory.verified.json | 6 ++- ...Tests.ScrubSolutionDirectory.verified.json | 6 ++- ...onTests.ScrubProjectDirectory.verified.txt | 6 ++- ...nTests.ScrubSolutionDirectory.verified.txt | 6 ++- .../Serialization/SerializationTests.cs | 18 ++++--- src/Verify/IoHelpers.cs | 16 ++++++ .../Serialization/Scrubbers/ApplyScrubbers.cs | 20 ++++---- 8 files changed, 78 insertions(+), 50 deletions(-) diff --git a/docs/serializer-settings.md b/docs/serializer-settings.md index f039bb5c79..484a0de5ab 100644 --- a/docs/serializer-settings.md +++ b/docs/serializer-settings.md @@ -347,7 +347,7 @@ public Task ScopedSerializerFluent() .AddExtraSettings(_ => _.TypeNameHandling = TypeNameHandling.All); } ``` -snippet source | anchor +snippet source | anchor Result: @@ -475,7 +475,7 @@ public Task IgnoreTypeFluent() .IgnoreMembersWithType(); } ``` -snippet source | anchor +snippet source | anchor Or globally: @@ -485,7 +485,7 @@ Or globally: ```cs VerifierSettings.IgnoreMembersWithType(); ``` -snippet source | anchor +snippet source | anchor Result: @@ -622,7 +622,7 @@ public Task ScrubTypeFluent() .ScrubMembersWithType(); } ``` -snippet source | anchor +snippet source | anchor Or globally: @@ -632,7 +632,7 @@ Or globally: ```cs VerifierSettings.ScrubMembersWithType(); ``` -snippet source | anchor +snippet source | anchor Result: @@ -711,7 +711,7 @@ public Task AddIgnoreInstanceFluent() .IgnoreInstance(_ => _.Property == "Ignore"); } ``` -snippet source | anchor +snippet source | anchor Or globally: @@ -721,7 +721,7 @@ Or globally: ```cs VerifierSettings.IgnoreInstance(_ => _.Property == "Ignore"); ``` -snippet source | anchor +snippet source | anchor Result: @@ -783,7 +783,7 @@ public Task AddScrubInstanceFluent() .ScrubInstance(_ => _.Property == "Ignore"); } ``` -snippet source | anchor +snippet source | anchor Or globally: @@ -793,7 +793,7 @@ Or globally: ```cs VerifierSettings.ScrubInstance(_ => _.Property == "Ignore"); ``` -snippet source | anchor +snippet source | anchor Result: @@ -838,7 +838,7 @@ public Task WithObsoleteProp() return Verify(target); } ``` -snippet source | anchor +snippet source | anchor Result: @@ -886,7 +886,7 @@ public Task WithObsoletePropIncludedFluent() .IncludeObsoletes(); } ``` -snippet source | anchor +snippet source | anchor Or globally: @@ -896,7 +896,7 @@ Or globally: ```cs VerifierSettings.IncludeObsoletes(); ``` -snippet source | anchor +snippet source | anchor Result: @@ -957,7 +957,7 @@ public Task IgnoreMemberByExpressionFluent() _ => _.PropertyThatThrows); } ``` -snippet source | anchor +snippet source | anchor Or globally @@ -972,7 +972,7 @@ VerifierSettings.IgnoreMembers( _ => _.GetOnlyProperty, _ => _.PropertyThatThrows); ``` -snippet source | anchor +snippet source | anchor Result: @@ -1032,7 +1032,7 @@ public Task ScrubMemberByExpressionFluent() _ => _.PropertyThatThrows); } ``` -snippet source | anchor +snippet source | anchor Or globally @@ -1047,7 +1047,7 @@ VerifierSettings.ScrubMembers( _ => _.GetOnlyProperty, _ => _.PropertyThatThrows); ``` -snippet source | anchor +snippet source | anchor Result: @@ -1126,7 +1126,7 @@ public Task IgnoreMemberByNameFluent() .IgnoreMember(_ => _.PropertyThatThrows); } ``` -snippet source | anchor +snippet source | anchor Or globally: @@ -1146,7 +1146,7 @@ VerifierSettings.IgnoreMember("Field"); // For a specific type with expression VerifierSettings.IgnoreMember(_ => _.PropertyThatThrows); ``` -snippet source | anchor +snippet source | anchor Result: @@ -1221,7 +1221,7 @@ public Task ScrubMemberByNameFluent() .ScrubMember(_ => _.PropertyThatThrows); } ``` -snippet source | anchor +snippet source | anchor Or globally: @@ -1241,7 +1241,7 @@ VerifierSettings.ScrubMember("Field"); // For a specific type with expression VerifierSettings.ScrubMember(_ => _.PropertyThatThrows); ``` -snippet source | anchor +snippet source | anchor Result: @@ -1292,7 +1292,7 @@ public Task CustomExceptionPropFluent() .IgnoreMembersThatThrow(); } ``` -snippet source | anchor +snippet source | anchor Or globally: @@ -1302,7 +1302,7 @@ Or globally: ```cs VerifierSettings.IgnoreMembersThatThrow(); ``` -snippet source | anchor +snippet source | anchor Result: @@ -1339,7 +1339,7 @@ public Task ExceptionMessagePropFluent() .IgnoreMembersThatThrow(_ => _.Message == "Ignore"); } ``` -snippet source | anchor +snippet source | anchor Or globally: @@ -1349,7 +1349,7 @@ Or globally: ```cs VerifierSettings.IgnoreMembersThatThrow(_ => _.Message == "Ignore"); ``` -snippet source | anchor +snippet source | anchor Result: @@ -1507,7 +1507,7 @@ public Task MemberConverterByExpression() return Verify(input); } ``` -snippet source | anchor +snippet source | anchor diff --git a/src/StrictJsonTests/SerializationTests.ScrubProjectDirectory.verified.json b/src/StrictJsonTests/SerializationTests.ScrubProjectDirectory.verified.json index dd85b155c6..adac9e0957 100644 --- a/src/StrictJsonTests/SerializationTests.ScrubProjectDirectory.verified.json +++ b/src/StrictJsonTests/SerializationTests.ScrubProjectDirectory.verified.json @@ -1,6 +1,8 @@ { "path": "{ProjectDirectory}Foo", "trimmed": "{ProjectDirectory}Foo", - "altPath": "{ProjectDirectory}Foo", - "altPathTrimmed": "{ProjectDirectory}Foo" + "altPath1": "{ProjectDirectory}Foo", + "altPath1Trimmed": "{ProjectDirectory}Foo", + "altPath2": "{ProjectDirectory}Foo", + "altPath2Trimmed": "{ProjectDirectory}Foo" } \ No newline at end of file diff --git a/src/StrictJsonTests/SerializationTests.ScrubSolutionDirectory.verified.json b/src/StrictJsonTests/SerializationTests.ScrubSolutionDirectory.verified.json index 9d825603a9..dc41d574e9 100644 --- a/src/StrictJsonTests/SerializationTests.ScrubSolutionDirectory.verified.json +++ b/src/StrictJsonTests/SerializationTests.ScrubSolutionDirectory.verified.json @@ -1,6 +1,8 @@ { "path": "{SolutionDirectory}Foo", "trimmed": "{SolutionDirectory}Foo", - "altPath": "{SolutionDirectory}Foo", - "altPathTrimmed": "{SolutionDirectory}Foo" + "altPath1": "{SolutionDirectory}Foo", + "altPath1Trimmed": "{SolutionDirectory}Foo", + "altPath2": "{SolutionDirectory}Foo", + "altPath2Trimmed": "{SolutionDirectory}Foo" } \ No newline at end of file diff --git a/src/Verify.Tests/Serialization/SerializationTests.ScrubProjectDirectory.verified.txt b/src/Verify.Tests/Serialization/SerializationTests.ScrubProjectDirectory.verified.txt index 462c1d6fbf..4f5815a2a1 100644 --- a/src/Verify.Tests/Serialization/SerializationTests.ScrubProjectDirectory.verified.txt +++ b/src/Verify.Tests/Serialization/SerializationTests.ScrubProjectDirectory.verified.txt @@ -1,6 +1,8 @@ { path: {ProjectDirectory}Foo, trimmed: {ProjectDirectory}Foo, - altPath: {ProjectDirectory}Foo, - altPathTrimmed: {ProjectDirectory}Foo + altPath1: {ProjectDirectory}Foo, + altPath1Trimmed: {ProjectDirectory}Foo, + altPath2: {ProjectDirectory}Foo, + altPath2Trimmed: {ProjectDirectory}Foo } \ No newline at end of file diff --git a/src/Verify.Tests/Serialization/SerializationTests.ScrubSolutionDirectory.verified.txt b/src/Verify.Tests/Serialization/SerializationTests.ScrubSolutionDirectory.verified.txt index cefb3b33ed..21b5d9909f 100644 --- a/src/Verify.Tests/Serialization/SerializationTests.ScrubSolutionDirectory.verified.txt +++ b/src/Verify.Tests/Serialization/SerializationTests.ScrubSolutionDirectory.verified.txt @@ -1,6 +1,8 @@ { path: {SolutionDirectory}Foo, trimmed: {SolutionDirectory}Foo, - altPath: {SolutionDirectory}Foo, - altPathTrimmed: {SolutionDirectory}Foo + altPath1: {SolutionDirectory}Foo, + altPath1Trimmed: {SolutionDirectory}Foo, + altPath2: {SolutionDirectory}Foo, + altPath2Trimmed: {SolutionDirectory}Foo } \ No newline at end of file diff --git a/src/Verify.Tests/Serialization/SerializationTests.cs b/src/Verify.Tests/Serialization/SerializationTests.cs index 79d969a400..f01ced0c82 100644 --- a/src/Verify.Tests/Serialization/SerializationTests.cs +++ b/src/Verify.Tests/Serialization/SerializationTests.cs @@ -2018,14 +2018,17 @@ public Task ScrubProjectDirectory() { var projectDirectory = AttributeReader.GetProjectDirectory(); var path = Path.GetFullPath(Path.Combine(projectDirectory, "Foo")); - var altPath = path.Replace(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar); + var altPath1 = path.Replace('/', '\\'); + var altPath2 = path.Replace('\\', '/'); return Verify( new { path, trimmed = path.TrimEnd('/', '\\'), - altPath, - altPathTrimmed = altPath.TrimEnd('/', '\\') + altPath1, + altPath1Trimmed = altPath1.TrimEnd('/', '\\'), + altPath2, + altPath2Trimmed = altPath2.TrimEnd('/', '\\') }); } @@ -2034,14 +2037,17 @@ public Task ScrubSolutionDirectory() { var solutionDirectory = AttributeReader.GetSolutionDirectory(); var path = Path.GetFullPath(Path.Combine(solutionDirectory, "Foo")); - var altPath = path.Replace(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar); + var altPath1 = path.Replace('/', '\\'); + var altPath2 = path.Replace('\\', '/'); return Verify( new { path, trimmed = path.TrimEnd('/', '\\'), - altPath, - altPathTrimmed = altPath.TrimEnd('/', '\\') + altPath1, + altPath1Trimmed = altPath1.TrimEnd('/', '\\'), + altPath2, + altPath2Trimmed = altPath2.TrimEnd('/', '\\') }); } diff --git a/src/Verify/IoHelpers.cs b/src/Verify/IoHelpers.cs index 7fda358da4..32a337e124 100644 --- a/src/Verify/IoHelpers.cs +++ b/src/Verify/IoHelpers.cs @@ -1,5 +1,21 @@ static class IoHelpers { + static IoHelpers() + { + DirectorySeparator = Path.DirectorySeparatorChar; + if (DirectorySeparator == '/') + { + AltDirectorySeparator = '\\'; + } + else + { + AltDirectorySeparator = '/'; + } + } + + public static char DirectorySeparator { get; } + public static char AltDirectorySeparator { get; } + static readonly char[] Separators = [ '\\', diff --git a/src/Verify/Serialization/Scrubbers/ApplyScrubbers.cs b/src/Verify/Serialization/Scrubbers/ApplyScrubbers.cs index 1e5c820cfc..72718d830c 100644 --- a/src/Verify/Serialization/Scrubbers/ApplyScrubbers.cs +++ b/src/Verify/Serialization/Scrubbers/ApplyScrubbers.cs @@ -2,46 +2,44 @@ static class ApplyScrubbers { - static char dirSeparator = Path.DirectorySeparatorChar; - static char altDirSeparator = Path.AltDirectorySeparatorChar; static List> replacements = []; static string ReplaceAltDirChar(this string directory) => - directory.Replace(dirSeparator, altDirSeparator); + directory.Replace(IoHelpers.DirectorySeparator, IoHelpers.AltDirectorySeparator); public static void UseAssembly(string? solutionDir, string projectDir) { var replacements = new Dictionary(); var baseDir = CleanPath(AppDomain.CurrentDomain.BaseDirectory!); var altBaseDir = baseDir.ReplaceAltDirChar(); - replacements[baseDir + dirSeparator] = "{CurrentDirectory}"; + replacements[baseDir + IoHelpers.DirectorySeparator] = "{CurrentDirectory}"; replacements[baseDir] = "{CurrentDirectory}"; - replacements[altBaseDir + altDirSeparator] = "{CurrentDirectory}"; + replacements[altBaseDir + IoHelpers.AltDirectorySeparator] = "{CurrentDirectory}"; replacements[altBaseDir] = "{CurrentDirectory}"; var currentDir = CleanPath(Environment.CurrentDirectory); var altCurrentDir = currentDir.ReplaceAltDirChar(); - replacements[currentDir + dirSeparator] = "{CurrentDirectory}"; + replacements[currentDir + IoHelpers.DirectorySeparator] = "{CurrentDirectory}"; replacements[currentDir] = "{CurrentDirectory}"; - replacements[altCurrentDir + altDirSeparator] = "{CurrentDirectory}"; + replacements[altCurrentDir + IoHelpers.AltDirectorySeparator] = "{CurrentDirectory}"; replacements[altCurrentDir] = "{CurrentDirectory}"; #if !NET5_0_OR_GREATER if (CodeBaseLocation.CurrentDirectory is not null) { var codeBaseLocation = CleanPath(CodeBaseLocation.CurrentDirectory); var altCodeBaseLocation = codeBaseLocation.ReplaceAltDirChar(); - replacements[codeBaseLocation + dirSeparator] = "{CurrentDirectory}"; + replacements[codeBaseLocation + IoHelpers.DirectorySeparator] = "{CurrentDirectory}"; replacements[codeBaseLocation] = "{CurrentDirectory}"; - replacements[altCodeBaseLocation + altDirSeparator] = "{CurrentDirectory}"; + replacements[altCodeBaseLocation + IoHelpers.AltDirectorySeparator] = "{CurrentDirectory}"; replacements[altCodeBaseLocation] = "{CurrentDirectory}"; } #endif var tempPath = CleanPath(Path.GetTempPath()); var altTempPath = tempPath.ReplaceAltDirChar(); - replacements[tempPath + dirSeparator] = "{TempPath}"; + replacements[tempPath + IoHelpers.DirectorySeparator] = "{TempPath}"; replacements[tempPath] = "{TempPath}"; - replacements[altTempPath + altDirSeparator] = "{TempPath}"; + replacements[altTempPath + IoHelpers.AltDirectorySeparator] = "{TempPath}"; replacements[altTempPath] = "{TempPath}"; if (VerifierSettings.scrubUserProfile)