trial.reporters.stats 0/37(0%) 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
470
480
490
500
510
520
530
540
550
560
570
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
840
850
860
870
880
890
900
910
920
930
940
950
960
970
980
990
1000
1010
1020
1030
1040
1050
1060
1070
1080
1090
1100
1110
1120
1130
1140
1150
1160
1170
1180
1190
1200
1210
1220
1230
1240
1250
1260
1270
1280
1290
1300
1310
1320
1330
1340
1350
1360
1370
1380
1390
1400
1410
1420
1430
1440
1450
1460
1470
1480
1490
1500
1510
1520
1530
1540
1550
1560
1570
1580
1590
1600
1610
1620
1630
1640
1650
1660
1670
1680
1690
1700
1710
1720
1730
1740
1750
1760
1770
1780
1790
1800
1810
1820
1830
1840
1850
1860
1870
1880
1890
1900
1910
1920
1930
1940
1950
1960
1970
1980
1990
2000
2010
2020
2030
2040
2050
2060
2070
2080
2090
2100
2110
2120
2130
2140
2150
2160
2170
2180
2190
2200
2210
2220
2230
2240
2250
2260
2270
2280
2290
2300
2310
2320
2330
2340
2350
2360
2370
2380
2390
2400
2410
2420
2430
2440
2450
2460
2470
2480
2490
2500
2510
2520
2530
2540
2550
2560
2570
2580
2590
2600
2610
2620
2630
2640
2650
2660
2670
2680
2690
2700
2710
2720
2730
2740
2750
2760
2770
2780
2790
2800
2810
2820
2830
2840
2850
2860
2870
2880
2890
2900
2910
2920
2930
2940
2950
2960
2970
2980
2990
3000
3010
3020
3030
3040
3050
3060
3070
3080
3090
3100
3110
3120
3130
3140
3150
3160
3170
3180
3190
3200
3210
3220
3230
3240
3250
3260
3270
3280
3290
3300
3310
3320
3330
/++ 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; version (Have_fluent_asserts) { version = Have_fluent_asserts_core; } /// 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_core) { 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; }