github.com/u-root/u-root@v7.0.1-0.20200915234505-ad7babab0a8e+incompatible/cmds/core/comm/comm_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 "fmt" 9 "io/ioutil" 10 "os" 11 "path/filepath" 12 "testing" 13 14 "github.com/u-root/u-root/pkg/testutil" 15 ) 16 17 var commTests = []struct { 18 flags []string 19 in1 string 20 in2 string 21 out string 22 }{ 23 { 24 // Empty files 25 flags: []string{}, 26 in1: "", 27 in2: "", 28 out: "", 29 }, { 30 // Equal files 31 flags: []string{}, 32 in1: "Line1\nlIne2\nline2\nline3", 33 in2: "Line1\nlIne2\nline2\nline3", 34 out: "\t\tLine1\n\t\tlIne2\n\t\tline2\n\t\tline3\n", 35 }, { 36 // Empty file 1 37 flags: []string{}, 38 in1: "", 39 in2: "Line1\nlIne2\n", 40 out: "\tLine1\n\tlIne2\n", 41 }, { 42 // Empty file 2 43 flags: []string{}, 44 in1: "Line1\nlIne2\n", 45 in2: "", 46 out: "Line1\nlIne2\n", 47 }, { 48 // Mix of matchine lines 49 flags: []string{}, 50 in1: "1\n3\n5\n", 51 in2: "2\n3\n4\n", 52 out: "1\n\t2\n\t\t3\n\t4\n5\n", 53 }, 54 } 55 56 // TestComm implements a table-drivent test. 57 func TestComm(t *testing.T) { 58 tmpDir, err := ioutil.TempDir("", "comm") 59 if err != nil { 60 t.Fatal(err) 61 } 62 defer os.RemoveAll(tmpDir) 63 64 for _, test := range commTests { 65 // Write inputs into the two files 66 var files [2]string 67 for i, contents := range []string{test.in1, test.in2} { 68 files[i] = filepath.Join(tmpDir, fmt.Sprintf("txt%d", i)) 69 if err := ioutil.WriteFile(files[i], []byte(contents), 0600); err != nil { 70 t.Fatalf("Failed to create test file %d: %v", i, err) 71 } 72 } 73 74 cmd := testutil.Command(t, append(test.flags, files[0], files[1])...) 75 76 if output, err := cmd.CombinedOutput(); err != nil { 77 t.Errorf("Comm exited with error: %v; output:\n%s", err, string(output)) 78 } else if string(output) != test.out { 79 t.Errorf("Fail: want\n %#v\n got\n %#v", test.out, string(output)) 80 } 81 } 82 } 83 84 func TestMain(m *testing.M) { 85 testutil.Run(m, main) 86 }