diff --git a/runtime/nextjs_test.go b/runtime/nextjs_test.go new file mode 100644 index 0000000..3c1cdfd --- /dev/null +++ b/runtime/nextjs_test.go @@ -0,0 +1,100 @@ +package runtime_test + +import ( + "regexp" + "strings" + "testing" + + "github.com/flexstack/new-dockerfile/runtime" +) + +func TestNextJSMatch(t *testing.T) { + tests := []struct { + name string + path string + expected bool + }{ + { + name: "NextJS project", + path: "../testdata/nextjs", + expected: true, + }, + { + name: "NextJS project with standalone output", + path: "../testdata/nextjs-standalone", + expected: true, + }, + { + name: "Not a NextJS project", + path: "../testdata/deno", + expected: false, + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + nextjs := &runtime.NextJS{Log: logger} + if nextjs.Match(test.path) != test.expected { + t.Errorf("expected %v, got %v", test.expected, nextjs.Match(test.path)) + } + }) + } +} + +func TestNextJSGenerateDockerfile(t *testing.T) { + tests := []struct { + name string + path string + expected []any + }{ + { + name: "NextJS project", + path: "../testdata/nextjs", + expected: []any{`ARG VERSION=lts`, `CMD ["node_modules/.bin/next", "start", "-H", "0.0.0.0"]`}, + }, + { + name: "NextJS project with standalone output", + path: "../testdata/nextjs-standalone", + expected: []any{`ARG VERSION=16.0.0`, `CMD HOSTNAME="0.0.0.0" node server.js`}, + }, + { + name: "Not a NextJS project", + path: "../testdata/deno", + expected: []any{`ARG VERSION=lts`, `CMD ["node_modules/.bin/next", "start", "-H", "0.0.0.0"]`}, + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + nextjs := &runtime.NextJS{Log: logger} + dockerfile, err := nextjs.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/testdata/nextjs-standalone/.node-version b/testdata/nextjs-standalone/.node-version new file mode 100644 index 0000000..0e94e31 --- /dev/null +++ b/testdata/nextjs-standalone/.node-version @@ -0,0 +1 @@ +16.0.0 \ No newline at end of file diff --git a/testdata/nextjs-standalone/next.config.mjs b/testdata/nextjs-standalone/next.config.mjs new file mode 100644 index 0000000..9dbf091 --- /dev/null +++ b/testdata/nextjs-standalone/next.config.mjs @@ -0,0 +1,3 @@ +export default { + output: "standalone", +}; diff --git a/testdata/nextjs-standalone/package.json b/testdata/nextjs-standalone/package.json new file mode 100644 index 0000000..0967ef4 --- /dev/null +++ b/testdata/nextjs-standalone/package.json @@ -0,0 +1 @@ +{} diff --git a/testdata/nextjs/next.config.js b/testdata/nextjs/next.config.js new file mode 100644 index 0000000..da1bb77 --- /dev/null +++ b/testdata/nextjs/next.config.js @@ -0,0 +1,3 @@ +module.exports = { + reactStrictMode: true, +}; diff --git a/testdata/nextjs/package.json b/testdata/nextjs/package.json new file mode 100644 index 0000000..0967ef4 --- /dev/null +++ b/testdata/nextjs/package.json @@ -0,0 +1 @@ +{}