Skip to content

Commit b58d72c

Browse files
glen-84michaelstaib
authored andcommitted
Added strict mode to Cookie Crumble (#8126)
1 parent d2b9d1f commit b58d72c

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed

src/CookieCrumble/src/CookieCrumble/Snapshot.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ public async ValueTask MatchAsync(CancellationToken cancellationToken = default)
242242

243243
if (!File.Exists(snapshotFile))
244244
{
245+
CheckStrictMode();
245246
EnsureDirectoryExists(snapshotFile);
246247
await using var stream = File.Create(snapshotFile);
247248
await stream.WriteAsync(writer.WrittenMemory, cancellationToken);
@@ -274,6 +275,7 @@ public void Match()
274275

275276
if (!File.Exists(snapshotFile))
276277
{
278+
CheckStrictMode();
277279
EnsureDirectoryExists(snapshotFile);
278280
using var stream = File.Create(snapshotFile);
279281
stream.Write(writer.WrittenSpan);
@@ -309,6 +311,7 @@ public async ValueTask MatchMarkdownAsync(CancellationToken cancellationToken =
309311

310312
if (!File.Exists(snapshotFile))
311313
{
314+
CheckStrictMode();
312315
EnsureDirectoryExists(snapshotFile);
313316
await using var stream = File.Create(snapshotFile);
314317
await stream.WriteAsync(writer.WrittenMemory, cancellationToken);
@@ -346,6 +349,7 @@ public void MatchMarkdown()
346349

347350
if (!File.Exists(snapshotFile))
348351
{
352+
CheckStrictMode();
349353
EnsureDirectoryExists(snapshotFile);
350354
using var stream = File.Create(snapshotFile);
351355
stream.Write(writer.WrittenSpan);
@@ -720,6 +724,20 @@ from methodInfo in classDeclaringType.GetMethods()
720724
return actualMethodInfo;
721725
}
722726

727+
private static void CheckStrictMode()
728+
{
729+
var value = Environment.GetEnvironmentVariable("COOKIE_CRUMBLE_STRICT_MODE");
730+
731+
if (string.Equals(value, "on", StringComparison.Ordinal)
732+
|| (bool.TryParse(value, out var b) && b))
733+
{
734+
_testFramework.ThrowTestException(
735+
"Strict mode is enabled and no snapshot has been found " +
736+
"for the current test. Create a new snapshot locally and " +
737+
"rerun your tests.");
738+
}
739+
}
740+
723741
private readonly struct SnapshotSegment(string? name, object? value, ISnapshotValueFormatter formatter)
724742
: ISnapshotSegment
725743
{

src/CookieCrumble/test/CookieCrumble.Tests/SnapshotTests.cs

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
using System.Buffers;
2+
using System.Runtime.CompilerServices;
23
using System.Text;
34
using CookieCrumble.Formatters;
45
using CookieCrumble.Xunit;
6+
using Xunit.Sdk;
57

68
namespace CookieCrumble;
79

810
public class SnapshotTests
911
{
12+
private const string _strictModeExceptionMessage =
13+
"Strict mode is enabled and no snapshot has been found " +
14+
"for the current test. Create a new snapshot locally and " +
15+
"rerun your tests.";
16+
1017
static SnapshotTests()
1118
{
1219
Snapshot.RegisterTestFramework(new XunitFramework());
@@ -109,6 +116,83 @@ public void SnapshotBuilder_Segment_Custom_Global_Serializer()
109116
snapshot.Match();
110117
}
111118

119+
[Theory]
120+
[InlineData("on")]
121+
[InlineData("true")]
122+
public async Task Match_StrictMode_On(string strictMode)
123+
{
124+
Environment.SetEnvironmentVariable("COOKIE_CRUMBLE_STRICT_MODE", strictMode);
125+
126+
var snapshot = new Snapshot();
127+
snapshot.Add(new MyClass { Foo = "123", });
128+
129+
async Task Act1() => await snapshot.MatchAsync();
130+
void Act2() => snapshot.Match();
131+
async Task Act3() => await snapshot.MatchMarkdownAsync();
132+
void Act4() => snapshot.MatchMarkdown();
133+
134+
try
135+
{
136+
Assert.Equal(
137+
_strictModeExceptionMessage,
138+
(await Assert.ThrowsAsync<XunitException>(Act1)).Message);
139+
140+
Assert.Equal(_strictModeExceptionMessage, Assert.Throws<XunitException>(Act2).Message);
141+
142+
Assert.Equal(
143+
_strictModeExceptionMessage,
144+
(await Assert.ThrowsAsync<XunitException>(Act3)).Message);
145+
146+
Assert.Equal(_strictModeExceptionMessage, Assert.Throws<XunitException>(Act4).Message);
147+
}
148+
finally
149+
{
150+
Environment.SetEnvironmentVariable("COOKIE_CRUMBLE_STRICT_MODE", null);
151+
}
152+
}
153+
154+
[Theory]
155+
[InlineData(1, "off")]
156+
[InlineData(2, "false")]
157+
[InlineData(3, null)]
158+
public async Task Match_StrictMode_Off(int number, string? strictMode)
159+
{
160+
Environment.SetEnvironmentVariable("COOKIE_CRUMBLE_STRICT_MODE", strictMode);
161+
162+
var snapshot = new Snapshot();
163+
snapshot.Add(new MyClass { Foo = "123", });
164+
165+
async Task Act1() => await snapshot.SetPostFix($"MA_{number}").MatchAsync();
166+
void Act2() => snapshot.SetPostFix($"M_{number}").Match();
167+
async Task Act3() => await snapshot.SetPostFix($"MMA_{number}").MatchMarkdownAsync();
168+
void Act4() => snapshot.SetPostFix($"MM_{number}").MatchMarkdown();
169+
170+
try
171+
{
172+
var result1 = await Record.ExceptionAsync(Act1);
173+
var result2 = Record.Exception(Act2);
174+
var result3 = await Record.ExceptionAsync(Act3);
175+
var result4 = Record.Exception(Act4);
176+
177+
static string GetCallerFilePath([CallerFilePath] string filePath = "") => filePath;
178+
var directory = Path.GetDirectoryName(GetCallerFilePath()) + "/__snapshots__";
179+
180+
File.Delete($"{directory}/SnapshotTests.Match_StrictMode_Off_MA_{number}.snap");
181+
File.Delete($"{directory}/SnapshotTests.Match_StrictMode_Off_M_{number}.snap");
182+
File.Delete($"{directory}/SnapshotTests.Match_StrictMode_Off_MMA_{number}.md");
183+
File.Delete($"{directory}/SnapshotTests.Match_StrictMode_Off_MM_{number}.md");
184+
185+
Assert.Null(result1);
186+
Assert.Null(result2);
187+
Assert.Null(result3);
188+
Assert.Null(result4);
189+
}
190+
finally
191+
{
192+
Environment.SetEnvironmentVariable("COOKIE_CRUMBLE_STRICT_MODE", null);
193+
}
194+
}
195+
112196
public class MyClass
113197
{
114198
public string Foo { get; set; } = "Bar";

0 commit comments

Comments
 (0)