From 77a0a9879a6788b7f20d6d19d43170965fb5ed13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A1nos=20Mik=C3=B3?= Date: Wed, 8 Jan 2025 08:05:47 +0100 Subject: [PATCH] fix: regex for magento version checking (#90) --- internal/config/config.go | 11 ++- internal/config/config_test.go | 162 +++++++++++++++++++++++++++++++++ 2 files changed, 170 insertions(+), 3 deletions(-) create mode 100644 internal/config/config_test.go diff --git a/internal/config/config.go b/internal/config/config.go index 77e909da..e73d088d 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -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 = `^(?P0|[1-9]\d*)\.(?P0|[1-9]\d*)\.(?P0|[1-9]\d*)(?:-(?P(?: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[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") @@ -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( @@ -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( @@ -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( diff --git a/internal/config/config_test.go b/internal/config/config_test.go new file mode 100644 index 00000000..a18b0485 --- /dev/null +++ b/internal/config/config_test.go @@ -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() + + 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 { + 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 + } + + assert.Equal(suite.T(), tt.want, got) + }) + } +}