Skip to content

Commit

Permalink
golang tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredLunde committed May 15, 2024
1 parent c8bbb9d commit 89c6323
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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}`

---
Expand Down
100 changes: 100 additions & 0 deletions runtime/golang_test.go
Original file line number Diff line number Diff line change
@@ -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))
}
}
})
}
}
2 changes: 1 addition & 1 deletion runtime/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
Expand Down
1 change: 1 addition & 0 deletions testdata/go-mod/cmd/hello/hello.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package hello
3 changes: 3 additions & 0 deletions testdata/go-mod/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module helloworld

go 1.22.3
1 change: 1 addition & 0 deletions testdata/go/.tool-versions
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
golang 1.16.3
Empty file added testdata/go/main.go
Empty file.

0 comments on commit 89c6323

Please sign in to comment.