Skip to content

Commit

Permalink
TW-73982 .NET runner and C# script runner do not propagate TEAMCITY_P…
Browse files Browse the repository at this point in the history
…ROCESS_FLOW_ID environment variables to spawned processes
  • Loading branch information
NikolayPianikov authored and NikolayPianikov committed Nov 17, 2021
1 parent 6ae3c99 commit a17712d
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.0.0" />
<PackageReference Include="Moq" Version="4.16.1" />
<PackageReference Include="Shouldly" Version="4.0.3" />
<PackageReference Include="xunit" Version="2.4.1" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public void ShouldSendMessageInDefaultFlow()
public void ShouldNotCreateNestedServiceMessage(string serviceMessage)
{
// Given
var writer = CreateInstance(new Parameters {PlainServiceMessage = true});
var writer = CreateInstance(new Parameters(Mock.Of<IEnvironment>()) {PlainServiceMessage = true});
var serviceMessage1 = new ServiceMessage("message");
var serviceMessage2 = new ServiceMessage("publishArtifacts");
_serviceMessageParser.Setup(i => i.ParseServiceMessages(serviceMessage.Trim())).Returns(new IServiceMessage[] { serviceMessage1, serviceMessage2 });
Expand Down Expand Up @@ -554,7 +554,7 @@ public void ShouldNotOpenFlowWhenNoBlocks()
private TeamCityHierarchicalMessageWriter CreateInstance([CanBeNull] Parameters parameters = null)
{
var loggerContext = new Mock<ILoggerContext>();
loggerContext.SetupGet(i => i.Parameters).Returns(parameters ?? new Parameters());
loggerContext.SetupGet(i => i.Parameters).Returns(parameters ?? new Parameters(Mock.Of<IEnvironment>()));
return new TeamCityHierarchicalMessageWriter(
loggerContext.Object,
_colorTheme.Object,
Expand Down
1 change: 1 addition & 0 deletions TeamCity.MSBuild.Logger/Composer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ private static void Setup() =>
.Bind<IConsole>().Bind<IInitializable>().To<DefaultConsole>()
.Bind<IStringService>().To<StringService>()
.Bind<IPathService>().To<PathService>()
.Bind<Parameters>().To<Parameters>()
.Bind<IParametersParser>().To<ParametersParser>()
.Bind<IPerformanceCounterFactory>().To<PerformanceCounterFactory>()
.Bind<ILogFormatter>().To<LogFormatter>()
Expand Down
19 changes: 19 additions & 0 deletions TeamCity.MSBuild.Logger/Environment.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,27 @@
namespace TeamCity.MSBuild.Logger
{
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

// ReSharper disable once ClassNeverInstantiated.Global
internal class Environment : IEnvironment
{
private static readonly Dictionary<string, string> Envs = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);

static Environment()
{
foreach (var entry in System.Environment.GetEnvironmentVariables().OfType<DictionaryEntry>())
{
var key = entry.Key?.ToString()?.Trim() ?? string.Empty;
var value = entry.Value?.ToString()?.Trim() ?? string.Empty;
Envs[key] = value;
}
}

public string GetEnvironmentVariable(string name) => Envs.TryGetValue(name, out var val) ? val : string.Empty;

public string DiagnosticsFile => System.Environment.GetEnvironmentVariable("MSBUILDDIAGNOSTICSFILE") ?? string.Empty;

public bool TargetOutputLogging => !string.IsNullOrEmpty(System.Environment.GetEnvironmentVariable("MSBUILDTARGETOUTPUTLOGGING"));
Expand Down
2 changes: 2 additions & 0 deletions TeamCity.MSBuild.Logger/IEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

internal interface IEnvironment
{
string GetEnvironmentVariable(string name);

bool TargetOutputLogging { get; }

[NotNull] string DiagnosticsFile { get; }
Expand Down
4 changes: 3 additions & 1 deletion TeamCity.MSBuild.Logger/NodeLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
// ReSharper disable once ClassWithVirtualMembersNeverInherited.Global
internal class NodeLogger : INodeLogger
{
[NotNull] private readonly Parameters _parameters;
[NotNull] private readonly ILoggerContext _context;
[NotNull] private readonly IEnvironment _environment;
[NotNull] private readonly IDiagnostics _diagnostics;
Expand All @@ -29,14 +30,14 @@ internal class NodeLogger : INodeLogger
[NotNull] private readonly IBuildEventHandler<BuildStartedEventArgs> _buildStartedEventHandler;
[NotNull] private readonly IParametersParser _parametersParser;
[NotNull] private readonly ILogWriter _logWriter;
[NotNull] private readonly Parameters _parameters = new Parameters();
[NotNull] private readonly object _lockObject = new object();
// ReSharper disable once IdentifierTypo
private int _reentrancy;

public NodeLogger(
// ReSharper disable once UnusedParameter.Local
// ReSharper disable once IdentifierTypo
[NotNull] Parameters parameters,
[NotNull] IInitializable[] initializables,
[NotNull] IParametersParser parametersParser,
[NotNull] ILogWriter logWriter,
Expand All @@ -57,6 +58,7 @@ public NodeLogger(
[NotNull] IBuildEventHandler<BuildWarningEventArgs> warningHandler,
[NotNull] IBuildEventHandler<CustomBuildEventArgs> customEventHandler)
{
_parameters = parameters ?? throw new ArgumentNullException(nameof(parameters));
_context = context ?? throw new ArgumentNullException(nameof(context));
_environment = environment ?? throw new ArgumentNullException(nameof(environment));
_diagnostics = diagnostics ?? throw new ArgumentNullException(nameof(diagnostics));
Expand Down
9 changes: 8 additions & 1 deletion TeamCity.MSBuild.Logger/Parameters.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
namespace TeamCity.MSBuild.Logger
{
using System;
using JetBrains.Annotations;
using Microsoft.Build.Framework;

internal class Parameters
{
private readonly IEnvironment _environment;

public Parameters([NotNull] IEnvironment environment) =>
_environment = environment ?? throw new ArgumentNullException(nameof(environment));

public bool Debug { get; set; }

public bool ShowOnlyWarnings { get; set; }
Expand Down Expand Up @@ -47,7 +54,7 @@ internal class Parameters
public bool PlainServiceMessage { get; set; }

// ReSharper disable once MemberCanBeMadeStatic.Global
public string FlowId => System.Environment.GetEnvironmentVariable("TEAMCITY_PROCESS_FLOW_ID") ?? string.Empty;
public string FlowId => _environment.GetEnvironmentVariable("TEAMCITY_PROCESS_FLOW_ID") ?? string.Empty;

public override string ToString() => $"{nameof(Debug)}: {Debug}, {nameof(ShowOnlyWarnings)}: {ShowOnlyWarnings}, {nameof(ShowEnvironment)}: {ShowEnvironment}, {nameof(Verbosity)}: {Verbosity}, {nameof(ShowPerfSummary)}: {ShowPerfSummary}, {nameof(ShowItemAndPropertyList)}: {ShowItemAndPropertyList}, {nameof(ShowSummary)}: {ShowSummary}, {nameof(ShowOnlyErrors)}: {ShowOnlyErrors}, {nameof(ShowProjectFile)}: {ShowProjectFile}, {nameof(ShowCommandLine)}: {ShowCommandLine}, {nameof(ShowTimeStamp)}: {ShowTimeStamp}, {nameof(ShowEventId)}: {ShowEventId}, {nameof(ForceNoAlign)}: {ForceNoAlign}, {nameof(AlignMessages)}: {AlignMessages}, {nameof(ShowTargetOutputs)}: {ShowTargetOutputs}, {nameof(BufferWidth)}: {BufferWidth}, {nameof(ColorMode)}: {ColorMode}, {nameof(TeamCityMode)}: {TeamCityMode}, {nameof(StatisticsMode)}: {StatisticsMode}, {nameof(ColorThemeMode)}: {ColorThemeMode}, {nameof(PlainServiceMessage)}: {PlainServiceMessage}";
}
Expand Down
4 changes: 2 additions & 2 deletions TeamCity.MSBuild.Logger/TeamCity.MSBuild.Logger.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="JetBrains.Annotations" Version="2021.2.0" />
<PackageReference Include="Pure.DI" Version="1.1.3">
<PackageReference Include="JetBrains.Annotations" Version="2021.3.0" />
<PackageReference Include="Pure.DI" Version="1.1.18">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
6 changes: 2 additions & 4 deletions TeamCity.MSBuild.Logger/TeamCityHierarchicalMessageWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -193,10 +193,8 @@ private void Write([NotNull] string message, [NotNull] Flow flow)
}
}

private string FormatMessage(MessageState messageState, MessageInfo messageInfo, string text)
{
return messageState == MessageState.Normal && messageInfo.Color.HasValue ? $"\x001B[{_colorTheme.GetAnsiColor(messageInfo.Color.Value)}m{text}" : text;
}
private string FormatMessage(MessageState messageState, MessageInfo messageInfo, string text) =>
messageState == MessageState.Normal && !string.IsNullOrWhiteSpace(text) && messageInfo.Color.HasValue ? $"\x001B[{_colorTheme.GetAnsiColor(messageInfo.Color.Value)}m{text}" : text;

private enum MessageState
{
Expand Down
17 changes: 4 additions & 13 deletions TeamCity.MSBuild.Logger/TeamCityMSBuildLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace TeamCity.MSBuild.Logger
// ReSharper disable once UnusedMember.Global
public class TeamCityMsBuildLogger : INodeLogger
{
private readonly INodeLogger _logger = Composer.Resolve<INodeLogger>();
private readonly INodeLogger _logger = Composer.ResolveINodeLogger();

public string Parameters
{
Expand All @@ -20,19 +20,10 @@ public LoggerVerbosity Verbosity
set => _logger.Verbosity = value;
}

public void Initialize(IEventSource eventSource, int nodeCount)
{
_logger.Initialize(eventSource, nodeCount);
}
public void Initialize(IEventSource eventSource, int nodeCount) => _logger.Initialize(eventSource, nodeCount);

public void Initialize(IEventSource eventSource)
{
_logger.Initialize(eventSource);
}
public void Initialize(IEventSource eventSource) => _logger.Initialize(eventSource);

public void Shutdown()
{
_logger.Shutdown();
}
public void Shutdown() => _logger.Shutdown();
}
}

0 comments on commit a17712d

Please sign in to comment.