2016-09-22 20:56:36 +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.
|
|
|
|
|
2016-11-01 16:29:50 +00:00
|
|
|
using System;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Linq.Expressions;
|
|
|
|
using System.Reflection;
|
2017-02-01 00:47:41 +00:00
|
|
|
using System.Runtime.InteropServices;
|
2016-09-22 20:56:36 +00:00
|
|
|
using FluentAssertions;
|
2017-06-19 21:45:38 +00:00
|
|
|
using Microsoft.DotNet.Cli;
|
2016-11-01 16:29:50 +00:00
|
|
|
using Microsoft.DotNet.Configurer;
|
|
|
|
using Microsoft.DotNet.Tools.MSBuild;
|
2016-09-22 20:56:36 +00:00
|
|
|
using Microsoft.DotNet.Tools.Test.Utilities;
|
2016-11-01 16:29:50 +00:00
|
|
|
using NuGet.Protocol;
|
2016-09-22 20:56:36 +00:00
|
|
|
using Xunit;
|
2017-02-01 22:12:34 +00:00
|
|
|
using Xunit.Abstractions;
|
2016-11-01 16:29:50 +00:00
|
|
|
using MSBuildCommand = Microsoft.DotNet.Tools.Test.Utilities.MSBuildCommand;
|
2017-04-21 23:14:44 +00:00
|
|
|
using System.Diagnostics;
|
|
|
|
using System.Threading;
|
2016-09-22 20:56:36 +00:00
|
|
|
|
2017-06-19 21:45:38 +00:00
|
|
|
// There are tests which modify static Telemetry.CurrentSessionId and they cannot run in parallel
|
|
|
|
[assembly: CollectionBehavior(DisableTestParallelization = true)]
|
|
|
|
|
2016-09-23 16:11:44 +00:00
|
|
|
namespace Microsoft.DotNet.Cli.MSBuild.Tests
|
2016-09-22 20:56:36 +00:00
|
|
|
{
|
|
|
|
public class GivenDotnetMSBuildBuildsProjects : TestBase
|
|
|
|
{
|
2017-02-01 22:12:34 +00:00
|
|
|
private readonly ITestOutputHelper _output;
|
|
|
|
|
|
|
|
public GivenDotnetMSBuildBuildsProjects(ITestOutputHelper output)
|
|
|
|
{
|
|
|
|
_output = output;
|
|
|
|
}
|
|
|
|
|
2016-09-22 20:56:36 +00:00
|
|
|
[Fact]
|
|
|
|
public void ItRunsSpecifiedTargetsWithPropertiesCorrectly()
|
|
|
|
{
|
2017-02-15 23:35:03 +00:00
|
|
|
var testInstance = TestAssets.Get("MSBuildBareBonesProject")
|
|
|
|
.CreateInstance()
|
|
|
|
.WithSourceFiles();
|
2016-09-22 20:56:36 +00:00
|
|
|
|
2017-02-15 23:35:03 +00:00
|
|
|
var testProjectDirectory = testInstance.Root;
|
2016-09-22 20:56:36 +00:00
|
|
|
|
|
|
|
new MSBuildCommand()
|
|
|
|
.WithWorkingDirectory(testProjectDirectory)
|
|
|
|
.ExecuteWithCapturedOutput("/t:SayHello")
|
|
|
|
.Should()
|
|
|
|
.Pass()
|
|
|
|
.And
|
|
|
|
.HaveStdOutContaining("Hello, from MSBuild!");
|
|
|
|
|
|
|
|
new MSBuildCommand()
|
|
|
|
.WithWorkingDirectory(testProjectDirectory)
|
|
|
|
.ExecuteWithCapturedOutput("/t:SayGoodbye")
|
|
|
|
.Should()
|
|
|
|
.Pass()
|
|
|
|
.And
|
|
|
|
.HaveStdOutContaining("Goodbye, from MSBuild. :'(");
|
|
|
|
|
|
|
|
new MSBuildCommand()
|
|
|
|
.WithWorkingDirectory(testProjectDirectory)
|
|
|
|
.ExecuteWithCapturedOutput("/t:SayThis /p:This=GreatScott")
|
|
|
|
.Should()
|
|
|
|
.Pass()
|
|
|
|
.And
|
|
|
|
.HaveStdOutContaining("You want me to say 'GreatScott'");
|
|
|
|
}
|
2016-10-12 00:29:09 +00:00
|
|
|
|
2017-03-09 20:19:27 +00:00
|
|
|
[Theory(Skip="New parser feature needed")]
|
2017-02-15 23:35:03 +00:00
|
|
|
[InlineData("build")]
|
|
|
|
[InlineData("clean")]
|
|
|
|
[InlineData("pack")]
|
|
|
|
[InlineData("publish")]
|
|
|
|
[InlineData("restore")]
|
|
|
|
public void When_help_is_invoked_Then_MSBuild_extra_options_text_is_included_in_output(string commandName)
|
2016-10-12 00:29:09 +00:00
|
|
|
{
|
2016-12-17 07:59:58 +00:00
|
|
|
const string MSBuildHelpText = " Any extra options that should be passed to MSBuild. See 'dotnet msbuild -h' for available options.";
|
2016-10-12 00:29:09 +00:00
|
|
|
|
2017-02-15 23:35:03 +00:00
|
|
|
var projectDirectory = TestAssets.CreateTestDirectory(commandName);
|
2016-10-12 00:29:09 +00:00
|
|
|
var result = new TestCommand("dotnet")
|
2017-02-15 23:35:03 +00:00
|
|
|
.WithWorkingDirectory(projectDirectory)
|
2016-10-12 00:29:09 +00:00
|
|
|
.ExecuteWithCapturedOutput($"{commandName} --help");
|
2016-11-01 16:29:50 +00:00
|
|
|
|
2016-10-12 00:29:09 +00:00
|
|
|
result.ExitCode.Should().Be(0);
|
2017-02-15 23:35:03 +00:00
|
|
|
result.StdOut.Should().Contain(MSBuildHelpText);
|
2016-10-12 00:29:09 +00:00
|
|
|
}
|
|
|
|
|
2017-02-01 00:47:41 +00:00
|
|
|
[Fact]
|
|
|
|
public void WhenRestoreSourcesStartsWithUnixPathThenHttpsSourceIsParsedCorrectly()
|
|
|
|
{
|
|
|
|
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// this is a workaround for https://github.com/Microsoft/msbuild/issues/1622
|
2017-02-01 22:12:34 +00:00
|
|
|
var testInstance = TestAssets.Get("LibraryWithUnresolvablePackageReference")
|
2017-02-01 00:47:41 +00:00
|
|
|
.CreateInstance()
|
2017-02-01 22:12:34 +00:00
|
|
|
.WithSourceFiles();
|
2017-02-01 00:47:41 +00:00
|
|
|
|
|
|
|
var root = testInstance.Root;
|
|
|
|
var somePathThatExists = "/usr/local/bin";
|
|
|
|
|
|
|
|
var result = new DotnetCommand()
|
|
|
|
.WithWorkingDirectory(root)
|
2017-02-01 22:12:34 +00:00
|
|
|
.Execute($"msbuild /p:RestoreSources={somePathThatExists};https://api.nuget.org/v3/index.json /t:restore LibraryWithUnresolvablePackageReference.csproj");
|
|
|
|
|
|
|
|
_output.WriteLine($"[STDOUT]\n{result.StdOut}\n[STDERR]\n{result.StdErr}");
|
|
|
|
|
|
|
|
result.Should().Fail();
|
|
|
|
|
2017-06-20 03:52:19 +00:00
|
|
|
result.StdOut.Should().ContainVisuallySameFragment("NU1101");
|
2017-02-01 00:47:41 +00:00
|
|
|
}
|
|
|
|
|
2016-12-20 06:11:39 +00:00
|
|
|
[Fact]
|
|
|
|
public void WhenDotnetRunHelpIsInvokedAppArgumentsTextIsIncludedInOutput()
|
|
|
|
{
|
2017-06-14 02:05:16 +00:00
|
|
|
string AppArgumentsText = Tools.Run.LocalizableStrings.RunCommandAdditionalArgsHelpText;
|
2016-12-20 06:11:39 +00:00
|
|
|
|
2017-02-15 23:35:03 +00:00
|
|
|
var projectDirectory = TestAssets.CreateTestDirectory("RunContainsAppArgumentsText");
|
2016-12-20 06:11:39 +00:00
|
|
|
var result = new TestCommand("dotnet")
|
2017-02-15 23:35:03 +00:00
|
|
|
.WithWorkingDirectory(projectDirectory)
|
2016-12-20 06:11:39 +00:00
|
|
|
.ExecuteWithCapturedOutput("run --help");
|
2017-02-01 00:47:41 +00:00
|
|
|
|
2016-12-20 06:11:39 +00:00
|
|
|
result.ExitCode.Should().Be(0);
|
|
|
|
result.StdOut.Should().Contain(AppArgumentsText);
|
|
|
|
}
|
|
|
|
|
2016-11-01 16:29:50 +00:00
|
|
|
[Fact]
|
|
|
|
public void WhenTelemetryIsEnabledTheLoggerIsAddedToTheCommandLine()
|
|
|
|
{
|
2016-11-14 22:26:03 +00:00
|
|
|
Telemetry telemetry;
|
|
|
|
string[] allArgs = GetArgsForMSBuild(() => true, out telemetry);
|
2017-06-19 21:45:38 +00:00
|
|
|
// telemetry will still be disabled if environment variable is set
|
2016-11-14 22:26:03 +00:00
|
|
|
if (telemetry.Enabled)
|
|
|
|
{
|
|
|
|
allArgs.Should().NotBeNull();
|
2016-11-01 16:29:50 +00:00
|
|
|
|
2016-11-14 22:26:03 +00:00
|
|
|
allArgs.Should().Contain(
|
|
|
|
value => value.IndexOf("/Logger", StringComparison.OrdinalIgnoreCase) >= 0,
|
|
|
|
"The MSBuild logger argument should be specified when telemetry is enabled.");
|
|
|
|
}
|
2016-11-01 16:29:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public void WhenTelemetryIsDisabledTheLoggerIsNotAddedToTheCommandLine()
|
|
|
|
{
|
|
|
|
string[] allArgs = GetArgsForMSBuild(() => false);
|
|
|
|
|
|
|
|
allArgs.Should().NotBeNull();
|
|
|
|
|
2016-11-02 20:06:44 +00:00
|
|
|
allArgs.Should().NotContain(
|
|
|
|
value => value.IndexOf("/Logger", StringComparison.OrdinalIgnoreCase) >= 0,
|
|
|
|
$"The MSBuild logger argument should not be specified when telemetry is disabled.");
|
2016-11-01 16:29:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private string[] GetArgsForMSBuild(Func<bool> sentinelExists)
|
|
|
|
{
|
2016-11-14 22:26:03 +00:00
|
|
|
Telemetry telemetry;
|
|
|
|
return GetArgsForMSBuild(sentinelExists, out telemetry);
|
|
|
|
}
|
|
|
|
|
|
|
|
private string[] GetArgsForMSBuild(Func<bool> sentinelExists, out Telemetry telemetry)
|
|
|
|
{
|
2017-06-19 21:45:38 +00:00
|
|
|
Telemetry.CurrentSessionId = null; // reset static session id modified by telemetry constructor
|
2016-11-14 22:26:03 +00:00
|
|
|
telemetry = new Telemetry(new MockNuGetCacheSentinel(sentinelExists));
|
2016-11-01 16:29:50 +00:00
|
|
|
|
|
|
|
MSBuildForwardingApp msBuildForwardingApp = new MSBuildForwardingApp(Enumerable.Empty<string>());
|
|
|
|
|
2017-04-21 23:14:44 +00:00
|
|
|
object forwardingAppWithoutLogging = msBuildForwardingApp
|
|
|
|
.GetType()
|
|
|
|
.GetField("_forwardingAppWithoutLogging", BindingFlags.Instance | BindingFlags.NonPublic)
|
|
|
|
?.GetValue(msBuildForwardingApp);
|
|
|
|
|
|
|
|
FieldInfo forwardingAppFieldInfo = forwardingAppWithoutLogging
|
2016-11-02 20:06:44 +00:00
|
|
|
.GetType()
|
|
|
|
.GetField("_forwardingApp", BindingFlags.Instance | BindingFlags.NonPublic);
|
2016-11-01 16:29:50 +00:00
|
|
|
|
2017-04-21 23:14:44 +00:00
|
|
|
object forwardingApp = forwardingAppFieldInfo?.GetValue(forwardingAppWithoutLogging);
|
2016-11-01 16:29:50 +00:00
|
|
|
|
2016-11-02 20:06:44 +00:00
|
|
|
FieldInfo allArgsFieldinfo = forwardingApp?
|
|
|
|
.GetType()
|
|
|
|
.GetField("_allArgs", BindingFlags.Instance | BindingFlags.NonPublic);
|
2016-11-01 16:29:50 +00:00
|
|
|
|
|
|
|
return allArgsFieldinfo?.GetValue(forwardingApp) as string[];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-20 03:52:19 +00:00
|
|
|
public sealed class MockNuGetCacheSentinel : IFirstTimeUseNoticeSentinel
|
2016-11-01 16:29:50 +00:00
|
|
|
{
|
|
|
|
private readonly Func<bool> _exists;
|
|
|
|
|
2017-05-31 20:44:03 +00:00
|
|
|
public bool UnauthorizedAccess => true;
|
|
|
|
|
2016-11-01 16:29:50 +00:00
|
|
|
public MockNuGetCacheSentinel(Func<bool> exists = null)
|
|
|
|
{
|
|
|
|
_exists = exists ?? (() => true);
|
|
|
|
}
|
2017-05-31 20:44:03 +00:00
|
|
|
|
2016-11-01 16:29:50 +00:00
|
|
|
public void Dispose()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool InProgressSentinelAlreadyExists() => false;
|
|
|
|
|
|
|
|
public bool Exists() => _exists();
|
|
|
|
|
|
|
|
public void CreateIfNotExists()
|
|
|
|
{
|
|
|
|
}
|
2016-09-22 20:56:36 +00:00
|
|
|
}
|
|
|
|
}
|