2016-10-28 01:46:43 +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.Linq;
|
|
|
|
|
using System.Runtime.CompilerServices;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Microsoft.DotNet.Cli.Utils;
|
|
|
|
|
using Microsoft.DotNet.PlatformAbstractions;
|
2016-11-19 03:28:38 +00:00
|
|
|
|
using NuGet.Common;
|
2016-10-28 01:46:43 +00:00
|
|
|
|
|
|
|
|
|
namespace Microsoft.DotNet.TestFramework
|
|
|
|
|
{
|
|
|
|
|
public class TestAssetInfo
|
|
|
|
|
{
|
|
|
|
|
private const string DataDirectoryName = ".tam";
|
|
|
|
|
|
|
|
|
|
private readonly string [] FilesToExclude = { ".DS_Store", ".noautobuild" };
|
|
|
|
|
|
|
|
|
|
private readonly DirectoryInfo [] _directoriesToExclude;
|
|
|
|
|
|
|
|
|
|
private readonly string _assetName;
|
|
|
|
|
|
|
|
|
|
private readonly DirectoryInfo _dataDirectory;
|
|
|
|
|
|
|
|
|
|
private readonly DirectoryInfo _root;
|
|
|
|
|
|
|
|
|
|
private readonly TestAssetInventoryFiles _inventoryFiles;
|
|
|
|
|
|
|
|
|
|
internal DirectoryInfo Root
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return _root;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal TestAssetInfo(DirectoryInfo root, string assetName)
|
|
|
|
|
{
|
|
|
|
|
if (!root.Exists)
|
|
|
|
|
{
|
|
|
|
|
throw new DirectoryNotFoundException($"Directory not found at '{root}'");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_assetName = assetName;
|
|
|
|
|
|
|
|
|
|
_root = root;
|
|
|
|
|
|
|
|
|
|
_dataDirectory = new DirectoryInfo(Path.Combine(_root.FullName, DataDirectoryName));
|
|
|
|
|
|
|
|
|
|
_inventoryFiles = new TestAssetInventoryFiles(_dataDirectory);
|
|
|
|
|
|
|
|
|
|
_directoriesToExclude = new []
|
|
|
|
|
{
|
|
|
|
|
_dataDirectory
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (!_dataDirectory.Exists)
|
|
|
|
|
{
|
|
|
|
|
_dataDirectory.Create();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public TestAssetInstance CreateInstance([CallerMemberName] string callingMethod = "", string identifier = "")
|
|
|
|
|
{
|
|
|
|
|
var instancePath = GetTestDestinationDirectoryPath(callingMethod, identifier);
|
|
|
|
|
|
|
|
|
|
var testInstance = new TestAssetInstance(this, new DirectoryInfo(instancePath));
|
|
|
|
|
|
|
|
|
|
return testInstance;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string GetTestDestinationDirectoryPath(string callingMethod, string identifier)
|
|
|
|
|
{
|
|
|
|
|
#if NET451
|
|
|
|
|
string baseDirectory = AppDomain.CurrentDomain.BaseDirectory;
|
|
|
|
|
#else
|
|
|
|
|
string baseDirectory = AppContext.BaseDirectory;
|
|
|
|
|
#endif
|
|
|
|
|
return Path.Combine(baseDirectory, callingMethod + identifier, _assetName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private List<FileInfo> LoadInventory(FileInfo file)
|
|
|
|
|
{
|
|
|
|
|
if (!file.Exists)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var inventory = new List<FileInfo>();
|
|
|
|
|
|
|
|
|
|
var lines = file.OpenText();
|
|
|
|
|
|
|
|
|
|
while (lines.Peek() > 0)
|
|
|
|
|
{
|
|
|
|
|
inventory.Add(new FileInfo(lines.ReadLine()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return inventory;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void SaveInventory(FileInfo file, IEnumerable<FileInfo> inventory)
|
|
|
|
|
{
|
2016-11-19 03:28:38 +00:00
|
|
|
|
FileUtility.ReplaceWithLock(
|
|
|
|
|
filePath =>
|
2016-10-28 01:46:43 +00:00
|
|
|
|
{
|
2016-11-19 03:28:38 +00:00
|
|
|
|
using (var stream =
|
|
|
|
|
new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None))
|
|
|
|
|
{
|
|
|
|
|
using (var writer = new StreamWriter(stream))
|
|
|
|
|
{
|
|
|
|
|
foreach (var path in inventory.Select(i => i.FullName))
|
|
|
|
|
{
|
|
|
|
|
writer.WriteLine(path);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
file.FullName);
|
2016-10-28 01:46:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private IEnumerable<FileInfo> GetFileList()
|
|
|
|
|
{
|
|
|
|
|
return _root.GetFiles("*.*", SearchOption.AllDirectories)
|
|
|
|
|
.Where(f => !_directoriesToExclude.Any(d => d.Contains(f)))
|
|
|
|
|
.Where(f => !FilesToExclude.Contains(f.Name));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal IEnumerable<FileInfo> GetSourceFiles()
|
|
|
|
|
{
|
|
|
|
|
return GetInventory(_inventoryFiles.Source, null, () => {});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal IEnumerable<FileInfo> GetRestoreFiles()
|
|
|
|
|
{
|
|
|
|
|
return GetInventory(_inventoryFiles.Restore, GetSourceFiles, DoRestore);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal IEnumerable<FileInfo> GetBuildFiles()
|
|
|
|
|
{
|
2016-11-19 03:28:38 +00:00
|
|
|
|
return GetInventory(
|
|
|
|
|
_inventoryFiles.Build,
|
|
|
|
|
() =>
|
|
|
|
|
{
|
|
|
|
|
var preInventory = new List<FileInfo>(GetRestoreFiles());
|
|
|
|
|
preInventory.AddRange(GetSourceFiles());
|
|
|
|
|
return preInventory;
|
|
|
|
|
},
|
|
|
|
|
DoBuild);
|
2016-10-28 01:46:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-11-19 03:28:38 +00:00
|
|
|
|
private IEnumerable<FileInfo> GetInventory(
|
|
|
|
|
FileInfo file,
|
|
|
|
|
Func<IEnumerable<FileInfo>> beforeAction,
|
|
|
|
|
Action action)
|
2016-10-28 01:46:43 +00:00
|
|
|
|
{
|
2016-11-19 03:28:38 +00:00
|
|
|
|
var inventory = Enumerable.Empty<FileInfo>();
|
2016-10-28 01:46:43 +00:00
|
|
|
|
if (file.Exists)
|
|
|
|
|
{
|
2016-11-19 03:28:38 +00:00
|
|
|
|
inventory = LoadInventory(file);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(inventory.Any())
|
|
|
|
|
{
|
|
|
|
|
return inventory;
|
2016-10-28 01:46:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
IEnumerable<FileInfo> preInventory;
|
|
|
|
|
|
|
|
|
|
if (beforeAction == null)
|
|
|
|
|
{
|
|
|
|
|
preInventory = new List<FileInfo>();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2016-11-19 03:28:38 +00:00
|
|
|
|
preInventory = beforeAction();
|
2016-10-28 01:46:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
action();
|
|
|
|
|
|
2016-11-19 03:28:38 +00:00
|
|
|
|
inventory = GetFileList().Where(i => !preInventory.Select(p => p.FullName).Contains(i.FullName));
|
2016-10-28 01:46:43 +00:00
|
|
|
|
|
|
|
|
|
SaveInventory(file, inventory);
|
|
|
|
|
|
|
|
|
|
return inventory;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void DoRestore()
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine($"TestAsset Restore '{_assetName}'");
|
|
|
|
|
|
|
|
|
|
var projFiles = _root.GetFiles("*.csproj", SearchOption.AllDirectories);
|
|
|
|
|
|
|
|
|
|
foreach (var projFile in projFiles)
|
|
|
|
|
{
|
|
|
|
|
var restoreArgs = new string[] { "restore", projFile.FullName, "/p:SkipInvalidConfigurations=true" };
|
|
|
|
|
|
|
|
|
|
var commandResult = Command.Create(new PathCommandResolverPolicy(), "dotnet", restoreArgs)
|
|
|
|
|
.CaptureStdOut()
|
|
|
|
|
.CaptureStdErr()
|
|
|
|
|
.Execute();
|
|
|
|
|
|
|
|
|
|
int exitCode = commandResult.ExitCode;
|
|
|
|
|
|
|
|
|
|
if (exitCode != 0)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine(commandResult.StdOut);
|
|
|
|
|
|
|
|
|
|
Console.WriteLine(commandResult.StdErr);
|
|
|
|
|
|
|
|
|
|
string message = string.Format($"TestAsset Restore '{_assetName}'@'{projFile.FullName}' Failed with {exitCode}");
|
|
|
|
|
|
|
|
|
|
throw new Exception(message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void DoBuild()
|
|
|
|
|
{
|
|
|
|
|
string[] args = new string[] { "build" };
|
|
|
|
|
|
|
|
|
|
Console.WriteLine($"TestAsset Build '{_assetName}'");
|
|
|
|
|
|
|
|
|
|
var commandResult = Command.Create(new PathCommandResolverPolicy(), "dotnet", args)
|
|
|
|
|
.WorkingDirectory(_root.FullName)
|
|
|
|
|
.CaptureStdOut()
|
|
|
|
|
.CaptureStdErr()
|
|
|
|
|
.Execute();
|
|
|
|
|
|
|
|
|
|
int exitCode = commandResult.ExitCode;
|
|
|
|
|
|
|
|
|
|
if (exitCode != 0)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine(commandResult.StdOut);
|
2016-11-09 07:23:13 +00:00
|
|
|
|
|
2016-10-28 01:46:43 +00:00
|
|
|
|
Console.WriteLine(commandResult.StdErr);
|
2016-11-09 07:23:13 +00:00
|
|
|
|
|
2016-10-28 01:46:43 +00:00
|
|
|
|
string message = string.Format($"TestAsset Build '{_assetName}' Failed with {exitCode}");
|
2016-11-09 07:23:13 +00:00
|
|
|
|
|
2016-10-28 01:46:43 +00:00
|
|
|
|
throw new Exception(message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|