From 484f35691a49f8c13165a3a41f47cd0f7cb49bb2 Mon Sep 17 00:00:00 2001 From: Pranav K Date: Wed, 7 Sep 2016 11:19:56 -0700 Subject: [PATCH] Don't use DOTNET_BUILD_VERSION in code. Clean up test projects --- .../Scenario/DotNetWatchScenario.cs | 2 +- .../Scenario/ProjectToolScenario.cs | 178 ++++++++++++++++++ test/TestApps/AppWithDeps/project.json | 13 +- test/TestApps/Dependency/project.json | 10 +- test/TestApps/GlobbingApp/project.json | 9 +- test/TestApps/NoDepsApp/project.json | 9 +- 6 files changed, 190 insertions(+), 31 deletions(-) create mode 100644 test/Microsoft.DotNet.Watcher.Tools.FunctionalTests/Scenario/ProjectToolScenario.cs diff --git a/test/Microsoft.DotNet.Watcher.Tools.FunctionalTests/Scenario/DotNetWatchScenario.cs b/test/Microsoft.DotNet.Watcher.Tools.FunctionalTests/Scenario/DotNetWatchScenario.cs index 5b6799fab4..4af46b1171 100644 --- a/test/Microsoft.DotNet.Watcher.Tools.FunctionalTests/Scenario/DotNetWatchScenario.cs +++ b/test/Microsoft.DotNet.Watcher.Tools.FunctionalTests/Scenario/DotNetWatchScenario.cs @@ -5,7 +5,6 @@ using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; -using Microsoft.AspNetCore.Testing.Functional; using Microsoft.Extensions.Internal; namespace Microsoft.DotNet.Watcher.Tools.FunctionalTests @@ -22,6 +21,7 @@ namespace Microsoft.DotNet.Watcher.Tools.FunctionalTests public DotNetWatchScenario() { _scenario = new ProjectToolScenario(); + Directory.CreateDirectory(_artifactsFolder); _scenario.AddNugetFeed(DotnetWatch, _artifactsFolder); } diff --git a/test/Microsoft.DotNet.Watcher.Tools.FunctionalTests/Scenario/ProjectToolScenario.cs b/test/Microsoft.DotNet.Watcher.Tools.FunctionalTests/Scenario/ProjectToolScenario.cs new file mode 100644 index 0000000000..48af21836e --- /dev/null +++ b/test/Microsoft.DotNet.Watcher.Tools.FunctionalTests/Scenario/ProjectToolScenario.cs @@ -0,0 +1,178 @@ +// 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.Collections.Generic; +using System.Diagnostics; +using System.IO; +using System.Threading; +using System.Xml.Linq; +using Newtonsoft.Json.Linq; + +namespace Microsoft.DotNet.Watcher.Tools.FunctionalTests +{ + public class ProjectToolScenario : IDisposable + { + private const string NugetConfigFileName = "NuGet.config"; + + private static readonly object _restoreLock = new object(); + + public ProjectToolScenario() + { + Console.WriteLine($"The temporary test folder is {TempFolder}"); + + WorkFolder = Path.Combine(TempFolder, "work"); + + CreateTestDirectory(); + } + + public string TempFolder { get; } = Path.Combine(Path.GetDirectoryName(FindNugetConfig()), "testWorkDir", Guid.NewGuid().ToString()); + + public string WorkFolder { get; } + + public void AddProject(string projectFolder) + { + var destinationFolder = Path.Combine(WorkFolder, Path.GetFileName(projectFolder)); + Console.WriteLine($"Copying project {projectFolder} to {destinationFolder}"); + + Directory.CreateDirectory(destinationFolder); + + foreach (var directory in Directory.GetDirectories(projectFolder, "*", SearchOption.AllDirectories)) + { + Directory.CreateDirectory(directory.Replace(projectFolder, destinationFolder)); + } + + foreach (var file in Directory.GetFiles(projectFolder, "*.*", SearchOption.AllDirectories)) + { + File.Copy(file, file.Replace(projectFolder, destinationFolder), true); + } + } + + public void AddNugetFeed(string feedName, string feed) + { + var tempNugetConfigFile = Path.Combine(WorkFolder, NugetConfigFileName); + + var nugetConfig = XDocument.Load(tempNugetConfigFile); + var packageSource = nugetConfig.Element("configuration").Element("packageSources"); + packageSource.Add(new XElement("add", new XAttribute("key", feedName), new XAttribute("value", feed))); + using (var stream = File.OpenWrite(tempNugetConfigFile)) + { + nugetConfig.Save(stream); + } + } + + public void AddToolToProject(string projectName, string toolName) + { + var projectFile = Path.Combine(WorkFolder, projectName, "project.json"); + Console.WriteLine($"Adding {toolName} to {projectFile}"); + + var projectJson = JObject.Parse(File.ReadAllText(projectFile)); + projectJson.Add("tools", + new JObject( + new JProperty(toolName, + new JObject( + new JProperty("version", "1.0.0-*"), + new JProperty("target", "package"))))); + + File.WriteAllText(projectFile, projectJson.ToString()); + } + + public void Restore(string project = null) + { + if (project == null) + { + project = WorkFolder; + } + else + { + project = Path.Combine(WorkFolder, project); + } + + // Tests are run in parallel and they try to restore tools concurrently. + // This causes issues because the deps json file for a tool is being written from + // multiple threads - which results in either sharing violation or corrupted json. + lock (_restoreLock) + { + var restore = ExecuteDotnet($"restore -v Minimal", project); + restore.WaitForExit(); + + if (restore.ExitCode != 0) + { + throw new Exception($"Exit code {restore.ExitCode}"); + } + } + } + + private void CreateTestDirectory() + { + Directory.CreateDirectory(WorkFolder); + File.WriteAllText(Path.Combine(WorkFolder, "global.json"), "{}"); + + var nugetConfigFilePath = FindNugetConfig(); + + var tempNugetConfigFile = Path.Combine(WorkFolder, NugetConfigFileName); + File.Copy(nugetConfigFilePath, tempNugetConfigFile); + } + + public Process ExecuteDotnet(string arguments, string workDir, IDictionary environmentVariables = null) + { + Console.WriteLine($"Running dotnet {arguments} in {workDir}"); + + var psi = new ProcessStartInfo("dotnet", arguments) + { + UseShellExecute = false, + WorkingDirectory = workDir, + }; + + if (environmentVariables != null) + { + foreach (var newEnvVar in environmentVariables) + { + var varKey = newEnvVar.Key; + var varValue = newEnvVar.Value; +#if NET451 + psi.EnvironmentVariables[varKey] = varValue; + +#else + psi.Environment[varKey] = varValue; +#endif + } + } + + return Process.Start(psi); + } + + private static string FindNugetConfig() + { + var currentDirPath = Directory.GetCurrentDirectory(); + + string nugetConfigFile; + while (true) + { + nugetConfigFile = Path.Combine(currentDirPath, NugetConfigFileName); + if (File.Exists(nugetConfigFile)) + { + break; + } + + currentDirPath = Path.GetDirectoryName(currentDirPath); + } + + return nugetConfigFile; + } + + public void Dispose() + { + try + { + Directory.Delete(TempFolder, recursive: true); + } + catch + { + Console.WriteLine($"Failed to delete {TempFolder}. Retrying..."); + Thread.Sleep(TimeSpan.FromSeconds(5)); + Directory.Delete(TempFolder, recursive: true); + } + } + } +} \ No newline at end of file diff --git a/test/TestApps/AppWithDeps/project.json b/test/TestApps/AppWithDeps/project.json index 8fbde9fc51..d197767c64 100644 --- a/test/TestApps/AppWithDeps/project.json +++ b/test/TestApps/AppWithDeps/project.json @@ -1,23 +1,20 @@ { - "version": "1.0.0-*", "buildOptions": { "emitEntryPoint": true }, "dependencies": { - "Dependency": "1.0.0" + "Dependency": { + "target": "project" + } }, "frameworks": { "netcoreapp1.0": { "dependencies": { "Microsoft.NETCore.App": { "type": "platform", - "version": "1.0.0-*" + "version": "1.0.0" } - }, - "imports": [ - "dnxcore50", - "portable-net451+win8" - ] + } } } } \ No newline at end of file diff --git a/test/TestApps/Dependency/project.json b/test/TestApps/Dependency/project.json index d8ac93175b..49853f2561 100644 --- a/test/TestApps/Dependency/project.json +++ b/test/TestApps/Dependency/project.json @@ -1,14 +1,8 @@ { - "version": "1.0.0", "dependencies": { - "NETStandard.Library": "1.5.0-*" + "NETStandard.Library": "1.6.0" }, "frameworks": { - "netstandard1.5": { - "imports": [ - "dnxcore50", - "portable-net451+win8" - ] - } + "netstandard1.5": {} } } \ No newline at end of file diff --git a/test/TestApps/GlobbingApp/project.json b/test/TestApps/GlobbingApp/project.json index 68104394d3..99e2421e43 100644 --- a/test/TestApps/GlobbingApp/project.json +++ b/test/TestApps/GlobbingApp/project.json @@ -1,5 +1,4 @@ { - "version": "1.0.0-*", "buildOptions": { "emitEntryPoint": true, "compile": { @@ -17,13 +16,9 @@ "dependencies": { "Microsoft.NETCore.App": { "type": "platform", - "version": "1.0.0-*" + "version": "1.0.0" } - }, - "imports": [ - "dnxcore50", - "portable-net451+win8" - ] + } } } } \ No newline at end of file diff --git a/test/TestApps/NoDepsApp/project.json b/test/TestApps/NoDepsApp/project.json index f0eb960bb6..327fafbef0 100644 --- a/test/TestApps/NoDepsApp/project.json +++ b/test/TestApps/NoDepsApp/project.json @@ -1,5 +1,4 @@ { - "version": "1.0.0-*", "buildOptions": { "emitEntryPoint": true }, @@ -8,13 +7,9 @@ "dependencies": { "Microsoft.NETCore.App": { "type": "platform", - "version": "1.0.0-*" + "version": "1.0.0" } - }, - "imports": [ - "dnxcore50", - "portable-net451+win8" - ] + } } } } \ No newline at end of file