moby/builder/remotecontext/detect_test.go
Sebastiaan van Stijn 9d44956d8c
builder: fix empty-lines (revive)
builder/remotecontext/detect_test.go:64:66: empty-lines: extra empty line at the end of a block (revive)
    builder/remotecontext/detect_test.go:78:46: empty-lines: extra empty line at the end of a block (revive)
    builder/remotecontext/detect_test.go:91:51: empty-lines: extra empty line at the end of a block (revive)
    builder/dockerfile/internals_test.go:95:38: empty-lines: extra empty line at the end of a block (revive)
    builder/dockerfile/copy.go:86:112: empty-lines: extra empty line at the end of a block (revive)
    builder/dockerfile/dispatchers_test.go:286:39: empty-lines: extra empty line at the start of a block (revive)
    builder/dockerfile/builder.go:280:38: empty-lines: extra empty line at the end of a block (revive)
    builder/dockerfile/dispatchers.go:66:85: empty-lines: extra empty line at the start of a block (revive)
    builder/dockerfile/dispatchers.go:559:85: empty-lines: extra empty line at the start of a block (revive)
    builder/builder-next/adapters/localinlinecache/inlinecache.go:26:183: empty-lines: extra empty line at the start of a block (revive)
    builder/builder-next/adapters/containerimage/pull.go:441:9: empty-lines: extra empty line at the start of a block (revive)

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
(cherry picked from commit ecb4ed172b)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-09-30 23:59:29 +02:00

119 lines
3.3 KiB
Go

package remotecontext // import "github.com/docker/docker/builder/remotecontext"
import (
"errors"
"log"
"os"
"sort"
"testing"
"github.com/docker/docker/builder"
"github.com/docker/docker/pkg/containerfs"
)
const (
dockerfileContents = "FROM busybox"
dockerignoreFilename = ".dockerignore"
testfileContents = "test"
)
const shouldStayFilename = "should_stay"
func extractFilenames(files []os.DirEntry) []string {
filenames := make([]string, len(files))
for i, file := range files {
filenames[i] = file.Name()
}
return filenames
}
func checkDirectory(t *testing.T, dir string, expectedFiles []string) {
files, err := os.ReadDir(dir)
if err != nil {
t.Fatalf("Could not read directory: %s", err)
}
if len(files) != len(expectedFiles) {
log.Fatalf("Directory should contain exactly %d file(s), got %d", len(expectedFiles), len(files))
}
filenames := extractFilenames(files)
sort.Strings(filenames)
sort.Strings(expectedFiles)
for i, filename := range filenames {
if filename != expectedFiles[i] {
t.Fatalf("File %s should be in the directory, got: %s", expectedFiles[i], filename)
}
}
}
func executeProcess(t *testing.T, contextDir string) {
modifiableCtx := &stubRemote{root: containerfs.NewLocalContainerFS(contextDir)}
err := removeDockerfile(modifiableCtx, builder.DefaultDockerfileName)
if err != nil {
t.Fatalf("Error when executing Process: %s", err)
}
}
func TestProcessShouldRemoveDockerfileDockerignore(t *testing.T) {
contextDir, cleanup := createTestTempDir(t, "", "builder-dockerignore-process-test")
defer cleanup()
createTestTempFile(t, contextDir, shouldStayFilename, testfileContents, 0777)
createTestTempFile(t, contextDir, dockerignoreFilename, "Dockerfile\n.dockerignore", 0777)
createTestTempFile(t, contextDir, builder.DefaultDockerfileName, dockerfileContents, 0777)
executeProcess(t, contextDir)
checkDirectory(t, contextDir, []string{shouldStayFilename})
}
func TestProcessNoDockerignore(t *testing.T) {
contextDir, cleanup := createTestTempDir(t, "", "builder-dockerignore-process-test")
defer cleanup()
createTestTempFile(t, contextDir, shouldStayFilename, testfileContents, 0777)
createTestTempFile(t, contextDir, builder.DefaultDockerfileName, dockerfileContents, 0777)
executeProcess(t, contextDir)
checkDirectory(t, contextDir, []string{shouldStayFilename, builder.DefaultDockerfileName})
}
func TestProcessShouldLeaveAllFiles(t *testing.T) {
contextDir, cleanup := createTestTempDir(t, "", "builder-dockerignore-process-test")
defer cleanup()
createTestTempFile(t, contextDir, shouldStayFilename, testfileContents, 0777)
createTestTempFile(t, contextDir, builder.DefaultDockerfileName, dockerfileContents, 0777)
createTestTempFile(t, contextDir, dockerignoreFilename, "input1\ninput2", 0777)
executeProcess(t, contextDir)
checkDirectory(t, contextDir, []string{shouldStayFilename, builder.DefaultDockerfileName, dockerignoreFilename})
}
// TODO: remove after moving to a separate pkg
type stubRemote struct {
root containerfs.ContainerFS
}
func (r *stubRemote) Hash(path string) (string, error) {
return "", errors.New("not implemented")
}
func (r *stubRemote) Root() containerfs.ContainerFS {
return r.root
}
func (r *stubRemote) Close() error {
return errors.New("not implemented")
}
func (r *stubRemote) Remove(p string) error {
return r.root.Remove(r.root.Join(r.root.Path(), p))
}