Skip to content

Commit 5746d7e

Browse files
EvangelinkCopilot
andauthored
Extract duplicate banner logic into OutputDeviceBannerHelper (#9181)
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 6d14166 commit 5746d7e

3 files changed

Lines changed: 52 additions & 65 deletions

File tree

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using Microsoft.Testing.Platform.Helpers;
5+
using Microsoft.Testing.Platform.Services;
6+
7+
namespace Microsoft.Testing.Platform.OutputDevice;
8+
9+
internal static class OutputDeviceBannerHelper
10+
{
11+
#pragma warning disable SA1310 // Field names should not contain underscore
12+
internal const string TESTINGPLATFORM_CONSOLEOUTPUTDEVICE_SKIP_BANNER = nameof(TESTINGPLATFORM_CONSOLEOUTPUTDEVICE_SKIP_BANNER);
13+
#pragma warning restore SA1310 // Field names should not contain underscore
14+
15+
internal static string BuildBannerText(IPlatformInformation platformInformation, IRuntimeFeature runtimeFeature, string? longArchitecture, string? runtimeFramework)
16+
{
17+
StringBuilder stringBuilder = new();
18+
stringBuilder.Append(platformInformation.Name);
19+
20+
if (platformInformation.Version is { } version)
21+
{
22+
stringBuilder.Append(CultureInfo.InvariantCulture, $" v{version}");
23+
if (platformInformation.CommitHash is { } commitHash)
24+
{
25+
stringBuilder.Append(CultureInfo.InvariantCulture, $"+{(commitHash.Length >= 10 ? commitHash[..10] : commitHash)}");
26+
}
27+
}
28+
29+
if (platformInformation.BuildDate is { } buildDate)
30+
{
31+
stringBuilder.Append(CultureInfo.InvariantCulture, $" (UTC {buildDate.UtcDateTime:d})");
32+
}
33+
34+
if (runtimeFeature.IsDynamicCodeSupported)
35+
{
36+
stringBuilder.Append(" [");
37+
stringBuilder.Append(longArchitecture);
38+
stringBuilder.Append(" - ");
39+
stringBuilder.Append(runtimeFramework);
40+
stringBuilder.Append(']');
41+
}
42+
43+
return stringBuilder.ToString();
44+
}
45+
}

‎src/Platform/Microsoft.Testing.Platform/OutputDevice/SimplifiedConsoleOutputDeviceBase.cs‎

Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,6 @@ internal abstract class SimplifiedConsoleOutputDeviceBase : IPlatformOutputDevic
2121
ITestSessionLifetimeHandler,
2222
IAsyncInitializableExtension
2323
{
24-
#pragma warning disable SA1310 // Field names should not contain underscore
25-
private const string TESTINGPLATFORM_CONSOLEOUTPUTDEVICE_SKIP_BANNER = nameof(TESTINGPLATFORM_CONSOLEOUTPUTDEVICE_SKIP_BANNER);
26-
#pragma warning restore SA1310 // Field names should not contain underscore
27-
2824
private readonly IConsole _console;
2925
private readonly IAsyncMonitor _asyncMonitor;
3026
private readonly IRuntimeFeature _runtimeFeature;
@@ -74,7 +70,7 @@ protected SimplifiedConsoleOutputDeviceBase(
7470

7571
_assemblyName = testApplicationModuleInfo.GetDisplayName();
7672

77-
if (environment.GetEnvironmentVariable(TESTINGPLATFORM_CONSOLEOUTPUTDEVICE_SKIP_BANNER) is not null)
73+
if (environment.GetEnvironmentVariable(OutputDeviceBannerHelper.TESTINGPLATFORM_CONSOLEOUTPUTDEVICE_SKIP_BANNER) is not null)
7874
{
7975
_bannerDisplayed = true;
8076
}
@@ -127,7 +123,7 @@ public async Task DisplayBannerAsync(string? bannerMessage, CancellationToken ca
127123
}
128124

129125
// skip the banner for the children processes
130-
_environment.SetEnvironmentVariable(TESTINGPLATFORM_CONSOLEOUTPUTDEVICE_SKIP_BANNER, "1");
126+
_environment.SetEnvironmentVariable(OutputDeviceBannerHelper.TESTINGPLATFORM_CONSOLEOUTPUTDEVICE_SKIP_BANNER, "1");
131127

132128
_bannerDisplayed = true;
133129

@@ -137,33 +133,7 @@ public async Task DisplayBannerAsync(string? bannerMessage, CancellationToken ca
137133
return;
138134
}
139135

140-
StringBuilder stringBuilder = new();
141-
stringBuilder.Append(_platformInformation.Name);
142-
143-
if (_platformInformation.Version is { } version)
144-
{
145-
stringBuilder.Append(CultureInfo.InvariantCulture, $" v{version}");
146-
if (_platformInformation.CommitHash is { } commitHash)
147-
{
148-
stringBuilder.Append(CultureInfo.InvariantCulture, $"+{commitHash[..10]}");
149-
}
150-
}
151-
152-
if (_platformInformation.BuildDate is { } buildDate)
153-
{
154-
stringBuilder.Append(CultureInfo.InvariantCulture, $" (UTC {buildDate.UtcDateTime:d})");
155-
}
156-
157-
if (_runtimeFeature.IsDynamicCodeSupported)
158-
{
159-
stringBuilder.Append(" [");
160-
stringBuilder.Append(_longArchitecture);
161-
stringBuilder.Append(" - ");
162-
stringBuilder.Append(_runtimeFramework);
163-
stringBuilder.Append(']');
164-
}
165-
166-
ConsoleLog(stringBuilder.ToString());
136+
ConsoleLog(OutputDeviceBannerHelper.BuildBannerText(_platformInformation, _runtimeFeature, _longArchitecture, _runtimeFramework));
167137
}
168138
}
169139

‎src/Platform/Microsoft.Testing.Platform/OutputDevice/TerminalOutputDevice.cs‎

Lines changed: 4 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ internal sealed partial class TerminalOutputDevice : IHotReloadPlatformOutputDev
2525
IAsyncInitializableExtension
2626
{
2727
#pragma warning disable SA1310 // Field names should not contain underscore
28-
private const string TESTINGPLATFORM_CONSOLEOUTPUTDEVICE_SKIP_BANNER = nameof(TESTINGPLATFORM_CONSOLEOUTPUTDEVICE_SKIP_BANNER);
29-
3028
// Opt-in knobs (env vars only) for the silence-driven heartbeat renderer used in non-cursor modes.
3129
private const string MTP_PROGRESS_SILENCE_SECONDS = nameof(MTP_PROGRESS_SILENCE_SECONDS);
3230
private const string MTP_PROGRESS_SLOW_TEST_SECONDS = nameof(MTP_PROGRESS_SLOW_TEST_SECONDS);
@@ -110,7 +108,7 @@ public TerminalOutputDevice(
110108

111109
_assemblyName = testApplicationModuleInfo.GetDisplayName();
112110

113-
if (environment.GetEnvironmentVariable(TESTINGPLATFORM_CONSOLEOUTPUTDEVICE_SKIP_BANNER) is not null)
111+
if (environment.GetEnvironmentVariable(OutputDeviceBannerHelper.TESTINGPLATFORM_CONSOLEOUTPUTDEVICE_SKIP_BANNER) is not null)
114112
{
115113
_bannerDisplayed = true;
116114
}
@@ -371,7 +369,7 @@ public async Task DisplayBannerAsync(string? bannerMessage, CancellationToken ca
371369
// The env var propagates to any child test host the controller spawns so they also
372370
// stay quiet — important because the JSON document must be the sole stdout content.
373371
_bannerDisplayed = true;
374-
_environment.SetEnvironmentVariable(TESTINGPLATFORM_CONSOLEOUTPUTDEVICE_SKIP_BANNER, "1");
372+
_environment.SetEnvironmentVariable(OutputDeviceBannerHelper.TESTINGPLATFORM_CONSOLEOUTPUTDEVICE_SKIP_BANNER, "1");
375373
return;
376374
}
377375

@@ -380,7 +378,7 @@ public async Task DisplayBannerAsync(string? bannerMessage, CancellationToken ca
380378
if (!_bannerDisplayed && !_isServerMode)
381379
{
382380
// skip the banner for the children processes
383-
_environment.SetEnvironmentVariable(TESTINGPLATFORM_CONSOLEOUTPUTDEVICE_SKIP_BANNER, "1");
381+
_environment.SetEnvironmentVariable(OutputDeviceBannerHelper.TESTINGPLATFORM_CONSOLEOUTPUTDEVICE_SKIP_BANNER, "1");
384382

385383
_bannerDisplayed = true;
386384

@@ -390,33 +388,7 @@ public async Task DisplayBannerAsync(string? bannerMessage, CancellationToken ca
390388
}
391389
else
392390
{
393-
StringBuilder stringBuilder = new();
394-
stringBuilder.Append(_platformInformation.Name);
395-
396-
if (_platformInformation.Version is { } version)
397-
{
398-
stringBuilder.Append(CultureInfo.InvariantCulture, $" v{version}");
399-
if (_platformInformation.CommitHash is { } commitHash)
400-
{
401-
stringBuilder.Append(CultureInfo.InvariantCulture, $"+{commitHash[..10]}");
402-
}
403-
}
404-
405-
if (_platformInformation.BuildDate is { } buildDate)
406-
{
407-
stringBuilder.Append(CultureInfo.InvariantCulture, $" (UTC {buildDate.UtcDateTime:d})");
408-
}
409-
410-
if (_runtimeFeature.IsDynamicCodeSupported)
411-
{
412-
stringBuilder.Append(" [");
413-
stringBuilder.Append(_longArchitecture);
414-
stringBuilder.Append(" - ");
415-
stringBuilder.Append(_runtimeFramework);
416-
stringBuilder.Append(']');
417-
}
418-
419-
_terminalTestReporter.WriteMessage(stringBuilder.ToString());
391+
_terminalTestReporter.WriteMessage(OutputDeviceBannerHelper.BuildBannerText(_platformInformation, _runtimeFeature, _longArchitecture, _runtimeFramework));
420392
}
421393
}
422394

0 commit comments

Comments
 (0)