walk_test.go 2.0 KB

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