vile / scheme / port_file_test.v
245 lines · 231 sloc · 11.26 KB · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
1module scheme
2
3import os
4import rand
5
6fn file_test_directory() string {
7 path := os.join_path(os.vtmp_dir(), 'vile-file-ports-' + rand.ulid())
8 os.mkdir_all(path) or { panic(err) }
9 return path
10}
11
12fn file_eval(mut vm VM, source string) Value {
13 return vm.eval(source) or { panic('${source}: ${err}') }
14}
15
16fn file_expect_error(mut vm VM, source string) {
17 if value := vm.eval(source) {
18 assert false, '${source} returned ${value.kind()}'
19 }
20 assert vm.active.len == 0
21 assert isnil(vm.ports) || isnil(vm.ports.bindings)
22}
23
24fn test_file_ports_read_utf8_datums_and_flush_buffered_and_direct_writes() {
25 root := file_test_directory()
26 defer { os.rmdir_all(root) or {} }
27 path := os.join_path(root, 'λ😀.scm')
28 mut vm := new()
29 out := vm.open_output_file(path) or { panic(err) }
30 vm.define('out', out) or { panic(err) }
31 file_eval(mut vm, r'(display "(alpha \"😀\")" out) (newline out) (write-char #\λ out)')
32 assert out.output_pointer().stream.pending.len > 0
33 vm.close_output_port(out) or { panic(err) }
34 assert !out.output_pointer().file.is_opened
35 assert os.read_file(path) or { panic(err) } == '(alpha "😀")\nλ'
36 vm.close_output_port(out) or { panic(err) }
37 input := vm.open_input_file(path) or { panic(err) }
38 assert write(vm.read(input) or { panic(err) }) or { panic(err) } == '(alpha "😀")'
39 assert vm.read_char(input) or { panic(err) }.as_character() or { panic(err) } == `\n`
40 assert vm.peek_char(input) or { panic(err) }.as_character() or { panic(err) } == `λ`
41 assert vm.read_char(input) or { panic(err) }.as_character() or { panic(err) } == `λ`
42 assert vm.read(input) or { panic(err) }.tag == .eof
43 vm.close_input_port(input) or { panic(err) }
44 assert !input.input_pointer().file.is_opened
45 assert input.input_pointer().buffer.len == 0
46
47 for length in [1, 4095, 4096, 4097, 65537] {
48 mut sink := vm.open_output_file(path) or { panic(err) }.output_pointer()
49 sink.write_text('prefix') or { panic(err) }
50 sink.write_text('x'.repeat(length)) or { panic(err) }
51 sink.write_text('suffix') or { panic(err) }
52 sink.stream.close() or { panic(err) }
53 assert os.read_file(path) or { panic(err) } == 'prefix' + 'x'.repeat(length) + 'suffix'
54 }
55}
56
57fn test_file_controller_returns_multiple_values_and_closes_escaped_ports() {
58 root := file_test_directory()
59 defer { os.rmdir_all(root) or {} }
60 mut vm := new()
61 vm.define('path', string_value(os.join_path(root, 'data'))) or { panic(err) }
62 assert file_eval(mut vm, '(define held #f)
63 (call-with-values (lambda () (call-with-output-file path (lambda (p)
64 (set! held p) (display "hello" p) (values 20 22)))) +)').as_integer() or { panic(err) } == 42
65 held := vm.lookup('held') or { panic(err) }
66 assert held.output_pointer().closed
67 assert !held.output_pointer().file.is_opened
68 assert os.read_file(os.join_path(root, 'data')) or { panic(err) } == 'hello'
69 assert file_eval(mut vm, '(call-with-values (lambda ()
70 (call-with-input-file path (lambda (p) (set! held p) (values)))) (lambda () 99))').as_integer() or { panic(err) } == 99
71 assert vm.lookup('held') or { panic(err) }.input_pointer().closed
72 assert isnil(vm.ports.bindings)
73 file_expect_error(mut vm, '(read-char held)')
74 file_expect_error(mut vm, '(write 1 held)')
75}
76
77fn test_file_selection_restores_nested_ports_on_normal_return_and_ordinary_error() {
78 root := file_test_directory()
79 defer { os.rmdir_all(root) or {} }
80 mut vm := new()
81 for name in ['first', 'second'] {
82 vm.define(name, string_value(os.join_path(root, name))) or { panic(err) }
83 }
84 prior := vm.current_output_port() or { panic(err) }
85 result := file_eval(mut vm, '(define outer #f) (define inner #f)
86 (with-output-to-file first (lambda () (set! outer (current-output-port))
87 (display "a") (with-output-to-file second (lambda ()
88 (set! inner (current-output-port)) (display "b"))) (display "c") 42))')
89 assert result.as_integer() or { panic(err) } == 42
90 assert eqv(vm.current_output_port() or { panic(err) }, prior)
91 assert os.read_file(os.join_path(root, 'first')) or { panic(err) } == 'ac'
92 assert os.read_file(os.join_path(root, 'second')) or { panic(err) } == 'b'
93 for name in ['outer', 'inner'] {
94 assert vm.lookup(name) or { panic(err) }.output_pointer().closed
95 }
96 file_expect_error(mut vm, '(with-output-to-file first (lambda ()
97 (set! outer (current-output-port)) (display "x")
98 (with-output-to-file second (lambda () (set! inner (current-output-port)) (display "y") (car 3)))))')
99 assert eqv(vm.current_output_port() or { panic(err) }, prior)
100 assert os.read_file(os.join_path(root, 'first')) or { panic(err) } == 'x'
101 assert os.read_file(os.join_path(root, 'second')) or { panic(err) } == 'y'
102 for name in ['outer', 'inner'] {
103 assert vm.lookup(name) or { panic(err) }.output_pointer().closed
104 }
105 os.write_file(os.join_path(root, 'first'), '20 22') or { panic(err) }
106 input_prior := vm.current_input_port() or { panic(err) }
107 assert file_eval(mut vm, '(with-input-from-file first (lambda () (+ (read) (read))))').as_integer() or { panic(err) } == 42
108 assert eqv(vm.current_input_port() or { panic(err) }, input_prior)
109}
110
111fn test_file_continuation_escape_closes_and_reentry_restores_the_new_enclosing_port() {
112 root := file_test_directory()
113 defer { os.rmdir_all(root) or {} }
114 mut vm := new()
115 vm.define('path', string_value(os.join_path(root, 'data'))) or { panic(err) }
116 prior := vm.current_output_port() or { panic(err) }
117 assert file_eval(mut vm, '(define saved #f) (define held #f)
118 (call/cc (lambda (exit) (with-output-to-file path (lambda ()
119 (set! held (current-output-port)) (display "a")
120 (let ((n (call/cc (lambda (k) (set! saved k) 0)))) (exit (+ n 10)))))))').as_integer() or { panic(err) } == 10
121 assert eqv(vm.current_output_port() or { panic(err) }, prior)
122 assert vm.lookup('held') or { panic(err) }.output_pointer().closed
123 alternate := vm.open_output_file(os.join_path(root, 'alternate')) or { panic(err) }
124 vm.set_output_port(alternate) or { panic(err) }
125 for n in 1 .. 5 {
126 gc_collect()
127 assert vm.call(vm.lookup('saved') or { panic(err) }, [integer(n)]) or { panic(err) }.as_integer() or { panic(err) } == n + 10
128 assert eqv(vm.current_output_port() or { panic(err) }, alternate)
129 assert !alternate.output_pointer().closed
130 assert isnil(vm.ports.bindings)
131 }
132 assert os.read_file(os.join_path(root, 'data')) or { panic(err) } == 'a'
133 vm.close_output_port(alternate) or { panic(err) }
134}
135
136fn file_nested_failure(mut vm VM, args []Value) !Value {
137 check_arity('nested-failure', args.len, 0, 0)!
138 outer := vm.current_output_port()!
139 if _ := vm.eval('(with-output-to-file second (lambda () (display "inner") (car 3)))') {
140 return failure('expected inner failure')
141 }
142 if !eqv(vm.current_output_port()!, outer) { return failure('inner cleanup changed outer port') }
143 if outer.output_pointer().closed { return failure('inner cleanup closed outer port') }
144 return vm.eval('(display "after") 42')
145}
146
147fn test_nested_host_entry_failure_cleans_only_its_own_file_extents() {
148 root := file_test_directory()
149 defer { os.rmdir_all(root) or {} }
150 mut vm := new()
151 for name in ['first', 'second'] {
152 vm.define(name, string_value(os.join_path(root, name))) or { panic(err) }
153 }
154 vm.register('nested-failure', file_nested_failure) or { panic(err) }
155 prior := vm.current_output_port() or { panic(err) }
156 assert file_eval(mut vm, '(with-output-to-file first (lambda () (display "before") (nested-failure)))').as_integer() or { panic(err) } == 42
157 assert os.read_file(os.join_path(root, 'first')) or { panic(err) } == 'beforeafter'
158 assert os.read_file(os.join_path(root, 'second')) or { panic(err) } == 'inner'
159 assert eqv(vm.current_output_port() or { panic(err) }, prior)
160 assert isnil(vm.ports.bindings)
161}
162
163fn test_file_open_errors_and_close_failure_do_not_leak_descriptors_or_selection() {
164 root := file_test_directory()
165 defer { os.rmdir_all(root) or {} }
166 mut vm := new()
167 vm.define('path', string_value(os.join_path(root, 'missing', 'data'))) or { panic(err) }
168 for source in ['(open-input-file path)', '(open-output-file path)',
169 '(call-with-input-file path (lambda (p) 1))', '(with-output-to-file path (lambda () 1))',
170 '(open-input-file 1)', '(open-output-file)', '(call-with-output-file path 1)',
171 '(with-input-from-file path 1)', '(call-with-input-file path)', '(with-output-to-file)'] {
172 file_expect_error(mut vm, source)
173 }
174 vm.define('path', string_value(os.join_path(root, 'data') + '\x00suffix')) or { panic(err) }
175 file_expect_error(mut vm, '(open-output-file path)')
176 assert !os.exists(os.join_path(root, 'data'))
177 $if linux {
178 if os.exists('/dev/full') {
179 prior := vm.current_output_port() or { panic(err) }
180 file_expect_error(mut vm, '(define held #f) (with-output-to-file "/dev/full" (lambda ()
181 (set! held (current-output-port)) (display "cannot flush")))')
182 assert eqv(vm.current_output_port() or { panic(err) }, prior)
183 held := vm.lookup('held') or { panic(err) }
184 assert held.output_pointer().closed
185 assert !held.output_pointer().file.is_opened
186 vm.close_output_port(held) or { panic(err) }
187 }
188 }
189}
190
191fn test_file_finalizer_callback_flushes_once_and_ignores_repeated_close() {
192 root := file_test_directory()
193 defer { os.rmdir_all(root) or {} }
194 mut vm := new()
195 path := os.join_path(root, 'data')
196 out := vm.open_output_file(path) or { panic(err) }
197 mut port := out.output_pointer()
198 port.write_text('pending') or { panic(err) }
199 finalize_port_file(port.stream, unsafe { nil })
200 assert !port.stream.file.is_opened
201 assert os.read_file(path) or { panic(err) } == 'pending'
202 // Reuse the path with a new descriptor; stale finalization cannot close it.
203 other := vm.open_output_file(path) or { panic(err) }
204 finalize_port_file(port.stream, unsafe { nil })
205 assert other.output_pointer().file.is_opened
206 vm.close_output_port(out) or { panic(err) }
207 vm.close_output_port(other) or { panic(err) }
208}
209
210fn test_call_with_file_keeps_escaped_port_open_until_callback_really_returns() {
211 root := file_test_directory()
212 defer { os.rmdir_all(root) or {} }
213 mut vm := new()
214 path := os.join_path(root, 'data')
215 vm.define('path', string_value(path)) or { panic(err) }
216 assert file_eval(mut vm, '(define held #f) (define saved #f)
217 (call/cc (lambda (exit) (call-with-output-file path (lambda (p)
218 (set! held p) (display "a" p)
219 (call/cc (lambda (k) (set! saved k) (exit 10)))
220 (display "b" p) 42))))').as_integer() or { panic(err) } == 10
221 held := vm.lookup('held') or { panic(err) }
222 assert !held.output_pointer().closed
223 assert held.output_pointer().file.is_opened
224 assert isnil(vm.ports.bindings)
225 gc_collect()
226 assert vm.call(vm.lookup('saved') or { panic(err) }, [integer(1)]) or { panic(err) }.as_integer() or { panic(err) } == 42
227 assert held.output_pointer().closed
228 assert !held.output_pointer().file.is_opened
229 assert os.read_file(path) or { panic(err) } == 'ab'
230 // The actual callback already returned, so subsequent reentry may observe
231 // a closed port. It must fail cleanly and leave the VM's bindings restored.
232 file_expect_error(mut vm, '(saved 2)')
233 os.write_file(path, '20 22') or { panic(err) }
234 assert file_eval(mut vm, '(call/cc (lambda (exit)
235 (call-with-input-file path (lambda (p) (set! held p)
236 (let ((first (read p)))
237 (call/cc (lambda (k) (set! saved k) (exit first)))
238 (+ first (read p)))))))').as_integer() or { panic(err) } == 20
239 input := vm.lookup('held') or { panic(err) }
240 assert !input.input_pointer().closed
241 gc_collect()
242 assert vm.call(vm.lookup('saved') or { panic(err) }, [integer(0)]) or { panic(err) }.as_integer() or { panic(err) } == 42
243 assert input.input_pointer().closed
244 assert isnil(vm.ports.bindings)
245}
246