Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[auto] Add php detector #2390

Merged
merged 2 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions pkg/autodetect/autodetect.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,10 @@ func populateConfig(ctx context.Context, path string, config *devconfig.Config)

func detectors(path string) []detector.Detector {
return []detector.Detector{
&detector.PythonDetector{Root: path},
&detector.PoetryDetector{Root: path},
&detector.GoDetector{Root: path},
&detector.PHPDetector{Root: path},
&detector.PoetryDetector{Root: path},
&detector.PythonDetector{Root: path},
}
}

Expand All @@ -62,6 +63,13 @@ func relevantDetector(path string) (detector.Detector, error) {
relevantScore := 0.0
var mostRelevantDetector detector.Detector
for _, detector := range detectors(path) {
if d, ok := detector.(interface {
Init() error
}); ok {
if err := d.Init(); err != nil {
return nil, err
}
}
score, err := detector.Relevance(path)
if err != nil {
return nil, err
Expand Down
6 changes: 2 additions & 4 deletions pkg/autodetect/detector/go.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,8 @@ func (d *GoDetector) Packages(ctx context.Context) ([]string, error) {

// Parse the Go version from go.mod
goVersion := parseGoVersion(string(goModContent))
if goVersion != "" {
return []string{"go@" + goVersion}, nil
}
return []string{"go@latest"}, nil
goVersion = determineBestVersion(ctx, "go", goVersion)
return []string{"go@" + goVersion}, nil
}

func parseGoVersion(goModContent string) string {
Expand Down
92 changes: 92 additions & 0 deletions pkg/autodetect/detector/php.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package detector

import (
"context"
"encoding/json"
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
)

type composerJSON struct {
Require map[string]string `json:"require"`
}

type PHPDetector struct {
Root string
composerJSON *composerJSON
}

var _ Detector = &PHPDetector{}

func (d *PHPDetector) Init() error {
composer, err := loadComposerJSON(d.Root)
if err != nil && !os.IsNotExist(err) {
return err
}
d.composerJSON = composer
return nil
}

func (d *PHPDetector) Relevance(path string) (float64, error) {
if d.composerJSON == nil {
return 1, nil
}
return 0, nil
}

func (d *PHPDetector) Packages(ctx context.Context) ([]string, error) {
version, err := d.phpVersion()
if err != nil {
return nil, err
}
return []string{fmt.Sprintf("php@%s", version)}, nil
}

func (d *PHPDetector) phpVersion() (string, error) {
require := d.composerJSON.Require

if require["php"] == "" {
return "latest", nil
}
// Remove the caret (^) if present
version := strings.TrimPrefix(require["php"], "^")

// Extract version in the format x, x.y, or x.y.z
re := regexp.MustCompile(`^(\d+(\.\d+){0,2})`)
match := re.FindString(version)
if match == "" {
return "latest", nil
}

version = match

return version, nil
}

func (d *PHPDetector) phpExtensions() ([]string, error) {
extensions := []string{}
for key := range d.composerJSON.Require {
if strings.HasPrefix(key, "ext-") {
extensions = append(extensions, "phpExtensions."+strings.TrimPrefix(key, "ext-"))
}
}

return extensions, nil
}

func loadComposerJSON(root string) (*composerJSON, error) {
composerPath := filepath.Join(root, "composer.json")
composerData, err := os.ReadFile(composerPath)
if err != nil {
return nil, err
}
var composer composerJSON
err = json.Unmarshal(composerData, &composer)
if err != nil {
return nil, err
}
return &composer, nil
}
164 changes: 164 additions & 0 deletions pkg/autodetect/detector/php_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
package detector

import (
"context"
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestPHPDetector_Relevance(t *testing.T) {
tests := []struct {
name string
setup func(t *testing.T) string
expected float64
}{
{
name: "no composer.json",
setup: func(t *testing.T) string {
dir := t.TempDir()
return dir
},
expected: 1,
},
{
name: "with composer.json",
setup: func(t *testing.T) string {
dir := t.TempDir()
err := os.WriteFile(filepath.Join(dir, "composer.json"), []byte(`{
"require": {
"php": "^8.1"
}
}`), 0644)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggest looking into using https://pkg.go.dev/testing/fstest

require.NoError(t, err)
return dir
},
expected: 0,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
dir := tt.setup(t)
d := &PHPDetector{Root: dir}
err := d.Init()
require.NoError(t, err)

score, err := d.Relevance(dir)
require.NoError(t, err)
assert.Equal(t, tt.expected, score)
})
}
}

func TestPHPDetector_Packages(t *testing.T) {
tests := []struct {
name string
composerJSON string
expectedPHP string
expectedError bool
}{
{
name: "no php version specified",
composerJSON: `{
"require": {}
}`,
expectedPHP: "php@latest",
},
{
name: "specific php version",
composerJSON: `{
"require": {
"php": "^8.1"
}
}`,
expectedPHP: "[email protected]",
},
{
name: "php version with patch",
composerJSON: `{
"require": {
"php": "^8.1.2"
}
}`,
expectedPHP: "[email protected]",
},
{
name: "invalid composer.json",
composerJSON: `invalid json`,
expectedError: true,
},
}

for _, curTest := range tests {
t.Run(curTest.name, func(t *testing.T) {
dir := t.TempDir()
if curTest.composerJSON != "" {
err := os.WriteFile(filepath.Join(dir, "composer.json"), []byte(curTest.composerJSON), 0644)
require.NoError(t, err)
}

d := &PHPDetector{Root: dir}
err := d.Init()
if curTest.expectedError {
require.Error(t, err)
return
}
require.NoError(t, err)

packages, err := d.Packages(context.Background())
require.NoError(t, err)
assert.Equal(t, []string{curTest.expectedPHP}, packages)
})
}
}

func TestPHPDetector_PHPExtensions(t *testing.T) {
tests := []struct {
name string
composerJSON string
expectedExtensions []string
}{
{
name: "no extensions",
composerJSON: `{
"require": {
"php": "^8.1"
}
}`,
expectedExtensions: []string{},
},
{
name: "multiple extensions",
composerJSON: `{
"require": {
"ext-mbstring": "*",
"ext-imagick": "*"
}
}`,
expectedExtensions: []string{
"phpExtensions.mbstring",
"phpExtensions.imagick",
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
dir := t.TempDir()
err := os.WriteFile(filepath.Join(dir, "composer.json"), []byte(tt.composerJSON), 0644)
require.NoError(t, err)

d := &PHPDetector{Root: dir}
err = d.Init()
require.NoError(t, err)

extensions, err := d.phpExtensions()
require.NoError(t, err)
assert.ElementsMatch(t, tt.expectedExtensions, extensions)
})
}
}
Loading