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

Commit 37b0abb

Browse files
committed
Merge pull request #2630 from stephentoub/consolidate_tempfiles
Change System.IO.FileSystem.Tests to use FileCleanupTestBase
2 parents 1f20371 + d7a19e9 commit 37b0abb

File tree

9 files changed

+44
-88
lines changed

9 files changed

+44
-88
lines changed

src/Common/tests/System/IO/FileCleanupTestBase.cs

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,17 @@
66
namespace System.IO
77
{
88
/// <summary>Base class for test classes the use temporary files that need to be cleaned up.</summary>
9-
public abstract class TemporaryFilesCleanupTestBase : IDisposable
9+
public abstract class FileCleanupTestBase : IDisposable
1010
{
11-
/// <summary>Initialize the test class base.</summary>
12-
protected TemporaryFilesCleanupTestBase()
11+
/// <summary>Initialize the test class base. This creates the associated test directory.</summary>
12+
protected FileCleanupTestBase()
1313
{
14-
// Use a unique test directory per test class
15-
TestDirectory = Path.Combine(Path.GetTempPath(), GetType().Name + "_" + Guid.NewGuid().ToString("N"));
14+
// Use a unique test directory per test class. The test directory lives in the user's temp directory,
15+
// and includes both the name of the test class and a random string. The test class name is included
16+
// so that it can be easily correlated if necessary, and the random string to helps avoid conflicts if
17+
// the same test should be run concurrently with itself (e.g. if a [Fact] method lives on a base class)
18+
// or if some stray files were left over from a previous run.
19+
TestDirectory = Path.Combine(Path.GetTempPath(), GetType().Name + "_" + Path.GetRandomFileName());
1620
try
1721
{
1822
Directory.CreateDirectory(TestDirectory);
@@ -24,38 +28,49 @@ protected TemporaryFilesCleanupTestBase()
2428
}
2529
}
2630

27-
~TemporaryFilesCleanupTestBase()
31+
/// <summary>Delete the associated test directory.</summary>
32+
~FileCleanupTestBase()
2833
{
2934
Dispose(false);
3035
}
3136

37+
/// <summary>Delete the associated test directory.</summary>
3238
public void Dispose()
3339
{
3440
Dispose(true);
3541
GC.SuppressFinalize(this);
3642
}
3743

38-
protected void Dispose(bool disposing)
44+
/// <summary>Delete the associated test directory.</summary>
45+
protected virtual void Dispose(bool disposing)
3946
{
40-
// No managed resources to clean up.
47+
// No managed resources to clean up, so disposing is ignored.
4148

4249
try { Directory.Delete(TestDirectory, recursive: true); }
43-
catch { } // avoid exceptions from Dispose
50+
catch { } // avoid exceptions escaping Dispose
4451
}
4552

4653
/// <summary>Gets the test directory into which all files and directories created by tests should be stored.</summary>
4754
protected string TestDirectory { get; private set; }
4855

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)
56+
/// <summary>Gets a test file full path that is associated with the call site.</summary>
57+
/// <param name="index">An optional index value to use as a suffix on the file name. Typically a loop index.</param>
58+
/// <param name="memberName">The member name of the function calling this method.</param>
59+
/// <param name="lineNumber">The line number of the function calling this method.</param>
60+
protected string GetTestFilePath(int? index = null, [CallerMemberName] string memberName = null, [CallerLineNumber] int lineNumber = 0)
5161
{
52-
return Path.Combine(TestDirectory, GetTestFileName(fileName, lineNumber));
62+
return Path.Combine(TestDirectory, GetTestFileName(index, memberName, lineNumber));
5363
}
5464

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)
65+
/// <summary>Gets a test file name that is associated with the call site.</summary>
66+
/// <param name="index">An optional index value to use as a suffix on the file name. Typically a loop index.</param>
67+
/// <param name="memberName">The member name of the function calling this method.</param>
68+
/// <param name="lineNumber">The line number of the function calling this method.</para
69+
protected string GetTestFileName(int? index = null, [CallerMemberName] string memberName = null, [CallerLineNumber] int lineNumber = 0)
5770
{
58-
return string.Format("{0}_{1}", fileName ?? "TestBase", lineNumber);
71+
return string.Format(
72+
index.HasValue ? "{0}_{1}_{2}" : "{0}_{1}",
73+
memberName ?? "TestBase", lineNumber, index.GetValueOrDefault());
5974
}
6075
}
6176
}

src/Scenarios/tests/InterProcessCommunication/IpcTestBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
namespace InterProcessCommunication.Tests
1111
{
1212
/// <summary>Base class used for all inter-process communication tests.</summary>
13-
public abstract class IpcTestBase : TemporaryFilesCleanupTestBase
13+
public abstract class IpcTestBase : FileCleanupTestBase
1414
{
1515
/// <summary>The CoreCLR host used to host the test console app.</summary>
1616
private const string HostRunner = "corerun";

src/System.IO.FileSystem/tests/Directory/Exists.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ public void TrailingWhitespaceExistence()
208208

209209
Assert.All(IOInputs.GetSimpleWhiteSpace(), (component) =>
210210
{
211-
string path = GetTestFilePath("Extended") + component;
211+
string path = GetTestFilePath(memberName: "Extended") + component;
212212
testDir = Directory.CreateDirectory(@"\\?\" + path);
213213
Assert.False(Exists(path), path);
214214
Assert.True(Exists(testDir.FullName));

src/System.IO.FileSystem/tests/Directory/GetDirectoryRoot.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,28 +24,28 @@ public void GetRootOfRoot()
2424
public void RelativeDirectory()
2525
{
2626
string root = Directory.GetDirectoryRoot(Path.DirectorySeparatorChar + "testDir");
27-
Assert.Equal(Path.GetPathRoot(TestDirectory), root);
27+
Assert.Equal(Path.GetPathRoot(Directory.GetCurrentDirectory()), root);
2828
}
2929

3030
[Fact]
3131
public void NestedDirectories()
3232
{
3333
string root = Directory.GetDirectoryRoot(Path.Combine("a", "a", "a", "b") + Path.DirectorySeparatorChar);
34-
Assert.Equal(Path.GetPathRoot(TestDirectory), root);
34+
Assert.Equal(Path.GetPathRoot(Directory.GetCurrentDirectory()), root);
3535
}
3636

3737
[Fact]
3838
public void DotPaths()
3939
{
4040
string root = Directory.GetDirectoryRoot(Path.Combine("Test1", ".", "test2", "..", "test3"));
41-
Assert.Equal(Path.GetPathRoot(TestDirectory), root);
41+
Assert.Equal(Path.GetPathRoot(Directory.GetCurrentDirectory()), root);
4242
}
4343

4444
[Fact]
4545
public void WhitespacePaths()
4646
{
4747
string root = Directory.GetDirectoryRoot(Path.Combine("T es t1", "te s t2", "t est 3"));
48-
Assert.Equal(Path.GetPathRoot(TestDirectory), root);
48+
Assert.Equal(Path.GetPathRoot(Directory.GetCurrentDirectory()), root);
4949
}
5050

5151
[Fact]

src/System.IO.FileSystem/tests/Directory/GetFileSystemEntries_str_str_so.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public void SearchPatternIncludeSubDirectories()
6666
using (File.Create(testFile1))
6767
using (File.Create(testFile2))
6868
{
69-
string[] results = GetEntries(Directory.GetCurrentDirectory(), Path.Combine(new DirectoryInfo(TestDirectory).Name, "*"), SearchOption.AllDirectories);
69+
string[] results = GetEntries(Directory.GetParent(TestDirectory).FullName, Path.Combine(Path.GetFileName(TestDirectory), "*"), SearchOption.AllDirectories);
7070
if (TestFiles)
7171
{
7272
Assert.Contains(testFile1, results);

src/System.IO.FileSystem/tests/DirectoryInfo/Root.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public void DotsInPathAreValid()
3434
[PlatformSpecific(PlatformID.Windows)]
3535
public void UNCShares()
3636
{
37-
string root = Path.GetPathRoot(TestDirectory);
37+
string root = Path.GetPathRoot(Directory.GetCurrentDirectory());
3838
string path = Path.DirectorySeparatorChar + Path.Combine("Machine", "Test");
3939
Assert.Equal(root, new DirectoryInfo(path).Root.FullName);
4040

Lines changed: 1 addition & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,10 @@
11
// Copyright (c) Microsoft. All rights reserved.
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

4-
using System;
5-
using System.IO;
6-
using System.Runtime.CompilerServices;
7-
84
namespace System.IO.FileSystem.Tests
95
{
10-
public abstract class FileSystemTest : IDisposable
6+
public abstract class FileSystemTest : FileCleanupTestBase
117
{
128
public static readonly byte[] TestBuffer = { 0xBA, 0x5E, 0xBA, 0x11, 0xF0, 0x07, 0xBA, 0x11 };
13-
14-
public string TestDirectory { get; private set; }
15-
16-
public FileSystemTest()
17-
{
18-
// Use a unique test directory per test class
19-
TestDirectory = Path.Combine(Directory.GetCurrentDirectory(), GetType().Name);
20-
21-
try
22-
{
23-
Directory.CreateDirectory(TestDirectory);
24-
}
25-
catch
26-
{
27-
// Don't want this to crash the test, we'll fail appropriately in other test
28-
// cases if Directory.Create is broken.
29-
}
30-
}
31-
32-
// Generates a test file path to use that is unique name per test case / call
33-
public string GetTestFilePath([CallerMemberName]string memberName = null, [CallerLineNumber] int lineNumber = 0)
34-
{
35-
return Path.Combine(TestDirectory, String.Format("{0}_{1}", memberName ?? "testFile", lineNumber));
36-
}
37-
38-
// Generates a test file path to use that is unique name per test case / call with an additional
39-
// variable to specify a trailing identifier
40-
public string GetTestFilePath(int index, [CallerMemberName]string memberName = null, [CallerLineNumber] int lineNumber = 0)
41-
{
42-
return Path.Combine(TestDirectory, String.Format("{0}_{1}_{2}", memberName ?? "testFile", lineNumber, index));
43-
}
44-
45-
// Generates a test file name to use that is unique name per test case / call
46-
public string GetTestFileName([CallerMemberName]string memberName = null, [CallerLineNumber] int lineNumber = 0)
47-
{
48-
return String.Format("{0}_{1}", memberName ?? "testFile", lineNumber);
49-
}
50-
51-
public void Dispose()
52-
{
53-
Dispose(true);
54-
GC.SuppressFinalize(this);
55-
}
56-
57-
protected void Dispose(bool disposing)
58-
{
59-
// if (disposing) no managed resources
60-
61-
// clean up non-managed resources
62-
try
63-
{
64-
Directory.Delete(TestDirectory, true);
65-
}
66-
catch
67-
{
68-
// Don't throw during dispose
69-
}
70-
}
719
}
7210
}

src/System.IO.FileSystem/tests/System.IO.FileSystem.Tests.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,9 @@
157157
<Compile Include="$(CommonPath)\Interop\Interop.PlatformDetection.cs">
158158
<Link>Common\Interop\Interop.PlatformDetection.cs</Link>
159159
</Compile>
160+
<Compile Include="$(CommonTestPath)\System\IO\FileCleanupTestBase.cs">
161+
<Link>Common\System\IO\FileCleanupTestBase.cs</Link>
162+
</Compile>
160163
</ItemGroup>
161164
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" />
162165
</Project>

src/System.IO.MemoryMappedFiles/tests/MemoryMappedFilesTestsBase.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
namespace System.IO.MemoryMappedFiles.Tests
1010
{
1111
/// <summary>Base class from which all of the memory mapped files test classes derive.</summary>
12-
public abstract class MemoryMappedFilesTestBase : TemporaryFilesCleanupTestBase
12+
public abstract class MemoryMappedFilesTestBase : FileCleanupTestBase
1313
{
1414
/// <summary>Gets whether named maps are supported by the current platform.</summary>
1515
protected static bool MapNamesSupported { get { return Interop.IsWindows; } }
@@ -66,7 +66,7 @@ protected IEnumerable<MemoryMappedFile> CreateSampleMaps(
6666
if (MapNamesSupported)
6767
{
6868
yield return MemoryMappedFile.CreateNew(CreateUniqueMapName(), capacity, access);
69-
yield return MemoryMappedFile.CreateFromFile(GetTestFilePath(fileName, lineNumber), FileMode.CreateNew, CreateUniqueMapName(), capacity, access);
69+
yield return MemoryMappedFile.CreateFromFile(GetTestFilePath(null, fileName, lineNumber), FileMode.CreateNew, CreateUniqueMapName(), capacity, access);
7070
}
7171
}
7272

0 commit comments

Comments
 (0)