context.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package fakecontext
  2. import (
  3. "bytes"
  4. "io/ioutil"
  5. "os"
  6. "path/filepath"
  7. )
  8. type testingT interface {
  9. Fatal(args ...interface{})
  10. Fatalf(string, ...interface{})
  11. }
  12. // New creates a fake build context
  13. func New(t testingT, dir string, modifiers ...func(*Fake) error) *Fake {
  14. fakeContext := &Fake{Dir: dir}
  15. if dir == "" {
  16. if err := newDir(fakeContext); err != nil {
  17. t.Fatal(err)
  18. }
  19. }
  20. for _, modifier := range modifiers {
  21. if err := modifier(fakeContext); err != nil {
  22. t.Fatal(err)
  23. }
  24. }
  25. return fakeContext
  26. }
  27. func newDir(fake *Fake) error {
  28. tmp, err := ioutil.TempDir("", "fake-context")
  29. if err != nil {
  30. return err
  31. }
  32. if err := os.Chmod(tmp, 0755); err != nil {
  33. return err
  34. }
  35. fake.Dir = tmp
  36. return nil
  37. }
  38. // WithFile adds the specified file (with content) in the build context
  39. func WithFile(name, content string) func(*Fake) error {
  40. return func(ctx *Fake) error {
  41. return ctx.Add(name, content)
  42. }
  43. }
  44. // WithDockerfile adds the specified content as Dockerfile in the build context
  45. func WithDockerfile(content string) func(*Fake) error {
  46. return WithFile("Dockerfile", content)
  47. }
  48. // WithFiles adds the specified files in the build context, content is a string
  49. func WithFiles(files map[string]string) func(*Fake) error {
  50. return func(fakeContext *Fake) error {
  51. for file, content := range files {
  52. if err := fakeContext.Add(file, content); err != nil {
  53. return err
  54. }
  55. }
  56. return nil
  57. }
  58. }
  59. // WithBinaryFiles adds the specified files in the build context, content is binary
  60. func WithBinaryFiles(files map[string]*bytes.Buffer) func(*Fake) error {
  61. return func(fakeContext *Fake) error {
  62. for file, content := range files {
  63. if err := fakeContext.Add(file, string(content.Bytes())); err != nil {
  64. return err
  65. }
  66. }
  67. return nil
  68. }
  69. }
  70. // Fake creates directories that can be used as a build context
  71. type Fake struct {
  72. Dir string
  73. }
  74. // Add a file at a path, creating directories where necessary
  75. func (f *Fake) Add(file, content string) error {
  76. return f.addFile(file, []byte(content))
  77. }
  78. func (f *Fake) addFile(file string, content []byte) error {
  79. fp := filepath.Join(f.Dir, filepath.FromSlash(file))
  80. dirpath := filepath.Dir(fp)
  81. if dirpath != "." {
  82. if err := os.MkdirAll(dirpath, 0755); err != nil {
  83. return err
  84. }
  85. }
  86. return ioutil.WriteFile(fp, content, 0644)
  87. }
  88. // Delete a file at a path
  89. func (f *Fake) Delete(file string) error {
  90. fp := filepath.Join(f.Dir, filepath.FromSlash(file))
  91. return os.RemoveAll(fp)
  92. }
  93. // Close deletes the context
  94. func (f *Fake) Close() error {
  95. return os.RemoveAll(f.Dir)
  96. }