walk_test.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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")
  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 files = []File{
  49. {Name: "foo/bar"},
  50. {Name: "foo/quux"},
  51. {Name: "foo/zuux"},
  52. {Name: "foo/qzuux"},
  53. {Name: "foo/baz/t1"},
  54. {Name: "foo/baz/t2"},
  55. {Name: "foo/baz/bar"},
  56. {Name: "foo/baz/quuxa"},
  57. {Name: "foo/baz/aquux"},
  58. {Name: "foo/baz/.quux"},
  59. {Name: "foo/baz/zquux"},
  60. {Name: "foo/baz/quux"},
  61. {Name: "foo/bazz/quux"},
  62. }
  63. var remaining = []File{
  64. {Name: "foo/quux"},
  65. {Name: "foo/qzuux"},
  66. {Name: "foo/baz/t1"},
  67. {Name: "foo/baz/quuxa"},
  68. {Name: "foo/baz/aquux"},
  69. {Name: "foo/bazz/quux"},
  70. }
  71. var filtered = ignoreFilter(patterns, files)
  72. if !reflect.DeepEqual(filtered, remaining) {
  73. t.Errorf("Filtering mismatch\n %v\n %v", remaining, filtered)
  74. }
  75. }