You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

findup_test.go 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package gofindup
  2. import (
  3. "path/filepath"
  4. "testing"
  5. "github.com/spf13/afero"
  6. "github.com/stretchr/testify/assert"
  7. )
  8. type given struct {
  9. file string
  10. base string
  11. }
  12. type expect struct {
  13. found bool
  14. path string
  15. err string
  16. }
  17. type finderAssertion struct {
  18. given
  19. expect
  20. }
  21. type finderAssertions []finderAssertion
  22. type finderFunc func(string, string, afero.Fs) (bool, error)
  23. type findupFunc func(string, string, afero.Fs) (string, error)
  24. var fakefs = afero.NewMemMapFs()
  25. func init() {
  26. // Build fake file system
  27. fakefs.MkdirAll("/test", 0755)
  28. fakefs.MkdirAll("/test/a", 0755)
  29. fakefs.MkdirAll("/test/a/ab", 0755)
  30. afero.WriteFile(fakefs, "/test/a/ab/f.txt", []byte("some mock file"), 0644)
  31. fakefs.MkdirAll("/test/a/ac", 0755)
  32. afero.WriteFile(fakefs, "/test/a/ad.txt", []byte("some mock file"), 0644)
  33. fakefs.MkdirAll("/test/b", 0755)
  34. fakefs.MkdirAll("/test/b/bc", 0755)
  35. afero.WriteFile(fakefs, "/test/b/bc/f.txt", []byte("some mock file"), 0644)
  36. afero.WriteFile(fakefs, "/test/b/bd.txt", []byte("some mock file"), 0644)
  37. fakefs.MkdirAll("/test/c", 0755)
  38. fakefs.MkdirAll("/test/c/cd", 0755)
  39. afero.WriteFile(fakefs, "/test/c/cd/f.txt", []byte("some mock file"), 0644)
  40. afero.WriteFile(fakefs, "/test/t.txt", []byte("some mock file"), 0644)
  41. afero.WriteFile(fakefs, "r.txt", []byte("some mock file"), 0644)
  42. }
  43. func TestFindIn(t *testing.T) {
  44. assertions := finderAssertions{
  45. {given{"ab", "/test/a"}, expect{found: true, err: ""}},
  46. {given{"ac", "/test/a"}, expect{found: true, err: ""}},
  47. {given{"ae", "/test/a"}, expect{found: false, err: ""}},
  48. {given{"de", "/test/d"}, expect{found: false, err: "open " + filepath.Join("/", "test", "d") + ": file does not exist"}},
  49. }
  50. for _, a := range assertions {
  51. found, err := hasFile(a.given.file, a.given.base, fakefs)
  52. if a.expect.found {
  53. assert.True(t, found)
  54. } else {
  55. assert.False(t, found)
  56. }
  57. if a.expect.err == "" {
  58. assert.Nil(t, err)
  59. } else {
  60. assert.EqualError(t, err, a.expect.err)
  61. }
  62. }
  63. }
  64. func TestFindUpFrom(t *testing.T) {
  65. assertions := finderAssertions{
  66. {given{"ab", "/test/a"}, expect{path: filepath.Join("/", "test", "a", "ab"), err: ""}},
  67. {given{"ab", "/test/a/ac"}, expect{path: filepath.Join("/", "test", "a", "ab"), err: ""}},
  68. {given{"t.txt", "/test/a/ac"}, expect{path: filepath.Join("/", "test", "t.txt"), err: ""}},
  69. {given{"r.txt", "/test/a/ac"}, expect{path: filepath.Join("/", "r.txt"), err: ""}},
  70. {given{"r.txt", "/test/d"}, expect{path: "", err: "open " + filepath.Join("/", "test", "d") + ": file does not exist"}},
  71. }
  72. for _, a := range assertions {
  73. found, err := findupFrom(a.given.file, a.given.base, fakefs)
  74. assert.Equal(t, found, a.expect.path)
  75. if a.expect.err == "" {
  76. assert.Nil(t, err)
  77. } else {
  78. assert.EqualError(t, err, a.expect.err)
  79. }
  80. }
  81. }