github.com/mvdan/u-root-coreutils@v0.0.0-20230122170626-c2eef2898555/cmds/core/cmp/cmp_test.go (about) 1 // Copyright 2015-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 "fmt" 10 "os" 11 "path/filepath" 12 "testing" 13 ) 14 15 func TestCmp(t *testing.T) { 16 tmpdir := t.TempDir() 17 for _, tt := range []struct { 18 name string 19 args []string 20 file1 string 21 file2 string 22 long bool 23 line bool 24 silent bool 25 want string 26 }{ 27 { 28 name: "empty args", 29 args: []string{}, 30 want: fmt.Sprintf("expected two filenames (and one to two optional offsets), got %d", 0), 31 }, 32 { 33 name: "err in open file", 34 args: []string{"filedoesnotexist", "filealsodoesnotexist"}, 35 want: fmt.Sprintf("failed to open %s: %s", "filedoesnotexist", "open filedoesnotexist: no such file or directory"), 36 }, 37 { 38 name: "cmp two files without flags, without offsets", 39 args: []string{filepath.Join(tmpdir, "file1"), filepath.Join(tmpdir, "file2")}, 40 file1: "hello\nthis is a test\n", 41 file2: "hello\nthiz is a text", 42 want: fmt.Sprintf("%s %s differ: char %d", filepath.Join(tmpdir, "file1"), filepath.Join(tmpdir, "file2"), 10), 43 }, 44 { 45 name: "cmp two files without flags, with wrong first offset", 46 args: []string{filepath.Join(tmpdir, "file1"), filepath.Join(tmpdir, "file2"), ""}, 47 want: fmt.Sprintf("bad offset1: %s: %v", "", "invalid size \"\""), 48 }, 49 { 50 name: "cmp two files without flags, with correct first offset", 51 args: []string{filepath.Join(tmpdir, "file1"), filepath.Join(tmpdir, "file2"), "4"}, 52 file1: "hello\nthis is a test\n", 53 file2: "hello\nthiz is a text", 54 want: fmt.Sprintf("%s %s differ: char %d", filepath.Join(tmpdir, "file1"), filepath.Join(tmpdir, "file2"), 1), 55 }, 56 { 57 name: "cmp two files without flags, with both offsets correct", 58 args: []string{filepath.Join(tmpdir, "file1"), filepath.Join(tmpdir, "file2"), "4", "4"}, 59 file1: "hello\nthis is a test\n", 60 file2: "hello\nthiz is a text", 61 want: fmt.Sprintf("%s %s differ: char %d", filepath.Join(tmpdir, "file1"), filepath.Join(tmpdir, "file2"), 6), 62 }, 63 { 64 name: "cmp two files without flags, with both offset set but first not valid", 65 args: []string{filepath.Join(tmpdir, "file1"), filepath.Join(tmpdir, "file2"), "", "4"}, 66 file1: "hello\nthis is a test\n", 67 file2: "hello\nthiz is a text", 68 want: fmt.Sprintf("bad offset1: %s: %v", "", "invalid size \"\""), 69 }, 70 { 71 name: "cmp two files without flags, with both offset set but second not valid", 72 args: []string{filepath.Join(tmpdir, "file1"), filepath.Join(tmpdir, "file2"), "4", ""}, 73 file1: "hello\nthis is a test\n", 74 file2: "hello\nthiz is a text", 75 want: fmt.Sprintf("bad offset2: %s: %v", "", "invalid size \"\""), 76 }, 77 { 78 name: "cmp two files, flag line = true", 79 args: []string{filepath.Join(tmpdir, "file1"), filepath.Join(tmpdir, "file2")}, 80 file1: "hello\nthis is a test\n", 81 file2: "hello\nthiz is a text", 82 line: true, 83 want: fmt.Sprintf("%s %s differ: char %d line %d", filepath.Join(tmpdir, "file1"), filepath.Join(tmpdir, "file2"), 10, 2), 84 }, 85 { 86 name: "cmp two files, flag silent = true", 87 args: []string{filepath.Join(tmpdir, "file1"), filepath.Join(tmpdir, "file2")}, 88 file1: "hello\nthis is a test\n", 89 file2: "hello\nthiz is a text", 90 silent: true, 91 want: "", 92 }, 93 { 94 name: "cmp two files, flag long = true", 95 args: []string{filepath.Join(tmpdir, "file1"), filepath.Join(tmpdir, "file2")}, 96 file1: "hello\nthis is a test", 97 file2: "hello\nthiz is a text", 98 long: true, 99 want: fmt.Sprintf("%8d %#.2o %#.2o\n%8d %#.2o %#.2o\n", 10, 0163, 0172, 19, 0163, 0170), 100 }, 101 { 102 name: "cmp two files, flag long = true, first file ends first", 103 args: []string{filepath.Join(tmpdir, "file1"), filepath.Join(tmpdir, "file2")}, 104 file1: "hello\nthis is a tes", 105 file2: "hello\nthiz is a text", 106 long: true, 107 want: fmt.Sprintf("EOF on %s", filepath.Join(tmpdir, "file1")), 108 }, 109 { 110 name: "cmp two files, flag long = true, second file ends first", 111 args: []string{filepath.Join(tmpdir, "file1"), filepath.Join(tmpdir, "file2")}, 112 file1: "hello\nthis is a test", 113 file2: "hello\nthiz is a tex", 114 long: true, 115 want: fmt.Sprintf("EOF on %s", filepath.Join(tmpdir, "file2")), 116 }, 117 } { 118 t.Run(tt.name, func(t *testing.T) { 119 // Write data in file that should be compared 120 file1, err := os.Create(filepath.Join(tmpdir, "file1")) 121 if err != nil { 122 t.Errorf("failed to create file1: %v", err) 123 } 124 file2, err := os.Create(filepath.Join(tmpdir, "file2")) 125 if err != nil { 126 t.Errorf("failed to create file2: %v", err) 127 } 128 _, err = file1.WriteString(tt.file1) 129 if err != nil { 130 t.Errorf("failed to write to file1: %v", err) 131 } 132 _, err = file2.WriteString(tt.file2) 133 if err != nil { 134 t.Errorf("failed to write to file2: %v", err) 135 } 136 137 // Set flags 138 *long = tt.long 139 *line = tt.line 140 *silent = tt.silent 141 142 // Start tests 143 buf := &bytes.Buffer{} 144 if got := cmp(buf, tt.args...); got != nil { 145 if got.Error() != tt.want { 146 t.Errorf("cmp() = %q, want: %q", got.Error(), tt.want) 147 } 148 } else { 149 if buf.String() != tt.want { 150 t.Errorf("cmp() = %q, want: %q", buf.String(), tt.want) 151 } 152 } 153 154 }) 155 } 156 }