Files

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

220 lines
7.8 KiB
C#
Raw Permalink Normal View History

2017-12-18 16:14:47 -08:00
// Copyright (c) .NET Foundation. All rights reserved.
2016-05-24 17:40:24 -07:00
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
#nullable disable
2016-05-24 17:40:24 -07:00
using System;
2015-05-28 16:18:11 -07:00
using System.IO;
namespace NuGet.Test.Utility
{
2016-05-24 17:40:24 -07:00
public static class TestFileSystemUtility
2015-05-28 16:18:11 -07:00
{
private static readonly Lazy<string> RootDirectoryLazy = new Lazy<string>(() => GetRootDirectory());
private static readonly Lazy<bool> SkipCleanupLazy = new Lazy<bool>(() => SkipCleanUp());
private const string DotnetCliBinary = "dotnet";
private const string DotnetCliExe = "dotnet.exe";
2015-05-28 16:18:11 -07:00
2017-12-18 16:14:47 -08:00
/// <summary>
/// Root test folder where temporary test outputs should go.
/// </summary>
public static string NuGetTestFolder => RootDirectoryLazy.Value;
2017-12-18 16:14:47 -08:00
private static bool SkipCleanUp()
2015-05-28 16:18:11 -07:00
{
2017-12-18 16:14:47 -08:00
// Option to leave files around for debugging
var val = Environment.GetEnvironmentVariable("NUGET_PERSIST_TESTFOLDERS");
if (StringComparer.OrdinalIgnoreCase.Equals(val, "true"))
{
return true;
}
return false;
}
private static string GetRootDirectory()
{
string repoRoot = GetRepositoryRoot();
2017-12-18 16:14:47 -08:00
DirectoryInfo root;
2017-12-18 16:14:47 -08:00
if (repoRoot == null)
2017-12-18 16:14:47 -08:00
{
// Use a folder under %TEMP% if the repository root can't be determined
root = Directory.CreateDirectory(Path.Combine(Path.GetTempPath(), "NuGetTestFolder"));
}
else
{
DirectoryInfo testDirectory = Directory.CreateDirectory(Path.Combine(repoRoot, ".test"));
try
{
File.WriteAllText(Path.Combine(testDirectory.FullName, "Directory.Build.props"), "<Project />");
File.WriteAllText(Path.Combine(testDirectory.FullName, "Directory.Build.targets"), "<Project />");
File.WriteAllText(Path.Combine(testDirectory.FullName, "Directory.Solution.props"), "<Project />");
File.WriteAllText(Path.Combine(testDirectory.FullName, "Directory.Solution.targets"), "<Project />");
File.WriteAllText(Path.Combine(testDirectory.FullName, "Directory.Build.rsp"), string.Empty);
File.WriteAllText(Path.Combine(testDirectory.FullName, "Directory.Packages.props"), "<Project />");
}
catch (Exception)
{
// Ignored
}
root = testDirectory.CreateSubdirectory("work");
2017-12-18 16:14:47 -08:00
}
return root.FullName;
2017-12-18 16:14:47 -08:00
}
private static string GetRepositoryRoot()
{
// First, check if an override is specified
string testWorkPathOverride = Environment.GetEnvironmentVariable("NUGET_TEST_WORK_PATH");
if (!string.IsNullOrEmpty(testWorkPathOverride))
{
// Override if set
return new DirectoryInfo(testWorkPathOverride).FullName;
}
// Second, check next to this assembly
2024-06-12 04:02:04 +10:00
var assemblyFileInfo = new FileInfo(typeof(TestFileSystemUtility).Assembly.Location);
DirectoryInfo repoRoot = GetRepositoryRootFromStartingDirectory(assemblyFileInfo.Directory);
if (repoRoot != null)
{
return repoRoot.FullName;
}
// Finally, try walking up from the current directory since the test assembly is sometimes
// placed in a temp location due to shadow copy functionality in test runners like xunit
repoRoot = GetRepositoryRootFromStartingDirectory(new DirectoryInfo(Directory.GetCurrentDirectory()));
return repoRoot?.FullName;
}
/// <summary>
/// Walks up the specified directory looking for NuGet.sln.
/// </summary>
/// <param name="startingDirectory">The <see cref="DirectoryInfo" /> to use as a starting directory.</param>
2023-09-06 09:26:29 -07:00
/// <returns>A <see cref="DirectoryInfo" /> representing the root of the respository if one is found, otherwise <see langword="null" />.</returns>
private static DirectoryInfo GetRepositoryRootFromStartingDirectory(DirectoryInfo startingDirectory)
{
DirectoryInfo currentDir = startingDirectory;
2017-12-18 16:14:47 -08:00
while (currentDir != null)
{
string candidatePath = Path.Combine(currentDir.FullName, "NuGet.sln");
if (File.Exists(candidatePath))
2017-12-18 16:14:47 -08:00
{
// We have found the repo root.
return currentDir;
2017-12-18 16:14:47 -08:00
}
currentDir = currentDir.Parent;
}
return null;
2017-12-18 16:14:47 -08:00
}
public static string GetDotnetCli()
{
DirectoryInfo dir = GetDirectoryOfPathAbove(Path.Combine(".test", "dotnet"));
if (dir != null)
{
var dotnetCli = Path.Combine(dir.FullName, DotnetCliExe);
if (File.Exists(dotnetCli))
{
return dotnetCli;
}
dotnetCli = Path.Combine(dir.FullName, DotnetCliBinary);
if (File.Exists(dotnetCli))
{
return dotnetCli;
}
}
if (dir == null)
{
var configureScript = NuGet.Common.RuntimeEnvironmentHelper.IsWindows ? "configure.ps1" : "configure.sh";
throw new Exception($"Failed to determine the path to the .NET SDK. Run {configureScript} to set up the test environment.");
}
return null;
}
public static string GetArtifactsDirectoryInRepo()
{
return GetDirectoryOfPathAbove("artifacts")?.FullName;
}
public static string GetNuGetExeDirectoryInRepo()
{
var ArtifactDir = GetArtifactsDirectoryInRepo();
return Path.Combine(ArtifactDir, "VS15");
}
2017-12-18 16:14:47 -08:00
public static bool DeleteRandomTestFolder(string randomTestPath)
{
2024-06-12 04:02:04 +10:00
// Avoid cleaning up test folders if
if (!SkipCleanupLazy.Value && Directory.Exists(randomTestPath))
2015-05-28 16:18:11 -07:00
{
AssertNotTempPath(randomTestPath);
try
2015-05-28 16:18:11 -07:00
{
Directory.Delete(randomTestPath, recursive: true);
}
catch
{
2017-12-18 16:14:47 -08:00
// Failure
return false;
}
2015-05-28 16:18:11 -07:00
}
2017-12-18 16:14:47 -08:00
// Success or skipped
return true;
}
public static void AssertNotTempPath(string path)
{
var expanded = Path.GetFullPath(path).TrimEnd(new char[] { '\\', '/' });
var expandedTempPath = Path.GetFullPath(Path.GetTempPath()).TrimEnd(new char[] { '\\', '/' });
if (expanded.Equals(expandedTempPath, StringComparison.OrdinalIgnoreCase))
2015-05-28 16:18:11 -07:00
{
throw new InvalidOperationException("Trying to delete the temp folder in a test");
}
if (expanded.Equals(Path.GetFullPath(NuGetTestFolder), StringComparison.OrdinalIgnoreCase))
{
throw new InvalidOperationException("Trying to delete the root test folder in a test");
2015-05-28 16:18:11 -07:00
}
}
public static DirectoryInfo GetDirectoryOfPathAbove(string relativePath)
{
var currentDirInfo = new DirectoryInfo(Directory.GetCurrentDirectory());
while (currentDirInfo != null)
{
DirectoryInfo candidateDir = new DirectoryInfo(Path.Combine(currentDirInfo.FullName, relativePath));
if (candidateDir.Exists)
{
return candidateDir;
}
currentDirInfo = currentDirInfo.Parent;
}
return null;
}
2015-05-28 16:18:11 -07:00
}
}