Unit tests for the logger command-line argument when telemetry is enabled or disabled
This commit is contained in:
parent
efa1cef866
commit
9c10867194
2 changed files with 68 additions and 2 deletions
|
@ -1,9 +1,17 @@
|
||||||
// Copyright (c) .NET Foundation and contributors. All rights reserved.
|
// 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.
|
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||||
|
|
||||||
|
using System;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Linq.Expressions;
|
||||||
|
using System.Reflection;
|
||||||
using FluentAssertions;
|
using FluentAssertions;
|
||||||
|
using Microsoft.DotNet.Configurer;
|
||||||
|
using Microsoft.DotNet.Tools.MSBuild;
|
||||||
using Microsoft.DotNet.Tools.Test.Utilities;
|
using Microsoft.DotNet.Tools.Test.Utilities;
|
||||||
|
using NuGet.Protocol;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
using MSBuildCommand = Microsoft.DotNet.Tools.Test.Utilities.MSBuildCommand;
|
||||||
|
|
||||||
namespace Microsoft.DotNet.Cli.MSBuild.Tests
|
namespace Microsoft.DotNet.Cli.MSBuild.Tests
|
||||||
{
|
{
|
||||||
|
@ -69,5 +77,60 @@ namespace Microsoft.DotNet.Cli.MSBuild.Tests
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void WhenTelemetryIsEnabledTheLoggerIsAddedToTheCommandLine()
|
||||||
|
{
|
||||||
|
string[] allArgs = GetArgsForMSBuild(() => true);
|
||||||
|
|
||||||
|
allArgs.Should().NotBeNull();
|
||||||
|
|
||||||
|
allArgs.Should().Contain(value => value.StartsWith("\"/Logger:"), "The MSBuild logger argument should be specified when telemetry is enabled.");
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void WhenTelemetryIsDisabledTheLoggerIsNotAddedToTheCommandLine()
|
||||||
|
{
|
||||||
|
string[] allArgs = GetArgsForMSBuild(() => false);
|
||||||
|
|
||||||
|
allArgs.Should().NotBeNull();
|
||||||
|
|
||||||
|
allArgs.Should().NotContain(value => value.IndexOf("/Logger", StringComparison.OrdinalIgnoreCase) >= 0, $"The MSBuild logger argument should not be specified when telemetry is disabled.");
|
||||||
|
}
|
||||||
|
|
||||||
|
private string[] GetArgsForMSBuild(Func<bool> sentinelExists)
|
||||||
|
{
|
||||||
|
Telemetry telemetry = new Telemetry(new MockNuGetCacheSentinel(sentinelExists));
|
||||||
|
|
||||||
|
MSBuildForwardingApp msBuildForwardingApp = new MSBuildForwardingApp(Enumerable.Empty<string>());
|
||||||
|
|
||||||
|
FieldInfo forwardingAppFieldInfo = msBuildForwardingApp.GetType().GetField("_forwardingApp", BindingFlags.Instance | BindingFlags.NonPublic);
|
||||||
|
|
||||||
|
ForwardingApp forwardingApp = forwardingAppFieldInfo?.GetValue(msBuildForwardingApp) as ForwardingApp;
|
||||||
|
|
||||||
|
FieldInfo allArgsFieldinfo = forwardingApp?.GetType().GetField("_allArgs", BindingFlags.Instance | BindingFlags.NonPublic);
|
||||||
|
|
||||||
|
return allArgsFieldinfo?.GetValue(forwardingApp) as string[];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public sealed class MockNuGetCacheSentinel : INuGetCacheSentinel
|
||||||
|
{
|
||||||
|
private readonly Func<bool> _exists;
|
||||||
|
|
||||||
|
public MockNuGetCacheSentinel(Func<bool> exists = null)
|
||||||
|
{
|
||||||
|
_exists = exists ?? (() => true);
|
||||||
|
}
|
||||||
|
public void Dispose()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool InProgressSentinelAlreadyExists() => false;
|
||||||
|
|
||||||
|
public bool Exists() => _exists();
|
||||||
|
|
||||||
|
public void CreateIfNotExists()
|
||||||
|
{
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,7 +10,10 @@
|
||||||
"target": "project"
|
"target": "project"
|
||||||
},
|
},
|
||||||
"xunit": "2.2.0-beta3-build3330",
|
"xunit": "2.2.0-beta3-build3330",
|
||||||
"dotnet-test-xunit": "1.0.0-rc2-350904-49"
|
"dotnet-test-xunit": "1.0.0-rc2-350904-49",
|
||||||
|
"dotnet": {
|
||||||
|
"target": "project"
|
||||||
|
}
|
||||||
},
|
},
|
||||||
"frameworks": {
|
"frameworks": {
|
||||||
"netcoreapp1.0": {
|
"netcoreapp1.0": {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue