Skip to content

Commit

Permalink
Implement ability for project properties to suppress and elevate warn…
Browse files Browse the repository at this point in the history
…ings.

Introduce MSBuildTreatWarningsAsErrors property that when set to true will treat all warnings as errors when building a project.
Introduce MSBuildWarningsAsErrors property which is a semicolon delmited list of warning codes to treat as errors when building a project.
Introduce MSBuildWarningsAsMessages property which is a semicolon delimited list of warning codes to treat as low importance messages.

This allows users to control warnings at the project level via standard MSBuild properties.  They can be set in a project, an import, or from the command-line.

The limitation here is that the warnings can only include ones that occur during a build.  This is because the properties are not read until after parsing so warnings generated during parse/evaluation happen too soon.  For these warnings, users will only be able to treat them as errors/messages with the /WarnAsError and /WarnAsMessage command-line arguments.

Closes #1886
  • Loading branch information
jeffkl committed Mar 30, 2017
1 parent 2d07945 commit 81b0bf8
Show file tree
Hide file tree
Showing 11 changed files with 725 additions and 6 deletions.
257 changes: 256 additions & 1 deletion src/Build.UnitTests/BackEnd/EventSourceSink_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//-----------------------------------------------------------------------

using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using Microsoft.Build.Framework;
using Microsoft.Build.BackEnd.Logging;
Expand Down Expand Up @@ -170,6 +171,7 @@ public void TreatWarningsAsErrorWhenAllSpecified()
Assert.IsType<BuildErrorEventArgs>(eventHandlerHelper.RaisedEvent);
}


/// <summary>
/// Verifies that a warning is logged as a low importance message when it's warning code is specified.
/// </summary>
Expand Down Expand Up @@ -239,6 +241,256 @@ public void NotTreatWarningsAsMessagesWhenNotSpecified()
Assert.Equal(expectedBuildEvent, eventHandlerHelper.RaisedEvent);
}

/// <summary>
/// Verifies that warnings are treated as an error for a particular project when codes are specified.
/// </summary>
[Fact]
public void TreatWarningsAsErrorByProjectWhenSpecified()
{
BuildWarningEventArgs expectedBuildEvent = RaiseEventHelper.Warning;

EventSourceSink eventSourceSink = new EventSourceSink()
{
WarningsAsErrorsByProject = new Dictionary<int, ISet<string>>
{
{
RaiseEventHelper.Warning.BuildEventContext.ProjectInstanceId,
new HashSet<string>
{
"123",
expectedBuildEvent.Code,
"ABC"
}
}
}
};

RaiseEventHelper raiseEventHelper = new RaiseEventHelper(eventSourceSink);
EventHandlerHelper eventHandlerHelper = new EventHandlerHelper(eventSourceSink, null);

raiseEventHelper.RaiseBuildEvent(RaiseEventHelper.Warning);

Assert.IsType<BuildErrorEventArgs>(eventHandlerHelper.RaisedEvent);

BuildErrorEventArgs actualBuildEvent = (BuildErrorEventArgs)eventHandlerHelper.RaisedEvent;

Assert.Equal(expectedBuildEvent.Code, actualBuildEvent.Code);
Assert.Equal(expectedBuildEvent.File, actualBuildEvent.File);
Assert.Equal(expectedBuildEvent.ProjectFile, actualBuildEvent.ProjectFile);
Assert.Equal(expectedBuildEvent.Subcategory, actualBuildEvent.Subcategory);
Assert.Equal(expectedBuildEvent.HelpKeyword, actualBuildEvent.HelpKeyword);
Assert.Equal(expectedBuildEvent.Message, actualBuildEvent.Message);
Assert.Equal(expectedBuildEvent.SenderName, actualBuildEvent.SenderName);
Assert.Equal(expectedBuildEvent.ColumnNumber, actualBuildEvent.ColumnNumber);
Assert.Equal(expectedBuildEvent.EndColumnNumber, actualBuildEvent.EndColumnNumber);
Assert.Equal(expectedBuildEvent.EndLineNumber, actualBuildEvent.EndLineNumber);
Assert.Equal(expectedBuildEvent.LineNumber, actualBuildEvent.LineNumber);
Assert.Equal(expectedBuildEvent.BuildEventContext, actualBuildEvent.BuildEventContext);
Assert.Equal(expectedBuildEvent.ThreadId, actualBuildEvent.ThreadId);
Assert.Equal(expectedBuildEvent.Timestamp, actualBuildEvent.Timestamp);
}

/// <summary>
/// Verifies that warnings are not treated as errors for a particular project even though a matching code was added for another project.
/// </summary>
[Fact]
public void NotTreatWarningsAsErrorByProjectWhenProjectNotSpecified()
{
BuildWarningEventArgs expectedBuildEvent = RaiseEventHelper.Warning;

EventSourceSink eventSourceSink = new EventSourceSink()
{
WarningsAsErrorsByProject = new Dictionary<int, ISet<string>>
{
{
expectedBuildEvent.BuildEventContext.ProjectInstanceId + 100,
new HashSet<string>
{
"123",
expectedBuildEvent.Code,
"ABC"
}
}
}
};

RaiseEventHelper raiseEventHelper = new RaiseEventHelper(eventSourceSink);
EventHandlerHelper eventHandlerHelper = new EventHandlerHelper(eventSourceSink, null);

raiseEventHelper.RaiseBuildEvent(RaiseEventHelper.Warning);

Assert.Equal(expectedBuildEvent, eventHandlerHelper.RaisedEvent);
}

/// <summary>
/// Verifies that warnings are not treated as errors for a particular project when a code does not match.
/// </summary>
[Fact]
public void NotTreatWarningsAsErrorByProjectWhenCodeNotSpecified()
{
BuildWarningEventArgs expectedBuildEvent = RaiseEventHelper.Warning;

EventSourceSink eventSourceSink = new EventSourceSink()
{
WarningsAsErrorsByProject = new Dictionary<int, ISet<string>>
{
{
expectedBuildEvent.BuildEventContext.ProjectInstanceId,
new HashSet<string>
{
"123",
"ABC"
}
}
}
};

RaiseEventHelper raiseEventHelper = new RaiseEventHelper(eventSourceSink);
EventHandlerHelper eventHandlerHelper = new EventHandlerHelper(eventSourceSink, null);

raiseEventHelper.RaiseBuildEvent(RaiseEventHelper.Warning);

Assert.Equal(expectedBuildEvent, eventHandlerHelper.RaisedEvent);
}

/// <summary>
/// Verifies that all warnings are treated as errors for a particular project.
/// </summary>
[Fact]
public void TreatWarningsAsErrorByProjectWhenAllSpecified()
{
EventSourceSink eventSourceSink = new EventSourceSink()
{
WarningsAsErrorsByProject = new Dictionary<int, ISet<string>>
{
{
RaiseEventHelper.Warning.BuildEventContext.ProjectInstanceId,
new HashSet<string>()
}
}
};

RaiseEventHelper raiseEventHelper = new RaiseEventHelper(eventSourceSink);
EventHandlerHelper eventHandlerHelper = new EventHandlerHelper(eventSourceSink, null);

raiseEventHelper.RaiseBuildEvent(RaiseEventHelper.Warning);

Assert.IsType<BuildErrorEventArgs>(eventHandlerHelper.RaisedEvent);
}

/// <summary>
/// Verifies that warnings are treated as messages for a particular project.
/// </summary>
[Fact]
public void TreatWarningsAsMessagesByProjectWhenSpecified()
{
BuildWarningEventArgs expectedBuildEvent = RaiseEventHelper.Warning;

EventSourceSink eventSourceSink = new EventSourceSink()
{
WarningsAsMessagesByProject = new Dictionary<int, ISet<string>>
{
{
expectedBuildEvent.BuildEventContext.ProjectInstanceId,
new HashSet<string>
{
"FOO",
expectedBuildEvent.Code,
"BAR"
}
}
}
};

RaiseEventHelper raiseEventHelper = new RaiseEventHelper(eventSourceSink);
EventHandlerHelper eventHandlerHelper = new EventHandlerHelper(eventSourceSink, null);

raiseEventHelper.RaiseBuildEvent(RaiseEventHelper.Warning);

Assert.IsType<BuildMessageEventArgs>(eventHandlerHelper.RaisedEvent);

BuildMessageEventArgs actualBuildEvent = (BuildMessageEventArgs)eventHandlerHelper.RaisedEvent;

Assert.Equal(expectedBuildEvent.BuildEventContext, actualBuildEvent.BuildEventContext);
Assert.Equal(expectedBuildEvent.Code, actualBuildEvent.Code);
Assert.Equal(expectedBuildEvent.ColumnNumber, actualBuildEvent.ColumnNumber);
Assert.Equal(expectedBuildEvent.EndColumnNumber, actualBuildEvent.EndColumnNumber);
Assert.Equal(expectedBuildEvent.EndLineNumber, actualBuildEvent.EndLineNumber);
Assert.Equal(expectedBuildEvent.File, actualBuildEvent.File);
Assert.Equal(expectedBuildEvent.HelpKeyword, actualBuildEvent.HelpKeyword);
Assert.Equal(MessageImportance.Low, actualBuildEvent.Importance);
Assert.Equal(expectedBuildEvent.LineNumber, actualBuildEvent.LineNumber);
Assert.Equal(expectedBuildEvent.Message, actualBuildEvent.Message);
Assert.Equal(expectedBuildEvent.ProjectFile, actualBuildEvent.ProjectFile);
Assert.Equal(expectedBuildEvent.SenderName, actualBuildEvent.SenderName);
Assert.Equal(expectedBuildEvent.Subcategory, actualBuildEvent.Subcategory);
Assert.Equal(expectedBuildEvent.ThreadId, actualBuildEvent.ThreadId);
Assert.Equal(expectedBuildEvent.Timestamp, actualBuildEvent.Timestamp);
}

/// <summary>
/// Verifies that warnings are not treated as messages for a particular project when a code does not match.
/// </summary>
[Fact]
public void NotTreatWarningsAsMessagesByProjectWhenCodeNotSpecified()
{
BuildWarningEventArgs expectedBuildEvent = RaiseEventHelper.Warning;

EventSourceSink eventSourceSink = new EventSourceSink()
{
WarningsAsMessagesByProject = new Dictionary<int, ISet<string>>
{
{
expectedBuildEvent.BuildEventContext.ProjectInstanceId,
new HashSet<string>
{
"FOO",
"BAR"
}
}
}
};

RaiseEventHelper raiseEventHelper = new RaiseEventHelper(eventSourceSink);
EventHandlerHelper eventHandlerHelper = new EventHandlerHelper(eventSourceSink, null);

raiseEventHelper.RaiseBuildEvent(RaiseEventHelper.Warning);

Assert.Equal(expectedBuildEvent, eventHandlerHelper.RaisedEvent);
}

/// <summary>
/// Verifies that warnings are not treated as messages for a particular project even though a matching code was added for another project.
/// </summary>
[Fact]
public void NotTreatWarningsAsMessagesByProjectWhenProjectNotSpecified()
{
BuildWarningEventArgs expectedBuildEvent = RaiseEventHelper.Warning;

EventSourceSink eventSourceSink = new EventSourceSink()
{
WarningsAsMessagesByProject = new Dictionary<int, ISet<string>>
{
{
expectedBuildEvent.BuildEventContext.ProjectInstanceId + 100, // Ensures that the project instance ID doesn't match
new HashSet<string>
{
"FOO",
expectedBuildEvent.Code,
"BAR"
}
}
}
};

RaiseEventHelper raiseEventHelper = new RaiseEventHelper(eventSourceSink);
EventHandlerHelper eventHandlerHelper = new EventHandlerHelper(eventSourceSink, null);

raiseEventHelper.RaiseBuildEvent(RaiseEventHelper.Warning);

Assert.Equal(expectedBuildEvent, eventHandlerHelper.RaisedEvent);
}

#region TestsThrowingLoggingExceptions

/// <summary>
Expand Down Expand Up @@ -898,7 +1150,10 @@ internal class RaiseEventHelper
/// <summary>
/// Build Warning Event
/// </summary>
private static BuildWarningEventArgs s_buildWarning = new BuildWarningEventArgs("SubCategoryForSchemaValidationErrors", "MSB4000", "file", 1, 2, 3, 4, "message", "help", "sender");
private static BuildWarningEventArgs s_buildWarning = new BuildWarningEventArgs("SubCategoryForSchemaValidationErrors", "MSB4000", "file", 1, 2, 3, 4, "message", "help", "sender")
{
BuildEventContext = new BuildEventContext(1, 2, 3, 4, 5, 6)
};

/// <summary>
/// Build Error Event
Expand Down
10 changes: 10 additions & 0 deletions src/Build.UnitTests/BackEnd/MockLoggingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,16 @@ public bool SerializeAllProperties
set;
}

public void AddWarningsAsMessages(int projectInstanceId, ISet<string> codes)
{
throw new NotImplementedException();
}

public void AddWarningsAsErrors(int projectInstanceId, ISet<string> codes)
{
throw new NotImplementedException();
}

/// <summary>
/// Registers a distributed logger.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@
<Compile Include="TargetsFile_Test.cs" />
<Compile Include="TestUtilities.cs" />
<Compile Include="Utilities_Tests.cs" />
<Compile Include="WarningsAsMessagesAndErrors_Tests.cs" />
<None Include="..\Shared\UnitTests\App.config">
<Link>App.config</Link>
<SubType>Designer</SubType>
Expand Down
Loading

0 comments on commit 81b0bf8

Please sign in to comment.