-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Improve msbuild logger for testing workloads #9706
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 17 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
21f8e47
First take
nohwnd 4ecf486
Fix timing and use extended messages
nohwnd 591a51e
Remove unused class
nohwnd 8a34eb8
doc and rename messages
nohwnd b53ee1c
Fix colored target bug, and add tests
nohwnd e4e4baa
Compile the regex
nohwnd 43e5429
verified renamed
nohwnd 9de8600
Fix class name
nohwnd 4a1696b
Fix rendering without target
nohwnd 40339e4
Localize
nohwnd 41afb1b
Merge branch 'main' into exp/terminal-logger-for-vstest
nohwnd e368731
Merge conflict
nohwnd 11248b1
duration fix
nohwnd 828745e
Pass colored parts of target separately
nohwnd 3d2b161
Document node status ctors, to make it more obvious what the paramete…
nohwnd a150ae8
Apply suggestions from code review
nohwnd 2e8c3c2
Update src/Framework/Logging/AnsiCodes.cs
nohwnd 8090920
Fix extra space
nohwnd fd23994
Merge branch 'exp/terminal-logger-for-vstest' of https://github.com/d…
nohwnd bbf5b79
Suppress nullcheck because we check it above
nohwnd 11b8d55
Fix nits
nohwnd File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,181 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.IO; | ||
| using System.Linq; | ||
| using System.Text; | ||
| using System.Text.RegularExpressions; | ||
| using System.Threading.Tasks; | ||
|
|
||
| using Microsoft.Build.Logging.TerminalLogger; | ||
| using Shouldly; | ||
| using VerifyTests; | ||
| using VerifyXunit; | ||
| using Xunit; | ||
|
|
||
| using static VerifyXunit.Verifier; | ||
|
|
||
|
|
||
| namespace Microsoft.Build.CommandLine.UnitTests; | ||
|
|
||
| [UsesVerify] | ||
| public class NodeStatus_Transition_Tests | ||
| { | ||
| public NodeStatus_Transition_Tests() | ||
| { | ||
| UseProjectRelativeDirectory("Snapshots"); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void NodeStatusTargetThrowsForInputWithAnsi() | ||
| { | ||
| #if DEBUG | ||
| // This is testing a Debug.Assert, which won't throw in Release mode. | ||
| Func<NodeStatus> newNodeStatus = () => new NodeStatus("project", "tfm", AnsiCodes.Colorize("colorized target", TerminalColor.Green), new MockStopwatch()); | ||
nohwnd marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| newNodeStatus.ShouldThrow<Exception>().Message.ShouldContain("Target should not contain any escape codes, if you want to colorize target use the other constructor."); | ||
| #endif | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task NodeTargetChanges() | ||
| { | ||
| var rendered = Animate( | ||
| [ | ||
| new("Namespace.Project", "TargetFramework", "Build", new MockStopwatch()) | ||
| ], | ||
| [ | ||
| new("Namespace.Project", "TargetFramework", "Testing", new MockStopwatch()) | ||
| ]); | ||
|
|
||
| await VerifyReplay(rendered); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task NodeTargetUpdatesTime() | ||
| { | ||
| // This test look like there is no change between the frames, but we ask the stopwatch for time they will increase the number. | ||
| // We need this because animations check that NodeStatus reference is the same. | ||
| // 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. | ||
| NodeStatus node = new("Namespace.Project", "TargetFramework", "Build", new TickingStopwatch()); | ||
| var rendered = Animate( | ||
| [ | ||
| node, | ||
| ], | ||
| [ | ||
| node, | ||
| ]); | ||
|
|
||
| await VerifyReplay(rendered); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task NodeTargetChangesToColoredTarget() | ||
| { | ||
| var rendered = Animate( | ||
| [ | ||
| new("Namespace.Project", "TargetFramework", "Testing", new MockStopwatch()) | ||
| ], | ||
| [ | ||
| new("Namespace.Project", "TargetFramework", TerminalColor.Red, "failed", "MyTestName1", new MockStopwatch()) | ||
| ]); | ||
|
|
||
| await VerifyReplay(rendered); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task NodeWithColoredTargetUpdatesTime() | ||
| { | ||
| // This test look like there is no change between the frames, but we ask the stopwatch for time they will increase the number. | ||
| // We need this because animations check that NodeStatus reference is the same. | ||
| // 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. | ||
| NodeStatus node = new("Namespace.Project", "TargetFramework", TerminalColor.Green, "passed", "MyTestName1", new TickingStopwatch()); | ||
| var rendered = Animate( | ||
| [ | ||
| node, | ||
| ], | ||
| [ | ||
| node | ||
| ]); | ||
|
|
||
| await VerifyReplay(rendered); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Chains and renders node status updates and outputs replay able string of all the transitions. | ||
| /// </summary> | ||
| /// <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> | ||
| /// <returns></returns> | ||
| private string Animate(params NodeStatus[][] nodeStatusesUpdates) | ||
| { | ||
| var width = 80; | ||
| var height = 1; | ||
|
|
||
| NodesFrame previousFrame = new(Array.Empty<NodeStatus>(), 0, 0); | ||
| StringBuilder result = new StringBuilder(); | ||
| foreach (var nodeStatuses in nodeStatusesUpdates) | ||
| { | ||
| NodesFrame currentFrame = new NodesFrame(nodeStatuses, width, height); | ||
| result.Append(currentFrame.Render(previousFrame)); | ||
| previousFrame = currentFrame; | ||
| } | ||
|
|
||
| return result.ToString(); | ||
| } | ||
|
|
||
| private async Task VerifyReplay(string rendered) | ||
| { | ||
| try | ||
| { | ||
| await Verify(rendered); | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| if (ex.GetType().Name != "VerifyException") | ||
| { | ||
| throw; | ||
| } | ||
|
|
||
| if (!ex.Message.StartsWith("Directory:")) | ||
| { | ||
| throw; | ||
| } | ||
|
|
||
| string? directory = null; | ||
| string? received = null; | ||
| string? verified = null; | ||
| foreach (var line in ex.Message.Split('\n')) | ||
| { | ||
| var trimmed = line.TrimStart(' ', '-'); | ||
| Extract(trimmed, "Directory", ref directory); | ||
| Extract(trimmed, "Received", ref received); | ||
| Extract(trimmed, "Verified", ref verified); | ||
| } | ||
|
|
||
| if (directory == null || received == null || verified == null) | ||
| { | ||
| throw; | ||
| } | ||
|
|
||
| var pipeline = $$""" | % { "`n`n" } { $_ -split "(?=`e)" | % { Write-Host -NoNewline $_; Start-Sleep 0.5 }; Write-Host }"""; | ||
| throw new Exception($$""" | ||
| {{ex.Message.TrimEnd('\n')}} | ||
|
|
||
| Received replay: | ||
| Get-Content {{Path.Combine(directory, received)}} {{pipeline}} | ||
nohwnd marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| Verified replay: | ||
| Get-Content {{Path.Combine(directory, verified)}} {{pipeline}} | ||
| """); | ||
| } | ||
|
|
||
| void Extract(string line, string prefix, ref string? output) | ||
| { | ||
| if (line.StartsWith($"{prefix}: ")) | ||
| { | ||
| output = line.Substring(prefix.Length + 2); | ||
| } | ||
| } | ||
| } | ||
| } | ||
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 4 additions & 0 deletions
4
src/MSBuild.UnitTests/Snapshots/NodeStatus_Transition_Tests.NodeTargetChanges.verified.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| [1F | ||
| Namespace.Project [36;1mTargetFramework[m [120G[12DBuild (0.0s) | ||
| [2F | ||
| [K Namespace.Project [36;1mTargetFramework[m [120G[14DTesting (0.0s) |
4 changes: 4 additions & 0 deletions
4
...Tests/Snapshots/NodeStatus_Transition_Tests.NodeTargetChangesToColoredTarget.verified.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| [1F | ||
| Namespace.Project [36;1mTargetFramework[m [120G[14DTesting (0.0s) | ||
| [2F | ||
| [K Namespace.Project [36;1mTargetFramework[m [120G[25D[31;1mfailed[m MyTestName1 (0.0s) |
4 changes: 4 additions & 0 deletions
4
...SBuild.UnitTests/Snapshots/NodeStatus_Transition_Tests.NodeTargetUpdatesTime.verified.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| [1F | ||
| Namespace.Project [36;1mTargetFramework[m [120G[12DBuild (0.0s) | ||
| [2F | ||
| [120G[6D(0.2s) |
4 changes: 4 additions & 0 deletions
4
...Tests/Snapshots/NodeStatus_Transition_Tests.NodeWithColoredTargetUpdatesTime.verified.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| [1F | ||
| Namespace.Project [36;1mTargetFramework[m [120G[25D[32;1mpassed[m MyTestName1 (0.0s) | ||
| [2F | ||
| [120G[6D(0.2s) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.Net.Http.Headers; | ||
| using Microsoft.Build.Logging.TerminalLogger; | ||
|
|
||
| namespace Microsoft.Build.CommandLine.UnitTests; | ||
|
|
||
| /// <summary> | ||
| /// Stopwatch that always show the time provided in constructor. | ||
| /// </summary> | ||
| internal sealed class StaticStopwatch : StopwatchAbstraction | ||
| { | ||
| public StaticStopwatch(double elapsedSeconds) | ||
| { | ||
| ElapsedSeconds = elapsedSeconds; | ||
| } | ||
|
|
||
| public override double ElapsedSeconds { get; } | ||
|
|
||
| public override void Start() => throw new System.NotImplementedException(); | ||
| public override void Stop() => throw new System.NotImplementedException(); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using Microsoft.Build.Logging.TerminalLogger; | ||
|
|
||
| namespace Microsoft.Build.CommandLine.UnitTests; | ||
|
|
||
| /// <summary> | ||
| /// Stopwatch that will increase by 0.1, every time you ask them for time. Useful for animations, because they check that NodeStatus | ||
| /// reference stays the same, and also for ensuring we are grabbing the time only once per frame. | ||
| /// </summary> | ||
| internal sealed class TickingStopwatch : StopwatchAbstraction | ||
| { | ||
| private double _elapsedSeconds; | ||
|
|
||
| public TickingStopwatch(double elapsedSeconds = 0.0) | ||
| { | ||
| _elapsedSeconds = elapsedSeconds; | ||
| } | ||
|
|
||
| public override double ElapsedSeconds | ||
| { | ||
| get | ||
| { | ||
| var elapsed = _elapsedSeconds; | ||
| _elapsedSeconds += 0.1; | ||
| return elapsed; | ||
| } | ||
| } | ||
| public override void Start() => throw new System.NotImplementedException(); | ||
| public override void Stop() => throw new System.NotImplementedException(); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.