walk_test.go 3.7 KB

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