From 6ca22e4a56a2f2d49b498300f6132eca5784bfaa Mon Sep 17 00:00:00 2001 From: Pavel Krymets Date: Fri, 19 Feb 2016 15:12:04 -0800 Subject: [PATCH] Fix run behaviour when only single target exists --- .../TestProjects/CompileFail/.noautobuild | 1 + .../RunTestsApps/TestAppFullClr/.noautobuild | 1 + .../RunTestsApps/TestAppFullClr/Program.cs | 12 +++++ .../RunTestsApps/TestAppFullClr/project.json | 10 ++++ .../TestAppMultiTarget/.noautobuild | 1 + .../TestAppMultiTarget/Program.cs | 16 ++++++ .../TestAppMultiTarget/project.json | 15 ++++++ .../TestAppMultiTargetNoCoreClr/.noautobuild | 1 + .../TestAppMultiTargetNoCoreClr/Program.cs | 16 ++++++ .../TestAppMultiTargetNoCoreClr/project.json | 11 ++++ scripts/dotnet-cli-build/TestTargets.cs | 4 +- src/dotnet/commands/dotnet-run/RunCommand.cs | 20 +++++-- test/dotnet-run.Tests/RunTests.cs | 54 +++++++++++++++++++ test/dotnet-run.Tests/dotnet-run.Tests.xproj | 19 +++++++ test/dotnet-run.Tests/project.json | 23 ++++++++ 15 files changed, 197 insertions(+), 7 deletions(-) create mode 100644 TestAssets/TestProjects/CompileFail/.noautobuild create mode 100644 TestAssets/TestProjects/RunTestsApps/TestAppFullClr/.noautobuild create mode 100644 TestAssets/TestProjects/RunTestsApps/TestAppFullClr/Program.cs create mode 100644 TestAssets/TestProjects/RunTestsApps/TestAppFullClr/project.json create mode 100644 TestAssets/TestProjects/RunTestsApps/TestAppMultiTarget/.noautobuild create mode 100644 TestAssets/TestProjects/RunTestsApps/TestAppMultiTarget/Program.cs create mode 100644 TestAssets/TestProjects/RunTestsApps/TestAppMultiTarget/project.json create mode 100644 TestAssets/TestProjects/RunTestsApps/TestAppMultiTargetNoCoreClr/.noautobuild create mode 100644 TestAssets/TestProjects/RunTestsApps/TestAppMultiTargetNoCoreClr/Program.cs create mode 100644 TestAssets/TestProjects/RunTestsApps/TestAppMultiTargetNoCoreClr/project.json create mode 100644 test/dotnet-run.Tests/RunTests.cs create mode 100644 test/dotnet-run.Tests/dotnet-run.Tests.xproj create mode 100644 test/dotnet-run.Tests/project.json diff --git a/TestAssets/TestProjects/CompileFail/.noautobuild b/TestAssets/TestProjects/CompileFail/.noautobuild new file mode 100644 index 000000000..8f7edc4ac --- /dev/null +++ b/TestAssets/TestProjects/CompileFail/.noautobuild @@ -0,0 +1 @@ +noautobuild \ No newline at end of file diff --git a/TestAssets/TestProjects/RunTestsApps/TestAppFullClr/.noautobuild b/TestAssets/TestProjects/RunTestsApps/TestAppFullClr/.noautobuild new file mode 100644 index 000000000..8f7edc4ac --- /dev/null +++ b/TestAssets/TestProjects/RunTestsApps/TestAppFullClr/.noautobuild @@ -0,0 +1 @@ +noautobuild \ No newline at end of file diff --git a/TestAssets/TestProjects/RunTestsApps/TestAppFullClr/Program.cs b/TestAssets/TestProjects/RunTestsApps/TestAppFullClr/Program.cs new file mode 100644 index 000000000..6c7b146ea --- /dev/null +++ b/TestAssets/TestProjects/RunTestsApps/TestAppFullClr/Program.cs @@ -0,0 +1,12 @@ +using System; + +namespace ConsoleApplication +{ + public class Program + { + public static void Main(string[] args) + { + .WriteLine("NET451"); + } + } +} diff --git a/TestAssets/TestProjects/RunTestsApps/TestAppFullClr/project.json b/TestAssets/TestProjects/RunTestsApps/TestAppFullClr/project.json new file mode 100644 index 000000000..e5a8b1229 --- /dev/null +++ b/TestAssets/TestProjects/RunTestsApps/TestAppFullClr/project.json @@ -0,0 +1,10 @@ +{ + "version": "1.0.0-*", + "compilationOptions": { + "emitEntryPoint": true + }, + + "frameworks": { + "net451": { } + } +} diff --git a/TestAssets/TestProjects/RunTestsApps/TestAppMultiTarget/.noautobuild b/TestAssets/TestProjects/RunTestsApps/TestAppMultiTarget/.noautobuild new file mode 100644 index 000000000..8f7edc4ac --- /dev/null +++ b/TestAssets/TestProjects/RunTestsApps/TestAppMultiTarget/.noautobuild @@ -0,0 +1 @@ +noautobuild \ No newline at end of file diff --git a/TestAssets/TestProjects/RunTestsApps/TestAppMultiTarget/Program.cs b/TestAssets/TestProjects/RunTestsApps/TestAppMultiTarget/Program.cs new file mode 100644 index 000000000..1a6535b48 --- /dev/null +++ b/TestAssets/TestProjects/RunTestsApps/TestAppMultiTarget/Program.cs @@ -0,0 +1,16 @@ +using System; + +namespace ConsoleApplication +{ + public class Program + { + public static void Main(string[] args) + { +#if NET451 + Console.WriteLine("NET451"); +#else + Console.WriteLine("CoreCLR"); +#endif + } + } +} diff --git a/TestAssets/TestProjects/RunTestsApps/TestAppMultiTarget/project.json b/TestAssets/TestProjects/RunTestsApps/TestAppMultiTarget/project.json new file mode 100644 index 000000000..265ca0bf2 --- /dev/null +++ b/TestAssets/TestProjects/RunTestsApps/TestAppMultiTarget/project.json @@ -0,0 +1,15 @@ +{ + "version": "1.0.0-*", + "compilationOptions": { + "emitEntryPoint": true + }, + + "frameworks": { + "dnxcore50": { + "dependencies": { + "NETStandard.Library": "1.0.0-rc2-23811" + }, + }, + "net451": { } + } +} diff --git a/TestAssets/TestProjects/RunTestsApps/TestAppMultiTargetNoCoreClr/.noautobuild b/TestAssets/TestProjects/RunTestsApps/TestAppMultiTargetNoCoreClr/.noautobuild new file mode 100644 index 000000000..8f7edc4ac --- /dev/null +++ b/TestAssets/TestProjects/RunTestsApps/TestAppMultiTargetNoCoreClr/.noautobuild @@ -0,0 +1 @@ +noautobuild \ No newline at end of file diff --git a/TestAssets/TestProjects/RunTestsApps/TestAppMultiTargetNoCoreClr/Program.cs b/TestAssets/TestProjects/RunTestsApps/TestAppMultiTargetNoCoreClr/Program.cs new file mode 100644 index 000000000..1a6535b48 --- /dev/null +++ b/TestAssets/TestProjects/RunTestsApps/TestAppMultiTargetNoCoreClr/Program.cs @@ -0,0 +1,16 @@ +using System; + +namespace ConsoleApplication +{ + public class Program + { + public static void Main(string[] args) + { +#if NET451 + Console.WriteLine("NET451"); +#else + Console.WriteLine("CoreCLR"); +#endif + } + } +} diff --git a/TestAssets/TestProjects/RunTestsApps/TestAppMultiTargetNoCoreClr/project.json b/TestAssets/TestProjects/RunTestsApps/TestAppMultiTargetNoCoreClr/project.json new file mode 100644 index 000000000..470e49702 --- /dev/null +++ b/TestAssets/TestProjects/RunTestsApps/TestAppMultiTargetNoCoreClr/project.json @@ -0,0 +1,11 @@ +{ + "version": "1.0.0-*", + "compilationOptions": { + "emitEntryPoint": true + }, + + "frameworks": { + "dummy1": { }, + "net451": { } + } +} diff --git a/scripts/dotnet-cli-build/TestTargets.cs b/scripts/dotnet-cli-build/TestTargets.cs index 76f2fe032..9329b2cae 100644 --- a/scripts/dotnet-cli-build/TestTargets.cs +++ b/scripts/dotnet-cli-build/TestTargets.cs @@ -114,10 +114,10 @@ namespace Microsoft.DotNet.Cli.Build public static BuildTargetResult BuildTestAssetProjects(BuildTargetContext c) { var dotnet = DotNetCli.Stage2; + var nobuildFileName = ".noautobuild"; string testProjectsRoot = Path.Combine(c.BuildContext.BuildDirectory, "TestAssets", "TestProjects"); - List exclusionList = new List { Path.Combine(testProjectsRoot, "CompileFail", "project.json") }; var projects = Directory.GetFiles(testProjectsRoot, "project.json", SearchOption.AllDirectories) - .Where(p => !exclusionList.Any(e => e.Contains(p))); + .Where(p => !File.Exists(Path.Combine(Path.GetDirectoryName(p), nobuildFileName))); foreach (var project in projects) { diff --git a/src/dotnet/commands/dotnet-run/RunCommand.cs b/src/dotnet/commands/dotnet-run/RunCommand.cs index 15284086b..4e0d1e059 100644 --- a/src/dotnet/commands/dotnet-run/RunCommand.cs +++ b/src/dotnet/commands/dotnet-run/RunCommand.cs @@ -61,6 +61,7 @@ namespace Microsoft.DotNet.Tools.Run } var rids = PlatformServices.Default.Runtime.GetAllCandidateRuntimeIdentifiers(); + if (Framework == null) { var defaultFrameworks = new[] @@ -69,14 +70,23 @@ namespace Microsoft.DotNet.Tools.Run FrameworkConstants.FrameworkIdentifiers.NetStandardApp, }; - var contexts = ProjectContext.CreateContextForEachFramework(Project, null, rids); - _context = contexts.FirstOrDefault(c => - defaultFrameworks.Contains(c.TargetFramework.Framework) && !string.IsNullOrEmpty(c.RuntimeIdentifier)); + var contexts = ProjectContext.CreateContextForEachFramework(Project, null); - if (_context == null) + ProjectContext context; + if (contexts.Count() == 1) { - throw new InvalidOperationException($"Couldn't find default target with framework: {string.Join(",", defaultFrameworks)}"); + context = contexts.Single(); } + else + { + context = contexts.FirstOrDefault(c => defaultFrameworks.Contains(c.TargetFramework.Framework)); + if (context == null) + { + throw new InvalidOperationException($"Couldn't find target to run. Defaults: {string.Join(", ", defaultFrameworks)}"); + } + } + + _context = context.CreateRuntimeContext(rids); } else { diff --git a/test/dotnet-run.Tests/RunTests.cs b/test/dotnet-run.Tests/RunTests.cs new file mode 100644 index 000000000..5b82db95c --- /dev/null +++ b/test/dotnet-run.Tests/RunTests.cs @@ -0,0 +1,54 @@ +// 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.IO; +using Microsoft.DotNet.Tools.Test.Utilities; +using Xunit; + +namespace Microsoft.DotNet.Tools.Compiler.Tests +{ + public class CompilerTests : TestBase + { + private static const string RunTestsBase = "RunTestsApps"; + + [WindowsOnlyFact] + public void RunsSingleTarget() + { + TestInstance instance = TestAssetsManager.CreateTestInstance(Path.Combine(RunTestsBase, "TestAppDesktopClr")) + .WithLockFiles() + .WithBuildArtifacts(); + new RunCommand(testInstance.TestRoot).Execute().Should().Pass(); + } + + public void RunsDefaultWhenPresent() + { + TestInstance instance = TestAssetsManager.CreateTestInstance(Path.Combine(RunTestsBase, "TestAppMultiTarget")) + .WithLockFiles() + .WithBuildArtifacts(); + new RunCommand(testInstance.TestRoot).Execute().Should().Pass(); + } + + public void FailsWithMultipleTargetAndNoDefault() + { + TestInstance instance = TestAssetsManager.CreateTestInstance(RunTestsBase, "TestAppMultiTargetNoCoreClr") + .WithLockFiles() + .WithBuildArtifacts(); + new RunCommand(testInstance.TestRoot).Execute().Should().Fail(); + } + + private void CopyProjectToTempDir(string projectDir, TempDirectory tempDir) + { + // copy all the files to temp dir + foreach (var file in Directory.EnumerateFiles(projectDir)) + { + tempDir.CopyFile(file); + } + } + + private string GetProjectPath(TempDirectory projectDir) + { + return Path.Combine(projectDir.Path, "project.json"); + } + } +} diff --git a/test/dotnet-run.Tests/dotnet-run.Tests.xproj b/test/dotnet-run.Tests/dotnet-run.Tests.xproj new file mode 100644 index 000000000..48df0207e --- /dev/null +++ b/test/dotnet-run.Tests/dotnet-run.Tests.xproj @@ -0,0 +1,19 @@ + + + + 14.0.24720 + $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) + + + + 35e3c2dc-9b38-4ec5-8dd7-c32458dc485f + dotnet-run.Tests + ..\..\artifacts\obj\$(MSBuildProjectName) + ..\..\artifacts\bin\$(MSBuildProjectName)\ + + + + 2.0 + + + \ No newline at end of file diff --git a/test/dotnet-run.Tests/project.json b/test/dotnet-run.Tests/project.json new file mode 100644 index 000000000..0629b58b1 --- /dev/null +++ b/test/dotnet-run.Tests/project.json @@ -0,0 +1,23 @@ +{ + "version": "1.0.0-*", + + "dependencies": { + "NETStandard.Library": "1.0.0-rc2-23811", + + "Microsoft.DotNet.Tools.Tests.Utilities": { "target": "project" }, + "Microsoft.DotNet.Cli.Utils": { + "target": "project" + }, + + "xunit": "2.1.0", + "dotnet-test-xunit": "1.0.0-dev-48273-16" + }, + + "frameworks": { + "dnxcore50": { + "imports": "portable-net45+win8" + } + }, + + "testRunner": "xunit" +}