walk_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // Copyright (C) 2014 Jakob Borg and other contributors. All rights reserved.
  2. // Use of this source code is governed by an MIT-style license that can be
  3. // found in the LICENSE file.
  4. package scanner
  5. import (
  6. "fmt"
  7. "reflect"
  8. "testing"
  9. "time"
  10. )
  11. var testdata = []struct {
  12. name string
  13. size int
  14. hash string
  15. }{
  16. {"bar", 10, "2f72cc11a6fcd0271ecef8c61056ee1eb1243be3805bf9a9df98f92f7636b05c"},
  17. {"empty", 0, "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"},
  18. {"foo", 7, "aec070645fe53ee3b3763059376134f058cc337247c978add178b6ccdfb0019f"},
  19. }
  20. var correctIgnores = map[string][]string{
  21. ".": {".*", "quux"},
  22. }
  23. func TestWalk(t *testing.T) {
  24. w := Walker{
  25. Dir: "testdata",
  26. BlockSize: 128 * 1024,
  27. IgnoreFile: ".stignore",
  28. }
  29. files, ignores, err := w.Walk()
  30. if err != nil {
  31. t.Fatal(err)
  32. }
  33. if l1, l2 := len(files), len(testdata); l1 != l2 {
  34. t.Fatalf("Incorrect number of walked files %d != %d", l1, l2)
  35. }
  36. for i := range testdata {
  37. if n1, n2 := testdata[i].name, files[i].Name; n1 != n2 {
  38. t.Errorf("Incorrect file name %q != %q for case #%d", n1, n2, i)
  39. }
  40. if h1, h2 := fmt.Sprintf("%x", files[i].Blocks[0].Hash), testdata[i].hash; h1 != h2 {
  41. t.Errorf("Incorrect hash %q != %q for case #%d", h1, h2, i)
  42. }
  43. t0 := time.Date(2010, 1, 1, 0, 0, 0, 0, time.UTC).Unix()
  44. t1 := time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC).Unix()
  45. if mt := files[i].Modified; mt < t0 || mt > t1 {
  46. t.Errorf("Unrealistic modtime %d for test %d", mt, i)
  47. }
  48. }
  49. if !reflect.DeepEqual(ignores, correctIgnores) {
  50. t.Errorf("Incorrect ignores\n %v\n %v", correctIgnores, ignores)
  51. }
  52. }
  53. func TestWalkError(t *testing.T) {
  54. w := Walker{
  55. Dir: "testdata-missing",
  56. BlockSize: 128 * 1024,
  57. IgnoreFile: ".stignore",
  58. }
  59. _, _, err := w.Walk()
  60. if err == nil {
  61. t.Error("no error from missing directory")
  62. }
  63. w = Walker{
  64. Dir: "testdata/bar",
  65. BlockSize: 128 * 1024,
  66. IgnoreFile: ".stignore",
  67. }
  68. _, _, err = w.Walk()
  69. if err == nil {
  70. t.Error("no error from non-directory")
  71. }
  72. }
  73. func TestIgnore(t *testing.T) {
  74. var patterns = map[string][]string{
  75. ".": {"t2"},
  76. "foo": {"bar", "z*"},
  77. "foo/baz": {"quux", ".*"},
  78. }
  79. var tests = []struct {
  80. f string
  81. r bool
  82. }{
  83. {"foo/bar", true},
  84. {"foo/quux", false},
  85. {"foo/zuux", true},
  86. {"foo/qzuux", false},
  87. {"foo/baz/t1", false},
  88. {"foo/baz/t2", true},
  89. {"foo/baz/bar", true},
  90. {"foo/baz/quuxa", false},
  91. {"foo/baz/aquux", false},
  92. {"foo/baz/.quux", true},
  93. {"foo/baz/zquux", true},
  94. {"foo/baz/quux", true},
  95. {"foo/bazz/quux", false},
  96. }
  97. w := Walker{}
  98. for i, tc := range tests {
  99. if r := w.ignoreFile(patterns, tc.f); r != tc.r {
  100. t.Errorf("Incorrect ignoreFile() #%d; E: %v, A: %v", i, tc.r, r)
  101. }
  102. }
  103. }