Debian Codegraph @

moreinterp/coreutils/coreutils_test.go

raw
1 // Copyright (c) 2025, Andrey Nering <[email protected]>
2 // See LICENSE for licensing information
3
4 package coreutils
5
6 import (
7 "bytes"
8 "context"
9 "fmt"
10 "strings"
11 "testing"
12
13 "mvdan.cc/sh/v3/interp"
14 "mvdan.cc/sh/v3/syntax"
15 )
16
17 func TestExecHandler(t *testing.T) {
18 for coreUtil := range commandBuilders {
19 t.Run(coreUtil, func(t *testing.T) {
20 var in bytes.Buffer
21 var out strings.Builder
22
23 r, err := interp.New(
24 interp.StdIO(&in, &out, &out),
25 interp.ExecHandlers(ExecHandler),
26 )
27 if err != nil {
28 t.Fatalf("failed to create interpreter: %v", err)
29 }
30
31 cmd := fmt.Sprintf("%s --badoption", coreUtil)
32
33 program, err := syntax.NewParser().Parse(strings.NewReader(cmd), "")
34 if err != nil {
35 t.Fatalf("failed to parse command %q: %v", cmd, err)
36 }
37 err = r.Run(context.Background(), program)
38 if err == nil {
39 t.Fatalf("expected error for command %q, got none", cmd)
40 }
41
42 if !strings.Contains(err.Error(), "flag provided but not defined: -badoption") {
43 t.Errorf("expected error for command %q, got: %v", cmd, err)
44 }
45 })
46 }
47 }