Skip to content

Commit

Permalink
Addition changes to enable Verify.Cli app (#888)
Browse files Browse the repository at this point in the history
- Add simple constructor to InnerVerifier for just comparing files
- Ensure replacements field is valid even if we haven't called UseAssembly
  • Loading branch information
flcdrg committed May 27, 2023
1 parent 2f1e8bf commit 45a060f
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 3 deletions.
9 changes: 8 additions & 1 deletion docs/mdsource/verify-file.source.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,11 @@ snippet: VerifyFile

An optional `info` parameter can be supplied to add more context to the test. The instance passed will be json serialized.

snippet: VerifyFileWithInfo
snippet: VerifyFileWithInfo


## Verify a file without using a unit test

Use the functionality of VerifyTests outside of a unit test.

snippet: VerifyFileWithoutUnitTest
32 changes: 32 additions & 0 deletions docs/verify-file.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,35 @@ public Task VerifyFileWithInfo() =>
```
<sup><a href='/src/Verify.Tests/StreamTests.cs#L184-L192' title='Snippet source file'>snippet source</a> | <a href='#snippet-verifyfilewithinfo' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->


## Verify a file without using a unit test

Use the functionality of VerifyTests outside of a unit test.

<!-- snippet: VerifyFileWithoutUnitTest -->
<a id='snippet-verifyfilewithoutunittest'></a>
```cs
public async Task VerifyExternalFile()
{
var solutionDirectory = AttributeReader.GetSolutionDirectory();
var settings = new VerifySettings();
settings.DisableRequireUniquePrefix();

var sourceFile = Path.Combine(solutionDirectory, "Verify.Tests", "sample.txt");

Func<InnerVerifier, Task<VerifyResult>> verify = _ => _.VerifyFile(sourceFile, null);
await new SettingsTask(
settings,
async verifySettings =>
{
using var verifier = new InnerVerifier(
sourceFile,
verifySettings
);
return await verify(verifier);
});
}
```
<sup><a href='/src/Verify.Tests/InnerVerifyTests.cs#L16-L39' title='Snippet source file'>snippet source</a> | <a href='#snippet-verifyfilewithoutunittest' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->
40 changes: 40 additions & 0 deletions src/Verify.Tests/InnerVerifyTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
public class InnerVerifyTests
{
[Fact]
public void InnerVerifier_FileConstructor()
{
const string sourceFile = "path/to/source/file.cs";
var settings = new VerifySettings();

var verifier = new InnerVerifier(sourceFile, settings);

Assert.NotNull(verifier);
}

[Fact]

#region VerifyFileWithoutUnitTest

public async Task VerifyExternalFile()
{
var solutionDirectory = AttributeReader.GetSolutionDirectory();
var settings = new VerifySettings();
settings.DisableRequireUniquePrefix();

var sourceFile = Path.Combine(solutionDirectory, "Verify.Tests", "sample.txt");

Func<InnerVerifier, Task<VerifyResult>> verify = _ => _.VerifyFile(sourceFile, null);
await new SettingsTask(
settings,
async verifySettings =>
{
using var verifier = new InnerVerifier(
sourceFile,
verifySettings
);
return await verify(verifier);
});
}

#endregion
}
1 change: 1 addition & 0 deletions src/Verify.Tests/sample.verified.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Foo
4 changes: 2 additions & 2 deletions src/Verify/Serialization/Scrubbers/ApplyScrubbers.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// ReSharper disable RedundantSuppressNullableWarningExpression
// ReSharper disable RedundantSuppressNullableWarningExpression

static class ApplyScrubbers
{
static char dirSeparator = Path.DirectorySeparatorChar;
static char altDirSeparator = Path.AltDirectorySeparatorChar;
static List<KeyValuePair<string, string>> replacements = null!;
static List<KeyValuePair<string, string>> replacements = new();

static string ReplaceAltDirChar(this string directory) =>
directory.Replace(dirSeparator, altDirSeparator);
Expand Down
37 changes: 37 additions & 0 deletions src/Verify/Verifier/InnerVerifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,43 @@ public static void ThrowIfVerifyHasBeenRun()
}
}

/// <summary>
/// Initialize a new instance of the <see cref="InnerVerifier"/> class for verifying the entire file (not just a specific type)
/// </summary>
/// <remarks>This constructor is used by 3rd party clients</remarks>
// ReSharper disable once UnusedMember.Global
public InnerVerifier(string sourceFile, VerifySettings settings)
{
Guard.AgainstEmpty(sourceFile);

this.settings = settings;
directory = ResolveDirectory(sourceFile, settings, new());

counter = Counter.Start(
#if NET6_0_OR_GREATER
settings.namedDates,
settings.namedTimes,
#endif
settings.namedDateTimes,
settings.namedGuids,
settings.namedDateTimeOffsets
);

IoHelpers.CreateDirectory(directory);

ValidatePrefix(settings, directory);

verifiedFiles = new List<string> { Path.Combine(directory, $"{Path.GetFileNameWithoutExtension(sourceFile)}.verified.{FileExtensions.GetExtension(sourceFile)}") };

getFileNames = target => new(
target.Extension,
sourceFile,
Path.Combine(directory, $"{Path.GetFileNameWithoutExtension(sourceFile)}.verified.{target.Extension}")
);

getIndexedFileNames = (_, _) => throw new NotImplementedException();
}

void InitForDirectoryConvention(Namer namer, string typeAndMethod, string parameters)
{
var verifiedPrefix = PrefixForDirectoryConvention(namer, typeAndMethod, parameters);
Expand Down

0 comments on commit 45a060f

Please sign in to comment.