Skip to content

Commit

Permalink
Сhange ProperyReassignment logged message type (#9494)
Browse files Browse the repository at this point in the history
  • Loading branch information
YuliiaKovalova committed Dec 11, 2023
1 parent 7ec0e97 commit 9276c4e
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 14 deletions.
65 changes: 58 additions & 7 deletions src/Build.UnitTests/Evaluation/Evaluator_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4597,7 +4597,7 @@ public void VerifyMSBuildLogsAMessageWhenLocalPropertyCannotOverrideValueOfGloba
public void VerifyPropertyTrackingLoggingDefault()
{
// Having just environment variables defined should default to nothing being logged except one environment variable read.
this.VerifyPropertyTrackingLoggingScenario(
VerifyPropertyTrackingLoggingScenario(
null,
logger =>
{
Expand All @@ -4616,7 +4616,8 @@ public void VerifyPropertyTrackingLoggingDefault()
logger
.AllBuildEvents
.OfType<PropertyReassignmentEventArgs>()
.ShouldBeEmpty();
.Count()
.ShouldBe(2);
logger
.AllBuildEvents
Expand All @@ -4628,7 +4629,7 @@ public void VerifyPropertyTrackingLoggingDefault()
[Fact]
public void VerifyPropertyTrackingLoggingPropertyReassignment()
{
this.VerifyPropertyTrackingLoggingScenario(
VerifyPropertyTrackingLoggingScenario(
"1",
logger =>
{
Expand Down Expand Up @@ -4677,13 +4678,14 @@ public void VerifyPropertyTrackingLoggingNone()
logger
.AllBuildEvents
.OfType<PropertyReassignmentEventArgs>()
.OfType<PropertyInitialValueSetEventArgs>()
.ShouldBeEmpty();
logger
.AllBuildEvents
.OfType<PropertyInitialValueSetEventArgs>()
.ShouldBeEmpty();
.AllBuildEvents
.OfType<PropertyReassignmentEventArgs>()
.Count()
.ShouldBe(2);
});
}

Expand Down Expand Up @@ -4919,6 +4921,55 @@ private void VerifyPropertyTrackingLoggingScenario(string envVarValue, Action<Mo
}
}

/// <summary>
/// Log when a property is being assigned a new value.
/// </summary>
[Fact]
public void VerifyLogPropertyReassignment()
{
string propertyName = "Prop";
string propertyOldValue = "OldValue";
string propertyNewValue = "NewValue";
string testtargets = ObjectModelHelpers.CleanupFileContents(@$"
<Project xmlns='msbuildnamespace'>
<PropertyGroup>
<{propertyName}>{propertyOldValue}</{propertyName}>
<{propertyName}>{propertyNewValue}</{propertyName}>
</PropertyGroup>
<Target Name=""Test""/>
</Project>");

string tempPath = Path.GetTempPath();
string targetDirectory = Path.Combine(tempPath, "LogPropertyAssignments");
string testTargetPath = Path.Combine(targetDirectory, "test.proj");

using (TestEnvironment env = TestEnvironment.Create())
{
env.CreateFolder(targetDirectory);
env.CreateFile(testTargetPath, testtargets);

MockLogger logger = new()
{
Verbosity = LoggerVerbosity.Diagnostic,
};
ProjectCollection pc = new();
pc.RegisterLogger(logger);
Project project = pc.LoadProject(testTargetPath);

bool result = project.Build();
result.ShouldBeTrue();
logger.BuildMessageEvents
.OfType<PropertyReassignmentEventArgs>()
.ShouldContain(r => r.PropertyName == propertyName
&& r.PreviousValue == propertyOldValue
&& r.NewValue == propertyNewValue
&& r.Message.StartsWith($"{
ResourceUtilities.FormatResourceStringIgnoreCodeAndKeyword(
"PropertyReassignment", propertyName, propertyNewValue, propertyOldValue, string.Empty)}"));
logger.BuildMessageEvents.ShouldBeOfTypes(new[] { typeof(PropertyReassignmentEventArgs) });
}
}

#if FEATURE_HTTP_LISTENER
/// <summary>
/// HTTP server code running on a separate thread that expects a connection request
Expand Down
31 changes: 24 additions & 7 deletions src/Build/Evaluation/Evaluator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1348,13 +1348,30 @@ private void LogPropertyReassignment(P predecessor, P property, string location)

if (newValue != oldValue)
{
_evaluationLoggingContext.LogComment(
MessageImportance.Low,
"PropertyReassignment",
property.Name,
newValue,
oldValue,
location);
if (ChangeWaves.AreFeaturesEnabled(ChangeWaves.Wave17_10))
{
var args = new PropertyReassignmentEventArgs(
property.Name,
oldValue,
newValue,
location,
message: null)
{
BuildEventContext = _evaluationLoggingContext.BuildEventContext,
};

_evaluationLoggingContext.LogBuildEvent(args);
}
else
{
_evaluationLoggingContext.LogComment(
MessageImportance.Low,
"PropertyReassignment",
property.Name,
newValue,
oldValue,
location);
}
}
}

Expand Down

0 comments on commit 9276c4e

Please sign in to comment.