gitee.com/mirrors_u-root/u-root@v7.0.0+incompatible/cmds/core/mktemp/mktemp_test.go (about) 1 // Copyright 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 "fmt" 10 "strings" 11 "testing" 12 13 "github.com/u-root/u-root/pkg/testutil" 14 ) 15 16 type test struct { 17 flags []string 18 out string 19 stdErr string 20 exitStatus int 21 } 22 23 func TestMkTemp(t *testing.T) { 24 25 var tests = []test{ 26 { 27 flags: []string{}, 28 out: "/tmp/", 29 stdErr: "", 30 exitStatus: 0, 31 }, 32 { 33 flags: []string{"-d"}, 34 out: "/tmp", 35 stdErr: "", 36 exitStatus: 0, 37 }, 38 { 39 flags: []string{"foofoo.XXXX"}, 40 out: "/tmp/foofoo", 41 stdErr: "", 42 exitStatus: 0, 43 }, 44 { 45 flags: []string{"foo.XXXXX", "--suffix", "baz"}, 46 out: "/tmp/foo.baz", 47 stdErr: "", 48 exitStatus: 0, 49 }, 50 { 51 flags: []string{"-u", "-q"}, 52 out: "", 53 stdErr: "", 54 exitStatus: 0, 55 }, 56 } 57 58 // Table-driven testing 59 for _, tt := range tests { 60 t.Run(fmt.Sprintf("Using flags %s", tt.flags), func(t *testing.T) { 61 var out, stdErr bytes.Buffer 62 cmd := testutil.Command(t, tt.flags...) 63 cmd.Stdout = &out 64 cmd.Stderr = &stdErr 65 err := cmd.Run() 66 67 if !strings.HasPrefix(out.String(), tt.out) { 68 t.Errorf("stdout got:\n%s\nwant starting with:\n%s", out.String(), tt.out) 69 } 70 71 if !strings.HasPrefix(stdErr.String(), tt.stdErr) { 72 t.Errorf("stderr got:\n%s\nwant starting with:\n%s", stdErr.String(), tt.stdErr) 73 } 74 75 if tt.exitStatus == 0 && err != nil { 76 t.Errorf("expected to exit with %d, but exited with err %s", tt.exitStatus, err) 77 } 78 }) 79 } 80 } 81 82 func TestMain(m *testing.M) { 83 testutil.Run(m, main) 84 }