From 56194a8e12f7190c5eed2fa47ef6dfb22de04ac9 Mon Sep 17 00:00:00 2001 From: Bryan Date: Mon, 18 Apr 2016 17:46:27 -0700 Subject: [PATCH] Add a test case for a desktop app using Command.CreateDotnet, expecting to fail --- .../.noautobuild | 0 .../DesktopAppWhichCallsDotnet/Program.cs | 21 +++++++ .../DesktopAppWhichCallsDotnet/project.json | 12 ++++ .../DesktopAppWithNativeDep/.noautobuild | 0 .../TestProjects/TestAppSimple/Program.cs | 12 ++++ .../TestProjects/TestAppSimple/project.json | 12 ++++ scripts/dotnet-cli-build/TestTargets.cs | 56 +++++++++++++------ src/Microsoft.DotNet.Cli.Utils/Command.cs | 3 +- ...ADesktopAppWhichUsesCommandCreateDotnet.cs | 44 +++++++++++++++ 9 files changed, 142 insertions(+), 18 deletions(-) create mode 100644 TestAssets/DesktopTestProjects/AppWithDirectDependencyDesktopAndPortable/.noautobuild create mode 100644 TestAssets/DesktopTestProjects/DesktopAppWhichCallsDotnet/Program.cs create mode 100644 TestAssets/DesktopTestProjects/DesktopAppWhichCallsDotnet/project.json create mode 100644 TestAssets/DesktopTestProjects/DesktopAppWithNativeDep/.noautobuild create mode 100644 TestAssets/TestProjects/TestAppSimple/Program.cs create mode 100644 TestAssets/TestProjects/TestAppSimple/project.json create mode 100644 test/Microsoft.DotNet.Cli.Utils.Tests/GivenADesktopAppWhichUsesCommandCreateDotnet.cs diff --git a/TestAssets/DesktopTestProjects/AppWithDirectDependencyDesktopAndPortable/.noautobuild b/TestAssets/DesktopTestProjects/AppWithDirectDependencyDesktopAndPortable/.noautobuild new file mode 100644 index 000000000..e69de29bb diff --git a/TestAssets/DesktopTestProjects/DesktopAppWhichCallsDotnet/Program.cs b/TestAssets/DesktopTestProjects/DesktopAppWhichCallsDotnet/Program.cs new file mode 100644 index 000000000..419984e14 --- /dev/null +++ b/TestAssets/DesktopTestProjects/DesktopAppWhichCallsDotnet/Program.cs @@ -0,0 +1,21 @@ +using System; +using Microsoft.DotNet.Cli.Utils; +using System.IO; + +namespace DesktopAppWhichCallsDotnet +{ + public class Program + { + public static int Main(string[] args) + { + var projectPath = args[0]; + + return Command.CreateDotNet("build", new string[] {}) + .WorkingDirectory(Path.GetDirectoryName(projectPath)) + .ForwardStdErr() + .ForwardStdOut() + .Execute() + .ExitCode; + } + } +} diff --git a/TestAssets/DesktopTestProjects/DesktopAppWhichCallsDotnet/project.json b/TestAssets/DesktopTestProjects/DesktopAppWhichCallsDotnet/project.json new file mode 100644 index 000000000..e531a3c5e --- /dev/null +++ b/TestAssets/DesktopTestProjects/DesktopAppWhichCallsDotnet/project.json @@ -0,0 +1,12 @@ +{ + "version": "1.0.0-*", + "dependencies": { + "Microsoft.DotNet.Cli.Utils": "1.0.0-*" + }, + "compilationOptions": { + "emitEntryPoint": true + }, + "frameworks": { + "net451": {} + } +} diff --git a/TestAssets/DesktopTestProjects/DesktopAppWithNativeDep/.noautobuild b/TestAssets/DesktopTestProjects/DesktopAppWithNativeDep/.noautobuild new file mode 100644 index 000000000..e69de29bb diff --git a/TestAssets/TestProjects/TestAppSimple/Program.cs b/TestAssets/TestProjects/TestAppSimple/Program.cs new file mode 100644 index 000000000..51233cffa --- /dev/null +++ b/TestAssets/TestProjects/TestAppSimple/Program.cs @@ -0,0 +1,12 @@ +using System; + +namespace ConsoleApplication +{ + public class Program + { + public static void Main(string[] args) + { + Console.WriteLine("Hello World!"); + } + } +} diff --git a/TestAssets/TestProjects/TestAppSimple/project.json b/TestAssets/TestProjects/TestAppSimple/project.json new file mode 100644 index 000000000..569c21461 --- /dev/null +++ b/TestAssets/TestProjects/TestAppSimple/project.json @@ -0,0 +1,12 @@ +{ + "version": "1.0.0-*", + "compilationOptions": { + "emitEntryPoint": true + }, + "dependencies": { + "Microsoft.NETCore.App": "1.0.0-rc2-*" + }, + "frameworks": { + "netcoreapp1.0": { } + } +} diff --git a/scripts/dotnet-cli-build/TestTargets.cs b/scripts/dotnet-cli-build/TestTargets.cs index 6a984a698..eb034c560 100644 --- a/scripts/dotnet-cli-build/TestTargets.cs +++ b/scripts/dotnet-cli-build/TestTargets.cs @@ -59,7 +59,10 @@ namespace Microsoft.DotNet.Cli.Build [Target(nameof(RestoreTestAssetPackages), nameof(BuildTestAssetPackages))] public static BuildTargetResult SetupTestPackages(BuildTargetContext c) => c.Success(); - [Target(nameof(RestoreTestAssetProjects), nameof(RestoreDesktopTestAssetProjects), nameof(BuildTestAssetProjects))] + [Target(nameof(RestoreTestAssetProjects), + nameof(RestoreDesktopTestAssetProjects), + nameof(BuildTestAssetProjects), + nameof(BuildDesktopTestAssetProjects))] public static BuildTargetResult SetupTestProjects(BuildTargetContext c) => c.Success(); [Target] @@ -236,25 +239,22 @@ namespace Microsoft.DotNet.Cli.Build [Target] public static BuildTargetResult BuildTestAssetProjects(BuildTargetContext c) { - CleanBinObj(c, Path.Combine(c.BuildContext.BuildDirectory, "TestAssets", "TestProjects")); - + var testAssetsRoot = Path.Combine(c.BuildContext.BuildDirectory, "TestAssets", "TestProjects"); var dotnet = DotNetCli.Stage2; - var nobuildFileName = ".noautobuild"; - string testProjectsRoot = Path.Combine(c.BuildContext.BuildDirectory, "TestAssets", "TestProjects"); - var projects = Directory.GetFiles(testProjectsRoot, "project.json", SearchOption.AllDirectories) - .Where(p => !ConditionalTestAssets.Where(s => !s.Skip() && p.EndsWith(Path.Combine(s.Path, "project.json"))).Any()) - .Where(p => !File.Exists(Path.Combine(Path.GetDirectoryName(p), nobuildFileName))); + var framework = "netcoreapp1.0"; - foreach (var project in projects) - { - c.Info($"Building: {project}"); - dotnet.Build("--framework", "netcoreapp1.0") - .WorkingDirectory(Path.GetDirectoryName(project)) - .Execute() - .EnsureSuccessful(); - } + return BuildTestAssets(c, testAssetsRoot, dotnet, framework); + } - return c.Success(); + [Target] + [BuildPlatforms(BuildPlatform.Windows)] + public static BuildTargetResult BuildDesktopTestAssetProjects(BuildTargetContext c) + { + var testAssetsRoot = Path.Combine(c.BuildContext.BuildDirectory, "TestAssets", "DesktopTestProjects"); + var dotnet = DotNetCli.Stage2; + var framework = "net451"; + + return BuildTestAssets(c, testAssetsRoot, dotnet, framework); } [Target] @@ -359,6 +359,28 @@ namespace Microsoft.DotNet.Cli.Build return c.Success(); } + private static BuildTargetResult BuildTestAssets(BuildTargetContext c, string testAssetsRoot, DotNetCli dotnet, string framework) + { + CleanBinObj(c, testAssetsRoot); + + var nobuildFileName = ".noautobuild"; + + var projects = Directory.GetFiles(testAssetsRoot, "project.json", SearchOption.AllDirectories) + .Where(p => !ConditionalTestAssets.Where(s => !s.Skip() && p.EndsWith(Path.Combine(s.Path, "project.json"))).Any()) + .Where(p => !File.Exists(Path.Combine(Path.GetDirectoryName(p), nobuildFileName))); + + foreach (var project in projects) + { + c.Info($"Building: {project}"); + dotnet.Build("--framework", framework) + .WorkingDirectory(Path.GetDirectoryName(project)) + .Execute() + .EnsureSuccessful(); + } + + return c.Success(); + } + private static Dictionary LoadVsVars(BuildTargetContext c) { if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) diff --git a/src/Microsoft.DotNet.Cli.Utils/Command.cs b/src/Microsoft.DotNet.Cli.Utils/Command.cs index 6bc62c57c..dfd608bcc 100644 --- a/src/Microsoft.DotNet.Cli.Utils/Command.cs +++ b/src/Microsoft.DotNet.Cli.Utils/Command.cs @@ -27,7 +27,8 @@ namespace Microsoft.DotNet.Cli.Utils FileName = commandSpec.Path, Arguments = commandSpec.Args, RedirectStandardError = true, - RedirectStandardOutput = true + RedirectStandardOutput = true, + UseShellExecute = false }; _stdOut = new StreamForwarder(); diff --git a/test/Microsoft.DotNet.Cli.Utils.Tests/GivenADesktopAppWhichUsesCommandCreateDotnet.cs b/test/Microsoft.DotNet.Cli.Utils.Tests/GivenADesktopAppWhichUsesCommandCreateDotnet.cs new file mode 100644 index 000000000..89500096a --- /dev/null +++ b/test/Microsoft.DotNet.Cli.Utils.Tests/GivenADesktopAppWhichUsesCommandCreateDotnet.cs @@ -0,0 +1,44 @@ +// 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.Runtime.InteropServices; +using System.Text; +using System.Linq; +using Xunit; +using Moq; +using Microsoft.DotNet.Cli.Utils; +using Microsoft.DotNet.ProjectModel; +using Microsoft.DotNet.Tools.Test.Utilities; +using Microsoft.Extensions.PlatformAbstractions; +using System.Threading; +using FluentAssertions; +using NuGet.Frameworks; + +namespace Microsoft.DotNet.Cli.Utils.Tests +{ + public class GivenADesktopAppWhichUsesCommandCreateDotnet : TestBase + { + [WindowsOnlyFact] + public void It_calls_dotnet_build_on_a_project_successfully() + { + var testAssetsManager = GetTestGroupTestAssetsManager("DesktopTestProjects"); + var testInstance = testAssetsManager + .CreateTestInstance("DesktopAppWhichCallsDotnet") + .WithLockFiles() + .WithBuildArtifacts(); + + var testProjectAssetManager = GetTestGroupTestAssetsManager("TestProjects"); + var testInstanceToBuild = testProjectAssetManager + .CreateTestInstance("TestAppSimple") + .WithLockFiles(); + + var testProject = Path.Combine(testInstance.TestRoot, "project.json"); + var testProjectToBuild = Path.Combine(testInstanceToBuild.TestRoot, "project.json"); + + new RunCommand(testProject).Execute(testProjectToBuild).Should().Pass(); + } + } +} \ No newline at end of file