-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess.echo
More file actions
244 lines (220 loc) · 5.46 KB
/
Copy pathprocess.echo
File metadata and controls
244 lines (220 loc) · 5.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
; std/process — args, environment, exit, spawn+wait.
; Privileged std may `/ runtime` and call `runtime.*` (docs/stdlib.md).
;
; Public API:
; args() — list of strings (argv; [0] is the program name when present)
; env(name) — option string (some value / none if unset)
; env_set(name, value) — set process environment
; env_unset(name) — remove environment variable
; exit(code) — terminate process with status code
; run(program, args) — spawn+wait; result exit status, or ! on spawn failure
;
; Suite: xo test std/process.echo (XO_TEST).
/ runtime
/ std/test
/ std/list
/ std/str
/ std/bytes
$ args = () {
^ runtime.process_args()
}
; Option-shaped: bare ^ = unset, ^ s = value.
$ env = (name) {
? runtime.process_env_has(name) == 0 {
^
}
^ runtime.process_env_get(name)
}
$ env_set = (name, value) {
runtime.process_env_set(name, value)
^
}
$ env_unset = (name) {
runtime.process_env_unset(name)
^
}
$ exit = (code) {
runtime.process_exit(code)
^
}
; Result-shaped: ^ code on success, ! on spawn failure.
$ run = (program, args) {
$ code = runtime.process_run(program, args)
? code < 0 {
! "failed to spawn process"
}
^ code
}
; Spawn+wait capturing stdout/stderr. Result product { code, stdout, stderr } or ! spawn fail.
$ run_capture = (program, args) {
$ r = runtime.process_run_capture(program, args)
? r == 0 {
! "failed to spawn process"
}
^ r
}
; Like run_capture but with working directory `cwd` (empty = inherit).
$ run_cwd = (program, args, cwd) {
$ r = runtime.process_run_cwd(program, args, cwd)
? r == 0 {
! "failed to spawn process"
}
^ r
}
; Spawn with piped stdin/stdout/stderr.
; Result product { ok, child, stdin, stdout, stderr } or ! spawn fail.
$ spawn_pipes = (program, args) {
$ r = runtime.process_spawn_pipes(program, args)
? r.ok == 0 {
! "failed to spawn process"
}
^ r
}
$ pipe_write = (pipe, data) {
$ st = runtime.process_pipe_write(pipe, data)
? st < 0 {
! "pipe write failed"
}
^
}
$ pipe_read = (pipe, limit) {
$ b = runtime.process_pipe_read(pipe, limit)
? b == 0 {
! "pipe read failed"
}
^ b
}
$ pipe_close = (pipe) {
runtime.process_pipe_close(pipe)
^
}
; Wait for child from spawn_pipes; exit code or ! fail.
$ wait = (child) {
$ code = runtime.process_wait(child)
? code < 0 {
! "wait failed"
}
^ code
}
; --- suite ---
test.it("args_nonempty", () {
$ a = args()
test.true(list.len(a) >= 1)
})
test.it("e26_argv_is_program", () {
; Same lock as echo26/run/process/005_argv_len: argv is the suite child, not `xo`.
$ a = args()
test.eq(list.len(a), 1)
})
test.it("env_roundtrip", () {
$ key = "ECHO_STD_PROCESS_TEST"
env_unset(key)
| env(key) {
$ v {
test.true(0 == 1)
}
: {
test.eq(1, 1)
}
}
env_set(key, "hello")
| env(key) {
$ v {
test.eq(v, "hello")
}
: {
test.true(0 == 1)
}
}
env_unset(key)
| env(key) {
$ v {
test.true(0 == 1)
}
: {
test.eq(1, 1)
}
}
})
; Spawn success (`true`/`false`) is covered by runtime unit tests (platform-specific).
test.it("run_missing", () {
$ empty = []
| run("__echo_missing_binary_xyz__", empty) {
$ code {
test.true(0 == 1)
}
! err {
test.eq(err, "failed to spawn process")
}
}
})
test.it("run_capture_missing", () {
$ empty = []
| run_capture("__echo_missing_binary_xyz__", empty) {
$ r {
test.true(0 == 1)
}
! err {
test.eq(err, "failed to spawn process")
}
}
})
test.it("run_cwd_missing", () {
$ empty = []
| run_cwd("__echo_missing_binary_xyz__", empty, "/tmp") {
$ r {
test.true(0 == 1)
}
! err {
test.eq(err, "failed to spawn process")
}
}
})
test.it("spawn_pipes_missing", () {
$ empty = []
| spawn_pipes("__echo_missing_binary_xyz__", empty) {
$ r {
test.true(0 == 1)
}
! err {
test.eq(err, "failed to spawn process")
}
}
})
test.it("spawn_pipes_cat", () {
$ empty = []
| spawn_pipes("cat", empty) {
$ r {
| pipe_write(r.stdin, "hi") {
$ ok {
pipe_close(r.stdin)
| pipe_read(r.stdout, 16) {
$ b {
test.eq(str.from_bytes(b), "hi")
pipe_close(r.stdout)
pipe_close(r.stderr)
| wait(r.child) {
$ code {
test.eq(code, 0)
}
! e {
test.fail(e)
}
}
}
! e {
test.fail(e)
}
}
}
! e {
test.fail(e)
}
}
}
! err {
test.fail(err)
}
}
})
\ args, env, env_set, env_unset, exit, run, run_capture, run_cwd, spawn_pipes, pipe_write, pipe_read, pipe_close, wait