Skip to content

Commit

Permalink
Merge pull request #14 from bmazzarol/no-replace
Browse files Browse the repository at this point in the history
feat: add ability to save non-deterministic examples [ci]
  • Loading branch information
bmazzarol authored Jun 7, 2023
2 parents 4288223 + dac2502 commit 55faae2
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 5 deletions.
11 changes: 11 additions & 0 deletions Docfx.ResultSnippets.Docs/articles/save-result.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,14 @@ the following name format relative to the code that produced the result,
```shell
./__examples__/{ClassName}.{MemberName}(.{PartName}).md
```

## Non Deterministic Examples

If the generated result is non-deterministic
@Docfx.ResultSnippets.ResultExtensions.SaveResults* can be called with the
`replaceExisting` flag set to `false`.

This will ensure the file is only created the first time and its only replaced
once the file is removed.

[!code-csharp[](../../Docfx.ResultSnippets.Tests/ResultExtensionsTests.cs#SaveResultsExample2)]
17 changes: 16 additions & 1 deletion Docfx.ResultSnippets.Tests/ResultExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using FluentAssertions;
Expand Down Expand Up @@ -140,4 +141,18 @@ public static void Case8()
var value = "this is a test".AsFencedResult();
value.Should().Be("```\nthis is a test\n```");
}

[Fact(DisplayName = "Save results skip files if they already exist")]
public static void Case9()
{
#region SaveResultsExample2

// some non-deterministic value
var result = Guid.NewGuid();
// now it can be saved as an example the first time only
result.SaveResults(replaceExisting: false);
#endregion

result.Should().NotBeEmpty();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2e8d500a-31e3-4c50-b919-46099b99eda8
13 changes: 9 additions & 4 deletions Docfx.ResultSnippets/ResultExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public static class ResultExtensions
/// <param name="folder">folder to save it under, default is `__examples__`</param>
/// <param name="extension">extension to use for the example file, default is `md`</param>
/// <param name="partName">optional part name</param>
/// <param name="replaceExisting">flag to indicate that existing example files are to be replaced, default is `true`</param>
/// <param name="sourceFilePath">source file path of the calling code</param>
/// <param name="memberName">member name of the calling code</param>
/// <typeparam name="T">some T to `ToString` and save</typeparam>
Expand All @@ -34,6 +35,7 @@ public static void SaveResults<T>(
string folder = "__examples__",
string extension = "md",
string partName = "",
bool replaceExisting = true,
[CallerFilePath] string sourceFilePath = "",
[CallerMemberName] string memberName = ""
)
Expand All @@ -44,11 +46,14 @@ public static void SaveResults<T>(
var root = Path.GetDirectoryName(sourceFilePath);
var name = Path.GetFileNameWithoutExtension(sourceFilePath);
var path = $"{root}/{folder}";
var examplePath =
$"{path}/{name}.{memberName}{(string.IsNullOrWhiteSpace(partName) ? string.Empty : $".{partName}")}.{extension}";

if (!replaceExisting && File.Exists(examplePath))
return;

Directory.CreateDirectory(path);
File.WriteAllText(
$"{path}/{name}.{memberName}{(string.IsNullOrWhiteSpace(partName) ? string.Empty : $".{partName}")}.{extension}",
data
);
File.WriteAllText(examplePath, data);
}

/// <summary>
Expand Down

0 comments on commit 55faae2

Please sign in to comment.