Skip to content

Commit

Permalink
fix: regex for magento version checking (#90)
Browse files Browse the repository at this point in the history
  • Loading branch information
janosmiko committed Jan 8, 2025
1 parent 668e21a commit 77a0a98
Show file tree
Hide file tree
Showing 2 changed files with 170 additions and 3 deletions.
11 changes: 8 additions & 3 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ import (
"github.com/rewardenv/reward/pkg/util"
)

const (
// https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string
SemverRegexp = `^(?P<major>0|[1-9]\d*)\.(?P<minor>0|[1-9]\d*)\.(?P<patch>0|[1-9]\d*)(?:-(?P<prerelease>(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+(?P<buildmetadata>[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$`
)

var (
// ErrEnvNameIsInvalid occurs when the environment name is invalid. It should be a valid hostname.
ErrEnvNameIsInvalid = errors.New("environment name is invalid, it should match RFC1178")
Expand Down Expand Up @@ -899,7 +904,7 @@ func (c *Config) MagentoVersion() (*version.Version, error) {
`^magento/magento2(ce|ee)$`,
composerJSON.Name,
) && composerJSON.Version != "" {
re := regexp.MustCompile(version.SemverRegexpRaw)
re := regexp.MustCompile(SemverRegexp)
ver := re.Find([]byte(composerJSON.Version))

log.Debugf(
Expand All @@ -916,7 +921,7 @@ func (c *Config) MagentoVersion() (*version.Version, error) {
if magentoVersion.String() == "" {
for key, val := range composerJSON.Require {
if util.CheckRegexInString(`^magento/product-(enterprise|community)-edition$`, key) {
re := regexp.MustCompile(version.SemverRegexpRaw)
re := regexp.MustCompile(SemverRegexp)
ver := re.Find([]byte(val))

log.Debugf(
Expand All @@ -932,7 +937,7 @@ func (c *Config) MagentoVersion() (*version.Version, error) {
)
}
} else if util.CheckRegexInString(`^magento/magento-cloud-metapackage$`, key) {
re := regexp.MustCompile(version.SemverRegexpRaw)
re := regexp.MustCompile(SemverRegexp)
ver := re.Find([]byte(val))

log.Debugf(
Expand Down
162 changes: 162 additions & 0 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
package config

import (
"os"
"testing"

"github.com/hashicorp/go-version"
"github.com/spf13/afero"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"

"github.com/rewardenv/reward/pkg/util"
)

type ConfigTestSuite struct {
suite.Suite
}

func (suite *ConfigTestSuite) SetupTest() {
FS = &afero.Afero{Fs: afero.NewMemMapFs()}
util.FS = FS
f, _ := FS.Create(".env")

defer f.Close()
}

func TestConfigTestSuite(t *testing.T) {
suite.Run(t, new(ConfigTestSuite))
}

func (suite *ConfigTestSuite) TestConfigMagentoVersion() {
c := New("reward", "0.0.1").Init()

Check failure on line 32 in internal/config/config_test.go

View workflow job for this annotation

GitHub Actions / Run golangci-lint

variable name 'c' is too short for the scope of its usage (varnamelen)

type fields struct {
composerJSON string
}
tests := []struct {
name string
fields fields
want *version.Version
wantErr bool
}{
{
name: "empty composer.json",
fields: fields{},
want: version.Must(version.NewVersion("2.4.7-p2")),
wantErr: false,
},
{
name: "composer.json with invalid json",
fields: fields{
composerJSON: `{]`,
},
want: version.Must(version.NewVersion("2.4.7-p2")),
wantErr: false,
},
{
name: "composer.json with valid version (old format)",
fields: fields{
composerJSON: `
{
"name": "magento/magento2ce",
"version": "2.3.8-p9"
}
`,
},
want: version.Must(version.NewVersion("2.3.8-p9")),
wantErr: false,
},
{
name: "composer.json with invalid version (old format)",
fields: fields{
composerJSON: `
{
"name": "magento/magento2ce",
"version": "invalid version"
}
`,
},
want: nil,
wantErr: true,
},
{
name: "composer.json with valid version (new format)",
fields: fields{
composerJSON: `
{
"name": "magento/project-community-edition",
"version": "2.4.5-p1",
"require": {
"magento/product-community-edition": "2.4.4-p1"
}
}
`,
},
want: version.Must(version.NewVersion("2.4.4-p1")),
wantErr: false,
},
{
name: "composer.json with invalid version",
fields: fields{
composerJSON: `
{
"name": "magento/project-community-edition",
"require": {
"magento/product-community-edition": "invalid version"
}
}
`,
},
want: nil,
wantErr: true,
},
{
name: "composer.json with valid version for cloud metapackage",
fields: fields{
composerJSON: `
{
"name": "magento/project-enterprise-edition",
"require": {
"magento/magento-cloud-metapackage": "2.4.5-p8"
}
}
`,
},
want: version.Must(version.NewVersion("2.4.5-p8")),
wantErr: false,
},
{
name: "composer.json with invalid version for cloud metapackage",
fields: fields{
composerJSON: `
{
"name": "magento/project-enterprise-edition",
"require": {
"magento/magento-cloud-metapackage": "invalid version"
}
}
`,
},
want: nil,
wantErr: true,
},
}
for _, tt := range tests {

Check failure on line 145 in internal/config/config_test.go

View workflow job for this annotation

GitHub Actions / Run golangci-lint

only one cuddle assignment allowed before range statement (wsl)
suite.T().Run(tt.name, func(t *testing.T) {
if tt.fields.composerJSON != "" {
_ = FS.WriteFile("composer.json", []byte(tt.fields.composerJSON), os.FileMode(0o644))
}

got, err := c.MagentoVersion()

if tt.wantErr {
assert.NotNil(suite.T(), err)
assert.Nil(suite.T(), got)
return

Check failure on line 156 in internal/config/config_test.go

View workflow job for this annotation

GitHub Actions / Run golangci-lint

return with no blank line before (nlreturn)
}

assert.Equal(suite.T(), tt.want, got)
})
}
}

0 comments on commit 77a0a98

Please sign in to comment.