github.com/mvdan/u-root-coreutils@v0.0.0-20230122170626-c2eef2898555/cmds/core/tail/tail_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 "io" 10 "os" 11 "testing" 12 ) 13 14 func TestTailReadBackwards(t *testing.T) { 15 input, err := os.Open("./test_samples/read_backwards.txt") 16 if err != nil { 17 t.Error(err) 18 } 19 output := &bytes.Buffer{} 20 err = readLastLinesBackwards(input, output, 2) 21 if err != nil { 22 t.Error(err) 23 } 24 expected := []byte("second\nthird\n") 25 got := output.Bytes() 26 if !bytes.Equal(got, expected) { 27 t.Fatalf("Invalid result reading backwards. Got %v; want %v", got, expected) 28 } 29 // try reading more, which should return EOF 30 buf := make([]byte, 16) 31 n, err := input.Read(buf) 32 if err == nil { 33 t.Fatalf("Expected EOF, got more bytes instead: %v", string(buf[:n])) 34 } 35 if err != io.EOF { 36 t.Fatalf("Expected EOF, got another error instead: %v", err) 37 } 38 } 39 40 func TestTailReadFromBeginning(t *testing.T) { 41 input, err := os.Open("./test_samples/read_from_beginning.txt") 42 if err != nil { 43 t.Error(err) 44 } 45 output := &bytes.Buffer{} 46 err = readLastLinesFromBeginning(input, output, 3) 47 if err != nil { 48 t.Error(err) 49 } 50 expected := []byte("eight\nnine\nten\n") 51 got := make([]byte, 4096) // anything larger than the expected result 52 n, err := output.Read(got) 53 if err != nil { 54 t.Fatal(err) 55 } 56 if !bytes.Equal(got[:n], expected) { 57 t.Fatalf("Invalid data while reading from the beginning. Got %v; want %v", string(got[:n]), string(expected)) 58 } 59 // try reading more, which should return EOF 60 buf := make([]byte, 16) 61 n, err = input.Read(buf) 62 if err == nil { 63 t.Fatalf("Expected EOF, got more bytes instead: %v", string(buf[:n])) 64 } 65 if err != io.EOF { 66 t.Fatalf("Expected EOF, got another error instead: %v", err) 67 } 68 } 69 70 func TestTailRun(t *testing.T) { 71 f, err := os.CreateTemp("", "tailRunTest") 72 if err != nil { 73 t.Fatal(err) 74 } 75 defer os.Remove(f.Name()) 76 77 input := "a\nb\nc\n" 78 _, err = f.WriteString(input) 79 if err != nil { 80 t.Fatal(err) 81 } 82 83 var b bytes.Buffer 84 err = run(os.Stdin, &b, []string{f.Name()}) 85 if err != nil { 86 t.Error(err) 87 } 88 89 if b.String() != input { 90 t.Errorf("tail output does not match, want %q, got %q", input, b.String()) 91 } 92 93 err = run(nil, nil, []string{"a", "b"}) 94 if err == nil { 95 t.Error("tail should return an error if more than one file specified") 96 } 97 98 b.Truncate(0) 99 *flagNumLines = -1 100 err = run(f, &b, nil) 101 if err != nil { 102 t.Error(err) 103 } 104 105 if b.String() != "c\n" { 106 t.Errorf("tail output does not match, want %q, got %q", input, b.String()) 107 } 108 } 109 110 func TestLastNLines(t *testing.T) { 111 tests := []struct { 112 input []byte 113 output []byte 114 n uint 115 }{ 116 { 117 input: []byte{'a', '\n', '\n', 'b', '\n'}, 118 output: []byte{'a', '\n', '\n', 'b', '\n'}, 119 n: 4, 120 }, 121 { 122 input: []byte{'a', '\n', '\n', 'b', '\n'}, 123 output: []byte{'a', '\n', '\n', 'b', '\n'}, 124 n: 3, 125 }, 126 { 127 input: []byte{'a', '\n', '\n', 'b', '\n'}, 128 output: []byte{'\n', 'b', '\n'}, 129 n: 2, 130 }, 131 { 132 input: []byte{'a', '\n', '\n', 'b', '\n'}, 133 output: []byte{'b', '\n'}, 134 n: 1, 135 }, 136 { 137 input: []byte{'a', '\n', 'b', '\n', 'c', '\n'}, 138 output: []byte{'c', '\n'}, 139 n: 1, 140 }, 141 { 142 input: []byte{'a', '\n', 'b', '\n', 'c'}, 143 output: []byte{'c'}, 144 n: 1, 145 }, 146 { 147 input: []byte{'\n'}, 148 output: []byte{'\n'}, 149 n: 1, 150 }, 151 } 152 153 for _, test := range tests { 154 r := lastNLines(test.input, test.n) 155 if !bytes.Equal(r, test.output) { 156 t.Errorf("want: %q, got: %q", string(test.output), string(r)) 157 } 158 } 159 }