Skip to content

Commit b1fde1a

Browse files
committed
Update TempFile.cs
1 parent e95db4b commit b1fde1a

File tree

1 file changed

+133
-0
lines changed

1 file changed

+133
-0
lines changed

src/Verify/TempFile.cs

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,43 @@ public TempFile(string? extension = null)
146146
}
147147
}
148148

149+
/// <summary>
150+
/// Creates a new temporary file with optional extension and encoding.
151+
/// </summary>
152+
/// <param name="extension">
153+
/// Optional file extension (e.g., ".txt", ".json").
154+
/// If not provided, no extension is added.
155+
/// The extension should include the leading dot.
156+
/// </param>
157+
/// <param name="encoding">
158+
/// Optional text encoding to use when creating the file.
159+
/// If provided and the extension is recognized as a text format, the file will be created with the appropriate BOM.
160+
/// If null, the file is created as an empty binary file.
161+
/// </param>
162+
/// <returns>
163+
/// A new <see cref="TempFile"/> instance representing the created file.
164+
/// The caller is responsible for disposing the instance to ensure cleanup.
165+
/// </returns>
166+
/// <remarks>
167+
/// The file is created immediately upon calling this method. If encoding is specified and the extension
168+
/// is recognized as a text format, the file will be initialized with the appropriate byte order mark (BOM).
169+
/// Otherwise, an empty file is created.
170+
/// </remarks>
171+
/// <exception cref="IOException">
172+
/// Thrown if the file cannot be created (e.g., due to permissions or disk space).
173+
/// </exception>
174+
/// <example>
175+
/// <code>
176+
/// // Create empty temp file
177+
/// using var temp = TempFile.Create();
178+
///
179+
/// // Create temp file with extension
180+
/// using var txtFile = TempFile.Create(".txt");
181+
///
182+
/// // Create temp file with UTF-8 encoding and BOM
183+
/// using var utf8File = TempFile.Create(".txt", Encoding.UTF8);
184+
/// </code>
185+
/// </example>
149186
public static TempFile Create(string? extension = null, Encoding? encoding = null)
150187
{
151188
var file = new TempFile(extension);
@@ -159,6 +196,56 @@ public static TempFile Create(string? extension = null, Encoding? encoding = nul
159196
return file;
160197
}
161198

199+
/// <summary>
200+
/// Creates a new temporary file with the specified text content.
201+
/// </summary>
202+
/// <param name="content">
203+
/// The text content to write to the file.
204+
/// </param>
205+
/// <param name="extension">
206+
/// Optional file extension (e.g., ".txt", ".json", ".xml").
207+
/// If not provided, no extension is added.
208+
/// The extension should include the leading dot.
209+
/// </param>
210+
/// <param name="encoding">
211+
/// Optional text encoding to use when writing the content.
212+
/// If null, the default UTF-8 encoding without BOM is used.
213+
/// </param>
214+
/// <returns>
215+
/// A task that represents the asynchronous operation.
216+
/// The task result contains a new <see cref="TempFile"/> instance with the written content.
217+
/// The caller is responsible for disposing the instance to ensure cleanup.
218+
/// </returns>
219+
/// <remarks>
220+
/// <para>
221+
/// The file is created and written asynchronously. The content is written using the specified
222+
/// encoding, or UTF-8 without BOM if no encoding is specified.
223+
/// </para>
224+
/// <para>
225+
/// This method is ideal for creating temporary test data files, configuration files, or
226+
/// any text-based content that needs to be written to disk for testing purposes.
227+
/// </para>
228+
/// </remarks>
229+
/// <exception cref="IOException">
230+
/// Thrown if the file cannot be created or written to (e.g., due to permissions or disk space).
231+
/// </exception>
232+
/// <example>
233+
/// <code>
234+
/// // Create temp file with simple text
235+
/// using var temp = await TempFile.CreateText("Hello, World!");
236+
///
237+
/// // Create JSON file
238+
/// using var json = await TempFile.CreateText(
239+
/// "{\"name\": \"test\"}",
240+
/// ".json");
241+
///
242+
/// // Create file with specific encoding
243+
/// using var utf8 = await TempFile.CreateText(
244+
/// "Content with special chars: äöü",
245+
/// ".txt",
246+
/// Encoding.UTF8);
247+
/// </code>
248+
/// </example>
162249
public static async Task<TempFile> CreateText(string content, string? extension = null, Encoding? encoding = null)
163250
{
164251
var file = new TempFile(extension);
@@ -175,6 +262,52 @@ public static async Task<TempFile> CreateText(string content, string? extension
175262
return file;
176263
}
177264

265+
/// <summary>
266+
/// Creates a new temporary file with the specified binary content.
267+
/// </summary>
268+
/// <param name="content">
269+
/// The binary content to write to the file.
270+
/// Can be empty to create an empty file.
271+
/// </param>
272+
/// <param name="extension">
273+
/// Optional file extension (e.g., ".bin", ".dat", ".png").
274+
/// If not provided, no extension is added.
275+
/// The extension should include the leading dot.
276+
/// </param>
277+
/// <returns>
278+
/// A task that represents the asynchronous operation.
279+
/// The task result contains a new <see cref="TempFile"/> instance with the written content.
280+
/// The caller is responsible for disposing the instance to ensure cleanup.
281+
/// </returns>
282+
/// <remarks>
283+
/// <para>
284+
/// The file is created and written asynchronously with the exact binary content provided.
285+
/// This method is suitable for creating temporary files containing binary data such as
286+
/// images, serialized objects, or any non-text data.
287+
/// </para>
288+
/// <para>
289+
/// The content is written as-is without any encoding or transformation.
290+
/// </para>
291+
/// </remarks>
292+
/// <exception cref="IOException">
293+
/// Thrown if the file cannot be created or written to (e.g., due to permissions or disk space).
294+
/// </exception>
295+
/// <example>
296+
/// <code>
297+
/// // Create temp file with binary data
298+
/// byte[] data = [0x01, 0x02, 0x03, 0x04];
299+
/// using var temp = await TempFile.CreateBinary(data);
300+
///
301+
/// // Create image file
302+
/// byte[] imageData = await File.ReadAllBytesAsync("source.png");
303+
/// using var image = await TempFile.CreateBinary(imageData, ".png");
304+
///
305+
/// // Create empty binary file
306+
/// using var empty = await TempFile.CreateBinary(
307+
/// ReadOnlyMemory&lt;byte&gt;.Empty,
308+
/// ".bin");
309+
/// </code>
310+
/// </example>
178311
public static async Task<TempFile> CreateBinary(ReadOnlyMemory<byte> content, string? extension = null)
179312
{
180313
var file = new TempFile(extension);

0 commit comments

Comments
 (0)