Skip to content

Commit 0ba2ead

Browse files
authored
Add more TempFile Create apis (#1654)
1 parent 0ca1c79 commit 0ba2ead

File tree

9 files changed

+702
-3
lines changed

9 files changed

+702
-3
lines changed

docs/mdsource/doc-index.include.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
* [Recording](/docs/recording.md)
4242
* [Explicit Targets](/docs/explicit-targets.md)
4343
* [TempDirectory](/docs/temp-directory.md)
44+
* [TempFile](/docs/temp-file.md)
4445
* [FSharp Usage](/docs/fsharp.md)
4546
* [Compared to ApprovalTests](/docs/compared-to-approvaltests.md)
4647
* [Plugins](/docs/plugins.md)

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: 136 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public void StringConversion()
9191
Trace.WriteLine(content);
9292
}
9393
```
94-
<sup><a href='/src/Verify.Tests/TempFileTests.cs#L333-L348' title='Snippet source file'>snippet source</a> | <a href='#snippet-TempFileStringConversion' title='Start of snippet'>anchor</a></sup>
94+
<sup><a href='/src/Verify.Tests/TempFileTests.cs#L344-L359' title='Snippet source file'>snippet source</a> | <a href='#snippet-TempFileStringConversion' title='Start of snippet'>anchor</a></sup>
9595
<!-- endSnippet -->
9696

9797

@@ -114,7 +114,7 @@ public void FileInfoConversion()
114114
Trace.WriteLine(directoryName);
115115
}
116116
```
117-
<sup><a href='/src/Verify.Tests/TempFileTests.cs#L350-L364' title='Snippet source file'>snippet source</a> | <a href='#snippet-TempFileFileInfoConversion' title='Start of snippet'>anchor</a></sup>
117+
<sup><a href='/src/Verify.Tests/TempFileTests.cs#L361-L375' title='Snippet source file'>snippet source</a> | <a href='#snippet-TempFileFileInfoConversion' title='Start of snippet'>anchor</a></sup>
118118
<!-- endSnippet -->
119119

120120

@@ -135,7 +135,7 @@ public void InfoProperty()
135135
Trace.WriteLine(directoryName);
136136
}
137137
```
138-
<sup><a href='/src/Verify.Tests/TempFileTests.cs#L366-L378' title='Snippet source file'>snippet source</a> | <a href='#snippet-TempFileInfoProperty' title='Start of snippet'>anchor</a></sup>
138+
<sup><a href='/src/Verify.Tests/TempFileTests.cs#L377-L389' title='Snippet source file'>snippet source</a> | <a href='#snippet-TempFileInfoProperty' title='Start of snippet'>anchor</a></sup>
139139
<!-- endSnippet -->
140140

141141

@@ -224,6 +224,139 @@ 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+
byte[] data = [0x01, 0x02, 0x03, 0x04];
354+
using var temp = await TempFile.CreateBinary(data, ".bin");
355+
```
356+
<sup><a href='/src/Verify.Tests/TempFileTests.cs#L727-L732' title='Snippet source file'>snippet source</a> | <a href='#snippet-TempFileCreateBinaryWithExtension' title='Start of snippet'>anchor</a></sup>
357+
<!-- endSnippet -->
358+
359+
227360
### Debugging
228361

229362
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

0 commit comments

Comments
 (0)