github.com/mvdan/u-root-coreutils@v0.0.0-20230122170626-c2eef2898555/cmds/exp/rush/cmd_test.go (about) 1 // Copyright 2022 the u-root Authors. All rights reserved 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package main 6 7 import ( 8 "bufio" 9 "bytes" 10 "errors" 11 "os" 12 "testing" 13 ) 14 15 // TestCmd tests the implementation of builtin commands. 16 func TestCmd(t *testing.T) { 17 18 var tests = []struct { 19 name string 20 line string 21 out string 22 err error 23 }{ 24 {name: "cd no args", line: "cd", err: errCdUsage}, 25 {name: "cd bad dir", line: "cd ZARDOX", err: os.ErrNotExist}, 26 {name: "cd .", line: "cd ."}, 27 {name: "rushinfo", line: "rushinfo", err: nil, out: "ama"}, 28 } 29 30 for _, tt := range tests { 31 c, _, err := getCommand(bufio.NewReader(bytes.NewReader([]byte(tt.line)))) 32 if err != nil && !errors.Is(err, tt.err) { 33 t.Errorf("%s: getCommand(%q): %v is not %v", tt.name, tt.line, err, tt.err) 34 continue 35 } 36 // We don't test broken parsing here, just that we get some expected 37 // arrays 38 doArgs(c) 39 if err := commands(c); err != nil { 40 t.Errorf("commands: %v != nil", err) 41 continue 42 } 43 t.Logf("cmd %q", c) 44 // We don't do pipelines in this test. 45 // We don't usually care about output. 46 o, e := &bytes.Buffer{}, &bytes.Buffer{} 47 c[0].Cmd.Stdout, c[0].Cmd.Stderr = o, e 48 err = command(c[0]) 49 t.Logf("Command output: %q, %q", o.String(), e.String()) 50 if err == nil && tt.err == nil { 51 continue 52 } 53 if err == nil && tt.err != nil { 54 t.Errorf("%q: got nil, want %v", c, tt.err) 55 } 56 if !errors.Is(err, tt.err) { 57 t.Errorf("%q: got %v, want %v", c, err, tt.err) 58 } 59 if len(tt.out) == 0 { 60 continue 61 } 62 if o.Len() == 0 { 63 t.Errorf("%q: stdout: got no data, want something", c) 64 } 65 t.Logf("Command stdout is %s", o.String()) 66 } 67 }