mirror of
https://github.com/aphrody-labs/NuGet.Client.git
synced 2026-10-14 13:29:05 +00:00
* Consolidate Restore/Build/Pack methods in Tests Test Fixture now has similar API surface across Restore/Build/Pack commands * Fix 'dotnet pack' tests when testing under VS IDE The NuGet Pack artifacts aren't being copied to the test SDK layout, when building/testing under VS IDE locally and not on CI/CLI. * Rename 'OutputPath' to 'ProjectExtensionsPath' in 'SimpleTestProjectContext' The 'OutputPath' property under 'SimpleTestProjectContext' is an alias of 'MSBuildProjectExtensionsPath', but there is an 'OutputPath' property already in MSBuild project system. So, Rename 'OutputPath' to 'ProjectExtensionsPath' to better reflect it's intent. * Refactor 'CreateDotnetToolProject' method & calls in 'DotNetToolTests' Rename 'source' param to 'packageSources' Make 'packageSources' param to accept null Fix template's 'MSBuildProjectExtensionsPath' with explicit Sdk import Remove redundant parameter labels on 'CreateDotnetToolProject' in 'DotNetToolTests' * Miscellaneous refactoring and whitespace fixes cosmetic refactors that are leftover from this patch tree * Trim trailing whitespaces Only files in this patch have been trimmed for trailing whitespace
104 lines
3.2 KiB
C#
104 lines
3.2 KiB
C#
// Copyright (c) .NET Foundation. All rights reserved.
|
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
|
|
|
using System;
|
|
using System.IO;
|
|
using PathIO = System.IO.Path;
|
|
|
|
namespace NuGet.Test.Utility
|
|
{
|
|
/// <summary>
|
|
/// Temporary new folder under .test/work/
|
|
/// </summary>
|
|
public class TestDirectory : IDisposable
|
|
{
|
|
private readonly string _parentPath;
|
|
|
|
private TestDirectory(string path, string parentPath)
|
|
{
|
|
Path = path;
|
|
_parentPath = parentPath;
|
|
Info = new DirectoryInfo(path);
|
|
}
|
|
|
|
public string Path { get; }
|
|
|
|
public DirectoryInfo Info { get; }
|
|
|
|
public void Dispose()
|
|
{
|
|
// Try to delete the sub folder first, avoid removing
|
|
// the parent folder containing extra test info
|
|
// if the sub folder cannot be removed.
|
|
if (TestFileSystemUtility.DeleteRandomTestFolder(Path))
|
|
{
|
|
TestFileSystemUtility.DeleteRandomTestFolder(_parentPath);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Create a temp folder under .test/work/
|
|
/// </summary>
|
|
public static TestDirectory Create()
|
|
{
|
|
var root = TestFileSystemUtility.NuGetTestFolder;
|
|
return Create(root);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Create a temp folder under %TEMP%
|
|
/// </summary>
|
|
public static TestDirectory CreateInTemp()
|
|
{
|
|
var root = PathIO.Combine(PathIO.GetTempPath(), "NuGetTestFolder");
|
|
Directory.CreateDirectory(root);
|
|
|
|
return Create(root);
|
|
}
|
|
|
|
public static TestDirectory Create(string root)
|
|
{
|
|
string parentPath;
|
|
|
|
// Loop until we find a directory that isn't taken (extremely unlikely this would need multiple guids).
|
|
while (true)
|
|
{
|
|
var guid = Guid.NewGuid().ToString();
|
|
|
|
// Use a shorter path to this easier when debugging
|
|
parentPath = PathIO.Combine(root, guid.Split('-')[0]);
|
|
|
|
if (Directory.Exists(parentPath))
|
|
{
|
|
// If a collision happens use the full guid
|
|
parentPath = PathIO.Combine(root, guid);
|
|
|
|
if (!Directory.Exists(parentPath))
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
|
|
Directory.CreateDirectory(parentPath);
|
|
|
|
// Record what created this folder in case there is a problem with clean up.
|
|
File.WriteAllText(PathIO.Combine(parentPath, "testStack.txt"), Environment.StackTrace);
|
|
|
|
// Create a random sub folder to use, this keeps tests from relying on the folder name.
|
|
var path = PathIO.Combine(parentPath, Guid.NewGuid().ToString().Split('-')[0]);
|
|
Directory.CreateDirectory(path);
|
|
|
|
return new TestDirectory(path, parentPath);
|
|
}
|
|
|
|
public static implicit operator string(TestDirectory directory) => directory.Path;
|
|
|
|
public override string ToString() => Path;
|
|
}
|
|
}
|