Repository: openllb/hlb
Pull request #46 (2020-03-23 20:53:27)

add compile tests, run go tests with github workflows
Fixes issue introduced in #27

Add basic compile/checker tests for some issues we have seen.

Modify github workflow to run go test


Patch:
diff --git a/checker/checker.go b/checker/checker.go
index 8882aee..f6f982c 100644
--- a/checker/checker.go
+++ b/checker/checker.go
@@ -358,7 +358,7 @@ func (c *checker) checkBlockStmt(scope *parser.Scope, typ parser.ObjType, block
 					callType = n.Type
 					// if we already have a source and this function is not a method
 					// so it will be returning an invalid second source
-					if foundSource && n.Method == nil && callType.ObjType == typ {
+					if foundSource && n.Method == nil && callType.ObjType == typ && callType.Primary() != parser.Option {
 						return ErrOnlyFirstSource{CallStmt: call}
 					}
 				case *parser.AliasDecl:


Test patch:
diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml
index 8538458..379a18a 100644
--- a/.github/workflows/test.yml
+++ b/.github/workflows/test.yml
@@ -33,6 +33,9 @@ jobs:
         go build -o build/docgen ./cmd/docgen
         go build -o build/builtingen ./cmd/builtingen
 
+    - name: Go Tests
+      run: go test -v ./...
+      
     - name: Start BuildkitD
       run: docker run --name buildkitd --rm -d --privileged openllb/buildkit:experimental
 
diff --git a/compile_test.go b/compile_test.go
new file mode 100644
index 0000000..225584e
--- /dev/null
+++ b/compile_test.go
@@ -0,0 +1,98 @@
+package hlb
+
+import (
+	"context"
+	"strings"
+	"testing"
+
+	"github.com/openllb/hlb/checker"
+	"github.com/openllb/hlb/solver"
+	"github.com/stretchr/testify/require"
+)
+
+type compileTestCase struct {
+	name    string
+	input   string
+	errType interface{}
+}
+
+func TestCompile(t *testing.T) {
+	for _, tc := range []compileTestCase{{
+		"empty",
+		`
+		fs default() {
+			scratch
+		}
+		`,
+		nil,
+	}, {
+		"image",
+		`
+		fs default() {
+			image "busybox:latest"
+		}
+		`,
+		nil,
+	}, {
+		"second source from function",
+		`
+		fs default() {
+			scratch
+			nothing fs { scratch; }
+		  }
+		fs nothing(fs repo) {
+		  scratch
+		}
+		`,
+		checker.ErrOnlyFirstSource{},
+	}, {
+		"single named option",
+		`
+		option::run myopt() {
+			dir "/tmp"
+		}
+		fs default() {
+			image "busybox:latest"
+			run "pwd" with myopt
+		}
+		`,
+		nil,
+	}, {
+		"combine named option",
+		`
+		option::run myopt() {
+			dir "/tmp"
+		}
+		fs default() {
+			image "busybox:latest"
+			run "pwd" with option {
+				dir "/etc"
+				myopt 
+			}
+		}
+		`,
+		nil,
+	}} {
+		tc := tc
+		t.Run(tc.name, func(t *testing.T) {
+			t.Parallel()
+			ctx := context.Background()
+			p, err := solver.NewProgress(ctx, solver.WithLogOutput(solver.LogOutputPlain))
+			require.NoError(t, err)
+			mw := p.MultiWriter()
+			in := strings.NewReader(cleanup(tc.input))
+			_, _, err = Compile(ctx, nil, mw, "default", in)
+			if tc.errType == nil {
+				require.NoError(t, err)
+			} else {
+				// assume if we got a semantic error we really want
+				// to validate the underlying error
+				if semErr, ok := err.(checker.ErrSemantic); ok {
+					require.IsType(t, tc.errType, semErr.Errs[0])
+				} else {
+					require.IsType(t, tc.errType, err)
+				}
+			}
+		})
+	}
+}
diff --git a/parse_test.go b/parse_test.go
index b19aa7c..9bc59a0 100644
--- a/parse_test.go
+++ b/parse_test.go
@@ -58,7 +58,7 @@ var (
 				allowNotFound
 				allowWildcard
 			}
-			copy fs { from bar; } "src" "dst" with option {
+			copy fs { bar; } "src" "dst" with option {
 				followSymlinks
 				contentsOnly
 				unpack