diff --git a/src/Verify.Tests/VerifyDirectoryTests.cs b/src/Verify.Tests/VerifyDirectoryTests.cs index 7d7cff9fb..336c3243d 100644 --- a/src/Verify.Tests/VerifyDirectoryTests.cs +++ b/src/Verify.Tests/VerifyDirectoryTests.cs @@ -1,6 +1,20 @@ public class VerifyDirectoryTests { static string directoryPathToVerify = Path.Combine(AttributeReader.GetSolutionDirectory(), "ToVerify"); +#if NET8_0_OR_GREATER + +#region AddTextFileConvention + [ModuleInitializer] + public static void InitTextFileConvention() => + FileExtensions.AddTextFileConvention( + path => + { + var name = Path.GetFileName(path); + return name.Equals("TextDocWithoutExtension", StringComparison.OrdinalIgnoreCase); + }); +#endregion + +#endif [Fact] public Task WithDirectory() => diff --git a/src/Verify/FilePair.cs b/src/Verify/FilePair.cs index 04181f618..8f82d4331 100644 --- a/src/Verify/FilePair.cs +++ b/src/Verify/FilePair.cs @@ -11,6 +11,15 @@ public FilePair(string extension, string receivedPath, string verifiedPath) IsText = FileExtensions.IsTextExtension(extension); } + public FilePair(string extension, string receivedPath, string verifiedPath, bool isText) + { + Guards.AgainstBadExtension(extension); + Extension = extension; + ReceivedPath = receivedPath; + VerifiedPath = verifiedPath; + IsText = isText; + } + public string Extension { get; } public string ReceivedPath { get; } public string VerifiedPath { get; } diff --git a/src/Verify/Target.cs b/src/Verify/Target.cs index dcd86c189..36979cbd1 100644 --- a/src/Verify/Target.cs +++ b/src/Verify/Target.cs @@ -58,17 +58,8 @@ public Target(string extension, Stream data, string? name = null) public Target(string extension, StringBuilder data, string? name = null) { - Guards.AgainstBadExtension(extension); Guard.NotEmpty(name); - if (!FileExtensions.IsTextExtension(extension)) - { - throw new( - $""" - Don't pass text for a binary extension. - If {extension} is a text extension then use `FileExtensions.AddTextExtension("{extension}")` at initialization; - Otherwise use `Target(string extension, Stream data, string? name)`. - """); - } + ValidateExtension(extension); Extension = extension; Name = name; @@ -76,20 +67,28 @@ public Target(string extension, StringBuilder data, string? name = null) stringBuilderData = data; } - public Target(string extension, string data, string? name = null) + static void ValidateExtension(string extension) { Guards.AgainstBadExtension(extension); - Guard.NotEmpty(name, nameof(name)); - if (!FileExtensions.IsTextExtension(extension)) + if (extension == "noextension" || + FileExtensions.IsTextExtension(extension)) { - throw new( - $""" - Don't pass a text for a binary extension. - If {extension} is a text extension then use `FileExtensions.AddTextExtension("{extension}")` at initialization; - Otherwise use `Target(string extension, Stream data, string? name)`. - """); + return; } + throw new( + $""" + Don't pass text for a binary extension. + If {extension} is a text extension then use `FileExtensions.AddTextExtension("{extension}")` at initialization; + Otherwise use `Target(string extension, Stream data, string? name)`. + """); + } + + public Target(string extension, string data, string? name = null) + { + Guard.NotEmpty(name, nameof(name)); + ValidateExtension(extension); + Extension = extension; Name = name; stringBuilderData = new(data); diff --git a/src/Verify/Verifier/InnerVerifier.cs b/src/Verify/Verifier/InnerVerifier.cs index 023aa32c2..c54a3f304 100644 --- a/src/Verify/Verifier/InnerVerifier.cs +++ b/src/Verify/Verifier/InnerVerifier.cs @@ -135,7 +135,8 @@ public InnerVerifier(string directory, string name, VerifySettings? settings = n new( target.Extension, $"{prefix}.received.{target.Extension}", - $"{prefix}.verified.{target.Extension}"); + $"{prefix}.verified.{target.Extension}", + target.IsString); getIndexedFileNames = (target, index) => { @@ -144,13 +145,15 @@ public InnerVerifier(string directory, string name, VerifySettings? settings = n return new( target.Extension, $"{prefix}#{index}.received.{target.Extension}", - $"{prefix}#{index}.verified.{target.Extension}"); + $"{prefix}#{index}.verified.{target.Extension}", + target.IsString); } return new( target.Extension, $"{prefix}#{target.Name}.{index}.received.{target.Extension}", - $"{prefix}#{target.Name}.{index}.verified.{target.Extension}"); + $"{prefix}#{target.Name}.{index}.verified.{target.Extension}", + target.IsString); }; } @@ -185,7 +188,8 @@ void InitForDirectoryConvention(Namer namer, string typeAndMethod, string verifi return new( target.Extension, Path.Combine(receivedDirectory, fileName), - Path.Combine(verifiedDirectory, fileName)); + Path.Combine(verifiedDirectory, fileName), + target.IsString); }; getIndexedFileNames = (target, index) => { @@ -193,7 +197,8 @@ void InitForDirectoryConvention(Namer namer, string typeAndMethod, string verifi return new( target.Extension, Path.Combine(receivedDirectory, fileName), - Path.Combine(verifiedDirectory, fileName)); + Path.Combine(verifiedDirectory, fileName), + target.IsString); }; IoHelpers.DeleteDirectory(receivedDirectory); @@ -207,12 +212,14 @@ void InitForDirectoryConvention(Namer namer, string typeAndMethod, string verifi new( target.Extension, Path.Combine(directoryPrefix, $"{target.NameOrTarget}.received.{target.Extension}"), - Path.Combine(directoryPrefix, $"{target.NameOrTarget}.verified.{target.Extension}")); + Path.Combine(directoryPrefix, $"{target.NameOrTarget}.verified.{target.Extension}"), + target.IsString); getIndexedFileNames = (target, index) => new( target.Extension, Path.Combine(directoryPrefix, $"{target.NameOrTarget}#{index}.received.{target.Extension}"), - Path.Combine(directoryPrefix, $"{target.NameOrTarget}#{index}.verified.{target.Extension}")); + Path.Combine(directoryPrefix, $"{target.NameOrTarget}#{index}.verified.{target.Extension}"), + target.IsString); IoHelpers.DeleteFiles(directoryPrefix, "*.received.*"); } @@ -257,13 +264,15 @@ void InitForFileConvention(Namer namer, string typeAndMethod, string receivedPar return new( target.Extension, $"{pathPrefixReceived}.received.{target.Extension}", - $"{pathPrefixVerified}.verified.{target.Extension}"); + $"{pathPrefixVerified}.verified.{target.Extension}", + target.IsString); } return new( target.Extension, $"{pathPrefixReceived}#{target.Name}.received.{target.Extension}", - $"{pathPrefixVerified}#{target.Name}.verified.{target.Extension}"); + $"{pathPrefixVerified}#{target.Name}.verified.{target.Extension}", + target.IsString); }; getIndexedFileNames = (target, index) => { @@ -272,13 +281,15 @@ void InitForFileConvention(Namer namer, string typeAndMethod, string receivedPar return new( target.Extension, $"{pathPrefixReceived}#{index}.received.{target.Extension}", - $"{pathPrefixVerified}#{index}.verified.{target.Extension}"); + $"{pathPrefixVerified}#{index}.verified.{target.Extension}", + target.IsString); } return new( target.Extension, $"{pathPrefixReceived}#{target.Name}.{index}.received.{target.Extension}", - $"{pathPrefixVerified}#{target.Name}.{index}.verified.{target.Extension}"); + $"{pathPrefixVerified}#{target.Name}.{index}.verified.{target.Extension}", + target.IsString); }; MatchingFileFinder.DeleteReceived(receivedPrefix, directory); diff --git a/src/Verify/Verifier/VerifyEngine.cs b/src/Verify/Verifier/VerifyEngine.cs index d6e219d12..0dea0e3a2 100644 --- a/src/Verify/Verifier/VerifyEngine.cs +++ b/src/Verify/Verifier/VerifyEngine.cs @@ -252,7 +252,14 @@ async Task RunDiffAutoCheck(FilePair file, bool autoVerify) if (diffEnabled) { - await DiffRunner.LaunchAsync(file.ReceivedPath, file.VerifiedPath, VerifierSettings.Encoding); + if (file.IsText) + { + await DiffRunner.LaunchForTextAsync(file.ReceivedPath, file.VerifiedPath, VerifierSettings.Encoding); + } + else + { + await DiffRunner.LaunchAsync(file.ReceivedPath, file.VerifiedPath, VerifierSettings.Encoding); + } } return autoVerify;