1
:- module(test_process,
5
:- asserta(user:file_search_path(foreign, '.')).
6
:- asserta(user:file_search_path(library, '.')).
7
:- asserta(user:file_search_path(library, '../plunit')).
9
:- use_module(library(plunit)).
10
:- use_module(library(readutil)).
11
:- use_module(process).
14
run_tests([ process_create,
18
read_process(In, Text) :-
19
read_stream_to_codes(In, Codes),
21
atom_codes(Text, Codes).
23
:- begin_tests(process_create, [sto(rational_trees)]).
26
process_create(path(true), [], []).
27
test(null_input, Codes == []) :-
28
process_create(path(cat), [], [stdin(null), stdout(pipe(Out))]),
29
read_stream_to_codes(Out, Codes),
31
test(null_output, true) :-
32
process_create(path(sh),
33
['-c', 'echo THIS IS AN ERROR'],
35
test(null_error, true) :-
36
process_create(path(sh),
37
['-c', 'echo "THIS IS AN ERROR" 1>&2'],
39
test(read_error, X == 'error\n') :-
40
process_create(path(sh),
41
['-c', 'echo "error" 1>&2'],
44
test(echo, X == 'hello\n') :-
45
process_create(path(sh),
50
test(lwr, X == 'HELLO') :-
51
process_create(path(tr), ['a-z', 'A-Z'],
55
format(In, hello, []),
58
test(cwd, [true, condition(\+current_prolog_flag(windows, true))]) :-
60
process_create(path(pwd), [],
64
read_process(Out, CWD0),
65
normalize_space(atom(CWD), CWD0),
67
test(cwd, [true, condition(current_prolog_flag(windows, true))]) :-
69
getenv('COMSPEC', Shell),
70
process_create(Shell, ['/c', cd],
74
read_process(Out, CWD0),
75
normalize_space(atom(CWD), CWD0),
79
getenv('TEMP', Dir), !.
82
:- end_tests(process_create).
85
:- begin_tests(process_wait, [sto(rational_trees)]).
87
test(wait_ok, X == exit(0)) :-
88
process_create(path(sh), ['-c', 'exit 0'], [process(PID)]),
90
test(wait_ok, X == exit(42)) :-
91
process_create(path(sh), ['-c', 'exit 42'], [process(PID)]),
93
test(kill_ok, [ X == killed(9),
94
condition(\+current_prolog_flag(windows, true))]) :-
95
process_create(path(sleep), [2], [process(PID)]),
98
test(kill_ok, [ X = exit(_),
99
condition(current_prolog_flag(windows, true))]) :-
100
process_create(path(sleep), [2], [process(PID)]),
101
process_kill(PID, 9),
102
process_wait(PID, X).
103
test(wait_timeout, [ X = timeout ]) :-
104
process_create(path(sleep), [2], [process(PID)]),
105
( current_prolog_flag(windows, true)
109
process_wait(PID, X, [timeout(TMO)]),
110
process_kill(PID, 9),
111
process_wait(PID, _).
113
:- end_tests(process_wait).