Skip to content

Commit

Permalink
Cleanup and simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
vbreuss committed Mar 20, 2024
1 parent e88da09 commit 9bc498c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ namespace Testably.Abstractions.Testing.FileSystemInitializer;
[ExcludeFromCodeCoverage]
internal sealed class DirectoryCleaner : IDirectoryCleaner
{
private const string EmptyDirectoryName = "d";
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 @@ -22,7 +22,7 @@ public DirectoryCleaner(IFileSystem fileSystem, string? prefix, Action<string>?
_pathToDelete = InitializeTestDirectory(
fileSystem.ExecuteOrDefault(),
prefix ?? "");
BasePath = _fileSystem.Path.Combine(_pathToDelete, SubDirectory);
BasePath = _fileSystem.Path.Combine(_pathToDelete, EmptyDirectoryName);
}

#region IDirectoryCleaner Members
Expand Down Expand Up @@ -141,35 +141,25 @@ private string GetPathCandidate(Execute execute, string prefix)

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

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

try
{
_fileSystem.Directory.CreateDirectory(pathToDelete);
try
{
_fileSystem.File.WriteAllText(
_fileSystem.Path.Combine(pathToDelete, LockFileName),
string.Empty);
basePath = _fileSystem.Path.Combine(pathToDelete, SubDirectory);
_fileSystem.Directory.CreateDirectory(basePath);
}
catch (Exception)
{
// Give a transient condition like antivirus/indexing a chance to go away
Thread.Sleep(10);
}

_fileSystem.File.WriteAllText(
_fileSystem.Path.Combine(pathToDelete, LockFileName),
string.Empty);
basePath = _fileSystem.Path.Combine(pathToDelete, EmptyDirectoryName);
_fileSystem.Directory.CreateDirectory(basePath);
break;
}
catch (Exception)
{
_fileSystem.Directory.Delete(pathToDelete);
// Give a transient condition like antivirus/indexing a chance to go away
Thread.Sleep(10);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public void Dispose_PermanentFailure_ShouldNotThrowException(
IDirectoryCleaner directoryCleaner =
sut.SetCurrentDirectoryToEmptyTemporaryDirectory(logger: m => receivedLogs.Add(m));
string currentDirectory = sut.Directory.GetCurrentDirectory();
string parentOfCurrentDirectory = sut.Path.GetDirectoryName(currentDirectory)!;
int exceptionCount = 0;
sut.Intercept.Event(_ =>
{
Expand All @@ -52,7 +53,7 @@ public void Dispose_PermanentFailure_ShouldNotThrowException(

receivedLogs.Should().Contain(m =>
m.Contains(exception.Message) &&
m.Contains($"'{sut.Path.GetDirectoryName(currentDirectory)}'"));
m.Contains($"'{parentOfCurrentDirectory}'"));
receivedLogs.Should().NotContain("Cleanup was successful :-)");
}

Expand Down

0 comments on commit 9bc498c

Please sign in to comment.