walk_test.go 4.5 KB

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