Skip to content

Commit 4513dbe

Browse files
committed
.
1 parent 187a8dc commit 4513dbe

File tree

7 files changed

+312
-1
lines changed

7 files changed

+312
-1
lines changed

docs/mdsource/temp-file.source.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,58 @@ Result:
9898
snippet: TempFileTests.Scrubbing.verified.txt
9999

100100

101+
### Create Method
102+
103+
Creates a new temporary file with optional extension and encoding.
104+
105+
snippet: TempFileCreate
106+
107+
108+
#### With Extension
109+
110+
Create a temporary file with a specific extension:
111+
112+
snippet: TempFileCreateWithExtension
113+
114+
115+
#### With Encoding
116+
117+
Create a temporary file with a specific text encoding and BOM:
118+
119+
snippet: TempFileCreateWithEncoding
120+
121+
122+
### CreateText Method
123+
124+
Creates a new temporary file with text content asynchronously.
125+
126+
snippet: TempFileCreateText
127+
128+
129+
#### With Extension
130+
131+
snippet: TempFileCreateTextWithExtension
132+
133+
134+
#### With Encoding
135+
136+
Create a text file with specific encoding:
137+
138+
snippet: TempFileCreateTextWithEncoding
139+
140+
141+
### CreateBinary Method
142+
143+
Creates a new temporary file with binary content asynchronously.
144+
145+
snippet: TempFileCreateBinary
146+
147+
148+
#### With Extension
149+
150+
snippet: TempFileCreateBinaryWithExtension
151+
152+
101153
### Debugging
102154

103155
Given `TempFile` deletes the file on test completion (even failure), it can be difficult to debug what caused the failure.

docs/readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ To change this file edit the source file and then run MarkdownSnippets.
5050
* [Recording](/docs/recording.md)
5151
* [Explicit Targets](/docs/explicit-targets.md)
5252
* [TempDirectory](/docs/temp-directory.md)
53+
* [TempFile](/docs/temp-file.md)
5354
* [FSharp Usage](/docs/fsharp.md)
5455
* [Compared to ApprovalTests](/docs/compared-to-approvaltests.md)
5556
* [Plugins](/docs/plugins.md)<!-- endInclude -->

docs/temp-file.md

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,140 @@ Result:
224224
<!-- endSnippet -->
225225

226226

227+
### Create Method
228+
229+
Creates a new temporary file with optional extension and encoding.
230+
231+
<!-- snippet: TempFileCreate -->
232+
<a id='snippet-TempFileCreate'></a>
233+
```cs
234+
using var temp = TempFile.Create();
235+
236+
File.WriteAllText(temp, "content");
237+
238+
// file automatically deleted here
239+
```
240+
<sup><a href='/src/Verify.Tests/TempFileTests.cs#L620-L628' title='Snippet source file'>snippet source</a> | <a href='#snippet-TempFileCreate' title='Start of snippet'>anchor</a></sup>
241+
<!-- endSnippet -->
242+
243+
244+
#### With Extension
245+
246+
Create a temporary file with a specific extension:
247+
248+
<!-- snippet: TempFileCreateWithExtension -->
249+
<a id='snippet-TempFileCreateWithExtension'></a>
250+
```cs
251+
using var temp = TempFile.Create(".txt");
252+
253+
File.WriteAllText(temp, "content");
254+
```
255+
<sup><a href='/src/Verify.Tests/TempFileTests.cs#L635-L641' title='Snippet source file'>snippet source</a> | <a href='#snippet-TempFileCreateWithExtension' title='Start of snippet'>anchor</a></sup>
256+
<!-- endSnippet -->
257+
258+
259+
#### With Encoding
260+
261+
Create a temporary file with a specific text encoding and BOM:
262+
263+
<!-- snippet: TempFileCreateWithEncoding -->
264+
<a id='snippet-TempFileCreateWithEncoding'></a>
265+
```cs
266+
using var temp = TempFile.Create(".txt", Encoding.UTF8);
267+
268+
File.Exists(temp.Path);
269+
```
270+
<sup><a href='/src/Verify.Tests/TempFileTests.cs#L649-L655' title='Snippet source file'>snippet source</a> | <a href='#snippet-TempFileCreateWithEncoding' title='Start of snippet'>anchor</a></sup>
271+
<!-- endSnippet -->
272+
273+
274+
### CreateText Method
275+
276+
Creates a new temporary file with text content asynchronously.
277+
278+
<!-- snippet: TempFileCreateText -->
279+
<a id='snippet-TempFileCreateText'></a>
280+
```cs
281+
using var temp = await TempFile.CreateText("Hello, World!");
282+
283+
var content = await File.ReadAllTextAsync(temp);
284+
Assert.Equal("Hello, World!", content);
285+
```
286+
<sup><a href='/src/Verify.Tests/TempFileTests.cs#L661-L668' title='Snippet source file'>snippet source</a> | <a href='#snippet-TempFileCreateText' title='Start of snippet'>anchor</a></sup>
287+
<!-- endSnippet -->
288+
289+
290+
#### With Extension
291+
292+
<!-- snippet: TempFileCreateTextWithExtension -->
293+
<a id='snippet-TempFileCreateTextWithExtension'></a>
294+
```cs
295+
var json = """
296+
{
297+
"name": "test",
298+
"value": 123
299+
}
300+
""";
301+
302+
using var temp = await TempFile.CreateText(json, ".json");
303+
304+
var content = await File.ReadAllTextAsync(temp);
305+
Assert.Equal(json, content);
306+
```
307+
<sup><a href='/src/Verify.Tests/TempFileTests.cs#L674-L688' title='Snippet source file'>snippet source</a> | <a href='#snippet-TempFileCreateTextWithExtension' title='Start of snippet'>anchor</a></sup>
308+
<!-- endSnippet -->
309+
310+
311+
#### With Encoding
312+
313+
Create a text file with specific encoding:
314+
315+
<!-- snippet: TempFileCreateTextWithEncoding -->
316+
<a id='snippet-TempFileCreateTextWithEncoding'></a>
317+
```cs
318+
using var temp = await TempFile.CreateText(
319+
"Content with special chars: äöü",
320+
".txt",
321+
Encoding.UTF8);
322+
323+
var content = await File.ReadAllTextAsync(temp, Encoding.UTF8);
324+
Assert.Equal("Content with special chars: äöü", content);
325+
```
326+
<sup><a href='/src/Verify.Tests/TempFileTests.cs#L694-L704' title='Snippet source file'>snippet source</a> | <a href='#snippet-TempFileCreateTextWithEncoding' title='Start of snippet'>anchor</a></sup>
327+
<!-- endSnippet -->
328+
329+
330+
### CreateBinary Method
331+
332+
Creates a new temporary file with binary content asynchronously.
333+
334+
<!-- snippet: TempFileCreateBinary -->
335+
<a id='snippet-TempFileCreateBinary'></a>
336+
```cs
337+
byte[] data = [0x01, 0x02, 0x03, 0x04];
338+
339+
using var temp = await TempFile.CreateBinary(data);
340+
341+
var readData = await File.ReadAllBytesAsync(temp);
342+
Assert.Equal(data, readData);
343+
```
344+
<sup><a href='/src/Verify.Tests/TempFileTests.cs#L712-L721' title='Snippet source file'>snippet source</a> | <a href='#snippet-TempFileCreateBinary' title='Start of snippet'>anchor</a></sup>
345+
<!-- endSnippet -->
346+
347+
348+
#### With Extension
349+
350+
<!-- snippet: TempFileCreateBinaryWithExtension -->
351+
<a id='snippet-TempFileCreateBinaryWithExtension'></a>
352+
```cs
353+
using var temp = await TempFile.CreateBinary(imageBinaryData, ".png");
354+
355+
// Process the temporary image file
356+
```
357+
<sup><a href='/src/Verify.Tests/TempFileTests.cs#L729-L735' title='Snippet source file'>snippet source</a> | <a href='#snippet-TempFileCreateBinaryWithExtension' title='Start of snippet'>anchor</a></sup>
358+
<!-- endSnippet -->
359+
360+
227361
### Debugging
228362

229363
Given `TempFile` deletes the file on test completion (even failure), it can be difficult to debug what caused the failure.

readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1112,6 +1112,7 @@ To opt out of this feature, include the following in the project file:
11121112
* [Recording](/docs/recording.md)
11131113
* [Explicit Targets](/docs/explicit-targets.md)
11141114
* [TempDirectory](/docs/temp-directory.md)
1115+
* [TempFile](/docs/temp-file.md)
11151116
* [FSharp Usage](/docs/fsharp.md)
11161117
* [Compared to ApprovalTests](/docs/compared-to-approvaltests.md)
11171118
* [Plugins](/docs/plugins.md)<!-- endInclude -->
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
test content

src/Verify.Tests/TempFileTests.cs

Lines changed: 122 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,7 @@ public async Task CreateText_WithExtension_CreatesFileWithExtensionAndContent()
409409
{
410410
var content = "Test content";
411411

412-
using var temp = await TempFile.CreateText(content, ".txt");
412+
using var temp = await TempFile.CreateText(content, ".txt");
413413

414414
Assert.EndsWith(".txt", temp.Path);
415415
Assert.True(File.Exists(temp.Path));
@@ -613,4 +613,125 @@ public async Task CreateText_WithXmlContent_CreatesValidXmlFile()
613613
var readContent = await File.ReadAllTextAsync(temp.Path);
614614
Assert.Equal(xmlContent, readContent);
615615
}
616+
617+
[Fact]
618+
public void Create()
619+
{
620+
#region TempFileCreate
621+
622+
using var temp = TempFile.Create();
623+
624+
File.WriteAllText(temp, "content");
625+
626+
// file automatically deleted here
627+
628+
#endregion
629+
}
630+
631+
632+
[Fact]
633+
public void CreateWithExtension()
634+
{
635+
#region TempFileCreateWithExtension
636+
637+
using var temp = TempFile.Create(".txt");
638+
639+
File.WriteAllText(temp, "content");
640+
641+
#endregion
642+
}
643+
644+
645+
646+
[Fact]
647+
public void CreateWithEncoding()
648+
{
649+
#region TempFileCreateWithEncoding
650+
651+
using var temp = TempFile.Create(".txt", Encoding.UTF8);
652+
653+
File.Exists(temp.Path);
654+
655+
#endregion
656+
}
657+
658+
[Fact]
659+
public async Task CreateText()
660+
{
661+
#region TempFileCreateText
662+
663+
using var temp = await TempFile.CreateText("Hello, World!");
664+
665+
var content = await File.ReadAllTextAsync(temp);
666+
Assert.Equal("Hello, World!", content);
667+
668+
#endregion
669+
}
670+
671+
[Fact]
672+
public async Task CreateTextWithExtension()
673+
{
674+
#region TempFileCreateTextWithExtension
675+
676+
var json = """
677+
{
678+
"name": "test",
679+
"value": 123
680+
}
681+
""";
682+
683+
using var temp = await TempFile.CreateText(json, ".json");
684+
685+
var content = await File.ReadAllTextAsync(temp);
686+
Assert.Equal(json, content);
687+
688+
#endregion
689+
}
690+
691+
[Fact]
692+
public async Task CreateTextWithEncoding()
693+
{
694+
#region TempFileCreateTextWithEncoding
695+
696+
using var temp = await TempFile.CreateText(
697+
"Content with special chars: äöü",
698+
".txt",
699+
Encoding.UTF8);
700+
701+
var content = await File.ReadAllTextAsync(temp, Encoding.UTF8);
702+
Assert.Equal("Content with special chars: äöü", content);
703+
704+
#endregion
705+
}
706+
707+
708+
709+
[Fact]
710+
public async Task CreateBinary()
711+
{
712+
#region TempFileCreateBinary
713+
714+
byte[] data = [0x01, 0x02, 0x03, 0x04];
715+
716+
using var temp = await TempFile.CreateBinary(data);
717+
718+
var readData = await File.ReadAllBytesAsync(temp);
719+
Assert.Equal(data, readData);
720+
721+
#endregion
722+
}
723+
724+
[Fact]
725+
public async Task CreateBinaryWithExtension()
726+
{
727+
var imageBinaryData = await File.ReadAllBytesAsync("source.png");
728+
729+
#region TempFileCreateBinaryWithExtension
730+
731+
using var temp = await TempFile.CreateBinary(imageBinaryData, ".png");
732+
733+
// Process the temporary image file
734+
735+
#endregion
736+
}
616737
}

0 commit comments

Comments
 (0)