gopkg.in/hugelgupf/u-root.v2@v2.0.0-20180831055005-3f8fdb0ce09d/cmds/id/id_test.go (about) 1 // Copyright 2017-2018 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 "os/exec" 10 "testing" 11 12 "github.com/u-root/u-root/pkg/testutil" 13 ) 14 15 var ( 16 logPrefixLength = len("2009/11/10 23:00:00 ") 17 ) 18 19 type test struct { 20 opt []string 21 out string 22 } 23 24 // Run the command, with the optional args, and return a string 25 // for stdout, stderr, and an error. 26 func run(c *exec.Cmd) (string, string, error) { 27 var o, e bytes.Buffer 28 c.Stdout, c.Stderr = &o, &e 29 err := c.Run() 30 return o.String(), e.String(), err 31 } 32 33 // Test incorrect invocation of id 34 func TestInvocation(t *testing.T) { 35 var tests = []test{ 36 {opt: []string{"-n"}, out: "id: cannot print only names in default format\n"}, 37 {opt: []string{"-G", "-g"}, out: "id: cannot print \"only\" of more than one choice\n"}, 38 {opt: []string{"-G", "-u"}, out: "id: cannot print \"only\" of more than one choice\n"}, 39 {opt: []string{"-g", "-u"}, out: "id: cannot print \"only\" of more than one choice\n"}, 40 {opt: []string{"-g", "-u", "-G"}, out: "id: cannot print \"only\" of more than one choice\n"}, 41 } 42 43 for _, test := range tests { 44 c := testutil.Command(t, test.opt...) 45 _, e, _ := run(c) 46 47 // Ignore the date and time because we're using Log.Fatalf 48 if e[logPrefixLength:] != test.out { 49 t.Errorf("id for '%v' failed: got '%s', want '%s'", test.opt, e, test.out) 50 } 51 } 52 } 53 54 func TestMain(m *testing.M) { 55 testutil.Run(m, main) 56 }