gopkg.in/hugelgupf/u-root.v7@v7.0.0-20180831062900-6a07824681b2/cmds/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", "", 1, []string{"-q", "hox"}}, 30 } 31 32 tmpDir, err := ioutil.TempDir("", "TestGrep") 33 if err != nil { 34 t.Fatal("TempDir failed: ", err) 35 } 36 defer os.RemoveAll(tmpDir) 37 38 for _, v := range tab { 39 c := testutil.Command(t, v.a...) 40 c.Stdin = bytes.NewReader([]byte(v.i)) 41 o, err := c.CombinedOutput() 42 if err := testutil.IsExitCode(err, v.s); err != nil { 43 t.Error(err) 44 continue 45 } 46 if string(o) != v.o { 47 t.Errorf("Grep %v < %v: want '%v', got '%v'", v.a, v.i, v.o, string(o)) 48 continue 49 } 50 } 51 } 52 53 func TestMain(m *testing.M) { 54 testutil.Run(m, main) 55 }