| 1 | module test_utils |
| 2 | |
| 3 | import os |
| 4 | import net |
| 5 | import time |
| 6 | |
| 7 | pub fn set_test_env(test_path string) { |
| 8 | unbuffer_stdout() |
| 9 | os.setenv('VMODULES', test_path, true) |
| 10 | os.setenv('VPM_DEBUG', '', true) |
| 11 | os.setenv('VPM_NO_INCREMENT', '1', true) |
| 12 | os.setenv('VPM_FAIL_ON_PROMPT', '1', true) |
| 13 | // Note: setting a local VTMP here, is *very important*, because VTMP is used for |
| 14 | // the destination of the temporary clones done by the child `v install` processes. |
| 15 | // If it is not done, then there is a small chance, that multiple parallel tests |
| 16 | // can do clones to the same exact folders at the same time, which can make them |
| 17 | // fail on the CI, with hard to diagnose spurious errors. |
| 18 | os.setenv('VTMP', os.join_path(test_path, 'vtmp'), true) |
| 19 | } |
| 20 | |
| 21 | pub fn hg_serve(hg_path string, path string, start_port int) (&os.Process, int) { |
| 22 | mut port := start_port |
| 23 | for { |
| 24 | if mut l := net.listen_tcp(.ip6, ':${port}') { |
| 25 | l.close() or { panic(err) } |
| 26 | break |
| 27 | } |
| 28 | port++ |
| 29 | } |
| 30 | mut p := os.new_process(hg_path) |
| 31 | p.set_work_folder(path) |
| 32 | p.set_args(['serve', '--print-url', '--port', port.str()]) |
| 33 | p.set_redirect_stdio() |
| 34 | p.run() |
| 35 | mut i := 0 |
| 36 | for p.is_alive() { |
| 37 | if i == 500 { // Wait max. 5 seconds. |
| 38 | p.signal_kill() |
| 39 | eprintln('Failed to serve mercurial repository on localhost.') |
| 40 | exit(1) |
| 41 | } |
| 42 | if p.stdout_read().contains(':${port}') { |
| 43 | break |
| 44 | } |
| 45 | time.sleep(10 * time.millisecond) |
| 46 | i++ |
| 47 | } |
| 48 | return p, port |
| 49 | } |
| 50 | |
| 51 | pub fn cmd_ok(location string, cmd string) os.Result { |
| 52 | println('> cmd_ok for cmd: "${cmd}"') |
| 53 | res := os.execute(cmd) |
| 54 | assert res.exit_code == 0, 'success expected, but not found\n location: ${location}\n cmd:\n${cmd}\n res:\n${res}\n' |
| 55 | return res |
| 56 | } |
| 57 | |
| 58 | pub fn cmd_fail(location string, cmd string) os.Result { |
| 59 | println('> cmd_fail for cmd: "${cmd}"') |
| 60 | res := os.execute(cmd) |
| 61 | assert res.exit_code == 1, 'failure expected, but not found\n location: ${location}\n cmd:\n${cmd}\n res:\n${res}\n' |
| 62 | return res |
| 63 | } |
| 64 | |