Skip to content

Commit

Permalink
php tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredLunde committed May 15, 2024
1 parent 83c80a7 commit b0a9ad9
Show file tree
Hide file tree
Showing 11 changed files with 142 additions and 6 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -300,8 +300,8 @@ Maven version:

#### Version Detection
- `.tool-versions` - `nodejs {VERSION}`
- `.nvmrc` - `{VERSION}`
- `.node-version` - `{VERSION}`
- `.nvmrc` - `v{VERSION}`
- `.node-version` - `v{VERSION}`

#### Runtime Image
`node:${VERSION}-slim`
Expand Down
4 changes: 4 additions & 0 deletions runtime/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,10 @@ func findNodeVersion(path string, log *slog.Logger) (*string, error) {
version = strings.TrimPrefix(line, "v")
log.Info("Detected Node version in " + file + ": " + version)
break
} else {
version = line
log.Info("Detected Node version in " + file + ": " + version)
break
}
}

Expand Down
17 changes: 13 additions & 4 deletions runtime/php.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,27 @@ func (d *PHP) GenerateDockerfile(path string) ([]byte, error) {
}

packageManager := ""
npmInstallCMD := ""
if _, err := os.Stat(filepath.Join(path, "package-lock.json")); err == nil {
packageManager = "npm"
installCMD = installCMD + " && npm ci"
npmInstallCMD = "npm ci"
} else if _, err := os.Stat(filepath.Join(path, "pnpm-lock.yaml")); err == nil {
packageManager = "pnpm"
installCMD = installCMD + " && corepack enable pnpm && pnpm i --frozen-lockfile"
npmInstallCMD = "corepack enable pnpm && pnpm i --frozen-lockfile"
} else if _, err := os.Stat(filepath.Join(path, "yarn.lock")); err == nil {
packageManager = "yarn"
installCMD = installCMD + " && yarn --frozen-lockfile"
npmInstallCMD = "yarn --frozen-lockfile"
} else if _, err := os.Stat(filepath.Join(path, "bun.lockb")); err == nil {
packageManager = "bun"
installCMD = installCMD + " && bun install"
npmInstallCMD = "bun install"
}

if npmInstallCMD != "" {
if installCMD == "" {
installCMD = npmInstallCMD
} else {
installCMD = fmt.Sprintf("%s && %s", installCMD, npmInstallCMD)
}
}

buildCMD := ""
Expand Down
110 changes: 110 additions & 0 deletions runtime/php_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package runtime_test

import (
"regexp"
"strings"
"testing"

"github.com/flexstack/new-dockerfile/runtime"
)

func TestPHPMatch(t *testing.T) {
tests := []struct {
name string
path string
expected bool
}{
{
name: "PHP project",
path: "../testdata/php",
expected: true,
},
{
name: "PHP project with composer",
path: "../testdata/php-composer",
expected: true,
},
{
name: "PHP project with NPM",
path: "../testdata/php-npm",
expected: true,
},
{
name: "Not a PHP project",
path: "../testdata/deno",
expected: false,
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
php := &runtime.PHP{Log: logger}
if php.Match(test.path) != test.expected {
t.Errorf("expected %v, got %v", test.expected, php.Match(test.path))
}
})
}
}

func TestPHPGenerateDockerfile(t *testing.T) {
tests := []struct {
name string
path string
expected []any
}{
{
name: "PHP project",
path: "../testdata/php",
expected: []any{`ARG VERSION=8.3`, regexp.MustCompile(`^ARG BUILD_CMD=$`), `ARG START_CMD="apache2-foreground`},
},
{
name: "PHP project with composer",
path: "../testdata/php-composer",
expected: []any{`ARG VERSION=5.3`, `ARG INSTALL_CMD="composer update \u0026\u0026 composer install --prefer-dist --no-dev --optimize-autoloader --no-interaction"`, regexp.MustCompile(`^ARG BUILD_CMD=$`), `ARG START_CMD="apache2-foreground`},
},
{
name: "PHP project with NPM",
path: "../testdata/php-npm",
expected: []any{`ARG VERSION=8.2.0`, `ARG INSTALL_CMD="yarn --frozen-lockfile"`, `ARG BUILD_CMD="yarn run build"`, `ARG START_CMD="apache2-foreground`},
},
{
name: "Not a PHP project",
path: "../testdata/deno",
expected: []any{`ARG VERSION=8.3`, regexp.MustCompile(`^ARG INSTALL_CMD=$`), regexp.MustCompile(`^ARG BUILD_CMD=$`), `ARG START_CMD="apache2-foreground`},
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
php := &runtime.PHP{Log: logger}
dockerfile, err := php.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))
}
}
})
}
}
7 changes: 7 additions & 0 deletions testdata/php-composer/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "test/test",
"description": "Test",
"require": {
"php": ">=5.3.0"
}
}
Empty file added testdata/php-composer/index.php
Empty file.
1 change: 1 addition & 0 deletions testdata/php-npm/.tool-versions
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
php 8.2.0
Empty file added testdata/php-npm/index.php
Empty file.
5 changes: 5 additions & 0 deletions testdata/php-npm/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"scripts": {
"build": "echo 'Building the project...'"
}
}
Empty file added testdata/php-npm/yarn.lock
Empty file.
Empty file added testdata/php/index.php
Empty file.

0 comments on commit b0a9ad9

Please sign in to comment.