walk_test.go 3.3 KB

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