Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
vbreuss committed Mar 20, 2024
1 parent 2b0b76e commit e88da09
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ namespace Testably.Abstractions.Testing.FileSystemInitializer;
[ExcludeFromCodeCoverage]
internal sealed class DirectoryCleaner : IDirectoryCleaner
{
private const string LockFileName = ".lock";
private const string SubDirectory = "d";
private readonly IFileSystem _fileSystem;
private readonly Action<string>? _logger;
private readonly string _pathToDelete;
Expand All @@ -17,10 +19,10 @@ public DirectoryCleaner(IFileSystem fileSystem, string? prefix, Action<string>?
{
_fileSystem = fileSystem;
_logger = logger;
BasePath = InitializeBasePath(
_pathToDelete = InitializeTestDirectory(
fileSystem.ExecuteOrDefault(),
prefix ?? "");
_pathToDelete = fileSystem.Path.GetDirectoryName(BasePath) ?? BasePath;
BasePath = _fileSystem.Path.Combine(_pathToDelete, SubDirectory);
}

#region IDirectoryCleaner Members
Expand Down Expand Up @@ -121,7 +123,7 @@ private void ForceDeleteDirectory(string path, bool recursive)
/// <summary>
/// Returns a candidate for a test-directory within the temporary directory that does not yet exist.
/// </summary>
private string GetPossiblePath(Execute execute, string prefix)
private string GetPathCandidate(Execute execute, string prefix)
{
string basePath;
do
Expand All @@ -137,23 +139,25 @@ private string GetPossiblePath(Execute execute, string prefix)
return basePath;
}

private string InitializeBasePath(Execute execute, string prefix)
private string InitializeTestDirectory(Execute execute, string prefix)
{
string pathToDelete = SubDirectory;
string basePath = "";

for (int j = 0; j <= 5; j++)
{
basePath = GetPossiblePath(execute, prefix);
pathToDelete = GetPathCandidate(execute, prefix);

try
{
_fileSystem.Directory.CreateDirectory(basePath);
_fileSystem.Directory.CreateDirectory(pathToDelete);
try
{
_fileSystem.File.WriteAllText(_fileSystem.Path.Combine(basePath, ".lock"), "");
string b = _fileSystem.Path.Combine(basePath, "d");
_fileSystem.Directory.CreateDirectory(b);
basePath = b;
_fileSystem.File.WriteAllText(
_fileSystem.Path.Combine(pathToDelete, LockFileName),
string.Empty);
basePath = _fileSystem.Path.Combine(pathToDelete, SubDirectory);
_fileSystem.Directory.CreateDirectory(basePath);
}
catch (Exception)
{
Expand All @@ -165,7 +169,7 @@ private string InitializeBasePath(Execute execute, string prefix)
}
catch (Exception)
{
_fileSystem.Directory.Delete(basePath);
_fileSystem.Directory.Delete(pathToDelete);
// Give a transient condition like antivirus/indexing a chance to go away
Thread.Sleep(10);
}
Expand All @@ -190,6 +194,6 @@ private string InitializeBasePath(Execute execute, string prefix)
$"Could not set current directory to '{basePath}' for tests");
}

return basePath;
return pathToDelete;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public RealFileSystemTests(ITestOutputHelper testOutputHelper, TestSettingsFixtu
#endif
_fixture = fixture;
_directoryCleaner = FileSystem
.SetCurrentDirectoryToEmptyTemporaryDirectory($""Testably.Abstractions.Tests{{FileSystem.Path.DirectorySeparatorChar}}{@class.Name}-"", testOutputHelper.WriteLine);
.SetCurrentDirectoryToEmptyTemporaryDirectory($""{@class.Namespace}{{FileSystem.Path.DirectorySeparatorChar}}{@class.Name}-"", testOutputHelper.WriteLine);
}}
/// <inheritdoc cref=""IDisposable.Dispose()"" />
Expand Down

0 comments on commit e88da09

Please sign in to comment.