impl test, log patterns when .daggerignore found, lint, fix stdlib/file test imports

Signed-off-by: Tony Worm <tony@hofstadter.io>
This commit is contained in:
Tony Worm 2021-03-18 12:38:50 -04:00
parent a1ba7aa59c
commit 3dab86694e
9 changed files with 63 additions and 46 deletions

View File

@ -7,15 +7,16 @@ import (
"errors" "errors"
"fmt" "fmt"
"io/fs" "io/fs"
"path"
"strings" "strings"
"github.com/docker/distribution/reference" "github.com/docker/distribution/reference"
bk "github.com/moby/buildkit/client" bk "github.com/moby/buildkit/client"
"github.com/moby/buildkit/client/llb" "github.com/moby/buildkit/client/llb"
dockerfilebuilder "github.com/moby/buildkit/frontend/dockerfile/builder" dockerfilebuilder "github.com/moby/buildkit/frontend/dockerfile/builder"
"github.com/moby/buildkit/frontend/dockerfile/dockerignore"
bkgw "github.com/moby/buildkit/frontend/gateway/client" bkgw "github.com/moby/buildkit/frontend/gateway/client"
bkpb "github.com/moby/buildkit/solver/pb" bkpb "github.com/moby/buildkit/solver/pb"
"github.com/moby/buildkit/frontend/dockerfile/dockerignore"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
@ -271,30 +272,27 @@ func (p *Pipeline) Local(ctx context.Context, op *compiler.Value, st llb.State)
llb.SessionID(p.s.SessionID()), llb.SessionID(p.s.SessionID()),
llb.FollowPaths([]string{daggerignoreFilename}), llb.FollowPaths([]string{daggerignoreFilename}),
llb.SharedKeyHint(dir+"-"+daggerignoreFilename), llb.SharedKeyHint(dir+"-"+daggerignoreFilename),
llb.WithCustomName(p.vertexNamef("Load %s", daggerignoreFilename)), llb.WithCustomName(p.vertexNamef("Try loading %s", path.Join(dir, daggerignoreFilename))),
) )
ref, err := p.s.Solve(ctx, daggerignoreState)
var daggerignore []byte
def, err := p.s.Marshal(ctx, daggerignoreState)
if err != nil {
return st, err
}
ref, err := p.s.SolveRequest(ctx, bkgw.SolveRequest{
Definition: def,
})
if err != nil { if err != nil {
return st, err return st, err
} }
// try to read file // try to read file
var daggerignore []byte
// bool in case file is empty
ignorefound := true
daggerignore, err = ref.ReadFile(ctx, bkgw.ReadRequest{ daggerignore, err = ref.ReadFile(ctx, bkgw.ReadRequest{
Filename: daggerignoreFilename, Filename: daggerignoreFilename,
}) })
// hack for string introspection because !errors.Is(err, os.ErrNotExist) does not work, same for fs // hack for string introspection because !errors.Is(err, os.ErrNotExist) does not work, same for fs
if err != nil && !strings.Contains(err.Error(), ".daggerignore: no such file or directory") { if err != nil {
if !strings.Contains(err.Error(), ".daggerignore: no such file or directory") {
return st, err return st, err
} }
ignorefound = false
}
// parse out excludes, works even if file does not exist // parse out excludes, works even if file does not exist
var excludes []string var excludes []string
@ -303,6 +301,14 @@ func (p *Pipeline) Local(ctx context.Context, op *compiler.Value, st llb.State)
return st, fmt.Errorf("%w failed to parse daggerignore", err) return st, fmt.Errorf("%w failed to parse daggerignore", err)
} }
// log out patterns if file exists
if ignorefound {
log.
Ctx(ctx).
Debug().
Str("patterns", fmt.Sprint(excludes)).
Msg("daggerignore exclude patterns")
}
// FIXME: Remove the `Copy` and use `Local` directly. // FIXME: Remove the `Copy` and use `Local` directly.
// //
// Copy'ing is a costly operation which should be unnecessary. // Copy'ing is a costly operation which should be unnecessary.

View File

@ -1,21 +1,41 @@
package main package test
import ( import (
"dagger.io/alpine"
"dagger.io/dagger" "dagger.io/dagger"
"dagger.io/llb" "dagger.io/llb"
) )
dir: dagger.#Artifact TestData: dagger.#Artifact
ignore: { _expected: """
/src/b.txt
/src/foo:
bar.txt
"""
TestIgnore: {
string string
#compute: [ #compute: [
llb.#FetchContainer & { ref: "debian:buster" }, llb.#Load & {from: alpine.#Image},
llb.#Exec & { llb.#Exec & {
args: ["bash", "-c", "ls -lh /src > /out.txt"] args: ["sh", "-c", "ls /src/* > /out.txt"]
mount: "/src": { from: dir } mount: "/src": from: TestData
}, },
llb.#Export & {source: "/out.txt"}, llb.#Export & {source: "/out.txt"},
llb.#Exec & {
args: [
"sh",
"-ec",
"""
cat > /test.txt << EOF
\(_expected)
EOF
test "$(cat /out.txt)" = "$(cat /test.txt)"
""",
]
},
] ]
} }

2
tests/ignore/testdata/.daggerignore vendored Normal file
View File

@ -0,0 +1,2 @@
a.txt
*/*.json

0
tests/ignore/testdata/a.txt vendored Normal file
View File

0
tests/ignore/testdata/b.txt vendored Normal file
View File

0
tests/ignore/testdata/foo/bar.txt vendored Normal file
View File

0
tests/ignore/testdata/foo/cow.json vendored Normal file
View File

View File

@ -1,7 +1,7 @@
package f package f
import ( import (
"dagger.io/dagger" "dagger.io/llb"
"dagger.io/alpine" "dagger.io/alpine"
"dagger.io/file" "dagger.io/file"
) )
@ -15,8 +15,8 @@ TestCreate: {
} }
test: #compute: [ test: #compute: [
dagger.#Load & {from: alpine.#Image}, llb.#Load & {from: alpine.#Image},
dagger.#Exec & { llb.#Exec & {
args: [ args: [
"sh", "sh",
"-ec", "-ec",
@ -38,8 +38,8 @@ TestRead: {
from: alpine.#Image & {version: "3.10.6"} from: alpine.#Image & {version: "3.10.6"}
} }
test: #compute: [ test: #compute: [
dagger.#Load & {from: alpine.#Image}, llb.#Load & {from: alpine.#Image},
dagger.#Exec & { llb.#Exec & {
args: [ args: [
"sh", "sh",
"-ec", "-ec",
@ -64,8 +64,8 @@ TestRead2: {
} }
test: #compute: [ test: #compute: [
dagger.#Load & {from: alpine.#Image}, llb.#Load & {from: alpine.#Image},
dagger.#Exec & { llb.#Exec & {
args: [ args: [
"sh", "sh",
"-ec", "-ec",
@ -101,23 +101,6 @@ TestAppend: {
new: read.contents new: read.contents
test: new & "hello worldfoo bar" test: new & "hello worldfoo bar"
//test: #compute: [
//dagger.#Load & {from: alpine.#Image},
//dagger.#Exec & {
//args: [
//"sh",
//"-ec",
//"""
//test "$(cat /file.txt)" = "hello worldfoo bar"
//""",
//]
//mount: "/file.txt": {
//from: append
//path: "/file.txt"
//}
//},
//]
} }
TestGlob: { TestGlob: {

View File

@ -247,6 +247,11 @@ test::dockerbuild() {
"$dagger" "${DAGGER_BINARY_ARGS[@]}" compute --input-dir TestData="$d"/dockerbuild/testdata "$d"/dockerbuild "$dagger" "${DAGGER_BINARY_ARGS[@]}" compute --input-dir TestData="$d"/dockerbuild/testdata "$d"/dockerbuild
} }
test::daggerignore() {
test::one "Dagger Ignore" --exit=0 \
"$dagger" "${DAGGER_BINARY_ARGS[@]}" compute --input-dir TestData="$d"/ignore/testdata "$d"/ignore
}
test::all(){ test::all(){
local dagger="$1" local dagger="$1"
@ -264,6 +269,7 @@ test::all(){
test::input "$dagger" test::input "$dagger"
test::subdir "$dagger" test::subdir "$dagger"
test::dockerbuild "$dagger" test::dockerbuild "$dagger"
test::daggerignore "$dagger"
test::stdlib "$dagger" test::stdlib "$dagger"
} }