Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmd/dagger/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"go.dagger.io/dagger/sdk/go/dagger"
)

// nolint:deadcode,unused,varcheck
var generateCmd = &cobra.Command{
Use: "generate",
Run: Generate,
Expand Down
3 changes: 2 additions & 1 deletion cmd/dagger/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ func init() {
rootCmd.PersistentFlags().StringVarP(&configPath, "project", "p", "", "project config file")
rootCmd.AddCommand(
doCmd,
generateCmd,
devCmd,
versionCmd,
// TODO:(sipsma) disabling generate for the moment since it's not currently used anywhere, keeping the plumbing around though
// generateCmd,
)

doCmd.Flags().StringVarP(&queryFile, "file", "f", "", "query file")
Expand Down
5 changes: 2 additions & 3 deletions core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,8 @@ func New(params InitializeArgs) (router.ExecutableSchema, error) {
&containerSchema{base},
&filesystemSchema{base},
&projectSchema{
baseSchema: base,
remoteSchemas: make(map[string]*project.RemoteSchema),
compiledSchemas: make(map[string]*project.CompiledRemoteSchema),
baseSchema: base,
projectStates: make(map[string]*project.State),
},
&execSchema{base},
&dockerBuildSchema{base},
Expand Down
87 changes: 87 additions & 0 deletions core/integration/examples_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package core

import (
"context"
"testing"

"github.com/Khan/genqlient/graphql"
"github.com/stretchr/testify/require"
"go.dagger.io/dagger/engine"
)

func TestExtensionAlpine(t *testing.T) {
startOpts := &engine.Config{
Workdir: "../../",
ConfigPath: "examples/alpine/cloak.yaml",
}

err := engine.Start(context.Background(), startOpts, func(ctx engine.Context) error {
data := struct {
Alpine struct {
Build struct {
Exec struct {
Stdout string
}
}
}
}{}
resp := &graphql.Response{Data: &data}
err := ctx.Client.MakeRequest(ctx,
&graphql.Request{
Query: `
query {
alpine {
build(pkgs: ["curl"]) {
exec(input:{args: ["curl", "--version"]}) {
stdout
}
}
}
}`,
},
resp,
)
require.NoError(t, err)
require.NotEmpty(t, data.Alpine.Build.Exec.Stdout)

return nil
})
require.NoError(t, err)
}

func TestExtensionNetlifyGo(t *testing.T) {
startOpts := &engine.Config{
Workdir: "../../",
ConfigPath: "examples/netlify/go/cloak.yaml",
}

err := engine.Start(context.Background(), startOpts, func(ctx engine.Context) error {
// TODO: until we setup some shared netlify auth tokens, this test just asserts on the schema showing up

res := struct {
Core struct {
Project struct {
Schema string
}
}
}{}
resp := &graphql.Response{Data: &res}
err := ctx.Client.MakeRequest(ctx,
&graphql.Request{
Query: `{
core {
project(name: "netlify") {
schema
}
}
}`,
},
resp,
)
require.NoError(t, err)
require.NotEmpty(t, res.Core.Project.Schema)

return nil
})
require.NoError(t, err)
}
200 changes: 148 additions & 52 deletions core/integration/project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,11 @@ package core

import (
"context"
"os"
"path/filepath"
"testing"

"github.com/Khan/genqlient/graphql"
"github.com/stretchr/testify/require"
"go.dagger.io/dagger/engine"
"go.dagger.io/dagger/internal/testutil"
"go.dagger.io/dagger/sdk/go/dagger"
)

func TestExtensionMount(t *testing.T) {
Expand Down Expand Up @@ -71,71 +67,171 @@ func TestExtensionMount(t *testing.T) {
require.NoError(t, err)
}

func TestGoGenerate(t *testing.T) {
tmpdir := t.TempDir()

yamlPath := filepath.Join(tmpdir, "cloak.yaml")
err := os.WriteFile(yamlPath, []byte(`
name: testgogenerate
scripts:
- path: .
sdk: go
`), 0644) // #nosec G306
require.NoError(t, err)

goModPath := filepath.Join(tmpdir, "go.mod")
err = os.WriteFile(goModPath, []byte(`
module testgogenerate
go 1.19
`), 0644) // #nosec G306
require.NoError(t, err)
/*
TODO:(sipsma) more test cases to add

* Lists of structs (probably works already but add test)
* Pointer and non-pointer receiver type for structs (probably works already)
* Spread across multiple files
* Go interfaces as input/output? At least need comprehensible errors if that's not allowed yet
* Generics? At least need comprehensible errors if that's not allowed yet
* Exported and unexported methods and struct fields
* Provide multiple structs that have overlapping "type trees"
* Unnamed (inlined) structs. e.g. `type Foo struct { Bar struct { Baz string } }`
* Circular types (i.e. structs that have fields that reference themselves, etc.)
*/
func TestCodeToSchema(t *testing.T) {
startOpts := &engine.Config{
LocalDirs: map[string]string{
"testgogenerate": tmpdir,
},
Workdir: "../../",
ConfigPath: "core/integration/testdata/codetoschema/cloak.yaml",
}

err = engine.Start(context.Background(), startOpts, func(ctx engine.Context) error {
data := struct {
Core struct {
Filesystem struct {
LoadProject struct {
GeneratedCode dagger.Filesystem
}
err := engine.Start(context.Background(), startOpts, func(ctx engine.Context) error {
type allTheSubTypes struct {
SubStr string
SubInt int
SubBool bool
SubStrArray []string
SubIntArray []int
SubBoolArray []bool
}
type allTheTypes struct {
Str string
Int int
Bool bool
StrArray []string
IntArray []int
BoolArray []bool
SubStruct allTheSubTypes
}

res := struct {
Test struct {
RequiredTypes string
OptionalTypesSet string
OptionalTypesUnset string
OptionalReturnNil *string
OptionalReturnNonil *string
IntArrayReturn []int
StringArrayReturn []*string
StructReturn allTheTypes
ParentResolver struct {
SubField string
}
ReturnFilesystem struct {
File string
}
}
}{}
resp := &graphql.Response{Data: &data}

err := ctx.Client.MakeRequest(ctx,
&graphql.Request{
Query: `
query GeneratedCode($fs: FSID!, $configPath: String!) {
core {
filesystem(id: $fs) {
loadProject(configPath: $configPath) {
generatedCode {
id
Query: `query TestCodeToSchema($unsetString: String, $unsetInt: Int, $unsetBool: Boolean) {
test {
requiredTypes(
str: "foo",
i: 42,
b: true,
strArray: ["foo", "bar"],
intArray: [1, 2],
boolArray: [true, false],
strukt: {
str: "bar",
int: 43,
bool: false,
strArray: ["baz", "qux"],
intArray: [3, 4],
boolArray: [false, true],
subStruct: {
subStr: "subBar",
subInt: 44,
subBool: true,
subStrArray: ["subBaz", "subQux"],
subIntArray: [5, 6],
subBoolArray: [true, false],
},
},
)
optionalTypesSet: optionalTypes(str: "foo", i: 42, b: true, strArray: ["foo", $unsetString], intArray: [1, $unsetInt], boolArray: [true, $unsetBool])
optionalTypesUnset: optionalTypes(strArray: [])
optionalReturnNil: optionalReturn(returnNil: true)
optionalReturnNonil: optionalReturn(returnNil: false)
intArrayReturn(intArray: [1, 2, 3])
stringArrayReturn(strArray: ["foo", $unsetString])
structReturn(strukt: {
str: "blah",
int: 45,
bool: false,
strArray: ["baq", "quz"],
intArray: [7, 8],
boolArray: [false, true],
subStruct: {
subStr: "subBar",
subInt: 48,
subBool: true,
subStrArray: ["subBaq", "subQuz"],
subIntArray: [9, 10],
subBoolArray: [true, false],
},
}
) {
str
int
bool
strArray
intArray
boolArray
subStruct {
subStr
subInt
subBool
subStrArray
subIntArray
subBoolArray
}
}
parentResolver(str: "parent") {
subField(str: "child")
}
returnFilesystem(ref: "alpine:3.16.2") {
file(path: "/etc/alpine-release")
}
}
}
}`,
Variables: map[string]any{
"fs": ctx.LocalDirs["testgogenerate"],
"configPath": ctx.ConfigPath,
},
}`,
},
resp,
&graphql.Response{Data: &res},
)
require.NoError(t, err)
require.Equal(t, `foo 42 true {Str:bar Int:43 Bool:false StrArray:[baz qux] IntArray:[3 4] BoolArray:[false true] SubStruct:{SubStr:subBar SubInt:44 SubBool:true SubStrArray:[subBaz subQux] SubIntArray:[5 6] SubBoolArray:[true false]}} [foo bar] [1 2] [true false]`, res.Test.RequiredTypes)
require.Equal(t, "foo 42 true [foo <nil>] [1 <nil>] [true <nil>]", res.Test.OptionalTypesSet)
require.Equal(t, "<nil> <nil> <nil> [] <nil> <nil>", res.Test.OptionalTypesUnset)
require.Nil(t, res.Test.OptionalReturnNil)
require.NotNil(t, res.Test.OptionalReturnNonil)
require.Equal(t, []int{1, 2, 3}, res.Test.IntArrayReturn)
require.Equal(t, []*string{ptrTo("foo"), nil}, res.Test.StringArrayReturn)
require.Equal(t, allTheTypes{
Str: "blah",
Int: 45,
Bool: false,
StrArray: []string{"baq", "quz"},
IntArray: []int{7, 8},
BoolArray: []bool{false, true},
SubStruct: allTheSubTypes{
SubStr: "subBar",
SubInt: 48,
SubBool: true,
SubStrArray: []string{"subBaq", "subQuz"},
SubIntArray: []int{9, 10},
SubBoolArray: []bool{true, false},
},
}, res.Test.StructReturn)
require.Equal(t, "parent-child", res.Test.ParentResolver.SubField)
require.Equal(t, "3.16.2\n", res.Test.ReturnFilesystem.File)

generatedFSID := data.Core.Filesystem.LoadProject.GeneratedCode.ID

_, err = testutil.ReadFile(ctx, ctx.Client, generatedFSID, "main.go")
require.NoError(t, err)
return nil
})
require.NoError(t, err)
}

func ptrTo[T any](v T) *T {
return &v
}
4 changes: 4 additions & 0 deletions core/integration/testdata/codetoschema/cloak.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
name: codetoschema
extensions:
- path: .
sdk: go
Loading