build.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package build // import "github.com/docker/docker/integration-cli/cli/build"
  2. import (
  3. "io"
  4. "strings"
  5. "testing"
  6. "github.com/docker/docker/testutil/fakecontext"
  7. "gotest.tools/v3/icmd"
  8. )
  9. // WithStdinContext sets the build context from the standard input with the specified reader
  10. func WithStdinContext(closer io.ReadCloser) func(*icmd.Cmd) func() {
  11. return func(cmd *icmd.Cmd) func() {
  12. cmd.Command = append(cmd.Command, "-")
  13. cmd.Stdin = closer
  14. return func() {
  15. // FIXME(vdemeester) we should not ignore the error here…
  16. closer.Close()
  17. }
  18. }
  19. }
  20. // WithDockerfile creates / returns a CmdOperator to set the Dockerfile for a build operation
  21. func WithDockerfile(dockerfile string) func(*icmd.Cmd) func() {
  22. return func(cmd *icmd.Cmd) func() {
  23. cmd.Command = append(cmd.Command, "-")
  24. cmd.Stdin = strings.NewReader(dockerfile)
  25. return nil
  26. }
  27. }
  28. // WithoutCache makes the build ignore cache
  29. func WithoutCache(cmd *icmd.Cmd) func() {
  30. cmd.Command = append(cmd.Command, "--no-cache")
  31. return nil
  32. }
  33. // WithContextPath sets the build context path
  34. func WithContextPath(path string) func(*icmd.Cmd) func() {
  35. return func(cmd *icmd.Cmd) func() {
  36. cmd.Command = append(cmd.Command, path)
  37. return nil
  38. }
  39. }
  40. // WithExternalBuildContext use the specified context as build context
  41. func WithExternalBuildContext(ctx *fakecontext.Fake) func(*icmd.Cmd) func() {
  42. return func(cmd *icmd.Cmd) func() {
  43. cmd.Dir = ctx.Dir
  44. cmd.Command = append(cmd.Command, ".")
  45. return nil
  46. }
  47. }
  48. // WithBuildContext sets up the build context
  49. func WithBuildContext(t testing.TB, contextOperators ...func(*fakecontext.Fake) error) func(*icmd.Cmd) func() {
  50. // FIXME(vdemeester) de-duplicate that
  51. ctx := fakecontext.New(t, "", contextOperators...)
  52. return func(cmd *icmd.Cmd) func() {
  53. cmd.Dir = ctx.Dir
  54. cmd.Command = append(cmd.Command, ".")
  55. return closeBuildContext(t, ctx)
  56. }
  57. }
  58. // WithFile adds the specified file (with content) in the build context
  59. func WithFile(name, content string) func(*fakecontext.Fake) error {
  60. return fakecontext.WithFile(name, content)
  61. }
  62. func closeBuildContext(t testing.TB, ctx *fakecontext.Fake) func() {
  63. return func() {
  64. if err := ctx.Close(); err != nil {
  65. t.Fatal(err)
  66. }
  67. }
  68. }