gitlab.com/apertussolutions/u-root@v7.0.0+incompatible/cmds/core/grep/grep_test.go (about) 1 // Copyright 2016-2017 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 "bytes" 9 "io/ioutil" 10 "os" 11 "testing" 12 13 "github.com/u-root/u-root/pkg/testutil" 14 ) 15 16 // GrepTest is a table-driven which spawns grep with a variety of options and inputs. 17 // We need to look at any output data, as well as exit status for things like the -q switch. 18 func TestGrep(t *testing.T) { 19 var tab = []struct { 20 i string 21 o string 22 s int 23 a []string 24 }{ 25 // BEWARE: the IO package seems to want this to be newline terminated. 26 // If you just use hix with no newline the test will fail. Yuck. 27 {"hix\n", "hix\n", 0, []string{"."}}, 28 {"hix\n", "", 0, []string{"-q", "."}}, 29 {"hix\n", "hix\n", 0, []string{"-i", "hix"}}, 30 {"hix\n", "", 0, []string{"-i", "hox"}}, 31 {"HiX\n", "HiX\n", 0, []string{"-i", "hix"}}, 32 } 33 34 tmpDir, err := ioutil.TempDir("", "TestGrep") 35 if err != nil { 36 t.Fatal("TempDir failed: ", err) 37 } 38 defer os.RemoveAll(tmpDir) 39 40 for _, v := range tab { 41 c := testutil.Command(t, v.a...) 42 c.Stdin = bytes.NewReader([]byte(v.i)) 43 o, err := c.CombinedOutput() 44 if err := testutil.IsExitCode(err, v.s); err != nil { 45 t.Error(err) 46 continue 47 } 48 if string(o) != v.o { 49 t.Errorf("Grep %v != %v: want '%v', got '%v'", v.a, v.i, v.o, string(o)) 50 continue 51 } 52 } 53 } 54 55 func TestMain(m *testing.M) { 56 testutil.Run(m, main) 57 }