Repository navigation
Expand file tree
/
Copy pathhost_test.go
More file actions
618 lines (584 loc) · 22.3 KB
/
Copy pathhost_test.go
File metadata and controls
618 lines (584 loc) · 22.3 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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
package ant_test
import (
"context"
"errors"
"io"
"strings"
"syscall"
"testing"
"testing/fstest"
"time"
"github.com/ant-src/ant"
"github.com/ant-src/ant/value"
)
func loadSrc(t *testing.T, src string, opts ...ant.Option) *ant.Program {
t.Helper()
opts = append([]ant.Option{ant.WithSource("main.ant", src), ant.WithFS(fstest.MapFS{})}, opts...)
prog, err := ant.Load("main.ant", opts...)
if err != nil {
t.Fatal(err)
}
return prog
}
func TestWithEnv(t *testing.T) {
prog := loadSrc(t, `println(Os.env("GREETING")); println(Os.env("MISSING"))`)
var out strings.Builder
lookup := func(name string) (string, bool) {
if name == "GREETING" {
return "hi", true
}
return "", false
}
if err := prog.Run(context.Background(), ant.WithStdout(&out), ant.WithEnv(lookup)); err != nil {
t.Fatal(err)
}
if out.String() != "hi\nnull\n" {
t.Errorf("stdout %q", out.String())
}
}
func TestInstanceValues(t *testing.T) {
prog := loadSrc(t, `
type Point = { x: int }
var total = 0
func add(n: int): int { total += n; total }
func apply(f: func(int): int, n: int): int { f(n) }
func fails(): void { panic("nope") }
func leaves(): void { Os.exit(4) }
`, ant.WithStderr(&strings.Builder{}))
in, err := prog.Start(context.Background())
if err != nil {
t.Fatal(err)
}
defer in.Close()
add, ok := in.Func("add")
if !ok {
t.Fatal("add not found")
}
if v, err := add.Call(int64(5)); err != nil || v != int64(5) {
t.Errorf("add(5) = %v, %v", v, err)
}
apply, _ := in.Func("apply")
if v, err := apply.Call(add.Value(), int64(2)); err != nil || v != int64(7) {
t.Errorf("apply(add, 2) = %v, %v", v, err)
}
if total, ok := in.Value("total"); !ok || total != int64(7) {
t.Errorf("total = %v, %v", total, ok)
}
if tag, ok := in.Value("Point"); !ok || value.KindOf(tag) != value.KindTag {
t.Errorf("Point = %v, %v", tag, ok)
}
if _, ok := in.Func("total"); ok {
t.Error("a var is not a Func")
}
if _, ok := in.Func("nothing"); ok {
t.Error("a missing name is not a Func")
}
fails, _ := in.Func("fails")
if _, err := fails.Call(); err == nil {
t.Error("expected a panic")
} else {
var pe *ant.PanicError
if !errors.As(err, &pe) || pe.Message != "nope" || len(pe.Trace) == 0 || pe.Error() != "panic: nope" {
t.Errorf("panic error: %#v", err)
}
}
leaves, _ := in.Func("leaves")
if _, err := leaves.Call(); err == nil {
t.Error("expected an exit")
} else {
var ee *ant.ExitError
if !errors.As(err, &ee) || ee.Status != 4 || ee.Error() != "exit status 4" || ant.ExitStatus(err) != 4 {
t.Errorf("exit error: %#v", err)
}
}
}
func TestWithoutNamespaces(t *testing.T) {
prog := loadSrc(t, `println(File.exists("x"))`, ant.WithoutNamespaces("File"))
if diags := prog.Check(); len(diags) == 0 || !strings.Contains(diags[0].Message, "File") {
t.Errorf("check: %v", diags)
}
var errOut strings.Builder
err := prog.Run(context.Background(), ant.WithStdout(&strings.Builder{}), ant.WithStderr(&errOut))
if _, ok := err.(*ant.PanicError); !ok {
t.Errorf("run: %v", err)
}
}
// TestWithoutOs: removing Os removes Cmd with it, so a host that forbids the process forbids
// running programs too.
func TestWithoutOs(t *testing.T) {
prog := loadSrc(t, `println(Cmd.read(Cmd.new("echo", ["ran"])))`, ant.WithoutNamespaces("Os"))
if diags := prog.Check(); len(diags) == 0 || !strings.Contains(diags[0].Message, "Cmd") {
t.Errorf("check: %v", diags)
}
var out strings.Builder
err := prog.Run(context.Background(), ant.WithStdout(&out), ant.WithStderr(&strings.Builder{}))
if _, ok := err.(*ant.PanicError); !ok || strings.Contains(out.String(), "ran") {
t.Errorf("run: %v, printed %q", err, out.String())
}
}
func TestErrorsAndStatuses(t *testing.T) {
_, err := ant.Load("main.ant", ant.WithSource("main.ant", "var x ="), ant.WithFS(fstest.MapFS{}))
var le *ant.LoadError
if !errors.As(err, &le) || !strings.Contains(le.Error(), "main.ant:1:") || ant.ExitStatus(err) != 2 {
t.Errorf("load error: %v", err)
}
if ant.ExitStatus(nil) != 0 || ant.ExitStatus(errors.New("other")) != 1 {
t.Error("ExitStatus of nil or an unknown error")
}
d := ant.Diagnostic{Message: "bare"}
if d.Error() != "bare" {
t.Errorf("bare diagnostic: %q", d.Error())
}
d.Pos = ant.Position{Path: "p.ant", Line: 2, Col: 3}
if d.Error() != "p.ant:2:3: bare" {
t.Errorf("positioned diagnostic: %q", d.Error())
}
if _, err := ant.Load("/nonexistent/program.ant"); err == nil {
t.Error("loading a missing file")
}
}
func TestSessionValue(t *testing.T) {
s, err := ant.NewSession(context.Background(), ant.WithStdout(&strings.Builder{}))
if err != nil {
t.Fatal(err)
}
defer s.Close()
if _, err := s.Eval("var answer = 42"); err != nil {
t.Fatal(err)
}
if v, ok := s.Value("answer"); !ok || v != int64(42) {
t.Errorf("answer = %v, %v", v, ok)
}
if _, ok := s.Value("nothing"); ok {
t.Error("nothing should be unbound")
}
if _, err := s.Eval("var y = "); err == nil {
t.Error("expected a load error")
} else if _, ok := err.(*ant.LoadError); !ok {
t.Errorf("error type %T", err)
}
if !ant.Incomplete("func f() {") || ant.Incomplete("1 +") || ant.Incomplete("1") {
t.Error("Incomplete")
}
}
// TestNamespaceDefaults: a host function declared with defaults may be called with fewer
// arguments or by name, and the omitted ones arrive as nil.
func TestNamespaceDefaults(t *testing.T) {
greet := ant.Namespace{
Name: "Greet",
Signatures: `func Greet.hello(name: string, greeting: string = "hi", punct: string = "!"): string`,
Funcs: map[string]ant.HostFunc{
"hello": func(rt ant.Runtime, args []value.Value) (value.Value, error) {
greeting, punct := "hi", "!"
if args[1] != nil {
greeting = args[1].(string)
}
if args[2] != nil {
punct = args[2].(string)
}
return greeting + " " + args[0].(string) + punct, nil
},
},
}
prog := loadSrc(t, `println(Greet.hello("ada"))
println(Greet.hello("ada", "yo"))
println(Greet.hello("ada", punct: "?"))`, ant.WithNamespace(greet))
if diags := prog.Check(); len(diags) != 0 {
t.Errorf("check: %v", diags)
}
var out strings.Builder
if err := prog.Run(context.Background(), ant.WithStdout(&out)); err != nil {
t.Fatal(err)
}
if out.String() != "hi ada!\nyo ada!\nhi ada?\n" {
t.Errorf("output %q", out.String())
}
}
// TestWithStdin: Os.stdin reads what the option gives, and a read blocked on input that never
// comes ends when the run is cancelled.
func TestWithStdin(t *testing.T) {
prog := loadSrc(t, `println(File.readAll(Os.stdin()))`)
var out strings.Builder
if err := prog.Run(context.Background(), ant.WithStdin(strings.NewReader("a\nb\n")), ant.WithStdout(&out)); err != nil {
t.Fatal(err)
}
if out.String() != "Ok{ value: \"a\\nb\\n\" }\n" {
t.Errorf("output %q", out.String())
}
pr, pw := io.Pipe()
defer pw.Close()
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
defer cancel()
start := time.Now()
err := prog.Run(ctx, ant.WithStdin(pr), ant.WithStdout(&strings.Builder{}))
if !errors.Is(err, context.DeadlineExceeded) || time.Since(start) > 5*time.Second {
t.Errorf("blocked read: %v after %v", err, time.Since(start))
}
}
// TestExecCancelled: cancelling the run kills the child, and Run returns promptly.
func TestExecCancelled(t *testing.T) {
prog := loadSrc(t, `println(Cmd.capture(Cmd.new("sleep", ["30"])))`)
ctx, cancel := context.WithTimeout(context.Background(), 200*time.Millisecond)
defer cancel()
start := time.Now()
err := prog.Run(ctx, ant.WithStdout(&strings.Builder{}))
if !errors.Is(err, context.DeadlineExceeded) || time.Since(start) > 5*time.Second {
t.Errorf("cancelled exec: %v after %v", err, time.Since(start))
}
}
// TestHostTimeNamespace: a host namespace named Time declares sync types, not time types, so
// the checker rejects is on them as it does for the prelude's sync types.
func TestHostTimeNamespace(t *testing.T) {
ns := ant.Namespace{
Name: "Time",
Signatures: "type Time.Stamp\nfunc Time.stamp(): Time.Stamp | null",
Funcs: map[string]ant.HostFunc{
"stamp": func(rt ant.Runtime, args []value.Value) (value.Value, error) { return value.Null, nil },
},
}
prog := loadSrc(t, `var s = Time.stamp(); println(s is Time.Stamp)`, ant.WithNamespace(ns), ant.WithoutNamespaces("Time"))
diags := prog.Check()
if len(diags) != 1 || !strings.Contains(diags[0].Message, "is cannot test Time.Stamp") {
t.Errorf("check: %v", diags)
}
}
// TestWithEnvironment: the whole environment, for Os.env, Os.environ and a child of a Cmd.
func TestWithEnvironment(t *testing.T) {
prog := loadSrc(t, `println(Os.env("ANT_ONLY"))
println(Os.env("PATH"))
println(Map.has(Os.environ(), "ANT_ONLY") && !Map.has(Os.environ(), "PATH"))
println(Cmd.capture(Cmd.new("/usr/bin/env")) | Result.map({ x.stdout }))`)
var out strings.Builder
err := prog.Run(context.Background(), ant.WithStdout(&out), ant.WithEnvironment(map[string]string{"ANT_ONLY": "yes"}))
if err != nil {
t.Fatal(err)
}
if want := "yes\nnull\ntrue\nOk{ value: \"ANT_ONLY=yes\\n\" }\n"; out.String() != want {
t.Errorf("output %q, want %q", out.String(), want)
}
}
// TestStartOptions: a started instance takes the same standard input and environment as a run.
func TestStartOptions(t *testing.T) {
prog := loadSrc(t, `
func input(): string { Result.unwrap(File.readAll(Os.stdin())) }
func vars(): bool { Map.has(Os.environ(), "ANT_ONLY") && !Map.has(Os.environ(), "PATH") }
`)
in, err := prog.Start(context.Background(), ant.WithStdin(strings.NewReader("typed")),
ant.WithEnvironment(map[string]string{"ANT_ONLY": "yes"}))
if err != nil {
t.Fatal(err)
}
defer in.Close()
for name, want := range map[string]value.Value{"input": "typed", "vars": true} {
f, _ := in.Func(name)
if got, err := f.Call(); err != nil || got != want {
t.Errorf("%s() = %v, %v; want %v", name, got, err, want)
}
}
}
// TestStartContext checks that the context given to Start ends a call that never waits, as a
// session's context ends such an input.
func TestStartContext(t *testing.T) {
src := "func spin(): int {\n var n = 0\n while true { n += 1 }\n n\n}\n"
prog, err := ant.Load("spin.ant", ant.WithSource("spin.ant", src))
if err != nil {
t.Fatal(err)
}
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
defer cancel()
in, err := prog.Start(ctx)
if err != nil {
t.Fatal(err)
}
defer in.Close()
spin, _ := in.Func("spin")
if _, err := spin.Call(); !errors.Is(err, context.DeadlineExceeded) {
t.Fatalf("an endless call ended with %v, want the context's error", err)
}
}
// TestCallSite: a panic of the call itself, such as a missing argument, names the function's
// position instead of 0:0.
func TestCallSite(t *testing.T) {
prog, err := ant.Load("id.ant", ant.WithSource("id.ant", "func id(x: int): int { x }\n"))
if err != nil {
t.Fatal(err)
}
in, err := prog.Start(context.Background())
if err != nil {
t.Fatal(err)
}
defer in.Close()
id, _ := in.Func("id")
_, err = id.Call()
var p *ant.PanicError
if !errors.As(err, &p) || len(p.Trace) == 0 {
t.Fatalf("id() ended with %v, want a panic with a trace", err)
}
if f := p.Trace[0]; f.Pos.Line != 1 {
t.Errorf("the call's frame is at %d:%d, want line 1", f.Pos.Line, f.Pos.Col)
}
}
// TestStartInterrupts: an instance honors WithInterrupts as a run does. An interrupt the
// program never asked for ends the instance, and the call in progress returns ErrInterrupted.
func TestStartInterrupts(t *testing.T) {
src := "func spin(): int {\n var n = 0\n while true { n += 1 }\n n\n}\n"
interrupts := make(chan struct{}, 1)
// The deadline only bounds the test: without the interrupt the call would never end.
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
in, err := loadSrc(t, src).Start(ctx, ant.WithInterrupts(interrupts))
if err != nil {
t.Fatal(err)
}
defer in.Close()
time.AfterFunc(50*time.Millisecond, func() { interrupts <- struct{}{} })
spin, _ := in.Func("spin")
if _, err := spin.Call(); !errors.Is(err, ant.ErrInterrupted) {
t.Fatalf("an interrupted call ended with %v, want ErrInterrupted", err)
}
}
// TestHostTextIsDecoded checks that a string a host hands to a program, in a host function's
// result or in a call's arguments, holds only scalar values, as outside text does
// (section 11, String): each ill-formed sequence becomes U+FFFD.
func TestHostTextIsDecoded(t *testing.T) {
db := ant.Namespace{
Name: "Db",
Signatures: "func Db.row(): Map[string, Array[string]]\n",
Funcs: map[string]ant.HostFunc{
"row": func(rt ant.Runtime, args []value.Value) (value.Value, error) {
m := value.NewMap()
m.Set("caf\xe9", value.NewArray([]value.Value{"ok", "\xff\xfe"}))
return m, nil
},
},
}
prog := loadSrc(t, `
func bytes(s: string): int { len(String.toBytes(s)) }
func show(s: string): string { String.from([s, bytes(s)]) }
for { key, value } in Db.row() {
println(show(key))
for v in value { println(show(v)) }
}
`, ant.WithNamespace(db))
var out strings.Builder
if err := prog.Run(context.Background(), ant.WithStdout(&out)); err != nil {
t.Fatal(err)
}
want := "[\"caf�\", 6]\n[\"ok\", 2]\n[\"��\", 6]\n"
if out.String() != want {
t.Errorf("stdout %q, want %q", out.String(), want)
}
in, err := loadSrc(t, `
func bytes(s: string): int { len(String.toBytes(s)) }
`).Start(context.Background())
if err != nil {
t.Fatal(err)
}
defer in.Close()
f, _ := in.Func("bytes")
if v, err := f.Call("a\xffb"); err != nil || v != int64(5) {
t.Errorf("bytes(\"a\\xffb\") = %v, %v; want 5", v, err)
}
}
// TestHostBytesAreKept: Bytes from a host, in a result or an argument, reach the program as they
// are, since only strings are outside text.
func TestHostBytesAreKept(t *testing.T) {
blob := ant.Namespace{
Name: "Blob",
Signatures: "func Blob.raw(): Array[Bytes]\n",
Funcs: map[string]ant.HostFunc{
"raw": func(rt ant.Runtime, args []value.Value) (value.Value, error) {
return value.NewArray([]value.Value{value.CopyBytes([]byte("\xff\xfe")), "\xff"}), nil
},
},
}
prog := loadSrc(t, `
println(Blob.raw())
func size(b: Bytes): int { len(b) }
`, ant.WithNamespace(blob))
var out strings.Builder
if err := prog.Run(context.Background(), ant.WithStdout(&out)); err != nil {
t.Fatal(err)
}
if want := "[Bytes(ff fe), \"�\"]\n"; out.String() != want {
t.Errorf("stdout %q, want %q", out.String(), want)
}
in, err := prog.Start(context.Background())
if err != nil {
t.Fatal(err)
}
defer in.Close()
f, _ := in.Func("size")
if v, err := f.Call(value.CopyBytes([]byte("\xff\xfe\xfd"))); err != nil || v != int64(3) {
t.Errorf("size(Bytes) = %v, %v; want 3", v, err)
}
}
// TestWithoutBytesNamespace: removing the Bytes namespace removes its functions and keeps the
// type, which is built in: is, len, for, b[i] and annotations still check and run.
func TestWithoutBytesNamespace(t *testing.T) {
prog := loadSrc(t, `var b: Bytes = String.toBytes("ab")
var u: string | Bytes = b
var total = 0
for x in b { total = total + int(x) }
println([u is Bytes, len(b) == 2, int(b[1]) == 98, total == 195])`, ant.WithoutNamespaces("Bytes"))
if diags := prog.Check(); len(diags) != 0 {
t.Fatalf("check: %v", diags)
}
var out strings.Builder
if err := prog.Run(context.Background(), ant.WithStdout(&out)); err != nil {
t.Fatal(err)
}
if out.String() != "[true, true, true, true]\n" {
t.Errorf("stdout %q", out.String())
}
gone := loadSrc(t, `println(Bytes.length(String.toBytes("a")))`, ant.WithoutNamespaces("Bytes"))
if diags := gone.Check(); len(diags) != 1 || !strings.Contains(diags[0].Message, "length") {
t.Errorf("check: %v", diags)
}
}
// TestArityPanicFrames: a call given the wrong arguments panics at the call site, in the caller,
// with no frame of the callee it never entered.
func TestArityPanicFrames(t *testing.T) {
src := "func f(a: int): int { a }\n\nprintln(f(1, 2))\n"
prog, err := ant.Load("arity.ant", ant.WithSource("arity.ant", src))
if err != nil {
t.Fatal(err)
}
err = prog.Run(context.Background(), ant.WithStdout(io.Discard), ant.WithStderr(io.Discard))
var p *ant.PanicError
if !errors.As(err, &p) {
t.Fatalf("the run ended with %v, want a panic", err)
}
if len(p.Trace) != 1 || p.Trace[0].Function != "module arity.ant" || p.Trace[0].Pos.Line != 3 || p.Trace[0].Pos.Col != 10 {
t.Errorf("trace %+v, want the one frame of the script at the call, 3:10", p.Trace)
}
}
// TestHostTypeOwner: a host namespace's types are qualified by the namespace, as in a declaration
// file, like its functions.
func TestHostTypeOwner(t *testing.T) {
ns := ant.Namespace{Name: "Db", Signatures: "type Row = Map[string, string]\nfunc Db.row(): Db.Row", Funcs: map[string]ant.HostFunc{
"row": func(rt ant.Runtime, args []value.Value) (value.Value, error) { return value.Null, nil },
}}
_, err := ant.Load("main.ant", ant.WithSource("main.ant", "println(1)\n"), ant.WithFS(fstest.MapFS{}), ant.WithNamespace(ns))
if err == nil || !strings.Contains(err.Error(), "namespace Db: type Row must be declared as Db.Row") {
t.Errorf("unqualified host type: %v", err)
}
}
// TestDeclarationFileProgram: a declaration file loads, checks, and cannot run (section 10,
// Declaration files).
func TestDeclarationFileProgram(t *testing.T) {
fsys := fstest.MapFS{"db.d.ant": {Data: []byte("type Db.Row = Map[string, string]\nfunc Db.query(sql: string): Array[Db.Row]\nfunc Db.count(): Count\n")}}
prog, err := ant.Load("db.d.ant", ant.WithFS(fsys))
if err != nil {
t.Fatal(err)
}
diags := prog.Check()
if len(diags) != 1 || diags[0].Error() != "db.d.ant:3:18: unknown type Count" {
t.Errorf("check: %v", diags)
}
err = prog.Run(context.Background())
var le *ant.LoadError
if !errors.As(err, &le) || ant.ExitStatus(err) != 2 || err.Error() != "db.d.ant: a declaration file declares names for the prelude and cannot be run" {
t.Errorf("run: %v", err)
}
if _, err := prog.Start(context.Background()); !errors.As(err, &le) {
t.Errorf("start: %v", err)
}
// A name a host namespace binds is a prelude name to the file; one the host removed is free.
ns := ant.Namespace{Name: "Db", Signatures: "func Db.row(): int", Funcs: map[string]ant.HostFunc{
"row": func(rt ant.Runtime, args []value.Value) (value.Value, error) { return value.Null, nil },
}}
fsys = fstest.MapFS{"x.d.ant": {Data: []byte("func Db.query(sql: string): int\nlet Os.LIMIT: int\ntype Cmd.Line = string\n")}}
prog, err = ant.Load("x.d.ant", ant.WithFS(fsys), ant.WithNamespace(ns), ant.WithoutNamespaces("Os"))
if err != nil {
t.Fatal(err)
}
if diags := prog.Check(); len(diags) != 1 || diags[0].Error() != "x.d.ant:1:6: Db is a prelude name and cannot be redeclared" {
t.Errorf("check against a host namespace: %v", diags)
}
// Defaults are checked as a module's are.
fsys = fstest.MapFS{"d.d.ant": {Data: []byte("type Db.Limits = { rows: int = \"many\" }\nfunc Db.log(to: File.Handle = Os.stdout(), rows: int = 1.5): void\n")}}
prog, err = ant.Load("d.d.ant", ant.WithFS(fsys))
if err != nil {
t.Fatal(err)
}
if diags := prog.Check(); len(diags) != 2 || diags[0].Error() != "d.d.ant:1:32: cannot use string where int is expected" ||
diags[1].Error() != "d.d.ant:2:56: cannot use float64 where int is expected" {
t.Errorf("defaults of a declaration file: %v", diags)
}
}
// TestHostSignatureLimits: a host namespace's signatures take no field defaults and no
// constants, which only the standard library fills.
func TestHostSignatureLimits(t *testing.T) {
row := map[string]ant.HostFunc{"run": func(rt ant.Runtime, args []value.Value) (value.Value, error) { return value.Null, nil }}
for sigs, want := range map[string]string{
"type Db.Opts = { limit: int = 1 }\nfunc Db.run(o: Db.Opts): string": "namespace Db: type Db.Opts gives a field a default, which a host namespace cannot fill",
"let Db.VERSION: string\nfunc Db.run(): string": "namespace Db: a host namespace declares no constants",
} {
ns := ant.Namespace{Name: "Db", Signatures: sigs, Funcs: row}
_, err := ant.Load("main.ant", ant.WithSource("main.ant", "println(1)\n"), ant.WithFS(fstest.MapFS{}), ant.WithNamespace(ns))
if err == nil || !strings.Contains(err.Error(), want) {
t.Errorf("%q: %v", sigs, err)
}
}
}
// TestStreamsInProgramOrder: neither stream is buffered, so where the two reach one writer they
// interleave in program order (stdlib.md, Os, Streams).
func TestStreamsInProgramOrder(t *testing.T) {
prog := loadSrc(t, `print("a"); eprint("b"); println("c"); File.writeText(Os.stderr(), "d"); eprintln(); println("e")`)
var both strings.Builder
if err := prog.Run(context.Background(), ant.WithStdout(&both), ant.WithStderr(&both)); err != nil {
t.Fatal(err)
}
if both.String() != "abc\nd\ne\n" {
t.Errorf("got %q", both.String())
}
}
// brokenPipe is an output whose reader is gone, as a host's writer or a pipe on Windows gives
// it; on Unix the Go runtime kills the process instead.
type brokenPipe struct{}
func (brokenPipe) Write([]byte) (int, error) { return 0, syscall.EPIPE }
// TestBrokenPipeEndsRun: print and eprint ignore a failed write, but a broken pipe ends the run
// with status 141, as SIGPIPE ends a shell's process, so a print loop does not spin.
func TestBrokenPipeEndsRun(t *testing.T) {
for _, src := range []string{`while !false { println("y") }`, `while !false { eprintln("y") }`} {
prog := loadSrc(t, src)
err := prog.Run(context.Background(), ant.WithStdout(brokenPipe{}), ant.WithStderr(brokenPipe{}))
var ee *ant.ExitError
if !errors.As(err, &ee) || ee.Status != 141 {
t.Errorf("%s: %v, want exit status 141", src, err)
}
}
}
// stuckWriter is an output nobody reads: a write blocks until release is closed.
type stuckWriter struct{ release chan struct{} }
func (w stuckWriter) Write(p []byte) (int, error) {
<-w.release
return len(p), nil
}
// TestStuckWriteDoesNotHoldRun: a goroutine blocked writing to a full pipe does not keep the
// run from ending once the script is done.
func TestStuckWriteDoesNotHoldRun(t *testing.T) {
w := stuckWriter{make(chan struct{})}
defer close(w.release)
prog := loadSrc(t, `go { println("y") }
Time.sleep(Time.duration(milliseconds: 20))`)
done := make(chan error, 1)
go func() { done <- prog.Run(context.Background(), ant.WithStdout(w)) }()
select {
case err := <-done:
if err != nil {
t.Fatal(err)
}
case <-time.After(5 * time.Second):
t.Fatal("the run waited behind the stuck write")
}
}
// TestProgram: Os.program is the path given to Load.
func TestProgram(t *testing.T) {
prog := loadSrc(t, `println(Os.program())`)
var out strings.Builder
if err := prog.Run(context.Background(), ant.WithStdout(&out)); err != nil || out.String() != "main.ant\n" {
t.Errorf("got %q, %v", out.String(), err)
}
}