github.com/rck/u-root@v0.0.0-20180106144920-7eb602e381bb/cmds/mkfifo/mkfifo_test.go (about) 1 // Copyright 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 "os" 10 "os/exec" 11 "path/filepath" 12 "strings" 13 "testing" 14 15 "github.com/u-root/u-root/pkg/testutil" 16 ) 17 18 type test struct { 19 name string 20 flags []string 21 stdErr string 22 } 23 24 func TestMkfifo(t *testing.T) { 25 tmpDir, execPath := testutil.CompileInTempDir(t) 26 defer os.RemoveAll(tmpDir) 27 28 // used later in testing 29 testDir := filepath.Join(tmpDir, "mkfifoDir") 30 err := os.Mkdir(testDir, 0700) 31 if err != nil { 32 t.Error(err) 33 } 34 35 var tests = []test{ 36 { 37 name: "no path or mode, error", 38 flags: []string{}, 39 stdErr: "please provide a path, or multiple, to create a fifo", 40 }, 41 { 42 name: "single path", 43 flags: []string{filepath.Join(testDir, "testfifo")}, 44 stdErr: "", 45 }, 46 { 47 name: "duplicate path", 48 flags: []string{filepath.Join(testDir, "testfifo1"), filepath.Join(testDir, "testfifo1")}, 49 stdErr: "file exists", 50 }, 51 } 52 53 for _, tt := range tests { 54 var out, stdErr bytes.Buffer 55 cmd := exec.Command(execPath, tt.flags...) 56 cmd.Stdout = &out 57 cmd.Stderr = &stdErr 58 err := cmd.Run() 59 60 if err != nil && !strings.Contains(stdErr.String(), tt.stdErr) { 61 t.Errorf("expected %v got %v", tt.stdErr, stdErr.String()) 62 } 63 64 for _, path := range tt.flags { 65 testFile, err := os.Stat(path) 66 67 if err != nil { 68 t.Errorf("Unable to stat file %s", path) 69 } 70 71 mode := testFile.Mode() 72 if typ := mode & os.ModeType; typ != os.ModeNamedPipe { 73 t.Errorf("got %v, want %v", typ, os.ModeNamedPipe) 74 } 75 } 76 } 77 }