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