dotnet-installer/test/Microsoft.DotNet.Tools.Tests.Utilities/TempFileSystem/TempRoot.cs

82 lines
2.3 KiB
C#
Raw Normal View History

2015-12-15 01:39:29 +00:00
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.CompilerServices;
namespace Microsoft.DotNet.Tools.Test.Utilities
{
public sealed class TempRoot : IDisposable
{
private static readonly bool DoDispose;
2015-12-15 01:39:29 +00:00
private readonly List<IDisposable> _temps = new List<IDisposable>();
public static readonly string Root;
static TempRoot()
{
var persistedRoot = Environment.GetEnvironmentVariable("TEST_ARTIFACTS");
if (string.IsNullOrWhiteSpace(persistedRoot))
{
Root = Path.Combine(Path.GetTempPath(), "DotnetCLITests");
DoDispose = true;
}
else
{
Root = persistedRoot;
DoDispose = false;
}
2015-12-15 01:39:29 +00:00
Directory.CreateDirectory(Root);
}
public void Dispose()
{
if (!DoDispose || _temps == null) return;
DisposeAll(_temps);
_temps.Clear();
2015-12-15 01:39:29 +00:00
}
private static void DisposeAll(IEnumerable<IDisposable> temps)
{
foreach (var temp in temps)
{
try
{
temp?.Dispose();
2015-12-15 01:39:29 +00:00
}
catch
{
// ignore
}
}
}
2016-07-21 20:04:05 +00:00
public DisposableDirectory CreateDirectory()
2015-12-15 01:39:29 +00:00
{
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)
{
return AddFile(new DisposableFile(prefix, extension, directory, callerSourcePath, callerLineNumber));
}
public DisposableFile AddFile(DisposableFile file)
{
_temps.Add(file);
return file;
}
internal static void CreateStream(string fullPath)
{
using (var file = new FileStream(fullPath, FileMode.CreateNew)) { }
}
}
}