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.8KB

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