Files
spark/src/client/sparkcmd/Cmd/TestCommand.cs
T

104 lines
3.5 KiB
C#

using bts.test;
using System;
using mbts;
using System.Collections.Generic;
using bts.api.test;
using System.Threading.Tasks;
using mbts.config;
using System.Linq;
using CommandLine;
using Newtonsoft.Json;
using bts;
namespace sparkcmd {
[Verb("test", HelpText="Run Tests")]
public class TestCommandOptions {
[Option('t', "timeout", HelpText="Timeout unit tets", Default=0u)]
public uint timeout { get; set; }
[Option('l', "list", HelpText="List tests")]
public bool list { get; set; }
[Option('i', "insular", HelpText="Tests run sparkd and use their own config (if specified)")]
public bool self_contained { get; set; }
[Option("type", HelpText ="Run all tests of type [Unit|System|Shell]")]
public TestType type { get; set; }
[Value(1, MetaName="Tests", HelpText="Test list")]
public IEnumerable<string> tests { get; set; }
}
public static class TestCommand {
static TestType test_type(string t) {
switch(t.ToLower()) {
case "unit": return TestType.Unit;
case "system": return TestType.System;
case "shell": return TestType.Shell;
}
return TestType.Unit;
}
public static int Run(TestCommandOptions o) {
if (o.type == TestType.Unit) {
// Skip loading any local config
Configuration.initialize_for_test();
}
else Configuration.initialize();
Console.WriteLine(JsonConvert.SerializeObject(o));
if (o.list) {
list_tests((o.type));
return 0;
}
if (o.timeout == 0 && o.tests.Count() == 1 && !o.self_contained) {
//System.Threading.Thread.Sleep(5000);
using (var trr = new TestResultReceiver(true)) {
InProcessTestRunner.run_test_synchronously(o.tests.First(), Enumerator, trr.get_reporter());
trr.print_summary();
return trr.ErrorCount;
}
}
else if (o.tests.Count() > 0) {
return run_tests(o, Enumerator.match(o.tests));
}
else {
return run_tests(o, Enumerator.get_of_type(o.type));
}
}
static Lazy<TestEnumerator> _enumerator = new Lazy<TestEnumerator>(() => new TestEnumerator("spark_test"));
static TestEnumerator Enumerator => _enumerator.Value;
static int run_tests(TestCommandOptions o, IEnumerable<TestMethod> tests) {
var runner = new TestRunner();
// waits for all child processes to exit or time out
return runner.run_tests(tests, o.timeout, o.self_contained);
}
static void list_tests(TestType test_type) {
Console.WriteLine("All tests: ");
foreach (var t in Enumerator.get_groups_of_type(test_type)) {
ConsoleEx.WriteLine(t.name, ConsoleColor.Green);
foreach (var test in t.methods) {
Console.Write($" {test.name}");
if (test.ignored) {
var reason = test.ignored_reason;
if (string.IsNullOrEmpty(reason)) {
ConsoleEx.Write(" (ignored)", ConsoleColor.Yellow);
}
else {
ConsoleEx.Write($" (ignored: '{test.ignored_reason}')", ConsoleColor.Yellow);
}
}
Console.WriteLine();
}
}
}
}
}