diff --git a/src/Microsoft.DotNet.TestFramework/TestAssetInfo.cs b/src/Microsoft.DotNet.TestFramework/TestAssetInfo.cs index c8502ec63..f5ca0aebe 100644 --- a/src/Microsoft.DotNet.TestFramework/TestAssetInfo.cs +++ b/src/Microsoft.DotNet.TestFramework/TestAssetInfo.cs @@ -6,13 +6,6 @@ using System.Collections.Generic; using System.IO; using System.Linq; using System.Runtime.CompilerServices; -using System.Threading; -using System.Threading.Tasks; -using Microsoft.DotNet.Cli.Utils; -using Microsoft.DotNet.PlatformAbstractions; -using NuGet.Common; -using PathUtility = Microsoft.DotNet.Tools.Common.PathUtility; -using System.Xml.Linq; namespace Microsoft.DotNet.TestFramework { @@ -57,8 +50,6 @@ namespace Microsoft.DotNet.TestFramework DotnetExeFile = dotnetExeFile; ProjectFilePattern = projectFilePattern; - - //throw new Exception($"root = {_root}\nassetName = {_assetName}\ndotnetExeFile = {_dotnetExeFile}\nprojectFilePattern = {_projectFilePattern}\ndataDir = {_dataDirectory}\ndirectoriesToExclude = {string.Join(";",_directoriesToExclude)}"); } public TestAssetInstance CreateInstance([CallerMemberName] string callingMethod = "", string identifier = "") @@ -88,87 +79,6 @@ namespace Microsoft.DotNet.TestFramework return new DirectoryInfo(Path.Combine(baseDirectory, callingMethod + identifier, AssetName)); } - private static string RebasePath(string path, string oldBaseDirectory, string newBaseDirectory) - { - path = Path.IsPathRooted(path) ? PathUtility.GetRelativePath(PathUtility.EnsureTrailingSlash(oldBaseDirectory), path) : path; - return Path.Combine(newBaseDirectory, path); - } - - private bool IsAncestor(FileInfo file, DirectoryInfo maybeAncestor) - { - var dir = file.Directory; - do - { - if (string.Equals(maybeAncestor.FullName, dir.FullName, StringComparison.OrdinalIgnoreCase)) - { - return true; - } - - dir = dir.Parent; - } - while (dir != null); - - return false; - } - - //private void DoCopyFiles() - //{ - // Console.WriteLine($"TestAsset CopyFiles '{AssetName}'"); - - // _operationDirectory.Refresh(); - // if (!_operationDirectory.Exists) - // { - // _operationDirectory.Create(); - // } - // else - // { - // if (_operationDirectory.GetFiles().Any()) - // { - // throw new Exception("operation files folder not empty"); - // } - // } - - // foreach (var f in GetOriginalFileList()) - // { - // string destinationPath = RebasePath(f.FullName, Root.FullName, _operationDirectory.FullName); - // var destinationDir = new FileInfo(destinationPath).Directory; - // if (!destinationDir.Exists) - // { - // destinationDir.Create(); - // } - // if (string.Equals(f.Name, "nuget.config", StringComparison.OrdinalIgnoreCase)) - // { - // var doc = XDocument.Load(f.FullName, LoadOptions.PreserveWhitespace); - // foreach (var v in doc.Root.Element("packageSources").Elements("add").Attributes("value")) - // { - // if (!Path.IsPathRooted(v.Value)) - // { - // string fullPath = Path.GetFullPath(Path.Combine(f.Directory.FullName, v.Value)); - // if (!IsAncestor(new FileInfo(fullPath), Root)) - // { - // v.Value = fullPath; - // } - // } - - // //throw new Exception($"\nvalue = {v.Value}\n" + - // // $"f.dir = {f.Directory.FullName}\n" + - // // $"fullPath = {fullPath}"); - - // } - - // using (var file = new FileStream(destinationPath, FileMode.CreateNew, FileAccess.ReadWrite)) - // { - // doc.Save(file, SaveOptions.None); - // } - // } - // else - // { - // f.CopyTo(destinationPath); - // } - // } - //} - - private void ThrowIfTestAssetDoesNotExist() { if (!Root.Exists) diff --git a/src/Microsoft.DotNet.TestFramework/TestAssetInstance.cs b/src/Microsoft.DotNet.TestFramework/TestAssetInstance.cs index f86b66451..94a13bb1a 100644 --- a/src/Microsoft.DotNet.TestFramework/TestAssetInstance.cs +++ b/src/Microsoft.DotNet.TestFramework/TestAssetInstance.cs @@ -35,7 +35,7 @@ namespace Microsoft.DotNet.TestFramework { throw new ArgumentException(nameof(testAssetInfo)); } - + if (root == null) { throw new ArgumentException(nameof(root)); @@ -64,9 +64,7 @@ namespace Microsoft.DotNet.TestFramework { if (!_filesCopied) { - var filesToCopy = TestAssetInfo.GetSourceFiles(); - - CopyFiles(filesToCopy); + CopySourceFiles(); _filesCopied = true; } @@ -176,19 +174,77 @@ namespace Microsoft.DotNet.TestFramework }); } - private void CopyFiles(IEnumerable filesToCopy) + private static string RebasePath(string path, string oldBaseDirectory, string newBaseDirectory) { + path = Path.IsPathRooted(path) ? PathUtility.GetRelativePath(PathUtility.EnsureTrailingSlash(oldBaseDirectory), path) : path; + return Path.Combine(newBaseDirectory, path); + } + + private void CopySourceFiles() + { + var filesToCopy = TestAssetInfo.GetSourceFiles(); foreach (var file in filesToCopy) { - var relativePath = file.FullName.Substring(TestAssetInfo.Root.FullName.Length + 1); - - var newPath = Path.Combine(Root.FullName, relativePath); + var newPath = RebasePath(file.FullName, TestAssetInfo.Root.FullName, Root.FullName); var newFile = new FileInfo(newPath); PathUtility.EnsureDirectoryExists(newFile.Directory.FullName); - file.CopyTo(newPath); + CopyFileAdjustingPaths(file, newFile); + } + } + + private bool IsAncestor(FileInfo file, DirectoryInfo maybeAncestor) + { + var dir = file.Directory; + do + { + if (string.Equals(maybeAncestor.FullName, dir.FullName, StringComparison.OrdinalIgnoreCase)) + { + return true; + } + + dir = dir.Parent; + } + while (dir != null); + + return false; + } + + private void CopyFileAdjustingPaths(FileInfo source, FileInfo destination) + { + if (string.Equals(source.Name, "nuget.config", StringComparison.OrdinalIgnoreCase)) + { + CopyNugetConfigAdjustingPath(source, destination); + } + else + { + source.CopyTo(destination.FullName); + } + } + + private void CopyNugetConfigAdjustingPath(FileInfo source, FileInfo destination) + { + var doc = XDocument.Load(source.FullName, LoadOptions.PreserveWhitespace); + foreach (var packageSource in doc.Root.Element("packageSources").Elements("add").Attributes("value")) + { + if (!Path.IsPathRooted(packageSource.Value)) + { + string fullPathAtSource = Path.GetFullPath(Path.Combine(source.Directory.FullName, packageSource.Value)); + if (!IsAncestor(new FileInfo(fullPathAtSource), TestAssetInfo.Root)) + { + packageSource.Value = fullPathAtSource; + } + } + + using (var file = new FileStream( + destination.FullName, + FileMode.CreateNew, + FileAccess.ReadWrite)) + { + doc.Save(file, SaveOptions.None); + } } }