Skip to content

Commit

Permalink
use LaunchForTextAsync (#1391)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonCropp authored Jan 14, 2025
1 parent 6502254 commit 3c14430
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 31 deletions.
14 changes: 14 additions & 0 deletions src/Verify.Tests/VerifyDirectoryTests.cs
Original file line number Diff line number Diff line change
@@ -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() =>
Expand Down
9 changes: 9 additions & 0 deletions src/Verify/FilePair.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down
37 changes: 18 additions & 19 deletions src/Verify/Target.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,38 +58,37 @@ 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;
streamData = 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);
Expand Down
33 changes: 22 additions & 11 deletions src/Verify/Verifier/InnerVerifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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) =>
{
Expand All @@ -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);
};
}

Expand Down Expand Up @@ -185,15 +188,17 @@ 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) =>
{
var fileName = $"{target.NameOrTarget}#{index}.{target.Extension}";
return new(
target.Extension,
Path.Combine(receivedDirectory, fileName),
Path.Combine(verifiedDirectory, fileName));
Path.Combine(verifiedDirectory, fileName),
target.IsString);
};

IoHelpers.DeleteDirectory(receivedDirectory);
Expand All @@ -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.*");
}
Expand Down Expand Up @@ -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) =>
{
Expand All @@ -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);
Expand Down
9 changes: 8 additions & 1 deletion src/Verify/Verifier/VerifyEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,14 @@ async Task<bool> 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;
Expand Down

0 comments on commit 3c14430

Please sign in to comment.