walk_test.go 1.9 KB

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