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