Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 6f1a32c

Browse files
committed
Add Common test helper classes for file cleanup
Later one we can consolidate to these with the ones used in other test projects (and have additional test projects use these to avoid leaving straggling test files behind).
1 parent efff1eb commit 6f1a32c

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using System.Runtime.CompilerServices;
5+
6+
namespace System.IO
7+
{
8+
/// <summary>Base class for test classes the use temporary files that need to be cleaned up.</summary>
9+
public abstract class TemporaryFilesCleanupTestBase : IDisposable
10+
{
11+
/// <summary>Initialize the test class base.</summary>
12+
protected TemporaryFilesCleanupTestBase()
13+
{
14+
// Use a unique test directory per test class
15+
TestDirectory = Path.Combine(Path.GetTempPath(), GetType().Name + "_" + Guid.NewGuid().ToString("N"));
16+
try
17+
{
18+
Directory.CreateDirectory(TestDirectory);
19+
}
20+
catch
21+
{
22+
// Don't throw exceptions during test class construction. Attempts to use paths
23+
// under this directory will instead appropriately fail later.
24+
}
25+
}
26+
27+
~TemporaryFilesCleanupTestBase()
28+
{
29+
Dispose(false);
30+
}
31+
32+
public void Dispose()
33+
{
34+
Dispose(true);
35+
GC.SuppressFinalize(this);
36+
}
37+
38+
protected void Dispose(bool disposing)
39+
{
40+
// No managed resources to clean up.
41+
42+
try { Directory.Delete(TestDirectory, recursive: true); }
43+
catch { } // avoid exceptions from Dispose
44+
}
45+
46+
/// <summary>Gets the test directory into which all files and directories created by tests should be stored.</summary>
47+
protected string TestDirectory { get; private set; }
48+
49+
/// <summary>Generates a test file full path that is unique to the call site.</summary>
50+
protected string GetTestFilePath([CallerMemberName]string fileName = null, [CallerLineNumber] int lineNumber = 0)
51+
{
52+
return Path.Combine(TestDirectory, GetTestFileName(fileName, lineNumber));
53+
}
54+
55+
/// <summary>Generates a test file name that is unique to the call site.</summary>
56+
protected string GetTestFileName([CallerMemberName]string fileName = null, [CallerLineNumber] int lineNumber = 0)
57+
{
58+
return string.Format("{0}_{1}", fileName ?? "TestBase", lineNumber);
59+
}
60+
}
61+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
namespace System.IO
5+
{
6+
/// <summary>
7+
/// Represents a temporary file. Creating an instance creates a file at the specified path,
8+
/// and disposing the instance deletes the file.
9+
/// </summary>
10+
public sealed class TempFile : IDisposable
11+
{
12+
/// <summary>Gets the created file's path.</summary>
13+
public string Path { get; private set; }
14+
15+
public TempFile(string path, long length = 0)
16+
{
17+
Path = path;
18+
using (FileStream fs = File.Create(path))
19+
{
20+
if (length > 0)
21+
{
22+
// Fill with zeros up to the desired length.
23+
fs.Write(new byte[length], 0, (int)length);
24+
}
25+
}
26+
}
27+
28+
~TempFile() { DeleteFile(); }
29+
30+
public void Dispose()
31+
{
32+
GC.SuppressFinalize(this);
33+
DeleteFile();
34+
}
35+
36+
private void DeleteFile()
37+
{
38+
File.Delete(Path);
39+
}
40+
}
41+
}

0 commit comments

Comments
 (0)