gopkg.in/hugelgupf/u-root.v7@v7.0.0-20180831062900-6a07824681b2/cmds/sort/sort_test.go (about) 1 // Copyright 2016 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 "path/filepath" 12 "strings" 13 "testing" 14 15 "github.com/u-root/u-root/pkg/testutil" 16 ) 17 18 type test struct { 19 flags []string 20 in string 21 out string 22 } 23 24 var sortTests = []test{ 25 // empty 26 {[]string{}, "", ""}, 27 // already sorted, in == out 28 {[]string{}, "a\nb\nc\n", "a\nb\nc\n"}, 29 // sort letters 30 {[]string{}, "c\na\nb\n", "a\nb\nc\n"}, 31 // sort lexicographic 32 {[]string{}, "abc \nab\na bc\n", "a bc\nab\nabc \n"}, 33 // sort without terminating newline 34 {[]string{}, "a\nb\nc", "a\nb\nc\n"}, 35 // sort with utf-8 characters 36 {[]string{}, "γ\nα\nβ\n", "α\nβ\nγ\n"}, 37 // reverse sort 38 {[]string{"-r"}, "c\na\nb\n", "c\nb\na\n"}, 39 // reverse sort without terminating newline 40 {[]string{"-r"}, "a\nb\nc", "c\nb\na\n"}, 41 } 42 43 // sort < in > out 44 func TestSortWithPipes(t *testing.T) { 45 tmpDir, err := ioutil.TempDir("", "ls") 46 if err != nil { 47 t.Fatal(err) 48 } 49 defer os.RemoveAll(tmpDir) 50 51 // Table-driven testing 52 for _, tt := range sortTests { 53 cmd := testutil.Command(t, tt.flags...) 54 cmd.Stdin = strings.NewReader(tt.in) 55 var out bytes.Buffer 56 cmd.Stdout = &out 57 if err := cmd.Run(); err != nil { 58 t.Errorf("sort(%#v): %v", tt.in, err) 59 } 60 if out.String() != tt.out { 61 t.Errorf("sort(%#v) = %#v; want %#v", tt.in, 62 out.String(), tt.out) 63 } 64 } 65 } 66 67 // Helper function to create input files, run sort and compare the output. 68 func sortWithFiles(t *testing.T, tt test, tmpDir string, 69 inFiles []string, outFile string) { 70 // Create input files 71 inPaths := make([]string, len(inFiles)) 72 for i, inFile := range inFiles { 73 inPaths[i] = filepath.Join(tmpDir, inFile) 74 if err := ioutil.WriteFile(inPaths[i], []byte(tt.in), 0600); err != nil { 75 t.Error(err) 76 return 77 } 78 } 79 outPath := filepath.Join(tmpDir, outFile) 80 81 args := append(append(tt.flags, "-o", outPath), inPaths...) 82 out, err := testutil.Command(t, args...).CombinedOutput() 83 if err != nil { 84 t.Errorf("sort %s: %v\n%s", strings.Join(args, " "), err, out) 85 return 86 } 87 88 out, err = ioutil.ReadFile(outPath) 89 if err != nil { 90 t.Errorf("Cannot open out file: %v", err) 91 return 92 } 93 if string(out) != tt.out { 94 t.Errorf("sort %s = %#v; want %#v", strings.Join(args, " "), 95 string(out), tt.out) 96 } 97 } 98 99 // sort -o in out 100 func TestSortWithFiles(t *testing.T) { 101 tmpDir, err := ioutil.TempDir("", "ls") 102 if err != nil { 103 t.Fatal(err) 104 } 105 defer os.RemoveAll(tmpDir) 106 107 // Table-driven testing 108 for _, tt := range sortTests { 109 sortWithFiles(t, tt, tmpDir, []string{"in"}, "out") 110 } 111 } 112 113 // sort -o file file 114 func TestInplaceSort(t *testing.T) { 115 tmpDir, err := ioutil.TempDir("", "ls") 116 if err != nil { 117 t.Fatal(err) 118 } 119 defer os.RemoveAll(tmpDir) 120 121 // Table-driven testing 122 for _, tt := range sortTests { 123 sortWithFiles(t, tt, tmpDir, []string{"file"}, "file") 124 } 125 } 126 127 // sort -o out in1 in2 in3 in4 128 func TestMultipleFileInputs(t *testing.T) { 129 tmpDir, err := ioutil.TempDir("", "ls") 130 if err != nil { 131 t.Fatal(err) 132 } 133 defer os.RemoveAll(tmpDir) 134 135 tt := test{[]string{}, "a\nb\nc\n", 136 "a\na\na\na\nb\nb\nb\nb\nc\nc\nc\nc\n"} 137 sortWithFiles(t, tt, tmpDir, 138 []string{"in1", "in2", "in3", "in4"}, "out") 139 140 // Run the test again without newline terminators. 141 tt = test{[]string{}, "a\nb\nc", 142 "a\na\na\na\nb\nb\nb\nb\nc\nc\nc\nc\n"} 143 sortWithFiles(t, tt, tmpDir, 144 []string{"in1", "in2", "in3", "in4"}, "out") 145 } 146 147 func TestMain(m *testing.M) { 148 testutil.Run(m, main) 149 }