Skip to content
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

Implement ability for project properties to suppress and elevate warnings #1928

Merged
merged 3 commits into from
Apr 4, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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