trial.reporters.stats 104/107(97%) 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
270
280
290
300
310
320
330
340
350
360
370
3810
390
407
410
425
430
440
452
460
470
480
490
500
510
520
530
540
550
560
570
580
590
605
610
625
635
640
650
662
670
682
690
700
710
720
730
740
755
765
770
780
790
800
810
820
830
840
850
860
870
880
890
9042
910
920
930
940
950
960
970
980
99208
1000
1010
1020
1030
1045
1055
1060
1070
1080
1090
1105
1110
1125
1135
1145
1150
1160
1170
1180
1190
1200
1210
1220
1232
1240
1253
1261
1270
1280
1292
1300
1312
1320
1332
1342
1350
1360
1370
1380
1390
1400
1410
1420
1430
1440
1450
1460
1470
1480
1491
1501
1510
1520
1530
1541
1551
1560
1572
1580
1590
1600
1610
1620
1631
1641
1650
1661
1670
1681
1691
1700
1712
1720
1731
1741
1751
1760
1772
1784
1793
1801
1814
1824
1830
1840
1850
1860
1870
1881
1891
1900
1911
1920
1931
1941
1950
1961
1971
1981
1990
2002
2010
2021
2031
2041
2051
2060
2072
2084
2093
2101
2114
2124
2130
2140
2150
2160
2170
2181
2191
2200
2211
2220
2231
2241
2251
2261
2270
2281
2291
2301
2311
2320
2332
2340
2351
2361
2371
2380
2392
2404
2413
2421
2434
2444
2450
2460
2470
2480
249254
250251
2510
2520
2530
2540
2550
2561
2571
2580
2592
2600
2610
2620
2630
2640
2651
2660
2675
2680
2690
2700
2711
2720
2730
2740
2750
2760
2771
2780
2790
2802
2812
2822
2832
2842
2850
2862
2872
2882
2892
2900
2910
2920
2930
2940
2950
2960
2970
2980
2990
3000
/++ A module containing the StatsReporter 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.reporters.stats; import std.algorithm; import std.string; import std.conv; import std.exception; import std.array; import std.datetime; import std.stdio; import std.file; import std.path; import trial.runner; import trial.interfaces; struct Stat { string name; SysTime begin; SysTime end; TestResult.Status status = TestResult.Status.unknown; } class StatStorage { Stat[] values; } Stat find(StatStorage storage, const(string) name) { auto res = storage.values.filter!(a => a.name == name); if (res.empty) { return Stat("", SysTime.min, SysTime.min); } return res.front; } /// The stats reporter creates a csv file with the duration and the result of all your steps and tests. /// It's usefull to use it with other reporters, like spec progress. class StatsReporter : ILifecycleListener, ITestCaseLifecycleListener, ISuiteLifecycleListener, IStepLifecycleListener { private { immutable string destination; StatStorage storage; string[][string] path; } this(StatStorage storage, string destination) { this.storage = storage; this.destination = destination; } this(string destination) { this(new StatStorage, destination); } private { auto lastItem(string key) { enforce(path[key].length > 0, "There is no defined path"); return path[key][path.length - 1]; } } void update() { } void begin(ref SuiteResult suite) { } void end(ref SuiteResult suite) { storage.values ~= Stat(suite.name, suite.begin, Clock.currTime); } void begin(string suite, ref TestResult test) { } void end(string suite, ref TestResult test) { storage.values ~= Stat(suite ~ "." ~ test.name, test.begin, Clock.currTime, test.status); } void begin(string suite, string test, ref StepResult step) { string key = suite ~ "." ~ test; path[key] ~= step.name; } void end(string suite, string test, ref StepResult step) { string key = suite ~ "." ~ test; enforce(lastItem(key) == step.name, "Invalid step name"); storage.values ~= Stat(key ~ "." ~ path[key].join('.'), step.begin, Clock.currTime); path[key] = path[key][0 .. $ - 1]; } void begin(ulong) { } void end(SuiteResult[]) { auto parent = buildPath(pathSplitter(destination).array[0..$-1]); if(parent != "" && !parent.exists) { mkdirRecurse(parent); } std.file.write(destination, storage.toCsv); auto attachment = const Attachment("stats", destination, "text/csv"); if(LifeCycleListeners.instance !is null) { LifeCycleListeners.instance.attach(attachment); } } } version (unittest) { import fluent.asserts; import std.datetime; import std.stdio; } /// It should write the stats to the expected path unittest { scope(exit) { if(exists("destination.csv")) { std.file.remove("destination.csv"); } } auto stats = new StatsReporter("destination.csv"); stats.end([]); "destination.csv".exists.should.equal(true); } @("it should add suite to the storage") unittest { auto storage = new StatStorage; auto stats = new StatsReporter(storage, "trial-stats.csv"); SuiteResult suite = SuiteResult("suite1"); stats.begin(suite); stats.end(suite); storage.values.length.should.equal(1); suite.name = "suite2"; stats.begin(suite); stats.end(suite); storage.values.length.should.equal(2); storage.values.map!(a => a.name).array.should.equal(["suite1", "suite2"]); storage.values.map!(a => a.status) .array.should.equal([TestResult.Status.unknown, TestResult.Status.unknown]); storage.values.map!(a => a.begin).array.should.equal([suite.begin, suite.begin]); storage.values.map!(a => a.end > a.begin).array.should.equal([true, true]); } @("it should add tests to the storage") unittest { auto storage = new StatStorage; auto stats = new StatsReporter(storage, "trial-stats.csv"); SuiteResult suite = SuiteResult("suite"); auto test = new TestResult("test1"); test.status = TestResult.Status.success; stats.begin(suite); stats.begin("suite", test); stats.end("suite", test); storage.values.length.should.equal(1); test.name = "test2"; test.status = TestResult.Status.failure; stats.begin("suite", test); stats.end("suite", test); storage.values.length.should.equal(2); storage.values.map!(a => a.name).array.should.equal(["suite.test1", "suite.test2"]); storage.values.map!(a => a.status) .array.should.equal([TestResult.Status.success, TestResult.Status.failure]); storage.values.map!(a => a.begin).array.should.equal([test.begin, test.begin]); storage.values.map!(a => a.end > a.begin).array.should.equal([true, true]); } @("it should add steps to the storage") unittest { auto storage = new StatStorage; auto stats = new StatsReporter(storage, "trial-stats.csv"); SuiteResult suite = SuiteResult("suite"); auto test = new TestResult("test"); auto step = new StepResult; step.name = "step1"; step.begin = Clock.currTime; stats.begin(suite); stats.begin("suite", test); stats.begin("suite", "test", step); stats.end("suite", "test", step); storage.values.length.should.equal(1); step.name = "step2"; stats.begin("suite", "test", step); stats.end("suite", "test", step); storage.values.length.should.equal(2); storage.values.map!(a => a.name).array.should.equal(["suite.test.step1", "suite.test.step2"]); storage.values.map!(a => a.status) .array.should.equal([TestResult.Status.unknown, TestResult.Status.unknown]); storage.values.map!(a => a.begin).array.should.equal([step.begin, step.begin]); storage.values.map!(a => a.end > a.begin).array.should.equal([true, true]); } string toCsv(const(StatStorage) storage) { return storage.values.map!(a => [a.name, a.begin.toISOExtString, a.end.toISOExtString, a.status.to!string]).map!(a => a.join(',')).join('\n'); } @("it should convert stat storage to csv") unittest { auto stats = new StatStorage; stats.values = [Stat("1", SysTime.min, SysTime.max), Stat("2", SysTime.min, SysTime.max)]; stats.toCsv.should.equal("1,-29227-04-19T21:11:54.5224192Z,+29228-09-14T02:48:05.4775807Z,unknown\n" ~ "2,-29227-04-19T21:11:54.5224192Z,+29228-09-14T02:48:05.4775807Z,unknown"); } StatStorage toStatStorage(const(string) data) { auto stat = new StatStorage; stat.values = data.split('\n').map!(a => a.split(',')).map!(a => Stat(a[0], SysTime.fromISOExtString(a[1]), SysTime.fromISOExtString(a[2]), a[3].to!(TestResult.Status))).array; return stat; } @("it should create stat storage from csv") unittest { auto storage = ("1,-29227-04-19T21:11:54.5224192Z,+29228-09-14T02:48:05.4775807Z,success\n" ~ "2,-29227-04-19T21:11:54.5224192Z,+29228-09-14T02:48:05.4775807Z,unknown").toStatStorage; storage.values.length.should.equal(2); storage.values[0].name.should.equal("1"); storage.values[0].begin.should.equal(SysTime.min); storage.values[0].end.should.equal(SysTime.max); storage.values[0].status.should.equal(TestResult.Status.success); storage.values[1].name.should.equal("2"); storage.values[1].begin.should.equal(SysTime.min); storage.values[1].end.should.equal(SysTime.max); storage.values[1].status.should.equal(TestResult.Status.unknown); } StatStorage statsFromFile(string fileName) { if (!fileName.exists) { return new StatStorage(); } return fileName.readText.toStatStorage; }