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