os: make process_test.v more portable (prepare for windows)

pull/8896/head weekly.2021.8
Delyan Angelov 2021-02-22 09:42:00 +02:00
parent c37daba41d
commit 5d653a37b6
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
2 changed files with 43 additions and 13 deletions

View File

@ -53,19 +53,20 @@ fn main() {
args := os.args[1..] args := os.args[1..]
if '-h' in args || '--help' in args { if '-h' in args || '--help' in args {
println("Usage: println("Usage:
test_os_process [-v] [-h] [-target stderr/stdout/both/alternate] [-exitcode 0] [-timeout_ms 1000] [-period_ms 100] test_os_process [-v] [-h] [-target stderr/stdout/both/alternate] [-exitcode 0] [-timeout_ms 200] [-period_ms 50]
Prints lines periodically (-period_ms), to stdout/stderr (-target). Prints lines periodically (-period_ms), to stdout/stderr (-target).
After a while (-timeout_ms), exit with (-exitcode). After a while (-timeout_ms), exit with (-exitcode).
This program is useful for platform independent testing This program is useful for platform independent testing
of child process/standart input/output control. of child process/standart input/output control.
It is used in V's `os` module tests. It is used in V's `os` module tests.
") ")
return
} }
ctx.is_verbose = '-v' in args ctx.is_verbose = '-v' in args
ctx.target = s2target(cmdline.option(args, '-target', 'both')) ctx.target = s2target(cmdline.option(args, '-target', 'both'))
ctx.exitcode = cmdline.option(args, '-exitcode', '0').int() ctx.exitcode = cmdline.option(args, '-exitcode', '0').int()
ctx.timeout_ms = cmdline.option(args, '-timeout_ms', '1000').int() ctx.timeout_ms = cmdline.option(args, '-timeout_ms', '200').int()
ctx.period_ms = cmdline.option(args, '-period_ms', '100').int() ctx.period_ms = cmdline.option(args, '-period_ms', '50').int()
if ctx.target == .alternate { if ctx.target == .alternate {
ctx.omode = .stdout ctx.omode = .stdout
} }

View File

@ -1,6 +1,19 @@
import os import os
import time import time
const vexe = os.getenv('VEXE')
const vroot = os.dir(vexe)
const test_os_process = os.join_path(os.temp_dir(), 'v', 'test_os_process.exe')
const test_os_process_source = os.join_path(vroot, 'cmd/tools/test_os_process.v')
fn testsuite_begin() {
os.rm(test_os_process) or { }
assert os.system('$vexe -o $test_os_process $test_os_process_source') == 0
}
fn test_getpid() { fn test_getpid() {
pid := os.getpid() pid := os.getpid()
eprintln('current pid: $pid') eprintln('current pid: $pid')
@ -12,8 +25,8 @@ fn test_run() {
return return
} }
// //
mut p := os.new_process('/bin/sleep') mut p := os.new_process(test_os_process)
p.set_args(['0.2']) p.set_args(['-timeout_ms', '150', '-period_ms', '50'])
p.run() p.run()
assert p.status == .running assert p.status == .running
assert p.pid > 0 assert p.pid > 0
@ -23,7 +36,9 @@ fn test_run() {
if !p.is_alive() { if !p.is_alive() {
break break
} }
os.system('ps -opid= -oppid= -ouser= -onice= -of= -ovsz= -orss= -otime= -oargs= -p $p.pid') $if trace_process_output ? {
os.system('ps -opid= -oppid= -ouser= -onice= -of= -ovsz= -orss= -otime= -oargs= -p $p.pid')
}
time.wait(50 * time.millisecond) time.wait(50 * time.millisecond)
i++ i++
} }
@ -39,24 +54,38 @@ fn test_wait() {
if os.user_os() == 'windows' { if os.user_os() == 'windows' {
return return
} }
mut p := os.new_process('/bin/date') mut p := os.new_process(test_os_process)
assert p.status != .exited
p.wait() p.wait()
assert p.pid != os.getpid()
assert p.code == 0
assert p.status == .exited assert p.status == .exited
assert p.code == 0
assert p.pid != os.getpid()
} }
fn test_slurping_output() { fn test_slurping_output() {
if os.user_os() == 'windows' { if os.user_os() == 'windows' {
return return
} }
mut p := os.new_process('/bin/date') mut p := os.new_process(test_os_process)
p.set_redirect_stdio() p.set_redirect_stdio()
assert p.status != .exited
p.wait() p.wait()
assert p.code == 0
assert p.status == .exited assert p.status == .exited
assert p.code == 0
output := p.stdout_slurp().trim_space() output := p.stdout_slurp().trim_space()
errors := p.stderr_slurp().trim_space() errors := p.stderr_slurp().trim_space()
eprintln('p output: "$output"') $if trace_process_output ? {
eprintln('p errors: "$errors"') eprintln('---------------------------')
eprintln('p output: "$output"')
eprintln('p errors: "$errors"')
eprintln('---------------------------')
}
assert output.contains('stdout, 1')
assert output.contains('stdout, 2')
assert output.contains('stdout, 3')
assert output.contains('stdout, 4')
assert errors.contains('stderr, 1')
assert errors.contains('stderr, 2')
assert errors.contains('stderr, 3')
assert errors.contains('stderr, 4')
} }