walk_test.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 := w.Walk()
  27. if l1, l2 := len(files), len(testdata); l1 != l2 {
  28. t.Fatalf("Incorrect number of walked files %d != %d", l1, l2)
  29. }
  30. for i := range testdata {
  31. if n1, n2 := testdata[i].name, files[i].Name; n1 != n2 {
  32. t.Errorf("Incorrect file name %q != %q for case #%d", n1, n2, i)
  33. }
  34. if h1, h2 := fmt.Sprintf("%x", files[i].Blocks[0].Hash), testdata[i].hash; h1 != h2 {
  35. t.Errorf("Incorrect hash %q != %q for case #%d", h1, h2, i)
  36. }
  37. t0 := time.Date(2010, 1, 1, 0, 0, 0, 0, time.UTC).Unix()
  38. t1 := time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC).Unix()
  39. if mt := files[i].Modified; mt < t0 || mt > t1 {
  40. t.Errorf("Unrealistic modtime %d for test %d", mt, i)
  41. }
  42. }
  43. if !reflect.DeepEqual(ignores, correctIgnores) {
  44. t.Errorf("Incorrect ignores\n %v\n %v", correctIgnores, ignores)
  45. }
  46. }
  47. func TestIgnore(t *testing.T) {
  48. var patterns = map[string][]string{
  49. "": {"t2"},
  50. "foo": {"bar", "z*"},
  51. "foo/baz": {"quux", ".*"},
  52. }
  53. var tests = []struct {
  54. f string
  55. r bool
  56. }{
  57. {"foo/bar", true},
  58. {"foo/quux", false},
  59. {"foo/zuux", true},
  60. {"foo/qzuux", false},
  61. {"foo/baz/t1", false},
  62. {"foo/baz/t2", true},
  63. {"foo/baz/bar", true},
  64. {"foo/baz/quuxa", false},
  65. {"foo/baz/aquux", false},
  66. {"foo/baz/.quux", true},
  67. {"foo/baz/zquux", true},
  68. {"foo/baz/quux", true},
  69. {"foo/bazz/quux", false},
  70. }
  71. w := Walker{}
  72. for i, tc := range tests {
  73. if r := w.ignoreFile(patterns, tc.f); r != tc.r {
  74. t.Errorf("Incorrect ignoreFile() #%d; E: %v, A: %v", i, tc.r, r)
  75. }
  76. }
  77. }