summaryrefslogtreecommitdiffstats
path: root/scanner/walk_test.go
blob: fb1c0b9dafac4c8b8f2467f2d41af3ed19448cb1 (plain)
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// Copyright (C) 2014 Jakob Borg and other contributors. All rights reserved.
// Use of this source code is governed by an MIT-style license that can be
// found in the LICENSE file.

package scanner

import (
	"fmt"
	"reflect"
	"testing"
	"time"
)

var testdata = []struct {
	name string
	size int
	hash string
}{
	{"bar", 10, "2f72cc11a6fcd0271ecef8c61056ee1eb1243be3805bf9a9df98f92f7636b05c"},
	{"empty", 0, "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"},
	{"foo", 7, "aec070645fe53ee3b3763059376134f058cc337247c978add178b6ccdfb0019f"},
}

var correctIgnores = map[string][]string{
	".": {".*", "quux"},
}

func TestWalk(t *testing.T) {
	w := Walker{
		Dir:        "testdata",
		BlockSize:  128 * 1024,
		IgnoreFile: ".stignore",
	}
	files, ignores, err := w.Walk()

	if err != nil {
		t.Fatal(err)
	}

	if l1, l2 := len(files), len(testdata); l1 != l2 {
		t.Fatalf("Incorrect number of walked files %d != %d", l1, l2)
	}

	for i := range testdata {
		if n1, n2 := testdata[i].name, files[i].Name; n1 != n2 {
			t.Errorf("Incorrect file name %q != %q for case #%d", n1, n2, i)
		}

		if h1, h2 := fmt.Sprintf("%x", files[i].Blocks[0].Hash), testdata[i].hash; h1 != h2 {
			t.Errorf("Incorrect hash %q != %q for case #%d", h1, h2, i)
		}

		t0 := time.Date(2010, 1, 1, 0, 0, 0, 0, time.UTC).Unix()
		t1 := time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC).Unix()
		if mt := files[i].Modified; mt < t0 || mt > t1 {
			t.Errorf("Unrealistic modtime %d for test %d", mt, i)
		}
	}

	if !reflect.DeepEqual(ignores, correctIgnores) {
		t.Errorf("Incorrect ignores\n  %v\n  %v", correctIgnores, ignores)
	}
}

func TestWalkError(t *testing.T) {
	w := Walker{
		Dir:        "testdata-missing",
		BlockSize:  128 * 1024,
		IgnoreFile: ".stignore",
	}
	_, _, err := w.Walk()

	if err == nil {
		t.Error("no error from missing directory")
	}

	w = Walker{
		Dir:        "testdata/bar",
		BlockSize:  128 * 1024,
		IgnoreFile: ".stignore",
	}
	_, _, err = w.Walk()

	if err == nil {
		t.Error("no error from non-directory")
	}
}

func TestIgnore(t *testing.T) {
	var patterns = map[string][]string{
		".":       {"t2"},
		"foo":     {"bar", "z*"},
		"foo/baz": {"quux", ".*"},
	}
	var tests = []struct {
		f string
		r bool
	}{
		{"foo/bar", true},
		{"foofoo", false},
		{"foo/quux", false},
		{"foo/zuux", true},
		{"foo/qzuux", false},
		{"foo/baz/t1", false},
		{"foo/baz/t2", true},
		{"foo/baz/bar", true},
		{"foo/baz/quuxa", false},
		{"foo/baz/aquux", false},
		{"foo/baz/.quux", true},
		{"foo/baz/zquux", true},
		{"foo/baz/quux", true},
		{"foo/bazz/quux", false},
	}

	w := Walker{}
	for i, tc := range tests {
		if r := w.ignoreFile(patterns, tc.f); r != tc.r {
			t.Errorf("Incorrect ignoreFile() #%d; E: %v, A: %v", i, tc.r, r)
		}
	}
}