Permalink
Browse files

Added console colors for Pass/Fail/Ignore for better readability

  • Loading branch information...
1 parent 471e986 commit c6819bb07f0d5f4f3e29d5db4f8ec82bd8ccea98 @HEskandari HEskandari committed May 24, 2012
View
1 src/Giles.Core/Giles.Core.csproj
@@ -82,6 +82,7 @@
<Compile Include="Runners\TestResult.cs" />
<Compile Include="Runners\TestFrameworkResolver.cs" />
<Compile Include="UI\DisplayScrubber.cs" />
+ <Compile Include="Utility\CollectionExtensions.cs" />
<Compile Include="Utility\TypeLoader.cs" />
<Compile Include="UI\ConsoleUserDisplay.cs" />
<Compile Include="UI\GrowlUserDisplay.cs" />
View
2 src/Giles.Core/Runners/BuildRunner.cs
@@ -42,7 +42,7 @@ private static string FormatBuildMessages(Stopwatch watch, ExecutionResult resul
result.ExitCode == 0 ? "Success" : "Failure");
if (result.ExitCode != 0)
- message += string.Format("\n{0}", result.Output);
+ message += string.Format("\n{0}", result.ErrorMessage);
return message;
}
}
View
37 src/Giles.Core/Runners/CommandProcessExecutor.cs
@@ -47,16 +47,45 @@ private static ExecutionResult RunExecutable(string fileName, string arguments)
return new ExecutionResult
{
ExitCode = exitCode,
- Output = output,
- Runner = new FileInfo(fileName).Name.ToUpper().Replace(".EXE", string.Empty)
+ Runner = new TestRunnerResult
+ {
+ RunnerName = new FileInfo(fileName).Name.ToUpper().Replace(".EXE", string.Empty)
+ }
};
}
}
public class ExecutionResult
{
- public string Runner { get; set; }
- public string Output { get; set; }
+ public ExecutionResult()
+ {
+ Runner = new TestRunnerResult();
+ }
+ public TestRunnerResult Runner { get; set; }
+ public string ErrorMessage { get; set; }
public int ExitCode { get; set; }
}
+
+ public class TestRunnerResult
+ {
+ public TestRunnerResult()
+ {
+ Stats = new TestStatistics();
+ }
+
+ public string RunnerName { get; set; }
+ public TestStatistics Stats { get; set; }
+
+ public override string ToString()
+ {
+ return string.Format("{0} Passed: {1}, Failed: {2}, Ignored: {3}", RunnerName, Stats.Passed, Stats.Failed, Stats.Ignored);
+ }
+ }
+
+ public class TestStatistics
+ {
+ public int Passed { get; set; }
+ public int Failed { get; set; }
+ public int Ignored { get; set; }
+ }
}
View
63 src/Giles.Core/Runners/GilesTestListener.cs
@@ -67,47 +67,42 @@ public void DisplayResults()
{
if (testRunnerResults.Count == 0)
{
- config.UserDisplay.ToList().ForEach(display =>
- display.DisplayResult(new ExecutionResult
- {
- ExitCode = 1,
- Output = "No tests were run. Check your filter names and test assembly options",
- Runner = ""
- }));
+ config.UserDisplay.ToList().ForEach(display => display.DisplayResult(new ExecutionResult
+ {
+ ExitCode = 1,
+ ErrorMessage = "No tests were run. Check your filter names and test assembly options",
+ }));
return;
}
- var messages = AggregateTestRunnerResults();
-
- var result = new ExecutionResult
- {
- ExitCode = totalResults[TestState.Failed] > 0 ? 1 : 0,
- Output = messages.ToString(),
- Runner = string.Empty
- };
-
- if (config.UserDisplay.Count() == 0)
+ var results = CreateResults();
+
+ if (config.UserDisplay.IsNullOrEmpty())
config.UserDisplay = new List<IUserDisplay> { new ConsoleUserDisplay() };
- config.UserDisplay.ToList().ForEach(display => display.DisplayResult(result));
+ config.UserDisplay.ToList().ForEach(display => results.ForEach(display.DisplayResult));
}
- StringBuilder AggregateTestRunnerResults()
+ private List<ExecutionResult> CreateResults()
{
- var messages = new StringBuilder();
- testRunnerResults.ToList().ForEach(x => messages.Append(
- string.Format(
- "{0} Results: Passed: {1}, Failed: {2}, Ignored: {3}\n",
- x.Key,
- x.Value[TestState.Passed],
- x.Value[TestState.Failed],
- x.Value[TestState.Ignored])));
-
- messages.Append(string.Format("Total Passed: {0}, Failed: {1}, Ignored: {2}",
- totalResults[TestState.Passed],
- totalResults[TestState.Failed],
- totalResults[TestState.Ignored]));
- return messages;
+ var result = new List<ExecutionResult>();
+
+ testRunnerResults.ToList().ForEach(x => result.Add(new ExecutionResult
+ {
+ Runner = new TestRunnerResult
+ {
+ RunnerName = x.Key,
+ Stats = new TestStatistics
+ {
+ Failed = x.Value[TestState.Failed],
+ Passed = x.Value[TestState.Passed],
+ Ignored = x.Value[TestState.Ignored]
+ }
+ },
+ ExitCode = x.Value[TestState.Failed] > 0 ? 1 : 0,
+ }));
+
+ return result;
}
public void DisplayVerboseResults()
@@ -118,7 +113,7 @@ public void DisplayVerboseResults()
public void DisplayErrors()
{
- var messages = new StringBuilder(string.Format("\n\nTest Run Errors ({0})\n", testRunnerFailures.ToList().Count));
+ var messages = new StringBuilder(string.Format("\n\nTest Run Errors ({0})\n", testRunnerFailures.Count()));
testRunnerFailures.Each(x =>
{
messages.AppendLine("-------------------");
View
64 src/Giles.Core/UI/ConsoleUserDisplay.cs
@@ -11,20 +11,74 @@ public ConsoleUserDisplay()
{
defaultConsoleColor = Console.ForegroundColor;
}
+
public void DisplayMessage(string message, params object[] parameters)
{
Console.WriteLine(message.ScrubDisplayStringForFormatting(), parameters);
}
public void DisplayResult(ExecutionResult result)
{
- Console.WriteLine("\n\n======= {0} TEST RUNNER RESULTS =======", result.Runner);
- Console.ForegroundColor = result.ExitCode != 0 ?
- ConsoleColor.Red : defaultConsoleColor;
+ try
+ {
+ Console.WriteLine("\n\n======= {0} TEST RUNNER RESULTS =======", result.Runner.RunnerName);
+
+ using(new ChangedColorContext(GetPassedColor(result)))
+ {
+ Console.Write(string.Format("Passed: {0}", result.Runner.Stats.Passed));
+ }
+
+ Console.Write(", ");
+
+ using (new ChangedColorContext(GetFailedColor(result)))
+ {
+ Console.Write(string.Format("Failed: {0}", result.Runner.Stats.Failed));
+ }
+
+ Console.Write(", ");
+
+ using (new ChangedColorContext(GetIgnoredColor(result)))
+ {
+ Console.Write(string.Format("Ignored: {0}", result.Runner.Stats.Ignored));
+ }
+
+ Console.Write(Environment.NewLine);
+ }
+ finally
+ {
+ Console.ForegroundColor = defaultConsoleColor;
+ }
+ }
+
+ public ConsoleColor GetPassedColor(ExecutionResult result)
+ {
+ return result.Runner.Stats.Passed > 0 ? ConsoleColor.Green : defaultConsoleColor;
+ }
+
+ public ConsoleColor GetFailedColor(ExecutionResult result)
+ {
+ return result.Runner.Stats.Failed > 0 ? ConsoleColor.Red : defaultConsoleColor;
+ }
+
+ public ConsoleColor GetIgnoredColor(ExecutionResult result)
+ {
+ return result.Runner.Stats.Ignored > 0 ? ConsoleColor.Yellow : defaultConsoleColor;
+ }
+
+ private class ChangedColorContext : IDisposable
+ {
+ private readonly ConsoleColor defaultConsoleColor;
- Console.WriteLine(result.Output);
+ public ChangedColorContext(ConsoleColor color)
+ {
+ defaultConsoleColor = Console.ForegroundColor;
+ Console.ForegroundColor = color;
+ }
- Console.ForegroundColor = defaultConsoleColor;
+ public void Dispose()
+ {
+ Console.ForegroundColor = defaultConsoleColor;
+ }
}
}
}
View
2 src/Giles.Core/UI/GrowlUserDisplay.cs
@@ -45,7 +45,7 @@ public void DisplayResult(ExecutionResult result)
{
var title = result.ExitCode == 0 ? "Success!" : "Failures!";
Resource icon = result.ExitCode == 0 ? LoadImage(successImage) : LoadImage(failureImage);
- var notification = new Notification(application.Name, notificationType.Name, DateTime.Now.Ticks.ToString(), title, result.Output) { Icon = icon };
+ var notification = new Notification(application.Name, notificationType.Name, DateTime.Now.Ticks.ToString(), title, result.Runner.ToString()) { Icon = icon };
growl.Notify(notification);
}
View
12 src/Giles.Core/Utility/CollectionExtensions.cs
@@ -0,0 +1,12 @@
+using System.Collections.Generic;
+
+namespace Giles.Core.Utility
+{
+ public static class CollectionExtensions
+ {
+ public static bool IsNullOrEmpty<T>(this IList<T> list)
+ {
+ return list == null || list.Count == 0;
+ }
+ }
+}
View
6 src/Giles.Specs/Core/Runners/GilesTestListenerSpecs.cs
@@ -70,13 +70,13 @@ public class when_displaying_the_results : with_a_giles_test_listener
fakeUserDisplay.DisplayResultsReceived.ShouldNotBeEmpty();
It should_display_the_number_of_successful_tests = () =>
- fakeUserDisplay.DisplayResultsReceived.Count(x => x.Output.Contains("Passed: 0")).ShouldEqual(1);
+ fakeUserDisplay.DisplayResultsReceived.Count(x => x.Runner.ToString().Contains("Passed: 0")).ShouldEqual(1);
It should_display_the_number_of_failed_tests = () =>
- fakeUserDisplay.DisplayResultsReceived.Count(x => x.Output.Contains("Failed: 1")).ShouldEqual(1);
+ fakeUserDisplay.DisplayResultsReceived.Count(x => x.Runner.ToString().Contains("Failed: 1")).ShouldEqual(1);
It should_display_the_number_of_ignored_tests = () =>
- fakeUserDisplay.DisplayResultsReceived.Count(x => x.Output.Contains("Ignored: 0")).ShouldEqual(1);
+ fakeUserDisplay.DisplayResultsReceived.Count(x => x.Runner.ToString().Contains("Ignored: 0")).ShouldEqual(1);
}

0 comments on commit c6819bb

Please sign in to comment.