Skip to content

Commit

Permalink
add requested changes
Browse files Browse the repository at this point in the history
  • Loading branch information
DannyBrito authored and cpuguy83 committed Oct 21, 2024
1 parent a43e893 commit 295356e
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 95 deletions.
6 changes: 2 additions & 4 deletions generator_gomod.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,17 @@ func withGomod(g *SourceGenerator, srcSt, worker llb.State, opts ...llb.Constrai
if g.Gomod.Paths == nil {
paths = []string{"."}
}
states := make([]llb.State, 0, len(paths))

for _, path := range paths {
currentState := worker.Run(
in = worker.Run(
ShArgs("go mod download"),
llb.AddEnv("GOMODCACHE", gomodCacheDir),
llb.Dir(filepath.Join(joinedWorkDir, path)),
srcMount,
WithConstraints(opts...),
).AddMount(gomodCacheDir, in)
states = append(states, currentState)
}
return MergeAtPath(in, states, "/")
return in
}
}

Expand Down
152 changes: 61 additions & 91 deletions test/source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,42 +363,12 @@ index ea874f5..ba38f84 100644
}
}

tests := map[string]struct {
module string
sourceInline *dalec.SourceInline
}{
"no patch": {
module: "github.com/cpuguy83/tar2go@v0.3.1",
},
"alternative go fixture": {
module: "github.com/stretchr/testify@v1.7.0",
sourceInline: &dalec.SourceInline{
Dir: &dalec.SourceInlineDir{
Files: map[string]*dalec.SourceInlineFile{
"main.go": {Contents: alternativeGomodFixtureMain},
"go.mod": {Contents: alternativeGomodFixtureMod},
"go.sum": {Contents: alternativeGomodFixtureSum},
},
},
},
},
}

for name, tt := range tests {

t.Run(name, func(t *testing.T) {
t.Parallel()
spec := baseSpec()
if tt.sourceInline != nil {
source := spec.Sources[srcName]
source.Inline = tt.sourceInline
spec.Sources[srcName] = source
}
testEnv.RunTest(baseCtx, t, func(ctx context.Context, gwc gwclient.Client) {
checkModule(ctx, gwc, tt.module, spec)
})
t.Run("no patch", func(t *testing.T) {
t.Parallel()
testEnv.RunTest(baseCtx, t, func(ctx context.Context, gwc gwclient.Client) {
checkModule(ctx, gwc, "github.com/cpuguy83/tar2go@v0.3.1", baseSpec())
})
}
})

t.Run("with patch", func(t *testing.T) {
t.Run("file", func(t *testing.T) {
Expand Down Expand Up @@ -446,76 +416,76 @@ index ea874f5..ba38f84 100644
})
})
})
}

func TestMultiGoModuleGen(t *testing.T) {
t.Parallel()
/*
dir/
module1/
go.mod
go.sum
main.go
module2/
go.mod
go.sum
main.go
*/
contextSt := llb.Scratch().File(llb.Mkdir("/dir", 0644)).
File(llb.Mkdir("/dir/module1", 0644)).
File(llb.Mkfile("/dir/module1/go.mod", 0644, []byte(alternativeGomodFixtureMod))).
File(llb.Mkfile("/dir/module1/go.sum", 0644, []byte(alternativeGomodFixtureSum))).
File(llb.Mkfile("/dir/module1/main.go", 0644, []byte(alternativeGomodFixtureMain))).
File(llb.Mkdir("/dir/module2", 0644)).
File(llb.Mkfile("/dir/module2/go.mod", 0644, []byte(gomodFixtureMod))).
File(llb.Mkfile("/dir/module2/go.sum", 0644, []byte(gomodFixtureSum))).
File(llb.Mkfile("/dir/module2/main.go", 0644, []byte(gomodFixtureMain)))

const contextName = "multi-module"
spec := &dalec.Spec{
Name: "test-dalec-context-source",
Sources: map[string]dalec.Source{
"src": {
Context: &dalec.SourceContext{Name: contextName},
Generate: []*dalec.SourceGenerator{
{
Gomod: &dalec.GeneratorGomod{
Paths: []string{"./dir/module1", "./dir/module2"},
t.Run("multi-module", func(t *testing.T) {
t.Parallel()
/*
dir/
module1/
go.mod
go.sum
main.go
module2/
go.mod
go.sum
main.go
*/
contextSt := llb.Scratch().File(llb.Mkdir("/dir", 0644)).
File(llb.Mkdir("/dir/module1", 0644)).
File(llb.Mkfile("/dir/module1/go.mod", 0644, []byte(alternativeGomodFixtureMod))).
File(llb.Mkfile("/dir/module1/go.sum", 0644, []byte(alternativeGomodFixtureSum))).
File(llb.Mkfile("/dir/module1/main.go", 0644, []byte(alternativeGomodFixtureMain))).
File(llb.Mkdir("/dir/module2", 0644)).
File(llb.Mkfile("/dir/module2/go.mod", 0644, []byte(gomodFixtureMod))).
File(llb.Mkfile("/dir/module2/go.sum", 0644, []byte(gomodFixtureSum))).
File(llb.Mkfile("/dir/module2/main.go", 0644, []byte(gomodFixtureMain)))

const contextName = "multi-module"
spec := &dalec.Spec{
Name: "test-dalec-context-source",
Sources: map[string]dalec.Source{
"src": {
Context: &dalec.SourceContext{Name: contextName},
Generate: []*dalec.SourceGenerator{
{
Gomod: &dalec.GeneratorGomod{
Paths: []string{"./dir/module1", "./dir/module2"},
},
},
},
},
},
},
Dependencies: &dalec.PackageDependencies{
Build: map[string]dalec.PackageConstraints{
"golang": {
Version: []string{},
Dependencies: &dalec.PackageDependencies{
Build: map[string]dalec.PackageConstraints{
"golang": {
Version: []string{},
},
},
},
},
}

runTest(t, func(ctx context.Context, gwc gwclient.Client) {
req := newSolveRequest(withSpec(ctx, t, spec), withBuildContext(ctx, t, contextName, contextSt), withBuildTarget("debug/gomods"))
res := solveT(ctx, t, gwc, req)
ref, err := res.SingleRef()
if err != nil {
t.Fatal(err)
}
deps := []string{"github.com/cpuguy83/tar2go@v0.3.1", "github.com/stretchr/testify@v1.7.0"}
for _, dep := range deps {
stat, err := ref.StatFile(ctx, gwclient.StatRequest{
Path: dep,
})

runTest(t, func(ctx context.Context, gwc gwclient.Client) {
req := newSolveRequest(withSpec(ctx, t, spec), withBuildContext(ctx, t, contextName, contextSt), withBuildTarget("debug/gomods"))
res := solveT(ctx, t, gwc, req)
ref, err := res.SingleRef()
if err != nil {
t.Fatal(err)
}
deps := []string{"github.com/cpuguy83/tar2go@v0.3.1", "github.com/stretchr/testify@v1.7.0"}
for _, dep := range deps {
stat, err := ref.StatFile(ctx, gwclient.StatRequest{
Path: dep,
})

if err != nil {
t.Fatal(err)
}

if !fs.FileMode(stat.Mode).IsDir() {
t.Fatal("expected directory")
if !fs.FileMode(stat.Mode).IsDir() {
t.Fatal("expected directory")
}
}
}
})
})
}

Expand Down

0 comments on commit 295356e

Please sign in to comment.