From 89c632376404f60ae98e31810e910adf5a1ac7f2 Mon Sep 17 00:00:00 2001 From: Jared Lunde Date: Wed, 15 May 2024 13:54:51 -0600 Subject: [PATCH] golang tests --- README.md | 2 +- runtime/golang_test.go | 100 +++++++++++++++++++++++++++++ runtime/node.go | 2 +- testdata/go-mod/cmd/hello/hello.go | 1 + testdata/go-mod/go.mod | 3 + testdata/go/.tool-versions | 1 + testdata/go/main.go | 0 7 files changed, 107 insertions(+), 2 deletions(-) create mode 100644 runtime/golang_test.go create mode 100644 testdata/go-mod/cmd/hello/hello.go create mode 100644 testdata/go-mod/go.mod create mode 100644 testdata/go/.tool-versions create mode 100644 testdata/go/main.go diff --git a/README.md b/README.md index 73ce705..df65604 100644 --- a/README.md +++ b/README.md @@ -369,7 +369,7 @@ In order of precedence: #### Start Command In order of precedence: - `package.json` scripts: `"serve", "start:prod", "start:production", "start-prod", "start-production", "start"` - - `package.json` scripts search for regex matching: `^.*?\bnode(mon)?\b.*?(index|main|server|client)\.([cm]?js)\b` + - `package.json` scripts search for regex matching: `^.*?\b(ts-)?node(mon)?\b.*?(index|main|server|client)\.([cm]?[tj]s)\b` - `package.json` main/module file: `node ${mainFile}` --- diff --git a/runtime/golang_test.go b/runtime/golang_test.go new file mode 100644 index 0000000..21f7c0f --- /dev/null +++ b/runtime/golang_test.go @@ -0,0 +1,100 @@ +package runtime_test + +import ( + "regexp" + "strings" + "testing" + + "github.com/flexstack/new-dockerfile/runtime" +) + +func TestGolangMatch(t *testing.T) { + tests := []struct { + name string + path string + expected bool + }{ + { + name: "Golang project", + path: "../testdata/go", + expected: true, + }, + { + name: "Golang project with go.mod file", + path: "../testdata/go-mod", + expected: true, + }, + { + name: "Not a Golang project", + path: "../testdata/deno", + expected: false, + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + golang := &runtime.Golang{Log: logger} + if golang.Match(test.path) != test.expected { + t.Errorf("expected %v, got %v", test.expected, golang.Match(test.path)) + } + }) + } +} + +func TestGolangGenerateDockerfile(t *testing.T) { + tests := []struct { + name string + path string + expected []any + }{ + { + name: "Golang project", + path: "../testdata/go", + expected: []any{`ARG VERSION=1.16.3`, `ARG PACKAGE=./main.go`}, + }, + { + name: "Golang project with go.mod file", + path: "../testdata/go-mod", + expected: []any{`ARG VERSION=1.22.3`, `ARG PACKAGE=./cmd/hello`}, + }, + { + name: "Not a Golang project", + path: "../testdata/ruby", + expected: []any{`ARG VERSION=1.17`, regexp.MustCompile(`^ARG PACKAGE=$`)}, + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + golang := &runtime.Golang{Log: logger} + dockerfile, err := golang.GenerateDockerfile(test.path) + if err != nil { + t.Errorf("unexpected error: %v", err) + } + + for _, line := range test.expected { + found := false + lines := strings.Split(string(dockerfile), "\n") + + for _, l := range lines { + switch v := line.(type) { + case string: + if strings.Contains(l, v) { + found = true + break + } + case *regexp.Regexp: + if v.MatchString(l) { + found = true + break + } + } + } + + if !found { + t.Errorf("expected %v, not found in %v", line, string(dockerfile)) + } + } + }) + } +} diff --git a/runtime/node.go b/runtime/node.go index a812ce8..ead088a 100644 --- a/runtime/node.go +++ b/runtime/node.go @@ -164,7 +164,7 @@ func (d *Node) GenerateDockerfile(path string) ([]byte, error) { return buf.Bytes(), nil } -var startScriptRe = regexp.MustCompile(`^.*?\bnode(mon)?\b.*?(index|main|server|client)\.([cm]?js)\b`) +var startScriptRe = regexp.MustCompile(`^.*?\b(ts-)?node(mon)?\b.*?(index|main|server|client)\.([cm]?[tj]s)\b`) var nodeTemplate = strings.TrimSpace(` ARG VERSION={{.Version}} diff --git a/testdata/go-mod/cmd/hello/hello.go b/testdata/go-mod/cmd/hello/hello.go new file mode 100644 index 0000000..cd35a48 --- /dev/null +++ b/testdata/go-mod/cmd/hello/hello.go @@ -0,0 +1 @@ +package hello diff --git a/testdata/go-mod/go.mod b/testdata/go-mod/go.mod new file mode 100644 index 0000000..de75006 --- /dev/null +++ b/testdata/go-mod/go.mod @@ -0,0 +1,3 @@ +module helloworld + +go 1.22.3 diff --git a/testdata/go/.tool-versions b/testdata/go/.tool-versions new file mode 100644 index 0000000..38fe77c --- /dev/null +++ b/testdata/go/.tool-versions @@ -0,0 +1 @@ +golang 1.16.3 \ No newline at end of file diff --git a/testdata/go/main.go b/testdata/go/main.go new file mode 100644 index 0000000..e69de29