Repository navigation
Expand file tree
/
Copy pathGivenCommandServices.cs
More file actions
113 lines (100 loc) · 5.31 KB
/
Copy pathGivenCommandServices.cs
File metadata and controls
113 lines (100 loc) · 5.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using Microsoft.DotNet.Cli.Commands.MSBuild;
using Microsoft.DotNet.Cli.Commands.Restore;
using Microsoft.DotNet.Cli.Commands.Run;
using Microsoft.DotNet.Cli.Commands.Tool.Store;
using Microsoft.DotNet.Cli.Telemetry;
using Microsoft.DotNet.Cli.Utils;
using BuildCommand = Microsoft.DotNet.Cli.Commands.Build.BuildCommand;
using CleanCommand = Microsoft.DotNet.Cli.Commands.Clean.CleanCommand;
using MSBuildCommand = Microsoft.DotNet.Cli.Commands.MSBuild.MSBuildCommand;
using PackCommand = Microsoft.DotNet.Cli.Commands.Pack.PackCommand;
using PublishCommand = Microsoft.DotNet.Cli.Commands.Publish.PublishCommand;
using RestoreCommand = Microsoft.DotNet.Cli.Commands.Restore.RestoreCommand;
namespace Microsoft.DotNet.Cli.MSBuild.Tests;
[TestClass]
public class GivenCommandServices : SdkTest
{
[TestMethod]
[DataRow("build")]
[DataRow("clean")]
[DataRow("msbuild")]
[DataRow("pack")]
[DataRow("publish")]
[DataRow("restore")]
[DataRow("store")]
public void LLMDetectionIsPerCommand(string commandName)
{
var llmServices = CreateServices(isLLMEnvironment: true);
var nonLLMServices = CreateServices(isLLMEnvironment: false);
var llmCommand = (MSBuildForwardingApp)CreateCommand(commandName, llmServices, args: []);
var nonLLMCommand = (MSBuildForwardingApp)CreateCommand(commandName, nonLLMServices, args: []);
var defaultCommand = CreateCommand(commandName, CommandServices.CreateDefault(), args: []);
llmCommand.Services.Should().BeSameAs(llmServices);
nonLLMCommand.Services.Should().BeSameAs(nonLLMServices);
defaultCommand.Services.LLMEnvironmentDetector.Should().BeOfType<LLMEnvironmentDetectorForTelemetry>();
AssertLLMArguments(llmCommand, expected: true);
AssertLLMArguments(nonLLMCommand, expected: false);
}
[TestMethod]
[DataRow(false)]
[DataRow(true)]
public void SeparateRestoreSharesCommandServices(bool isLLMEnvironment)
{
var services = CreateServices(isLLMEnvironment);
var command = (RestoringCommand)BuildCommand.FromArgs(
["-p:TargetFramework=tfm"],
msbuildPath: "<msbuildpath>",
services: services);
command.SeparateRestoreCommand.Should().NotBeNull();
command.SeparateRestoreCommand!.Services.Should().BeSameAs(command.Services).And.BeSameAs(services);
AssertLLMArguments(command, isLLMEnvironment);
AssertLLMArguments(command.SeparateRestoreCommand, isLLMEnvironment);
}
[TestMethod]
[DataRow("build")]
[DataRow("clean")]
[DataRow("pack")]
[DataRow("publish")]
[DataRow("restore")]
public void FileBasedCommandUsesItsServicesWhenCreatingLogger(string commandName)
{
var directory = TestAssetsManager.CreateTestDirectory(identifier: commandName);
var entryPoint = Path.Combine(directory.Path, "Program.cs");
File.WriteAllText(entryPoint, "Console.WriteLine(\"Hello\");");
// Stop at logger creation, before in-process MSBuild can change shared process state.
var stopBeforeBuild = new InvalidOperationException("Stop before MSBuild starts.");
var detector = new TestLLMEnvironmentDetector(stopBeforeBuild);
var services = new CommandServices(detector);
var command = (VirtualProjectBuildingCommand)CreateCommand(commandName, services, [entryPoint]);
command.Services.Should().BeSameAs(services);
detector.IsLLMEnvironmentCallCount.Should().Be(0);
Action execute = () => command.Execute(TestContext.CancellationToken);
execute.Should().Throw<InvalidOperationException>().Which.Should().BeSameAs(stopBeforeBuild);
detector.IsLLMEnvironmentCallCount.Should().Be(1);
}
private static CommandServices CreateServices(bool isLLMEnvironment) =>
new(new TestLLMEnvironmentDetector(isLLMEnvironment));
private static CommandBase CreateCommand(string commandName, CommandServices services, string[] args)
{
const string msbuildPath = "<msbuildpath>";
return commandName switch
{
"build" => BuildCommand.FromArgs(args, msbuildPath, services),
"clean" => CleanCommand.FromArgs(args, msbuildPath, services),
"msbuild" => MSBuildCommand.FromArgs(args, msbuildPath, services),
"pack" => PackCommand.FromArgs(args, CancellationToken.None, msbuildPath, services),
"publish" => PublishCommand.FromArgs(args, CancellationToken.None, msbuildPath, services),
"restore" => RestoreCommand.FromArgs(args, msbuildPath, services),
"store" => StoreCommand.FromArgs(["--manifest", "manifest.xml", .. args], msbuildPath, services),
_ => throw new ArgumentOutOfRangeException(nameof(commandName), commandName, "Unknown command.")
};
}
private static void AssertLLMArguments(MSBuildForwardingApp command, bool expected)
{
command.MSBuildArguments.Contains(Constants.TerminalLogger_DisableNodeDisplay).Should().Be(expected);
command.GetArgumentTokensToMSBuild().Should().BeEquivalentTo(command.MSBuildArguments);
command.GetProcessStartInfo().Arguments.Contains(Constants.TerminalLogger_DisableNodeDisplay).Should().Be(expected);
}
}