|
| 1 | +#! /usr/bin/env moon |
| 2 | + |
| 3 | +require 'moonscript' |
| 4 | +lfs = require 'lfs' |
| 5 | +json = (require 'dkjson').use_lpeg! |
| 6 | +getopt = require 'alt_getopt' |
| 7 | +local verbose |
| 8 | + |
| 9 | +import p from require 'moon' |
| 10 | + |
| 11 | + |
| 12 | +-- ----------------------------------------------------------- |
| 13 | +show_help = (args) -> |
| 14 | + print "Usage: #{args[0]} [-h] [-v] slug solution-dir output-dir" |
| 15 | + print "Where: -h show this help" |
| 16 | + print " -v verbose: show the output JSON" |
| 17 | + os.exit! |
| 18 | + |
| 19 | + |
| 20 | +-- ----------------------------------------------------------- |
| 21 | +file_exists = (path) -> |
| 22 | + attrs = lfs.attributes path |
| 23 | + not not attrs |
| 24 | + |
| 25 | +is_directory = (path) -> |
| 26 | + attrs = lfs.attributes path |
| 27 | + attrs and attrs.mode == 'directory' |
| 28 | + |
| 29 | +realpath = (path) -> |
| 30 | + fh = io.popen "realpath #{path}" |
| 31 | + dir = fh\read! |
| 32 | + fh\close! |
| 33 | + dir |
| 34 | + |
| 35 | +validate = (args) -> |
| 36 | + show_help args unless #args == 3 |
| 37 | + {slug, src_dir, dest_dir} = args |
| 38 | + assert slug != '', 'First arg, the slug, cannot be empty' |
| 39 | + assert is_directory(src_dir), 'Second arg, the solution directory, must be a directory' |
| 40 | + assert is_directory(dest_dir), 'Second arg, the output directory, must be a directory' |
| 41 | + |
| 42 | + slug, realpath(src_dir), realpath(dest_dir) |
| 43 | + |
| 44 | + |
| 45 | +-- ----------------------------------------------------------- |
| 46 | +run_tests = (slug, dir) -> |
| 47 | + ok, err = lfs.chdir dir |
| 48 | + assert ok, err |
| 49 | + |
| 50 | + -- unskip tests |
| 51 | + cmd = "perl -i.bak -pe 's{^\\s*\\Kpending\\b}{it}' *_spec.moon" |
| 52 | + ok, result_type, status = os.execute cmd |
| 53 | + assert ok |
| 54 | + |
| 55 | + -- launch `busted` |
| 56 | + fh = io.popen 'busted -o json', 'r' |
| 57 | + json_output = fh\read 'a' |
| 58 | + ok, exit_type, exit_status = fh\close! |
| 59 | + |
| 60 | + if exit_type == 'signal' |
| 61 | + return { |
| 62 | + status: 'error', |
| 63 | + message: json_output |
| 64 | + } |
| 65 | + |
| 66 | + data = json.decode json_output |
| 67 | + |
| 68 | + if not data |
| 69 | + return { |
| 70 | + status: 'error', |
| 71 | + message: json_output |
| 72 | + } |
| 73 | + |
| 74 | + if exit_status != 0 and #data.successes == 0 and #data.failures == 0 and #data.errors > 0 |
| 75 | + return { |
| 76 | + status: 'error', |
| 77 | + message: data.errors[1].message |
| 78 | + } |
| 79 | + |
| 80 | + results = {} |
| 81 | + |
| 82 | + for test in *data.successes |
| 83 | + results[test.element.name] = { |
| 84 | + status: 'pass', |
| 85 | + name: test.element.name, |
| 86 | + } |
| 87 | + |
| 88 | + for test in *data.failures |
| 89 | + results[test.element.name] = { |
| 90 | + status: 'fail', |
| 91 | + name: test.element.name, |
| 92 | + message: test.trace.message, |
| 93 | + } |
| 94 | + |
| 95 | + results |
| 96 | + |
| 97 | + |
| 98 | +-- ----------------------------------------------------------- |
| 99 | +get_test_bodies = (slug, dir) -> |
| 100 | + ok, err = lfs.chdir dir |
| 101 | + assert ok, err |
| 102 | + |
| 103 | + order = {} |
| 104 | + bodies = {} |
| 105 | + |
| 106 | + test_file = "#{slug\gsub('-', '_')}_spec.moon" |
| 107 | + return unless file_exists test_file -- let `busted` handle the error messaging |
| 108 | + |
| 109 | + fh = io.open test_file, 'r' |
| 110 | + |
| 111 | + pattern = (word) -> '^%s+' .. word .. '%s+[\'"](.+)[\'"],%s+->' |
| 112 | + patterns = it: pattern('it'), pending: pattern('pending') |
| 113 | + |
| 114 | + local test_name |
| 115 | + test_body = {} |
| 116 | + in_test = false |
| 117 | + |
| 118 | + for line in fh\lines! |
| 119 | + if line\match '^%s+describe ' |
| 120 | + if test_name |
| 121 | + bodies[test_name] = table.concat test_body, '\n' |
| 122 | + test_body = {} |
| 123 | + test_name = nil |
| 124 | + in_test = false |
| 125 | + |
| 126 | + m = line\match(patterns.it) or line\match(patterns.pending) |
| 127 | + if not m |
| 128 | + if in_test |
| 129 | + table.insert test_body, line |
| 130 | + else |
| 131 | + table.insert order, m |
| 132 | + if in_test |
| 133 | + bodies[test_name] = table.concat test_body, '\n' |
| 134 | + test_body = {} |
| 135 | + test_name = m |
| 136 | + in_test = true |
| 137 | + |
| 138 | + fh\close! |
| 139 | + bodies[test_name] = table.concat test_body, '\n' |
| 140 | + order, bodies |
| 141 | + |
| 142 | + |
| 143 | +-- ----------------------------------------------------------- |
| 144 | +write_results = (slug, test_results, names, bodies, dir) -> |
| 145 | + ok, err = lfs.chdir dir |
| 146 | + assert ok, "#{err}: #{dir}" |
| 147 | + |
| 148 | + results = version: 2, status: nil, tests: {} |
| 149 | + |
| 150 | + if test_results.status |
| 151 | + -- this was an error result |
| 152 | + results.status = test_results.status |
| 153 | + results.message = test_results.message |
| 154 | + |
| 155 | + else |
| 156 | + status = 'pass' |
| 157 | + for name in *names |
| 158 | + test = test_results[name] |
| 159 | + assert test, "no test result for #{name}" |
| 160 | + status = 'fail' if test.status == 'fail' |
| 161 | + test.test_code = bodies[name] |
| 162 | + table.insert results.tests, test |
| 163 | + results.status = status |
| 164 | + |
| 165 | + fh = io.open 'results.json', 'w' |
| 166 | + fh\write (json.encode results) .. '\n' |
| 167 | + fh\close! |
| 168 | + |
| 169 | + os.execute "jq . results.json" if verbose |
| 170 | + |
| 171 | + |
| 172 | +-- ----------------------------------------------------------- |
| 173 | +main = (args) -> |
| 174 | + opts,optind = getopt.get_opts args, 'hv', {} |
| 175 | + |
| 176 | + show_help args if opts.h |
| 177 | + verbose = not not opts.v |
| 178 | + table.remove args, 1 for _ = 1, optind - 1 |
| 179 | + |
| 180 | + slug, src_dir, dest_dir = validate args |
| 181 | + |
| 182 | + print "#{slug}: testing ..." |
| 183 | + |
| 184 | + test_names_ordered, test_code = get_test_bodies slug, src_dir |
| 185 | + |
| 186 | + test_results = run_tests slug, src_dir |
| 187 | + |
| 188 | + write_results slug, test_results, test_names_ordered, test_code, dest_dir |
| 189 | + |
| 190 | + print "#{slug}: ... done" |
| 191 | + |
| 192 | +main arg |
0 commit comments