1 module tests.utils;
2 
3 
4 import reggae;
5 import unit_threaded;
6 import std.file;
7 import tests.it.runtime: ReggaeSandbox;
8 
9 
10 struct WorkDir {
11     string value;
12 }
13 
14 auto shouldExecuteOk(in string[] args, in string file = __FILE__, in size_t line = __LINE__) {
15 
16     return shouldExecuteOk(args, WorkDir(ReggaeSandbox.currentTestPath), file, line);
17 }
18 
19 auto shouldExecuteOk(in string[] args, in WorkDir workDir,
20                      in string file = __FILE__, in size_t line = __LINE__) {
21     import std.process;
22     import std.array;
23     import std..string;
24     import std.algorithm: map;
25 
26     const string[string] env = null;
27     Config config = Config.none;
28     size_t maxOutput = size_t.max;
29 
30     immutable res = execute(args, env, config, maxOutput, workDir.value);
31 
32     auto lines = res.output.chomp.splitLines;
33     if(res.status != 0)
34         throw new UnitTestException(["Could not execute '" ~ args.join(" ") ~
35                                      "' in path " ~ workDir.value ~ ":"] ~
36                                     "" ~ lines,
37                                     file, line);
38     return lines;
39 }
40 
41 auto shouldExecuteOk(string[] args, in Options options,
42                      string file = __FILE__, size_t line = __LINE__) {
43     return shouldExecuteOk(args, WorkDir(options.workingDir), file, line);
44 }
45 
46 auto shouldExecuteOk(string arg, in Options options,
47                      string file = __FILE__, size_t line = __LINE__) {
48     return shouldExecuteOk([arg], options, file, line);
49 }
50 
51 auto shouldExecuteOk(string arg, string file = __FILE__, size_t line = __LINE__) {
52     import std.file: getcwd;
53     return shouldExecuteOk([arg], WorkDir(getcwd()), file, line);
54 }
55 
56 auto shouldFailToExecute(string arg, string workDir = ReggaeSandbox.currentTestPath,
57                          string file = __FILE__, size_t line = __LINE__) {
58     return shouldFailToExecute([arg], workDir, file, line);
59 }
60 
61 auto shouldFailToExecute(string[] args, string workDir = ReggaeSandbox.currentTestPath,
62                          string file = __FILE__, size_t line = __LINE__) {
63 
64     import std.process;
65     import std.array;
66     import std..string: splitLines, chomp;
67 
68     const string[string] env = null;
69     Config config = Config.none;
70     size_t maxOutput = size_t.max;
71 
72     try {
73         immutable res = execute(args, env, config, maxOutput, workDir);
74         if(res.status == 0)
75             throw new UnitTestException([args.join(" ") ~
76                                          " executed ok but was expected to fail"], file, line);
77         return res.output.chomp.splitLines;
78     } catch(ProcessException e) {
79     }
80 
81     return "".chomp.splitLines;
82 }
83 
84 
85 struct FakeFile {
86 
87     string soFar;
88     string[] lines;
89 
90     void write(T...)(auto ref T args) {
91         import std.conv: text;
92         static if(T.length > 0)
93             soFar ~= text(args);
94     }
95 
96     void writeln(T...)(auto ref T args) {
97         import std.conv: text;
98         static if(T.length > 0)
99             lines ~= soFar ~ text(args);
100         else
101             lines ~= soFar;
102     }
103 
104     string toString() @safe pure nothrow const {
105         import std.array: join;
106         return "--------------------\n" ~
107             lines.join("\n") ~
108             soFar ~ "\n" ~
109             "--------------------\n";
110     }
111 
112     void flush() @safe @nogc pure nothrow const {}
113 }