Skip to content

Commit 63aefc3

Browse files
authored
Adding a static factory for the TerminalLogger (dotnet#11318)
* Adds a static factory that can create a TerminalLogger or ConsoleLogger instance based on settings and terminal capabilities. * Disable TL progress ctrl codes when verbosity is 'quiet'
1 parent 1ef8857 commit 63aefc3

File tree

145 files changed

+2363
-2269
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

145 files changed

+2363
-2269
lines changed

src/Build.UnitTests/ConsoleLogger_Tests.cs

-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
using Shouldly;
1919
using Xunit;
2020
using Xunit.Abstractions;
21-
using Xunit.NetCore.Extensions;
2221
using TaskItem = Microsoft.Build.Execution.ProjectItemInstance.TaskItem;
2322

2423
#nullable disable
@@ -134,7 +133,6 @@ public ConsoleLoggerTest(ITestOutputHelper output)
134133
_output = output;
135134
}
136135

137-
138136
/// <summary>
139137
/// Verify when the project has not been named that we correctly get the same placeholder
140138
/// project name for project started event and the target started event.

src/Build.UnitTests/Microsoft.Build.Engine.UnitTests.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
<PackageReference Include="FakeItEasy" />
2323
<PackageReference Include="System.Net.Http" />
2424
<PackageReference Include="Microsoft.CodeAnalysis.Build.Tasks" />
25+
<PackageReference Include="Verify.Xunit" />
2526
<PackageReference Include="NuGet.Frameworks">
2627
<PrivateAssets>all</PrivateAssets>
2728
</PackageReference>

src/MSBuild.UnitTests/MockStopwatch.cs src/Build.UnitTests/MockStopwatch.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4-
using Microsoft.Build.Logging.TerminalLogger;
4+
using Microsoft.Build.Logging;
55

66
namespace Microsoft.Build.CommandLine.UnitTests;
77

src/MSBuild.UnitTests/NodeStatus_SizeChange_Tests.cs src/Build.UnitTests/NodeStatus_SizeChange_Tests.cs

+6-6
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
using System.Text;
99
using System.Threading.Tasks;
1010

11-
using Microsoft.Build.Logging.TerminalLogger;
11+
using Microsoft.Build.Logging;
1212

1313
using VerifyTests;
1414
using VerifyXunit;
@@ -22,7 +22,7 @@ namespace Microsoft.Build.CommandLine.UnitTests;
2222
[UsesVerify]
2323
public class NodeStatus_SizeChange_Tests : IDisposable
2424
{
25-
private readonly NodeStatus _status = new("Namespace.Project", "TargetFramework", "Target", new MockStopwatch());
25+
private readonly TerminalNodeStatus _status = new("Namespace.Project", "TargetFramework", "Target", new MockStopwatch());
2626
private CultureInfo _currentCulture;
2727

2828
public NodeStatus_SizeChange_Tests()
@@ -36,31 +36,31 @@ public NodeStatus_SizeChange_Tests()
3636
[Fact]
3737
public async Task EverythingFits()
3838
{
39-
NodesFrame frame = new([_status], width: 80, height: 5);
39+
TerminalNodesFrame frame = new([_status], width: 80, height: 5);
4040

4141
await Verify(frame.RenderNodeStatus(0).ToString());
4242
}
4343

4444
[Fact]
4545
public async Task TargetIsTruncatedFirst()
4646
{
47-
NodesFrame frame = new([_status], width: 45, height: 5);
47+
TerminalNodesFrame frame = new([_status], width: 45, height: 5);
4848

4949
await Verify(frame.RenderNodeStatus(0).ToString());
5050
}
5151

5252
[Fact]
5353
public async Task NamespaceIsTruncatedNext()
5454
{
55-
NodesFrame frame = new([_status], width: 40, height: 5);
55+
TerminalNodesFrame frame = new([_status], width: 40, height: 5);
5656

5757
await Verify(frame.RenderNodeStatus(0).ToString());
5858
}
5959

6060
[Fact]
6161
public async Task GoesToProject()
6262
{
63-
NodesFrame frame = new([_status], width: 10, height: 5);
63+
TerminalNodesFrame frame = new([_status], width: 10, height: 5);
6464

6565
await Verify(frame.RenderNodeStatus(0).ToString());
6666
}

src/MSBuild.UnitTests/NodeStatus_Transition_Tests.cs src/Build.UnitTests/NodeStatus_Transition_Tests.cs

+8-8
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
using System.Text;
99
using System.Text.RegularExpressions;
1010
using System.Threading.Tasks;
11-
12-
using Microsoft.Build.Logging.TerminalLogger;
11+
using Microsoft.Build.Framework.Logging;
12+
using Microsoft.Build.Logging;
1313
using Shouldly;
1414
using VerifyTests;
1515
using VerifyXunit;
@@ -33,7 +33,7 @@ public void NodeStatusTargetThrowsForInputWithAnsi()
3333
{
3434
#if DEBUG
3535
// This is testing a Debug.Assert, which won't throw in Release mode.
36-
Func<NodeStatus> newNodeStatus = () => new NodeStatus("project", "tfm", AnsiCodes.Colorize("colorized target", TerminalColor.Green), new MockStopwatch());
36+
Func<TerminalNodeStatus> newNodeStatus = () => new TerminalNodeStatus("project", "tfm", AnsiCodes.Colorize("colorized target", TerminalColor.Green), new MockStopwatch());
3737
newNodeStatus.ShouldThrow<ArgumentException>().Message.ShouldContain("Target should not contain any escape codes, if you want to colorize target use the other constructor.");
3838
#endif
3939
}
@@ -58,7 +58,7 @@ public async Task NodeTargetUpdatesTime()
5858
// This test look like there is no change between the frames, but we ask the stopwatch for time they will increase the number.
5959
// We need this because animations check that NodeStatus reference is the same.
6060
// And we cannot use MockStopwatch because we don't know when to call Tick on them, and if we do it right away, the time will update in "both" nodes.
61-
NodeStatus node = new("Namespace.Project", "TargetFramework", "Build", new TickingStopwatch());
61+
TerminalNodeStatus node = new("Namespace.Project", "TargetFramework", "Build", new TickingStopwatch());
6262
var rendered = Animate(
6363
[
6464
node,
@@ -90,7 +90,7 @@ public async Task NodeWithColoredTargetUpdatesTime()
9090
// This test look like there is no change between the frames, but we ask the stopwatch for time they will increase the number.
9191
// We need this because animations check that NodeStatus reference is the same.
9292
// And we cannot use MockStopwatch because we don't know when to call Tick on them, and if we do it right away, the time will update in "both" nodes.
93-
NodeStatus node = new("Namespace.Project", "TargetFramework", TerminalColor.Green, "passed", "MyTestName1", new TickingStopwatch());
93+
TerminalNodeStatus node = new("Namespace.Project", "TargetFramework", TerminalColor.Green, "passed", "MyTestName1", new TickingStopwatch());
9494
var rendered = Animate(
9595
[
9696
node,
@@ -107,16 +107,16 @@ public async Task NodeWithColoredTargetUpdatesTime()
107107
/// </summary>
108108
/// <param name="nodeStatusesUpdates">Takes array of arrays. The inner array is collection of nodes that are currently running. The outer array is how they update over time.</param>
109109
/// <returns></returns>
110-
private string Animate(params NodeStatus[][] nodeStatusesUpdates)
110+
private string Animate(params TerminalNodeStatus[][] nodeStatusesUpdates)
111111
{
112112
var width = 80;
113113
var height = 1;
114114

115-
NodesFrame previousFrame = new(Array.Empty<NodeStatus>(), 0, 0);
115+
TerminalNodesFrame previousFrame = new(Array.Empty<TerminalNodeStatus>(), 0, 0);
116116
StringBuilder result = new StringBuilder();
117117
foreach (var nodeStatuses in nodeStatusesUpdates)
118118
{
119-
NodesFrame currentFrame = new NodesFrame(nodeStatuses, width, height);
119+
TerminalNodesFrame currentFrame = new TerminalNodesFrame(nodeStatuses, width, height);
120120
result.Append(currentFrame.Render(previousFrame));
121121
previousFrame = currentFrame;
122122
}
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
]9;4;3;\directory/file(1,2,3,4): warning AA0000: Warning!
1+
directory/file(1,2,3,4): warning AA0000: Warning!
22
directory/file(1,2,3,4): warning AA0000:
33
A
44
Multi
55
Line
66
Warning!
77
directory/file(1,2,3,4): error AA0000: Error!
8-
]9;4;0;\
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
]9;4;3;\directory/file(1,2,3,4): warning AA0000: Warning!
1+
directory/file(1,2,3,4): warning AA0000: Warning!
22
directory/file(1,2,3,4): warning AA0000:
33
A
44
Multi
55
Line
66
Warning!
77
directory/file(1,2,3,4): error AA0000: Error!
8-
]9;4;0;\
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
emptyString
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
emptyString

src/MSBuild.UnitTests/StaticStopwatch.cs src/Build.UnitTests/StaticStopwatch.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System.Net.Http.Headers;
5-
using Microsoft.Build.Logging.TerminalLogger;
5+
using Microsoft.Build.Logging;
66

77
namespace Microsoft.Build.CommandLine.UnitTests;
88

src/MSBuild.UnitTests/TerminalLogger_Tests.cs src/Build.UnitTests/TerminalLogger_Tests.cs

+36-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
using Microsoft.Build.Evaluation;
1616
using Microsoft.Build.Framework;
1717
using Microsoft.Build.Logging;
18-
using Microsoft.Build.Logging.TerminalLogger;
1918
using Microsoft.Build.UnitTests.Shared;
2019
using Shouldly;
2120
using VerifyTests;
@@ -70,6 +69,42 @@ public TerminalLogger_Tests()
7069
Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
7170
}
7271

72+
[Theory]
73+
[InlineData(null, false, false, "", typeof(ConsoleLogger))]
74+
[InlineData(null, true, false, "", typeof(ConsoleLogger))]
75+
[InlineData(null, false, true, "", typeof(ConsoleLogger))]
76+
[InlineData(null, true, true, "off", typeof(ConsoleLogger))]
77+
[InlineData(null, true, true, "false", typeof(ConsoleLogger))]
78+
[InlineData("--tl:off", true, true, "", typeof(ConsoleLogger))]
79+
[InlineData(null, true, true, "", typeof(TerminalLogger))]
80+
[InlineData("-tl:on", true, true, "off", typeof(TerminalLogger))]
81+
public void CreateTerminalOrConsoleLogger_CreatesCorrectLoggerInstance(string? argsString, bool supportsAnsi, bool outputIsScreen, string evnVariableValue, Type expectedType)
82+
{
83+
using TestEnvironment testEnvironment = TestEnvironment.Create();
84+
testEnvironment.SetEnvironmentVariable("MSBUILDTERMINALLOGGER", evnVariableValue);
85+
86+
string[]? args = argsString?.Split(' ');
87+
ILogger logger = TerminalLogger.CreateTerminalOrConsoleLogger(args, supportsAnsi, outputIsScreen, default);
88+
89+
logger.ShouldNotBeNull();
90+
logger.GetType().ShouldBe(expectedType);
91+
}
92+
93+
[Theory]
94+
[InlineData("-v:q", LoggerVerbosity.Quiet)]
95+
[InlineData("-verbosity:minimal", LoggerVerbosity.Minimal)]
96+
[InlineData("--v:d", LoggerVerbosity.Detailed)]
97+
[InlineData("/verbosity:diag", LoggerVerbosity.Diagnostic)]
98+
[InlineData(null, LoggerVerbosity.Normal)]
99+
public void CreateTerminalOrConsoleLogger_ParsesVerbosity(string? argsString, LoggerVerbosity expectedVerbosity)
100+
{
101+
string[]? args = argsString?.Split(' ');
102+
ILogger logger = TerminalLogger.CreateTerminalOrConsoleLogger(args, true, true, default);
103+
104+
logger.ShouldNotBeNull();
105+
logger.Verbosity.ShouldBe(expectedVerbosity);
106+
}
107+
73108
#region IEventSource implementation
74109

75110
#pragma warning disable CS0067

src/MSBuild.UnitTests/TickingStopwatch.cs src/Build.UnitTests/TickingStopwatch.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4-
using Microsoft.Build.Logging.TerminalLogger;
4+
using Microsoft.Build.Logging;
55

66
namespace Microsoft.Build.CommandLine.UnitTests;
77

src/Build/Logging/ConsoleLogger.cs

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System;
5+
using System.Diagnostics.CodeAnalysis;
6+
using System.Linq;
7+
using System.Reflection;
8+
using System.Runtime.CompilerServices;
59
using Microsoft.Build.BackEnd.Logging;
610
using Microsoft.Build.Framework;
711
using Microsoft.Build.Framework.Logging;

src/Build/Logging/SimpleErrorLogger.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33

44
using System;
55
using Microsoft.Build.Framework;
6-
using Microsoft.Build.Logging.TerminalLogger;
6+
using Microsoft.Build.Framework.Logging;
7+
using Microsoft.Build.Logging;
78
using Microsoft.Build.Shared;
89

910
namespace Microsoft.Build.Logging.SimpleErrorLogger

src/MSBuild/TerminalLogger/ITerminal.cs src/Build/Logging/TerminalLogger/ITerminal.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System;
5+
using Microsoft.Build.Framework.Logging;
56

6-
namespace Microsoft.Build.Logging.TerminalLogger;
7+
namespace Microsoft.Build.Logging;
78

89
/// <summary>
910
/// An abstraction of a terminal, built specifically to fit the <see cref="TerminalLogger"/> needs.

src/MSBuild/TerminalLogger/StopwatchAbstraction.cs src/Build/Logging/TerminalLogger/StopwatchAbstraction.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4-
namespace Microsoft.Build.Logging.TerminalLogger;
4+
namespace Microsoft.Build.Logging;
55

66
internal abstract class StopwatchAbstraction
77
{

src/MSBuild/TerminalLogger/SystemStopwatch.cs src/Build/Logging/TerminalLogger/SystemStopwatch.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
using System.Diagnostics;
55

6-
namespace Microsoft.Build.Logging.TerminalLogger;
6+
namespace Microsoft.Build.Logging;
77

88
internal sealed class SystemStopwatch : StopwatchAbstraction
99
{

src/MSBuild/TerminalLogger/Terminal.cs src/Build/Logging/TerminalLogger/Terminal.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55
using System.IO;
66
using System.Runtime.InteropServices;
77
using System.Text;
8+
using Microsoft.Build.Framework.Logging;
89
#if NETFRAMEWORK
910
using Microsoft.Build.Shared;
1011
#endif
1112

12-
namespace Microsoft.Build.Logging.TerminalLogger;
13+
namespace Microsoft.Build.Logging;
1314

1415
/// <summary>
1516
/// An <see cref="ITerminal"/> implementation for ANSI/VT100 terminals.
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4-
namespace Microsoft.Build.Logging.TerminalLogger;
4+
namespace Microsoft.Build.Logging;
55

66
/// <summary>
77
/// Represents a piece of diagnostic output (message/warning/error).
88
/// </summary>
9-
internal record struct BuildMessage(MessageSeverity Severity, string Message)
9+
internal record struct TerminalBuildMessage(TerminalMessageSeverity Severity, string Message)
1010
{ }

0 commit comments

Comments
 (0)