forked from u-root/u-root
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcat_test.go
61 lines (51 loc) · 1.44 KB
/
cat_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// Copyright 2012 the u-root Authors. All rights reserved
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// by Rafael Campos Nunes <[email protected]>
package main
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"reflect"
"testing"
)
// setup writes a set of files, putting 1 byte in each file.
func setup(t *testing.T, data []byte) (string, error) {
t.Logf(":: Creating simulation data. ")
dir, err := ioutil.TempDir("", "cat.dir")
if err != nil {
return "", err
}
for i, d := range data {
n := fmt.Sprintf("%v%d", filepath.Join(dir, "file"), i)
if err := ioutil.WriteFile(n, []byte{d}, 0666); err != nil {
return "", err
}
}
return dir, nil
}
// TestCat test cat function against 4 files, in each file it is written a bit of someData
// array and the test expect the cat to return the exact same bit from someData array with
// the corresponding file.
func TestCat(t *testing.T) {
var files []string
someData := []byte{'l', 2, 3, 4, 'd'}
dir, err := setup(t, someData)
if err != nil {
t.Fatalf("setup has failed, %v", err)
}
defer os.RemoveAll(dir)
for i := range someData {
files = append(files, fmt.Sprintf("%v%d", filepath.Join(dir, "file"), i))
}
var b bytes.Buffer
if err := cat(&b, files); err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(b.Bytes(), someData) {
t.Fatalf("Reading files failed: got %v, want %v", b.Bytes(), someData)
}
}