2018-02-05 16:05:59 -05:00
|
|
|
package build // import "github.com/docker/docker/integration-cli/cli/build"
|
2017-03-23 13:35:22 -04:00
|
|
|
|
|
|
|
import (
|
2017-04-10 08:42:21 -04:00
|
|
|
"io"
|
2017-03-23 13:35:22 -04:00
|
|
|
"strings"
|
2019-09-23 07:54:51 -04:00
|
|
|
"testing"
|
2017-03-23 13:35:22 -04:00
|
|
|
|
2019-08-29 16:52:40 -04:00
|
|
|
"github.com/docker/docker/testutil/fakecontext"
|
2020-02-07 08:39:24 -05:00
|
|
|
"gotest.tools/v3/icmd"
|
2017-03-23 13:35:22 -04:00
|
|
|
)
|
|
|
|
|
2017-04-10 08:42:21 -04:00
|
|
|
// WithStdinContext sets the build context from the standard input with the specified reader
|
|
|
|
func WithStdinContext(closer io.ReadCloser) func(*icmd.Cmd) func() {
|
|
|
|
return func(cmd *icmd.Cmd) func() {
|
|
|
|
cmd.Command = append(cmd.Command, "-")
|
|
|
|
cmd.Stdin = closer
|
|
|
|
return func() {
|
|
|
|
// FIXME(vdemeester) we should not ignore the error here…
|
|
|
|
closer.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-23 13:35:22 -04:00
|
|
|
// WithDockerfile creates / returns a CmdOperator to set the Dockerfile for a build operation
|
|
|
|
func WithDockerfile(dockerfile string) func(*icmd.Cmd) func() {
|
|
|
|
return func(cmd *icmd.Cmd) func() {
|
|
|
|
cmd.Command = append(cmd.Command, "-")
|
|
|
|
cmd.Stdin = strings.NewReader(dockerfile)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithoutCache makes the build ignore cache
|
|
|
|
func WithoutCache(cmd *icmd.Cmd) func() {
|
|
|
|
cmd.Command = append(cmd.Command, "--no-cache")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-03-26 22:56:57 -04:00
|
|
|
// WithContextPath sets the build context path
|
2017-03-23 13:35:22 -04:00
|
|
|
func WithContextPath(path string) func(*icmd.Cmd) func() {
|
|
|
|
return func(cmd *icmd.Cmd) func() {
|
|
|
|
cmd.Command = append(cmd.Command, path)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2017-04-10 08:42:21 -04:00
|
|
|
|
|
|
|
// WithExternalBuildContext use the specified context as build context
|
|
|
|
func WithExternalBuildContext(ctx *fakecontext.Fake) func(*icmd.Cmd) func() {
|
|
|
|
return func(cmd *icmd.Cmd) func() {
|
|
|
|
cmd.Dir = ctx.Dir
|
|
|
|
cmd.Command = append(cmd.Command, ".")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithBuildContext sets up the build context
|
2019-09-23 07:54:51 -04:00
|
|
|
func WithBuildContext(t testing.TB, contextOperators ...func(*fakecontext.Fake) error) func(*icmd.Cmd) func() {
|
2017-04-10 08:42:21 -04:00
|
|
|
// FIXME(vdemeester) de-duplicate that
|
|
|
|
ctx := fakecontext.New(t, "", contextOperators...)
|
|
|
|
return func(cmd *icmd.Cmd) func() {
|
|
|
|
cmd.Dir = ctx.Dir
|
|
|
|
cmd.Command = append(cmd.Command, ".")
|
|
|
|
return closeBuildContext(t, ctx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithFile adds the specified file (with content) in the build context
|
|
|
|
func WithFile(name, content string) func(*fakecontext.Fake) error {
|
|
|
|
return fakecontext.WithFile(name, content)
|
|
|
|
}
|
|
|
|
|
2019-09-23 07:54:51 -04:00
|
|
|
func closeBuildContext(t testing.TB, ctx *fakecontext.Fake) func() {
|
2017-04-10 08:42:21 -04:00
|
|
|
return func() {
|
|
|
|
if err := ctx.Close(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|