Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add in integration testing for the Go SDK #10

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion .buildkite/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,12 @@ func runBranchBuild(pipeline *bk.StepBuilder) {
},
}).
AddCommand(&bk.Command{
Label: "Check Go SDK",
Label: "Test Go SDK",
DependsOn: []string{"build"},
Commands: []string{
"./scripts/install_go.sh",
"./scripts/check_for_changes.sh",
"cd ./sdk_tests/go && go test .",
},
Plugins: []map[string]interface{}{
{
Expand Down
1 change: 1 addition & 0 deletions pkg/codegen/go/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

func newTypesFile(fields []schema_types.Field, steps []schema.Step) string {
file := newFile()
file.AddImport("encoding/json", "encoding/json")

for _, field := range fields {
def := field.GetDefinition()
Expand Down
8 changes: 1 addition & 7 deletions pkg/schema/pipeline_step_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,7 @@ var retryOptionsField = schema_types.NewField().

var softFail = schema_types.NewField().
Name("soft_fail").
Description("Allow specified non-zero exit statuses not to fail the build.").
NumberMap()

var softFailAll = schema_types.NewField().
Name("soft_fail_all").
Description("Allow all non-zero exit statuses not to fail the build.").
Description("Make all exit statuses soft-fail.").
Boolean()

var timeoutInMinutes = schema_types.NewField().
Expand Down Expand Up @@ -103,7 +98,6 @@ var commandStep = Step{
retryOptionsField,
skip,
softFail,
softFailAll,
timeoutInMinutes,
},
}
2 changes: 1 addition & 1 deletion pkg/schema/pipeline_step_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ var group = schema_types.NewField().
var notify = schema_types.NewField().
Name("notify").
Description("Allows you to trigger build notifications to different services. You can also choose to conditionally send notifications based on pipeline events.").
String()
StringArray()

var steps = schema_types.NewField().
Name("steps").
Expand Down
7 changes: 6 additions & 1 deletion pkg/schema/pipeline_step_trigger.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ var trigger = schema_types.NewField().
Description("The slug of the pipeline to create a build. You can find it in the URL of your pipeline, and it corresponds to the name of the pipeline, converted to kebab-case.").
String()

var triggerSoftFail = schema_types.NewField().
Name("soft_fail").
Description("When true, failure of the triggered build will not cause the triggering build to fail.").
Boolean()

var triggerStep = Step{
Name: "trigger",
Description: "A trigger step creates a build on another pipeline.",
Expand All @@ -57,7 +62,7 @@ var triggerStep = Step{
ifField,
label,
skip,
softFail,
triggerSoftFail,
trigger,
},
}
10 changes: 8 additions & 2 deletions pkg/schema/pipeline_step_wait.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,16 @@ package schema
import "github.com/buildkite/pipeline-sdk/pkg/schema_types"

var wait = schema_types.NewField().
Name("wait_label").
Description("When providing options for the wait step, you will need to set this value to \"~\".").
Required().
Null("wait_label")

var waitField = schema_types.NewField().
Name("wait").
Description("When providing options for the wait step, you will need to set this value to \"~\".").
Required().
String()
FieldRef(&wait)

var continueOnFailure = schema_types.NewField().
Name("continue_on_failure").
Expand All @@ -21,6 +27,6 @@ var waitStep = Step{
continueOnFailure,
dependsOn,
ifField,
wait,
waitField,
},
}
1 change: 1 addition & 0 deletions pkg/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ var PipelinesSchema = PipelineSchema{
selectInput,
textInput,
steps,
wait,
},
Steps: []Step{
blockStep,
Expand Down
2 changes: 1 addition & 1 deletion pkg/schema/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ var selectInput = schema_types.NewField().
schema_types.NewField().
Name("options").
Description("The list of select field options. For 6 or less options they'll be displayed as radio buttons, otherwise they'll be displayed in a dropdown box. If selecting multiple options is permitted the options will be displayed as checkboxes.").
FieldRef(&selectInputOption),
CustomArray(selectInputOption),
})

var fields = schema_types.NewField().
Expand Down
10 changes: 10 additions & 0 deletions pkg/schema_types/array.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ func (s SchemaArray) GoType() string {
return s.Items.GoType()
}

if items, ok := s.Items.(SchemaObject); ok {
return fmt.Sprintf("[]%s", items.Name.TitleCase())
}

return fmt.Sprintf("[]%s", s.Items.GoType())
}

Expand Down Expand Up @@ -60,4 +64,10 @@ func (array) Union(name string, fields []Field) SchemaArray {
}
}

func (array) Custom(fieldref Field) SchemaArray {
return SchemaArray{
Items: fieldref.typ,
}
}

var Array = array{}
2 changes: 1 addition & 1 deletion pkg/schema_types/array_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func TestSchemaArray(t *testing.T) {
})

t.Run("should return a struct and interface for unions", func(t *testing.T) {
expectation := "type testFieldDefinition struct {\n\n}\ntype TestField interface {\n ToTestField() testFieldDefinition\n}"
expectation := "type TestField interface {\n MarshalJSON() ([]byte, error)\n}"
testUnionValue := SchemaArray{
Items: Union.New("test_field", []Field{}),
}
Expand Down
10 changes: 10 additions & 0 deletions pkg/schema_types/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ func (f *Field) FieldRef(ref *Field) Field {
return *f
}

func (f *Field) CustomArray(ref Field) Field {
f.typ = Array.Custom(ref)
return *f
}

func (f *Field) String() Field {
f.typ = Simple.String()
return *f
Expand Down Expand Up @@ -106,6 +111,11 @@ func (f *Field) Enum(values ...string) Field {
return *f
}

func (f *Field) Null(name string) Field {
f.typ = Null.New(name)
return *f
}

func (f Field) TypeScriptIdentifier() string {
return f.name.TitleCase()
}
Expand Down
32 changes: 32 additions & 0 deletions pkg/schema_types/null.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package schema_types

import "fmt"

type SchemaNull struct {
Name AttributeName
}

func (SchemaNull) IsUnion() bool {
return false
}

func (s SchemaNull) TypeScriptType() string {
return ""
}

func (s SchemaNull) GoType() string {
return fmt.Sprintf("type %s string\nfunc (%s) MarshalJSON() ([]byte,error) {\n return json.Marshal(nil) \n}",
s.Name.TitleCase(),
s.Name.TitleCase(),
)
}

type null struct{}

func (null) New(name string) SchemaNull {
return SchemaNull{
Name: AttributeName(name),
}
}

var Null = null{}
1 change: 1 addition & 0 deletions pkg/schema_types/null_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package schema_types
22 changes: 21 additions & 1 deletion pkg/schema_types/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,35 @@ func (s SchemaObject) GoType() string {
var properties []string
for _, prop := range s.Properties {
if prop.fieldref != nil {
arrayType := ""
typeName := fmt.Sprintf("*%s", prop.fieldref.name.TitleCase())
if typ, ok := prop.fieldref.typ.(SchemaArray); ok {
arrayType = "[]"

if typ.IsUnion() || typ.Items.IsUnion() {
typeName = prop.fieldref.name.TitleCase()
}
}

omitEmpty := ",omitempty"
if _, ok := prop.fieldref.typ.(SchemaNull); ok {
omitEmpty = ""
}

properties = append(properties, utils.CodeBlock{
utils.CodeGen.Comment.Go(prop.description, 0),
fmt.Sprintf("%s %s `json:\"%s,omitempty\"`", prop.name.TitleCase(), prop.fieldref.name.TitleCase(), prop.name),
fmt.Sprintf("%s %s%s `json:\"%s%s\"`", prop.name.TitleCase(), arrayType, typeName, prop.name, omitEmpty),
}.DisplayIndent(4))
continue
}

propType := prop.typ.GoType()
switch prop.typ.(type) {
case SchemaNull:
properties = append(properties, utils.CodeBlock{
utils.CodeGen.Comment.Go(prop.description, 0),
fmt.Sprintf("%s %s `json:\"%s\"`", prop.name.TitleCase(), prop.name.TitleCase(), prop.name),
}.DisplayIndent(4))
case SchemaUnion:
properties = append(properties, utils.CodeBlock{
utils.CodeGen.Comment.Go(prop.description, 0),
Expand Down
54 changes: 2 additions & 52 deletions pkg/schema_types/union.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,65 +28,15 @@ func (s SchemaUnion) TypeScriptType() string {
func (s SchemaUnion) GoType() string {
block := utils.CodeBlock{}
transformFuncs := utils.CodeGen.NewCodeBlock()
definitions := utils.CodeGen.NewCodeBlock()
definitionFields := make(map[string]bool)

for _, val := range s.Values {
var transformAssignments []string
switch val.typ.(type) {
case SchemaObject:
fields := val.typ.(SchemaObject).Fields()
for _, field := range fields {
if _, ok := definitionFields[field.name.TitleCase()]; !ok {
var propType string
if field.fieldref != nil {
switch field.fieldref.typ.(type) {
case SchemaEnum:
case SchemaArray:
case SchemaObject:
propType = field.fieldref.name.TitleCase()
default:
propType = field.fieldref.typ.GoType()
}
} else {
propType = field.typ.GoType()
}

definitionFields[field.name.TitleCase()] = true
definitions = append(definitions, fmt.Sprintf("%s\n %s %s `json:\"%s,omitempty\"`",
utils.CodeGen.Comment.Go(field.description, 4),
field.name.TitleCase(),
propType,
string(field.name),
))

transformAssignments = append(transformAssignments, fmt.Sprintf(" %s: x.%s,",
field.name.TitleCase(),
field.name.TitleCase(),
))
}
}
default:
panic("non object unions are currently not supported")
}

transformFuncs = append(transformFuncs, fmt.Sprintf("func (x %s) To%s() %sDefinition {\n return %sDefinition{\n%s\n }\n}",
transformFuncs = append(transformFuncs, fmt.Sprintf("func (x *%s) MarshalJSON() ([]byte, error) {\n return json.Marshal(*x)\n}",
val.name.TitleCase(),
s.Name.TitleCase(),
s.Name.CamelCase(),
s.Name.CamelCase(),
strings.Join(transformAssignments, "\n"),
))
}

block = append(block, fmt.Sprintf("type %sDefinition struct {\n%s\n}",
s.Name.CamelCase(),
strings.Join(definitions, "\n"),
))
block = append(block, fmt.Sprintf("type %s interface {\n To%s() %sDefinition\n}",
s.Name.TitleCase(),
block = append(block, fmt.Sprintf("type %s interface {\n MarshalJSON() ([]byte, error)\n}",
s.Name.TitleCase(),
s.Name.CamelCase(),
))
block = append(block, transformFuncs...)

Expand Down
2 changes: 1 addition & 1 deletion pkg/schema_types/union_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func TestSchemaUnion(t *testing.T) {
})

t.Run("should return the go type", func(t *testing.T) {
assert.Equal(t, "type unionDefinition struct {\n\n}\ntype Union interface {\n ToUnion() unionDefinition\n}\nfunc (x FieldOne) ToUnion() unionDefinition {\n return unionDefinition{\n\n }\n}\nfunc (x FieldTwo) ToUnion() unionDefinition {\n return unionDefinition{\n\n }\n}", testValue.GoType())
assert.Equal(t, "type Union interface {\n MarshalJSON() ([]byte, error)\n}\nfunc (x *FieldOne) MarshalJSON() ([]byte, error) {\n return json.Marshal(*x)\n}\nfunc (x *FieldTwo) MarshalJSON() ([]byte, error) {\n return json.Marshal(*x)\n}", testValue.GoType())
})
}

Expand Down
Loading