walk_test.go 2.5 KB

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