Skip to content

Commit

Permalink
Add disposal tracking for TempRoot
Browse files Browse the repository at this point in the history
  • Loading branch information
khyperia committed Dec 4, 2017
1 parent d641eaa commit b9bc86b
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/Test/Utilities/Portable/TempFiles/TempRoot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public sealed class TempRoot : IDisposable
{
private readonly ConcurrentBag<IDisposable> _temps = new ConcurrentBag<IDisposable>();
public static readonly string Root;
private bool _disposed;

static TempRoot()
{
Expand All @@ -20,6 +21,7 @@ static TempRoot()

public void Dispose()
{
_disposed = true;
while (_temps.TryTake(out var temp))
{
try
Expand All @@ -36,20 +38,31 @@ public void Dispose()
}
}

private void CheckDisposed()
{
if (this._disposed)
{
throw new ObjectDisposedException(nameof(TempRoot));
}
}

public TempDirectory CreateDirectory()
{
CheckDisposed();
var dir = new DisposableDirectory(this);
_temps.Add(dir);
return dir;
}

public TempFile CreateFile(string prefix = null, string extension = null, string directory = null, [CallerFilePath]string callerSourcePath = null, [CallerLineNumber]int callerLineNumber = 0)
{
CheckDisposed();
return AddFile(new DisposableFile(prefix, extension, directory, callerSourcePath, callerLineNumber));
}

public DisposableFile AddFile(DisposableFile file)
{
CheckDisposed();
_temps.Add(file);
return file;
}
Expand Down

0 comments on commit b9bc86b

Please sign in to comment.