Skip to content

Commit

Permalink
Add 'warning as error' parameter
Browse files Browse the repository at this point in the history
Fix #25
  • Loading branch information
Jérémie Bertrand committed Sep 4, 2016
1 parent d5a45ca commit bba2afa
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
27 changes: 26 additions & 1 deletion src/NVika.Tests/ParseReportCommandTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,31 @@ public void Execute_MultipleReports_ShouldWriteMessageFromIssues()
Assert.Contains("Message6", logs);
}

[Fact]
public void Execute_WarningAsError_ShouldExitWithCode5()
{
// arrange
var logger = GetLogger();
var fileSystem = new MockFileSystem(new Dictionary<string, MockFileData> { { "report.xml", new MockFileData("<root></root>") } });
var localBuildServer = new LocalBuildServer(logger);
var mockBuildServer = GetMockBuildServer(true);
var buildServers = new List<IBuildServer> { localBuildServer, mockBuildServer };
var parsers = new List<IReportParser> { GetMockReportParser(true, false) };
var buildServerCommand = new ParseReportCommand(logger, fileSystem, buildServers, localBuildServer, parsers);
var remainingArgs = buildServerCommand.GetActualOptions().Parse(new[] { "report.xml", "--warningaserror" });

// act
var exitCode = buildServerCommand.Run(remainingArgs.ToArray());

// assert
Assert.Equal(5, exitCode);
var logs = _loggerOutput.ToString();
Assert.Contains("[Suggestion] Message1", logs);
Assert.Contains("[Error] Message2", logs);
Assert.Contains("[Error] Message3", logs);
Assert.Contains("[Fatal] Issues with severity error was found: the build will fail", logs);
}

private IReportParser GetMockReportParser(bool canParse = false, bool issuesContainError = true, bool alternate = false)
{
var mockReportParser = Substitute.For<IReportParser>();
Expand Down Expand Up @@ -326,7 +351,7 @@ private IBuildServer GetMockBuildServer(bool canApplyToCurrentContext = false)
mockBuildServer.Name.Returns("MockBuildServer");
mockBuildServer.CanApplyToCurrentContext().Returns(canApplyToCurrentContext);
mockBuildServer.When(bs => bs.ApplyParameters(Arg.Any<bool>())).Do(ci => _mockBuildServer_IncludeSource = ci.Arg<bool>());
mockBuildServer.When(bs => bs.WriteMessage(Arg.Any<Issue>())).Do(ci => _loggerOutput.AppendLine(ci.Arg<Issue>().Message + (_mockBuildServer_IncludeSource ? " - " + ci.Arg<Issue>().Source : string.Empty)));
mockBuildServer.When(bs => bs.WriteMessage(Arg.Any<Issue>())).Do(ci => _loggerOutput.AppendLine("[" + ci.Arg<Issue>().Severity + "] " + ci.Arg<Issue>().Message + (_mockBuildServer_IncludeSource ? " - " + ci.Arg<Issue>().Source : string.Empty)));
return mockBuildServer;
}

Expand Down
17 changes: 17 additions & 0 deletions src/NVika/ParseReportCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ internal class ParseReportCommand : ConsoleCommand
private readonly LocalBuildServer _localBuildServer;
private readonly IEnumerable<IReportParser> _parsers;
private bool _includeSourceInMessage;
private bool _warningAsError;

[ImportingConstructor]
public ParseReportCommand(ILogger logger,
Expand All @@ -34,6 +35,7 @@ public ParseReportCommand(ILogger logger,

IsCommand("parsereport", "Parse the report and show warnings in console or inject them to the build server");
HasOption("includesource", "Include the source in messages", s => _includeSourceInMessage = true);
HasOption("warningaserror", "Consider all warnings as errors", s => _warningAsError = true);
AllowsAnyAdditionalArguments("Reports to analyze");
}

Expand Down Expand Up @@ -74,6 +76,8 @@ public override int Run(string[] reportPaths)
_logger.Debug("Report type is {Name}", parser.Name);

var issues = parser.Parse(reportPath);
issues = AlignIssuesSeverity(_warningAsError, issues);

_logger.Information("{Count} issues was found", issues.Count());

foreach (var issue in issues)
Expand All @@ -99,6 +103,19 @@ public override int Run(string[] reportPaths)
return returnCode;
}

private static IEnumerable<Issue> AlignIssuesSeverity(bool warningAsError, IEnumerable<Issue> issues)
{
issues = issues.Select(issue =>
{
if (warningAsError && issue.Severity == IssueSeverity.Warning)
{
issue.Severity = IssueSeverity.Error;
}
return issue;
});
return issues;
}

private IEnumerable<IBuildServer> GetApplicableBuildServer()
{
var applicableBuildServers = _buildServers.Where(bs => bs.CanApplyToCurrentContext());
Expand Down

0 comments on commit bba2afa

Please sign in to comment.