Skip to content

Commit

Permalink
Add AppendFile (#748)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonCropp authored Dec 19, 2022
1 parent add102e commit 26b467a
Show file tree
Hide file tree
Showing 42 changed files with 217 additions and 6 deletions.
38 changes: 38 additions & 0 deletions docs/append-file.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<!--
GENERATED FILE - DO NOT EDIT
This file was generated by [MarkdownSnippets](https://github.com/SimonCropp/MarkdownSnippets).
Source File: /docs/mdsource/append-file.source.md
To change this file edit the source file and then run MarkdownSnippets.
-->

# AppendFile

Allows extra files to be verified in addition to the primary target.


## Binary

<!-- snippet: BinaryFluent -->
<a id='snippet-binaryfluent'></a>
```cs
[Fact]
public Task BinaryFluent() =>
Verify("Foo", settings)
.AppendFile("sample.png");
```
<sup><a href='/src/Verify.Tests/Converters/InstanceFileAppenderTests.cs#L21-L28' title='Snippet source file'>snippet source</a> | <a href='#snippet-binaryfluent' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->


## Text

<!-- snippet: TextFluent -->
<a id='snippet-textfluent'></a>
```cs
[Fact]
public Task TextFluent() =>
Verify("Foo")
.AppendFile("extra content");
```
<sup><a href='/src/Verify.Tests/Converters/InstanceFileAppenderTests.cs#L30-L37' title='Snippet source file'>snippet source</a> | <a href='#snippet-textfluent' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->
13 changes: 13 additions & 0 deletions docs/mdsource/append-file.source.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# AppendFile

Allows extra files to be verified in addition to the primary target.


## Binary

snippet: BinaryFluent


## Text

snippet: TextFluent
1 change: 1 addition & 0 deletions docs/mdsource/doc-index.include.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* [VerifyXml](/docs/verify-xml.md)
* [Serializer Settings](/docs/serializer-settings.md)
* [File naming](/docs/naming.md)
* [AppendFile](/docs/append-file.md)
* [Parameterised tests](/docs/parameterised.md)
* [Named Tuples](/docs/named-tuples.md)
* [Scrubbers](/docs/scrubbers.md)
Expand Down
2 changes: 1 addition & 1 deletion docs/naming.md
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,7 @@ public static string NameWithParent(this Type type)
return type.Name;
}
```
<sup><a href='/src/Verify/Extensions.cs#L49-L61' title='Snippet source file'>snippet source</a> | <a href='#snippet-defaultderivepathinfo-4' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Verify/Extensions.cs#L52-L64' title='Snippet source file'>snippet source</a> | <a href='#snippet-defaultderivepathinfo-4' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->


Expand Down
1 change: 1 addition & 0 deletions docs/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ To change this file edit the source file and then run MarkdownSnippets.
* [VerifyXml](/docs/verify-xml.md)
* [Serializer Settings](/docs/serializer-settings.md)
* [File naming](/docs/naming.md)
* [AppendFile](/docs/append-file.md)
* [Parameterised tests](/docs/parameterised.md)
* [Named Tuples](/docs/named-tuples.md)
* [Scrubbers](/docs/scrubbers.md)
Expand Down
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,7 @@ Nick Chapsas (1 August 2022)](https://www.youtube.com/watch?v=Q1_YkcPwpqY)
* [VerifyXml](/docs/verify-xml.md)
* [Serializer Settings](/docs/serializer-settings.md)
* [File naming](/docs/naming.md)
* [AppendFile](/docs/append-file.md)
* [Parameterised tests](/docs/parameterised.md)
* [Named Tuples](/docs/named-tuples.md)
* [Scrubbers](/docs/scrubbers.md)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
appendedFile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sample.png
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
appendedFile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
appendedFile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Foo
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
extra content
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Foo
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
appendedFile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Foo
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
appendedFile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
extra content
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Foo
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
line1
line3
58 changes: 58 additions & 0 deletions src/Verify.Tests/Converters/InstanceFileAppenderTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
[UsesVerify]
public class InstanceFileAppenderTests
{
VerifySettings settings;

public InstanceFileAppenderTests()
{
settings = new();
settings.AppendFile("appendedFile");
}

[Fact]
public Task Text() =>
Verify("Foo", settings);

[Fact]
public Task WithName() =>
Verify("Foo", settings)
.AppendFile("extra content", name: "theName");

#region BinaryFluent

[Fact]
public Task BinaryFluent() =>
Verify("Foo", settings)
.AppendFile("sample.png");

#endregion

#region TextFluent

[Fact]
public Task TextFluent() =>
Verify("Foo")
.AppendFile("extra content");

#endregion

[Fact]
public Task WithScrubbing() =>
Verify("Foo")
.AppendFile("""
line1
line2
line3
""")
.ScrubLinesContaining("line2");

[Fact]
public Task TextBytesFluent() =>
Verify("Foo")
.AppendFile(Encoding.UTF8.GetBytes("appendedFile"));

[Fact]
public Task TextStreamFluent() =>
Verify("Foo")
.AppendFile(new MemoryStream(Encoding.UTF8.GetBytes("appendedFile")));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Foo
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Foo
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Foo
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[UsesVerify]
public class FileAppenderTests :
public class StaticFileAppenderTests :
IDisposable
{
static AsyncLocal<bool> isInThisTest = new();
Expand All @@ -17,7 +17,7 @@ public static void Initialize() =>
return new("txt", "data");
});

public FileAppenderTests() =>
public StaticFileAppenderTests() =>
isInThisTest.Value = true;

public void Dispose() =>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[UsesVerify]
public class FileAppenderWithScrubberTests :
public class StaticFileAppenderWithScrubberTests :
IDisposable
{
static AsyncLocal<bool> isInThisTest = new();
Expand Down Expand Up @@ -29,7 +29,7 @@ public static void Initialize()
});
}

public FileAppenderWithScrubberTests() =>
public StaticFileAppenderWithScrubberTests() =>
isInThisTest.Value = true;

public void Dispose() =>
Expand Down
3 changes: 3 additions & 0 deletions src/Verify/Extensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
static class Extensions
{
public static string Extension(this FileStream file) =>
FileExtensions.GetExtension(file.Name);

public static async Task<List<T>> ToList<T>(this IAsyncEnumerable<T> target)
{
var list = new List<T>();
Expand Down
78 changes: 78 additions & 0 deletions src/Verify/Splitters/Settings_FileAppender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,89 @@ internal static IEnumerable<Target> GetFileAppenders(VerifySettings settings)
yield return target.Value;
}
}

foreach (var target in settings.appendedFiles)
{
yield return target;
}
}

public static void RegisterFileAppender(FileAppender appender)
{
InnerVerifier.ThrowIfVerifyHasBeenRun();
fileAppenders.Add(appender);
}
}

public partial class VerifySettings
{
internal List<Target> appendedFiles = new();

public void AppendFile(string content, string extension = "txt", string? name = null) =>
appendedFiles.Add(new(extension, content, name));

public void AppendFile(StringBuilder content, string extension = "txt", string? name = null) =>
appendedFiles.Add(new(extension, content, name));

public void AppendFile(byte[] content, string extension = "txt", string? name = null)
{
if (FileExtensions.IsText(extension))
{
appendedFiles.Add(new(extension, Encoding.UTF8.GetString(content), name));
}
else
{
appendedFiles.Add(new(extension, new MemoryStream(content), name));
}
}

public void AppendFile(FileStream stream, string? name = null) =>
AppendFile(stream, stream.Extension(), name??Path.GetFileNameWithoutExtension(stream.Name));

public void AppendFile(Stream stream, string extension = "txt", string? name = null)
{
stream.MoveToStart();
if (FileExtensions.IsText(extension))
{
using var reader = new StreamReader(stream, Encoding.UTF8);
appendedFiles.Add(new(extension, reader.ReadToEnd(), name));
}
else
{
appendedFiles.Add(new(extension, stream, name));
}
}
}

public partial class SettingsTask
{
public SettingsTask AppendFile(StringBuilder content, string extension = "txt", string? name = null)
{
CurrentSettings.AppendFile(content, extension, name);
return this;
}

public SettingsTask AppendFile(string content, string extension = "txt", string? name = null)
{
CurrentSettings.AppendFile(content, extension, name);
return this;
}

public SettingsTask AppendFile(byte[] content, string extension = "txt", string? name = null)
{
CurrentSettings.AppendFile(content, extension, name);
return this;
}

public SettingsTask AppendFile(FileStream stream, string? name = null)
{
CurrentSettings.AppendFile(stream, name);
return this;
}

public SettingsTask AppendFile(Stream stream, string extension = "txt", string? name = null)
{
CurrentSettings.AppendFile(stream, extension, name);
return this;
}
}
2 changes: 1 addition & 1 deletion src/Verify/Verifier/InnerVerifier_Stream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public Task<VerifyResult> VerifyStream(FileStream? stream, object? info)
return Verify(info);
}

return VerifyStream(stream, FileExtensions.GetExtension(stream.Name), info);
return VerifyStream(stream, stream.Extension(), info);
}

public Task<VerifyResult> VerifyStream(byte[]? bytes, object? info) =>
Expand Down
1 change: 1 addition & 0 deletions src/Verify/VerifySettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public VerifySettings(VerifySettings? settings)
#endif
methodName = settings.methodName;
typeName = settings.typeName;
appendedFiles = settings.appendedFiles;
useUniqueDirectory = settings.useUniqueDirectory;
Directory = settings.Directory;
autoVerify = settings.autoVerify;
Expand Down

0 comments on commit 26b467a

Please sign in to comment.