walk_test.go 3.0 KB

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