walk_test.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. // Copyright (C) 2014 Jakob Borg and Contributors (see the CONTRIBUTORS file).
  2. // All rights reserved. Use of this source code is governed by an MIT-style
  3. // license that can be found in the LICENSE file.
  4. package scanner
  5. import (
  6. "fmt"
  7. "path/filepath"
  8. "reflect"
  9. "runtime"
  10. "sort"
  11. "testing"
  12. "time"
  13. "github.com/syncthing/syncthing/protocol"
  14. )
  15. var testdata = []struct {
  16. name string
  17. size int
  18. hash string
  19. }{
  20. {"bar", 10, "2f72cc11a6fcd0271ecef8c61056ee1eb1243be3805bf9a9df98f92f7636b05c"},
  21. {"baz", 0, ""},
  22. {"empty", 0, "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"},
  23. {"foo", 7, "aec070645fe53ee3b3763059376134f058cc337247c978add178b6ccdfb0019f"},
  24. }
  25. var correctIgnores = map[string][]string{
  26. ".": {".*", "quux"},
  27. }
  28. func TestWalkSub(t *testing.T) {
  29. w := Walker{
  30. Dir: "testdata",
  31. Sub: "foo",
  32. BlockSize: 128 * 1024,
  33. IgnoreFile: ".stignore",
  34. }
  35. fchan, _, err := w.Walk()
  36. var files []protocol.FileInfo
  37. for f := range fchan {
  38. files = append(files, f)
  39. }
  40. if err != nil {
  41. t.Fatal(err)
  42. }
  43. if len(files) != 1 {
  44. t.Fatalf("Incorrect length %d != 1", len(files))
  45. }
  46. if files[0].Name != "foo" {
  47. t.Errorf("Incorrect file %v != foo", files[0])
  48. }
  49. }
  50. func TestWalk(t *testing.T) {
  51. w := Walker{
  52. Dir: "testdata",
  53. BlockSize: 128 * 1024,
  54. IgnoreFile: ".stignore",
  55. }
  56. fchan, ignores, err := w.Walk()
  57. var files []protocol.FileInfo
  58. for f := range fchan {
  59. files = append(files, f)
  60. }
  61. sort.Sort(fileList(files))
  62. if err != nil {
  63. t.Fatal(err)
  64. }
  65. if l1, l2 := len(files), len(testdata); l1 != l2 {
  66. t.Log(files)
  67. t.Log(testdata)
  68. t.Fatalf("Incorrect number of walked files %d != %d", l1, l2)
  69. }
  70. for i := range testdata {
  71. if n1, n2 := testdata[i].name, files[i].Name; n1 != n2 {
  72. t.Errorf("Incorrect file name %q != %q for case #%d", n1, n2, i)
  73. }
  74. if testdata[i].hash != "" {
  75. if h1, h2 := fmt.Sprintf("%x", files[i].Blocks[0].Hash), testdata[i].hash; h1 != h2 {
  76. t.Errorf("Incorrect hash %q != %q for case #%d", h1, h2, i)
  77. }
  78. }
  79. t0 := time.Date(2010, 1, 1, 0, 0, 0, 0, time.UTC).Unix()
  80. t1 := time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC).Unix()
  81. if mt := files[i].Modified; mt < t0 || mt > t1 {
  82. t.Errorf("Unrealistic modtime %d for test %d", mt, i)
  83. }
  84. }
  85. if !reflect.DeepEqual(ignores, correctIgnores) {
  86. t.Errorf("Incorrect ignores\n %v\n %v", correctIgnores, ignores)
  87. }
  88. }
  89. func TestWalkError(t *testing.T) {
  90. w := Walker{
  91. Dir: "testdata-missing",
  92. BlockSize: 128 * 1024,
  93. IgnoreFile: ".stignore",
  94. }
  95. _, _, err := w.Walk()
  96. if err == nil {
  97. t.Error("no error from missing directory")
  98. }
  99. w = Walker{
  100. Dir: "testdata/bar",
  101. BlockSize: 128 * 1024,
  102. IgnoreFile: ".stignore",
  103. }
  104. _, _, err = w.Walk()
  105. if err == nil {
  106. t.Error("no error from non-directory")
  107. }
  108. }
  109. func TestIgnore(t *testing.T) {
  110. pattern := "q\\[abc\\]y"
  111. // On Windows, escaping is disabled.
  112. // Instead, '\\' is treated as path separator.
  113. if runtime.GOOS == "windows" {
  114. pattern = "q[abc]y"
  115. }
  116. var patterns = map[string][]string{
  117. ".": {"t2"},
  118. "foo": {"bar", "z*", "q[abc]x", pattern},
  119. filepath.Join("foo", "baz"): {"quux", ".*"},
  120. }
  121. var tests = []struct {
  122. f string
  123. r bool
  124. }{
  125. {filepath.Join("foo", "bar"), true},
  126. {filepath.Join("foofoo"), false},
  127. {filepath.Join("foo", "quux"), false},
  128. {filepath.Join("foo", "zuux"), true},
  129. {filepath.Join("foo", "qzuux"), false},
  130. {filepath.Join("foo", "baz", "t1"), false},
  131. {filepath.Join("foo", "baz", "t2"), true},
  132. {filepath.Join("foo", "baz", "bar"), true},
  133. {filepath.Join("foo", "baz", "quuxa"), false},
  134. {filepath.Join("foo", "baz", "aquux"), false},
  135. {filepath.Join("foo", "baz", ".quux"), true},
  136. {filepath.Join("foo", "baz", "zquux"), true},
  137. {filepath.Join("foo", "baz", "quux"), true},
  138. {filepath.Join("foo", "bazz", "quux"), false},
  139. {filepath.Join("foo", "bazz", "q[abc]x"), true},
  140. {filepath.Join("foo", "bazz", "qax"), true},
  141. {filepath.Join("foo", "bazz", "q[abc]y"), true},
  142. }
  143. w := Walker{}
  144. for i, tc := range tests {
  145. if r := w.ignoreFile(patterns, tc.f); r != tc.r {
  146. t.Errorf("Incorrect ignoreFile() #%d (%s); E: %v, A: %v", i, tc.f, tc.r, r)
  147. }
  148. }
  149. }
  150. type fileList []protocol.FileInfo
  151. func (f fileList) Len() int {
  152. return len(f)
  153. }
  154. func (f fileList) Less(a, b int) bool {
  155. return f[a].Name < f[b].Name
  156. }
  157. func (f fileList) Swap(a, b int) {
  158. f[a], f[b] = f[b], f[a]
  159. }