| 1 | // vtest retry: 3 |
| 2 | import os |
| 3 | import time |
| 4 | |
| 5 | const qvexe = os.quoted_path(@VEXE) |
| 6 | |
| 7 | fn depend_on_command(cmd string) ? { |
| 8 | path := os.find_abs_path_of_executable(cmd) or { |
| 9 | println('skip: ${cmd} not found') |
| 10 | return none |
| 11 | } |
| 12 | res := os.execute('${os.quoted_path(path)} --version') |
| 13 | if res.exit_code != 0 { |
| 14 | println('skip: ${cmd} does not support --version') |
| 15 | return none |
| 16 | } |
| 17 | if !res.output.contains('GNU coreutils') { |
| 18 | println('skip: ${cmd} is not from coreutils') |
| 19 | return none |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | fn execute(cmd string) os.Result { |
| 24 | sw := time.new_stopwatch() |
| 25 | res := os.execute(cmd) |
| 26 | dt := sw.elapsed().milliseconds() |
| 27 | eprintln('>> command: `${cmd:-60s}`, took: ${dt:5} ms, exit_code: ${res.exit_code:3}, output.len: ${res.output.len}') |
| 28 | return res |
| 29 | } |
| 30 | |
| 31 | fn test_normal_exit_without_timeout_echo() { |
| 32 | depend_on_command('echo') or { return } |
| 33 | ee := execute('${qvexe} timeout 0.5 echo') |
| 34 | assert ee.exit_code == 0, ee.output |
| 35 | res := execute('${qvexe} timeout 0.5 echo z123') |
| 36 | assert res.exit_code == 0, res.output |
| 37 | assert res.output.contains('z123') |
| 38 | } |
| 39 | |
| 40 | fn test_normal_exit_without_timeout_sleep() { |
| 41 | depend_on_command('sleep') or { return } |
| 42 | res := execute('${qvexe} timeout 0.5 sleep 0.1') |
| 43 | assert res.exit_code == 0, res.output |
| 44 | assert res.output == '' |
| 45 | } |
| 46 | |
| 47 | fn test_exit_with_timeout() { |
| 48 | depend_on_command('sleep') or { return } |
| 49 | res := execute('${qvexe} timeout 0.5 sleep 3') |
| 50 | assert res.exit_code == 124, res.output |
| 51 | } |
| 52 | |