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 void shouldFailToExecute(string arg, string workDir = getcwd(),
45                          string file = __FILE__, size_t line = __LINE__) {
46     return shouldFailToExecute([arg], workDir, file, line);
47 }
48 
49 void shouldFailToExecute(string[] args, string workDir = getcwd(),
50                          string file = __FILE__, size_t line = __LINE__) {
51 
52     import std.process;
53     import std.array;
54 
55     const string[string] env = null;
56     Config config = Config.none;
57     size_t maxOutput = size_t.max;
58 
59     try {
60         immutable res = execute(args, env, config, maxOutput, workDir);
61         if(res.status == 0)
62             throw new UnitTestException([args.join(" ") ~
63                                          " executed ok but was expected to fail"], file, line);
64     } catch(ProcessException) {}
65 }
66 
67 
68 struct FakeFile {
69     string[] lines;
70     void writeln(T...)(T args) {
71         import std.conv: text;
72         static if(T.length > 0)
73             lines ~= text(args);
74     }
75 }