Skip to content

Commit

Permalink
Add support for custom clipboard commands
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonCropp authored Jun 2, 2020
1 parent cf90a0a commit 9e263f3
Show file tree
Hide file tree
Showing 8 changed files with 194 additions and 85 deletions.
97 changes: 97 additions & 0 deletions docs/clipboard.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<!--
GENERATED FILE - DO NOT EDIT
This file was generated by [MarkdownSnippets](https://github.com/SimonCropp/MarkdownSnippets).
Source File: /docs/mdsource/clipboard.source.md
To change this file edit the source file and then run MarkdownSnippets.
-->

# Clipboard

Verify makes use of the clipboard.

This is done via the [TextCopy project](https://github.com/CopyText/TextCopy).


## Accept received

When a verification fails, a command to accept the received version is added to the clipboard:

On Windows:

> cmd /c move /Y "ReceivedFile" "VerifiedFile"
On Linux or OS:

> mv -f "ReceivedFile" "VerifiedFile"

## Cleanup dangling converter files

When a the number of files outputted from a [converter](converter.mc) reduces, a command to delete the extra files is added to the clipboard:

On Windows:

> cmd /c del "VerifiedFile"
On Linux or OS:

> rm -f "VerifiedFile"

## Custom Command

A custom command can be used by adding environment variables.


### Accept

Add a variable named `Verify.MoveCommand` where `{0}` and `{1}` will be replaced with the received and verified files respectively.


### Cleanup

Add a variable named `Verify.DeleteCommand` where `{0}` will be replaced with the file to be cleaned up.


## Disable Clipboard

The clipboard behavior can be disable using the following:


### Per Test

<!-- snippet: DisableClipboard -->
<a id='snippet-disableclipboard'/></a>
```cs
var settings = new VerifySettings();
settings.DisableClipboard();
```
<sup><a href='/src/Verify.Tests/Snippets/Snippets.cs#L38-L43' title='File snippet `disableclipboard` was extracted from'>snippet source</a> | <a href='#snippet-disableclipboard' title='Navigate to start of snippet `disableclipboard`'>anchor</a></sup>
<!-- endsnippet -->


### For all tests

<!-- snippet: DisableClipboardGlobal -->
<a id='snippet-disableclipboardglobal'/></a>
```cs
SharedVerifySettings.DisableClipboard();
```
<sup><a href='/src/Verify.Tests/Snippets/Snippets.cs#L58-L62' title='File snippet `disableclipboardglobal` was extracted from'>snippet source</a> | <a href='#snippet-disableclipboardglobal' title='Navigate to start of snippet `disableclipboardglobal`'>anchor</a></sup>
<!-- endsnippet -->

If clipboard is disabled for all tests, it can be re-enabled at the test level:

<!-- snippet: EnableClipboard -->
<a id='snippet-enableclipboard'/></a>
```cs
var settings = new VerifySettings();
settings.EnableClipboard();
```
<sup><a href='/src/Verify.Tests/Snippets/Snippets.cs#L48-L53' title='File snippet `enableclipboard` was extracted from'>snippet source</a> | <a href='#snippet-enableclipboard' title='Navigate to start of snippet `enableclipboard`'>anchor</a></sup>
<!-- endsnippet -->


### For a machine

Set a `Verify.DisableClipboard` environment variable to `true`. This overrides the above settings.
70 changes: 70 additions & 0 deletions docs/mdsource/clipboard.source.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# Clipboard

Verify makes use of the clipboard.

This is done via the [TextCopy project](https://github.com/CopyText/TextCopy).


## Accept received

When a verification fails, a command to accept the received version is added to the clipboard:

On Windows:

> cmd /c move /Y "ReceivedFile" "VerifiedFile"
On Linux or OS:

> mv -f "ReceivedFile" "VerifiedFile"

## Cleanup dangling converter files

When a the number of files outputted from a [converter](converter.mc) reduces, a command to delete the extra files is added to the clipboard:

On Windows:

> cmd /c del "VerifiedFile"
On Linux or OS:

> rm -f "VerifiedFile"

## Custom Command

A custom command can be used by adding environment variables.


### Accept

Add a variable named `Verify.MoveCommand` where `{0}` and `{1}` will be replaced with the received and verified files respectively.


### Cleanup

Add a variable named `Verify.DeleteCommand` where `{0}` will be replaced with the file to be cleaned up.


## Disable Clipboard

The clipboard behavior can be disable using the following:


### Per Test

snippet: DisableClipboard


### For all tests

snippet: DisableClipboardGlobal

If clipboard is disabled for all tests, it can be re-enabled at the test level:

snippet: EnableClipboard


### For a machine

Set a `Verify.DisableClipboard` environment variable to `true`. This overrides the above settings.
1 change: 1 addition & 0 deletions docs/mdsource/doc-index.include.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
* [Clipboard](/docs/clipboard.md)
* [Serializer Settings](/docs/serializer-settings.md)
* [File naming](/docs/naming.md)
* [Parameterised tests](/docs/parameterised.md)
Expand Down
3 changes: 2 additions & 1 deletion docs/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ To change this file edit the source file and then run MarkdownSnippets.

# Documentation

* [Serializer Settings](/docs/serializer-settings.md) <!-- include: doc-index. path: /docs/mdsource/doc-index.include.md -->
* [Clipboard](/docs/clipboard.md) <!-- include: doc-index. path: /docs/mdsource/doc-index.include.md -->
* [Serializer Settings](/docs/serializer-settings.md)
* [File naming](/docs/naming.md)
* [Parameterised tests](/docs/parameterised.md)
* [Named Tuples](/docs/named-tuples.md)
Expand Down
56 changes: 8 additions & 48 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ Support is available via a [Tidelift Subscription](https://tidelift.com/subscrip
* [bunit](#bunit)
* [Initial Verification](#initial-verification)
* [Subsequent Verification](#subsequent-verification)
* [Disable Clipboard](#disable-clipboard)
* [AutoVerify](#autoverify)
* [OnHandlers](#onhandlers)
* [Received and Verified](#received-and-verified)
Expand All @@ -38,7 +37,8 @@ Support is available via a [Tidelift Subscription](https://tidelift.com/subscrip
* [Extensions](#extensions)
* [Alternatives](#alternatives)
* [Security contact information](#security-contact-information)<!-- endtoc -->
* [Serializer Settings](/docs/serializer-settings.md) <!-- include: doc-index. path: /docs/mdsource/doc-index.include.md -->
* [Clipboard](/docs/clipboard.md) <!-- include: doc-index. path: /docs/mdsource/doc-index.include.md -->
* [Serializer Settings](/docs/serializer-settings.md)
* [File naming](/docs/naming.md)
* [Parameterised tests](/docs/parameterised.md)
* [Named Tuples](/docs/named-tuples.md)
Expand Down Expand Up @@ -355,7 +355,9 @@ Verification command has been copied to the clipboard.

The clipboard will contain the following:

> move /Y "C:\Code\Sample\Sample.Test.received.txt" "C:\Code\Sample\Sample.Test.verified.txt"
> cmd /c move /Y "C:\Code\Sample\Sample.Test.received.txt" "C:\Code\Sample\Sample.Test.verified.txt"
See also: [Clipboard](/docs/clipboard.md)

If a [Diff Tool](https://github.com/VerifyTests/DiffEngine) is detected it will display the diff:

Expand Down Expand Up @@ -444,7 +446,9 @@ Actual: ···\n GivenNames: 'John James',\n FamilyName: 'Smith',\n Spouse:

The clipboard will again contain the following:

> move /Y "C:\Code\Sample\Sample.Test.received.txt" "C:\Code\Sample\Sample.Test.verified.txt"
> cmd /c move /Y "C:\Code\Sample\Sample.Test.received.txt" "C:\Code\Sample\Sample.Test.verified.txt"
See also: [Clipboard](/docs/clipboard.md)

And the [Diff Tool](https://github.com/VerifyTests/DiffEngine) is will display the diff:

Expand All @@ -453,50 +457,6 @@ And the [Diff Tool](https://github.com/VerifyTests/DiffEngine) is will display t
The same approach can be used to verify the results and the change to `Sample.Test.verified.txt` is committed to source control along with the change to `ClassBeingTested`.


### Disable Clipboard

The clipboard behavior can be disable using the following:


#### Per Test

<!-- snippet: DisableClipboard -->
<a id='snippet-disableclipboard'/></a>
```cs
var settings = new VerifySettings();
settings.DisableClipboard();
```
<sup><a href='/src/Verify.Tests/Snippets/Snippets.cs#L38-L43' title='File snippet `disableclipboard` was extracted from'>snippet source</a> | <a href='#snippet-disableclipboard' title='Navigate to start of snippet `disableclipboard`'>anchor</a></sup>
<!-- endsnippet -->


#### For all tests

<!-- snippet: DisableClipboardGlobal -->
<a id='snippet-disableclipboardglobal'/></a>
```cs
SharedVerifySettings.DisableClipboard();
```
<sup><a href='/src/Verify.Tests/Snippets/Snippets.cs#L58-L62' title='File snippet `disableclipboardglobal` was extracted from'>snippet source</a> | <a href='#snippet-disableclipboardglobal' title='Navigate to start of snippet `disableclipboardglobal`'>anchor</a></sup>
<!-- endsnippet -->

If clipboard is disabled for all tests, it can be re-enabled at the test level:

<!-- snippet: EnableClipboard -->
<a id='snippet-enableclipboard'/></a>
```cs
var settings = new VerifySettings();
settings.EnableClipboard();
```
<sup><a href='/src/Verify.Tests/Snippets/Snippets.cs#L48-L53' title='File snippet `enableclipboard` was extracted from'>snippet source</a> | <a href='#snippet-enableclipboard' title='Navigate to start of snippet `enableclipboard`'>anchor</a></sup>
<!-- endsnippet -->


#### For a machine

Set a `Verify.DisableClipboard` environment variable to `true`. This overrides the above settings.


### AutoVerify

In some scenarios it makes sense to auto-accept any changes as part of a given test run. For example:
Expand Down
32 changes: 6 additions & 26 deletions readme.source.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,9 @@ Verification command has been copied to the clipboard.

The clipboard will contain the following:

> move /Y "C:\Code\Sample\Sample.Test.received.txt" "C:\Code\Sample\Sample.Test.verified.txt"
> cmd /c move /Y "C:\Code\Sample\Sample.Test.received.txt" "C:\Code\Sample\Sample.Test.verified.txt"
See also: [Clipboard](/docs/clipboard.md)

If a [Diff Tool](https://github.com/VerifyTests/DiffEngine) is detected it will display the diff:

Expand Down Expand Up @@ -169,7 +171,9 @@ Actual: ···\n GivenNames: 'John James',\n FamilyName: 'Smith',\n Spouse:

The clipboard will again contain the following:

> move /Y "C:\Code\Sample\Sample.Test.received.txt" "C:\Code\Sample\Sample.Test.verified.txt"
> cmd /c move /Y "C:\Code\Sample\Sample.Test.received.txt" "C:\Code\Sample\Sample.Test.verified.txt"
See also: [Clipboard](/docs/clipboard.md)

And the [Diff Tool](https://github.com/VerifyTests/DiffEngine) is will display the diff:

Expand All @@ -178,30 +182,6 @@ And the [Diff Tool](https://github.com/VerifyTests/DiffEngine) is will display t
The same approach can be used to verify the results and the change to `Sample.Test.verified.txt` is committed to source control along with the change to `ClassBeingTested`.


### Disable Clipboard

The clipboard behavior can be disable using the following:


#### Per Test

snippet: DisableClipboard


#### For all tests

snippet: DisableClipboardGlobal

If clipboard is disabled for all tests, it can be re-enabled at the test level:

snippet: EnableClipboard


#### For a machine

Set a `Verify.DisableClipboard` environment variable to `true`. This overrides the above settings.


### AutoVerify

In some scenarios it makes sense to auto-accept any changes as part of a given test run. For example:
Expand Down
2 changes: 1 addition & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<Project>
<PropertyGroup>
<NoWarn>CS1591;CS0649</NoWarn>
<Version>4.1.0</Version>
<Version>4.2.0</Version>
<AssemblyVersion>1.0.0</AssemblyVersion>
<PackageTags>Json, Testing, Verify, Snapshot, Approvals</PackageTags>
<Description>Enables verification of complex models and documents.</Description>
Expand Down
18 changes: 9 additions & 9 deletions src/Verify/Clipboard/ClipboardCapture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,24 @@ static ClipboardCapture()
{
moveCommand = "cmd /c move /Y \"{0}\" \"{1}\"";
deleteCommand = "cmd /c del \"{0}\"";
return;
}

if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
else
{
moveCommand = "mv -f \"{0}\" \"{1}\"";
deleteCommand = "rm -f \"{0}\"";
return;
}

if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
var envMoveCommand = Environment.GetEnvironmentVariable("Verify.MoveCommand");
if (envMoveCommand != null)
{
moveCommand = "mv -f \"{0}\" \"{1}\"";
deleteCommand = "rm -f \"{0}\"";
return;
moveCommand = envMoveCommand;
}

throw new Exception($"OS not supported: {RuntimeInformation.OSDescription}");
var envDeleteCommand = Environment.GetEnvironmentVariable("Verify.DeleteCommand");
if (envDeleteCommand != null)
{
deleteCommand = envDeleteCommand;
}
}

public static void Clear()
Expand Down

0 comments on commit 9e263f3

Please sign in to comment.