trial.reporters.stats 115/119(96%) 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
380
390
400
410
420
430
440
450
460
4710
480
497
500
515
520
530
542
550
560
570
580
590
600
610
620
630
640
650
660
670
680
695
700
715
725
730
740
752
760
772
780
790
800
810
820
830
845
855
860
870
880
890
900
910
920
930
940
950
960
970
980
9944
1000
1010
1020
1030
1040
1050
1060
1070
108223
1090
1100
1110
1120
1135
1145
1150
1160
1170
1180
1195
1200
1215
1225
1235
1240
1250
1260
1270
1280
1290
1300
1310
1322
1330
1343
1350
1360
1370
1382
1390
1402
1410
1422
1432
1440
1450
1460
1470
1480
1490
1500
1510
1520
1530
1540
1550
1560
1570
1580
1590
1601
1611
1620
1630
1640
1651
1661
1670
1682
1690
1700
1710
1720
1730
1741
1751
1760
1771
1780
1791
1801
1810
1822
1830
1841
1851
1861
1870
1882
1894
1903
1911
1924
1934
1940
1950
1960
1970
1980
1991
2001
2010
2021
2030
2041
2051
2061
2071
2080
2091
2101
2111
2120
2132
2140
2151
2161
2171
2181
2190
2201
2211
2220
2232
2244
2254
2264
2274
2284
2294
2300
2310
2320
2330
2340
2351
2361
2370
2381
2390
2401
2411
2421
2431
2440
2451
2461
2471
2481
2490
2502
2510
2521
2531
2541
2550
2562
2574
2583
2591
2604
2614
2620
2630
2640
2650
266271
267268
2680
2690
2700
2710
2720
2731
2741
2750
2762
2770
2780
2790
2800
2810
2821
2830
2841
2850
2864
2872
2880
2892
2900
2910
2920
2930
2940
2950
2961
2970
2980
2990
3000
3010
3021
3030
3040
3052
3062
3072
3082
3092
3102
3112
3120
3132
3142
3152
3162
3172
3182
3190
3200
3210
3220
3230
3240
3250
3260
3270
3280
3290
/++ 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; /// SourceLocation location; } /// 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, SourceLocation(test.fileName, test.line)); } 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) { version(Have_fluent_asserts) { 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.fileName = "file1.d"; test.line = 11; 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; test.fileName = "file2.d"; test.line = 22; 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]); storage.values.map!(a => a.location.fileName).array.should.equal(["file1.d", "file2.d"]); storage.values.map!(a => a.location.line).array.should.equal([11, 22].to!(size_t[])); } @("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, a.location.fileName, a.location.line.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, TestResult.Status.success, SourceLocation("file.d", 2))]; stats.toCsv.should.equal("1,-29227-04-19T21:11:54.5224192Z,+29228-09-14T02:48:05.4775807Z,unknown,,0\n" ~ "2,-29227-04-19T21:11:54.5224192Z,+29228-09-14T02:48:05.4775807Z,success,file.d,2"); } StatStorage toStatStorage(const(string) data) { auto stat = new StatStorage; stat.values = data .split('\n') .map!(a => a.split(',')) .filter!(a => a.length == 6) .map!(a => Stat(a[0], SysTime.fromISOExtString(a[1]), SysTime.fromISOExtString(a[2]), a[3].to!(TestResult.Status), SourceLocation(a[4], a[5].to!size_t))) .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,,0\n" ~ "2,-29227-04-19T21:11:54.5224192Z,+29228-09-14T02:48:05.4775807Z,unknown,file.d,12").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[0].location.fileName.should.equal(""); storage.values[0].location.line.should.equal(0); 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); storage.values[1].location.fileName.should.equal("file.d"); storage.values[1].location.line.should.equal(12); } StatStorage statsFromFile(string fileName) { if (!fileName.exists) { return new StatStorage(); } return fileName.readText.toStatStorage; }