trial.runner 143/188(76%) line coverage

      
10
20
30
40
50
60
70
80
90
100
110
120
130
140
150
160
170
180
190
200
210
220
230
240
250
260
271
2819
290
301
310
320
331
340
350
360
370
380
390
400
410
420
430
440
450
460
470
480
490
500
510
520
530
546
551
561
571
580
590
600
610
620
630
640
650
660
670
680
690
700
710
720
730
740
750
760
770
780
790
800
810
820
830
841
851
861
870
881
891
901
910
921
931
941
950
961
971
981
990
1001
1011
1021
1030
1040
1050
1060
1070
1080
1090
1100
1110
1120
1130
1140
1150
1160
1170
1180
1190
1200
1210
1220
1230
1240
1250
1261
1270
1289
1292
1300
1310
1321
1330
1340
1350
1360
1372
1380
1390
1400
1410
14213
1434
1444
1450
1460
1479
1486
1490
1500
1519
1520
1530
1540
1558
1568
1578
1588
1590
1608
1614
1626
1636
1640
1650
1660
1678
1685
1690
1706
1716
1720
1730
1740
1750
17612
1771
1780
1790
1807
1813
1820
1830
1844
1850
1860
1870
1882
1890
19018
1914
1920
1930
1942
1950
1960
1970
1980
1991
2000
2010
2020
2033
2040
2050
2060
2071
2080
2091
2101
2110
2122
2130
2140
2150
2160
2170
2180
2190
2200
2210
2220
2230
2240
2250
2260
2271
2280
2290
2300
2313
2320
2330
2340
2351
2360
2371
2381
2390
2402
2410
2420
2430
2440
2450
2460
2470
2480
2490
2500
2510
2520
2530
2540
2550
2560
2571
2580
2591
2601
2611
2620
2631
2640
2652
2662
2672
2682
2690
2700
2710
2720
27315
2740
27515
2760
27715
2780
2790
2800
28115
2820
2830
2840
28515
2860
28715
2880
289717
290224
2910
2920
29315
29415
2950
29615
2970
2980
2990
3000
3011
3021
3030
3041
3050
3060
3070
3080
3090
3101
3110
3120
3130
3140
315505
3160
3170
3180
3190
3200
3210
3220
3230
3242
3250
3260
3270
3280
3291
3301
3311
3320
3332
3340
3350
3360
3370
3381
3391
3401
3410
3422
3430
3440
3450
3460
3470
3480
3490
3500
3510
3520
3530
3540
3550
3560
3570
3580
3590
3600
3610
3620
3630
3640
3650
3660
3670
3680
3691
3700
3710
3720
3730
3740
3754
3760
3770
3780
3790
3800
3814
3829
3830
3840
3850
3865
38713
3880
3890
3900
39113
39230
3930
3940
3950
3966
39722
3980
3990
4000
40115
4020
4030
4040
4053
4069
4070
4080
4090
4109
41118
4120
4130
4140
4150
4160
4179
4180
4190
4200
4210
422170204
4230
4240
4250
4260
42727
4280
4290
4300
4310
43227
4330
4340
4350
4360
437220
4380
4390
4400
4410
442220
4430
4440
4450
4460
447224
4481466
4490
4500
4510
4520
453224
4541466
4550
4560
4570
4580
45921
46067
4610
4620
4630
4640
46521
46667
4670
4680
4690
4700
471224
4720
4730
4740
4750
47615
4770
4780
4790
4800
48115
4820
4830
4840
4850
4860
4871
4880
4890
4900
4912
4920
4930
4940
4950
4960
4970
4980
4990
5000
5010
5020
5030
5040
5050
5060
5070
5080
5090
5100
5110
5120
5130
5140
5150
5160
51715
5180
5190
5200
5210
5220
5230
5240
5250
5260
5270
5280
5290
5300
5310
5320
5330
5340
5350
5360
5370
5380
5390
5400
5410
5420
5430
5440
5450
5460
5470
5480
5490
5500
5510
5520
5530
5540
5550
5560
5570
55815
55915
56015
56115
56215
56315
56415
5650
5660
5670
/++ The main runner logic. You can find here some LifeCycle logic and test runner initalization Copyright: © 2017 Szabo Bogdan License: Subject to the terms of the MIT license, as written in the included LICENSE.txt file. Authors: Szabo Bogdan +/ module trial.runner; import std.stdio; import std.algorithm; import std.datetime; import std.range; import std.traits; import std.string; import std.conv; import std.path; import std.getopt; import trial.settings; import trial.executor.single; import trial.executor.parallel; /// setup the LifeCycle collection void setupLifecycle(Settings settings) { LifeCycleListeners.instance = new LifeCycleListeners; settings.reporters.map!(a => a.toLower).each!(a => addReporter(a, settings)); if(settings.runInParallel) { LifeCycleListeners.instance.add(new ParallelExecutor(settings.maxThreads)); } else { LifeCycleListeners.instance.add(new DefaultExecutor); } } /// Adds an embeded reporter listener to the LifeCycle listeners collection void addReporter(string name, Settings settings) { import trial.reporters.spec; import trial.reporters.specprogress; import trial.reporters.specsteps; import trial.reporters.dotmatrix; import trial.reporters.landing; import trial.reporters.progress; import trial.reporters.list; import trial.reporters.html; import trial.reporters.allure; import trial.reporters.stats; import trial.reporters.result; import trial.reporters.xunit; import trial.reporters.tap; import trial.reporters.visualtrial; switch(name) { case "spec": LifeCycleListeners.instance.add(new SpecReporter(settings)); break; case "spec-progress": auto storage = statsFromFile(buildPath(settings.artifactsLocation, "stats.csv")); LifeCycleListeners.instance.add(new SpecProgressReporter(storage)); break; case "spec-steps": LifeCycleListeners.instance.add(new SpecStepsReporter(settings)); break; case "dot-matrix": LifeCycleListeners.instance.add(new DotMatrixReporter(settings.glyphs.dotMatrix)); break; case "landing": LifeCycleListeners.instance.add(new LandingReporter(settings.glyphs.landing)); break; case "list": LifeCycleListeners.instance.add(new ListReporter(settings)); break; case "progress": LifeCycleListeners.instance.add(new ProgressReporter(settings.glyphs.progress)); break; case "html": LifeCycleListeners.instance.add(new HtmlReporter(buildPath(settings.artifactsLocation, "result.html"))); break; case "allure": LifeCycleListeners.instance.add(new AllureReporter(buildPath(settings.artifactsLocation, "allure"))); break; case "xunit": LifeCycleListeners.instance.add(new XUnitReporter(buildPath(settings.artifactsLocation, "xunit"))); break; case "result": LifeCycleListeners.instance.add(new ResultReporter(settings.glyphs.result)); break; case "stats": LifeCycleListeners.instance.add(new StatsReporter(buildPath(settings.artifactsLocation, "stats.csv"))); break; case "tap": LifeCycleListeners.instance.add(new TapReporter); break; case "visualtrial": LifeCycleListeners.instance.add(new VisualTrialReporter); break; default: writeln("There is no `" ~ name ~ "` reporter"); } } /// Returns an associative array of the detected tests, /// where the key is the suite name and the value is the TestCase const(TestCase)[][string] describeTests() { return describeTests(LifeCycleListeners.instance.getTestCases); } /// Returns an associative array of the detected tests, /// where the key is the suite name and the value is the TestCase const(TestCase)[][string] describeTests(const(TestCase)[] tests) { const(TestCase)[][string] groupedTests; foreach(test; tests) { groupedTests[test.suiteName] ~= test; } return groupedTests; } /// string toJSONHierarchy(T)(const(T)[][string] items) { struct Node { Node[string] nodes; const(T)[] values; void add(string[] path, const(T)[] values) { if(path.length == 0) { this.values = values; return; } if(path[0] !in nodes) { nodes[path[0]] = Node(); } nodes[path[0]].add(path[1..$], values); } string toString(int spaces = 2) { string prefix = leftJustify("", spaces); string endPrefix = leftJustify("", spaces - 2); string listValues = ""; string objectValues = ""; if(values.length > 0) { listValues = values .map!(a => a.toString) .map!(a => prefix ~ a) .join(",\n"); } if(nodes.keys.length > 0) { objectValues = nodes .byKeyValue .map!(a => `"` ~ a.key ~ `": ` ~ a.value.toString(spaces + 2)) .map!(a => prefix ~ a) .join(",\n"); } if(listValues != "" && objectValues != "") { return "{\n" ~ objectValues ~ ",\n" ~ prefix ~ "\"\": [\n" ~ listValues ~ "\n" ~ prefix ~ "]\n" ~ endPrefix ~ "}"; } if(listValues != "") { return "[\n" ~ listValues ~ "\n" ~ endPrefix ~ "]"; } return "{\n" ~ objectValues ~ "\n" ~ endPrefix ~ "}"; } } Node root; foreach(key; items.keys) { root.add(key.split("."), items[key]); } return root.toString; } /// convert an assoc array to JSON hierarchy unittest { struct Mock { string val; string toString() inout { return `"` ~ val ~ `"`; } } const(Mock)[][string] mocks; mocks["a.b"] = [ Mock("val1"), Mock("val2") ]; mocks["a.c"] = [ Mock("val3") ]; mocks.toJSONHierarchy.should.equal(`{ "a": { "b": [ "val1", "val2" ], "c": [ "val3" ] } }`); } /// it should have an empty key for items that contain both values and childs unittest { struct Mock { string val; string toString() inout { return `"` ~ val ~ `"`; } } const(Mock)[][string] mocks; mocks["a.b"] = [ Mock("val1"), Mock("val2") ]; mocks["a.b.c"] = [ Mock("val3") ]; mocks.toJSONHierarchy.should.equal(`{ "a": { "b": { "c": [ "val3" ], "": [ "val1", "val2" ] } } }`); } /// describeTests should return the tests cases serialised in json format unittest { void TestMock() @system { } TestCase[] tests; tests ~= TestCase("a.b", "some test", &TestMock, [ Label("some label", "label value") ]); tests ~= TestCase("a.c", "other test", &TestMock); auto result = describeTests(tests); result.values.length.should.equal(2); result.keys.should.containOnly([ "a.b", "a.c" ]); result["a.b"].length.should.equal(1); result["a.c"].length.should.equal(1); } /// Runs the tests and returns the results auto runTests(const(TestCase)[] tests, string testName = "", string suiteName = "") { setupSegmentationHandler!true(); const(TestCase)[] filteredTests = tests; if(testName != "") { filteredTests = tests.filter!(a => a.name.indexOf(testName) != -1).array; } if(suiteName != "") { filteredTests = tests.filter!(a => a.suiteName.indexOf(suiteName) != -1).array; } LifeCycleListeners.instance.begin(filteredTests.length); SuiteResult[] results = LifeCycleListeners.instance.beginExecution(filteredTests); foreach(test; filteredTests) { results ~= LifeCycleListeners.instance.execute(test); } results ~= LifeCycleListeners.instance.endExecution; LifeCycleListeners.instance.end(results); return results; } /// ditto auto runTests(string[] arguments) { string testName; string suiteName; getopt( arguments, "testName|t", &testName, "suiteName|s", &suiteName ); return runTests(LifeCycleListeners.instance.getTestCases, testName, suiteName); } /// Check if a suite result list is a success bool isSuccess(SuiteResult[] results) { return results.map!(a => a.tests).joiner.map!(a => a.status).all!(a => a == TestResult.Status.success || a == TestResult.Status.pending); } version(unittest) { import fluent.asserts; } /// It should return true for an empty result unittest { [].isSuccess.should.equal(true); } /// It should return true if all the tests succeded unittest { SuiteResult[] results = [ SuiteResult("") ]; results[0].tests = [ new TestResult("") ]; results[0].tests[0].status = TestResult.Status.success; results.isSuccess.should.equal(true); } /// It should return false if one the tests failed unittest { SuiteResult[] results = [ SuiteResult("") ]; results[0].tests = [ new TestResult("") ]; results[0].tests[0].status = TestResult.Status.failure; results.isSuccess.should.equal(false); } /// The lifecycle listeners collections. You must use this instance in order /// to extend the runner. You can have as many listeners as you want. The only restriction /// is for ITestExecutor, which has no sense to have more than one instance for a run class LifeCycleListeners { /// The global instange. static LifeCycleListeners instance; private { ISuiteLifecycleListener[] suiteListeners; ITestCaseLifecycleListener[] testListeners; IStepLifecycleListener[] stepListeners; ILifecycleListener[] lifecycleListeners; ITestDiscovery[] testDiscoveryListeners; IAttachmentListener[] attachmentListeners; ITestExecutor executor; string currentTest; } @property { /// Return an unique name for the current running test. If there is no test running it /// will return an empty string string runningTest() const nothrow { return currentTest; } } /// TestCase[] getTestCases() { return testDiscoveryListeners.map!(a => a.getTestCases).join; } /// Add a listener to the collection void add(T)(T listener) { static if(!is(CommonType!(ISuiteLifecycleListener, T) == void)) { suiteListeners ~= cast(ISuiteLifecycleListener) listener; suiteListeners = suiteListeners.filter!(a => a !is null).array; } static if(!is(CommonType!(ITestCaseLifecycleListener, T) == void)) { testListeners ~= cast(ITestCaseLifecycleListener) listener; testListeners = testListeners.filter!(a => a !is null).array; } static if(!is(CommonType!(IStepLifecycleListener, T) == void)) { stepListeners ~= cast(IStepLifecycleListener) listener; stepListeners = stepListeners.filter!(a => a !is null).array; } static if(!is(CommonType!(ILifecycleListener, T) == void)) { lifecycleListeners ~= cast(ILifecycleListener) listener; lifecycleListeners = lifecycleListeners.filter!(a => a !is null).array; } static if(!is(CommonType!(ITestExecutor, T) == void)) { executor = cast(ITestExecutor) listener; } static if(!is(CommonType!(ITestDiscovery, T) == void)) { testDiscoveryListeners ~= cast(ITestDiscovery) listener; testDiscoveryListeners = testDiscoveryListeners.filter!(a => a !is null).array; } static if(!is(CommonType!(IAttachmentListener, T) == void)) { attachmentListeners ~= cast(IAttachmentListener) listener; attachmentListeners = attachmentListeners.filter!(a => a !is null).array; } } /// Send the attachment to all listeners void attach(ref const Attachment attachment) { attachmentListeners.each!(a => a.attach(attachment)); } /// Send the update event to all listeners void update() { lifecycleListeners.each!"a.update"; } /// Send the begin run event to all listeners void begin(ulong testCount) { lifecycleListeners.each!(a => a.begin(testCount)); } /// Send the end runer event to all listeners void end(SuiteResult[] result) { lifecycleListeners.each!(a => a.end(result)); } /// Send the begin suite event to all listeners void begin(ref SuiteResult suite) { suiteListeners.each!(a => a.begin(suite)); } /// Send the end suite event to all listeners void end(ref SuiteResult suite) { suiteListeners.each!(a => a.end(suite)); } /// Send the begin test event to all listeners void begin(string suite, ref TestResult test) { currentTest = suite ~ "." ~ test.name; testListeners.each!(a => a.begin(suite, test)); } /// Send the end test event to all listeners void end(string suite, ref TestResult test) { currentTest = ""; testListeners.each!(a => a.end(suite, test)); } /// Send the begin step event to all listeners void begin(string suite, string test, ref StepResult step) { currentTest = suite ~ "." ~ test ~ "." ~ step.name; stepListeners.each!(a => a.begin(suite, test, step)); } /// Send the end step event to all listeners void end(string suite, string test, ref StepResult step) { currentTest = ""; stepListeners.each!(a => a.end(suite, test, step)); } /// Send the execute test to the executor listener SuiteResult[] execute(ref const(TestCase) func) { return executor.execute(func); } /// Send the begin execution with the test case list to the executor listener SuiteResult[] beginExecution(ref const(TestCase)[] tests) { return executor.beginExecution(tests); } /// Send the end execution the executor listener SuiteResult[] endExecution() { return executor.endExecution(); } } /// It should return the name of this test unittest { if(LifeCycleListeners.instance is null) { return; } LifeCycleListeners.instance.runningTest.should.equal("trial.runner.It should return the name of this test"); } void setupSegmentationHandler(bool testRunner)() { import core.runtime; // backtrace version(CRuntime_Glibc) import core.sys.linux.execinfo; else version(OSX) import core.sys.darwin.execinfo; else version(FreeBSD) import core.sys.freebsd.execinfo; else version(NetBSD) import core.sys.netbsd.execinfo; else version(Windows) import core.sys.windows.stacktrace; else version(Solaris) import core.sys.solaris.execinfo; static if( __traits( compiles, backtrace ) ) { version(Posix) { import core.sys.posix.signal; // segv handler static extern (C) void unittestSegvHandler(int signum, siginfo_t* info, void* ptr ) nothrow { import core.stdc.stdio; core.stdc.stdio.printf("\n\n"); static if(testRunner) { if(signum == SIGSEGV) { core.stdc.stdio.printf("Got a Segmentation Fault running "); } if(signum == SIGBUS) { core.stdc.stdio.printf("Got a bus error running "); } if(LifeCycleListeners.instance.runningTest != "") { core.stdc.stdio.printf("%s\n\n", LifeCycleListeners.instance.runningTest.ptr); } else { core.stdc.stdio.printf("some setup step. This is probably a Trial bug. Please create an issue on github.\n\n"); } } else { if(signum == SIGSEGV) { core.stdc.stdio.printf("Got a Segmentation Fault! "); } if(signum == SIGBUS) { core.stdc.stdio.printf("Got a bus error! "); } core.stdc.stdio.printf(" This is probably a Trial bug. Please create an issue on github.\n\n"); } static enum MAXFRAMES = 128; void*[MAXFRAMES] callstack; int numframes; numframes = backtrace( callstack.ptr, MAXFRAMES ); backtrace_symbols_fd( callstack.ptr, numframes, 2); } sigaction_t action = void; (cast(byte*) &action)[0 .. action.sizeof] = 0; sigfillset( &action.sa_mask ); // block other signals action.sa_flags = SA_SIGINFO | SA_RESETHAND; action.sa_sigaction = &unittestSegvHandler; sigaction( SIGSEGV, &action, null ); sigaction( SIGBUS, &action, null ); } } }