Skip to content

Commit 8f9ac5a

Browse files
JoshuaBehrensshyim
andauthored
Change source directory methods to return lists instead powered by PSR-4 to ensure all shipped code is checked (#518)
* Change source directory methods to return lists instead powered by PSR-4 to ensure all shipped code is checked * feat: add PSR-4 autoload configuration to composer.json in bundle test * refactor: simplify repository assertions in TestReadComposerJsonDifferentRepositoryWritings --------- Co-authored-by: Soner Sayakci <[email protected]> Co-authored-by: Shyim <[email protected]>
1 parent bbba361 commit 8f9ac5a

7 files changed

Lines changed: 115 additions & 36 deletions

File tree

extension/app.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,18 @@ func (a App) GetRootDir() string {
2222
return a.path
2323
}
2424

25+
func (a App) GetSourceDirs() []string {
26+
return []string{a.path}
27+
}
28+
2529
func (a App) GetResourcesDir() string {
2630
return path.Join(a.path, "Resources")
2731
}
2832

33+
func (a App) GetResourcesDirs() []string {
34+
return []string{path.Join(a.path, "Resources")}
35+
}
36+
2937
func (a App) GetComposerName() (string, error) {
3038
return "", fmt.Errorf("app does not have a composer name")
3139
}

extension/bundle.go

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,19 @@ func newShopwareBundle(path string) (*ShopwareBundle, error) {
5555
return &extension, nil
5656
}
5757

58+
type composerAutoload struct {
59+
Psr4 map[string]string `json:"psr-4"`
60+
}
61+
5862
type shopwareBundleComposerJson struct {
59-
Name string `json:"name"`
60-
Type string `json:"type"`
61-
License string `json:"license"`
62-
Version string `json:"version"`
63-
Require map[string]string `json:"require"`
64-
Extra shopwareBundleComposerJsonExtra `json:"extra"`
65-
Suggest map[string]string `json:"suggest"`
63+
Name string `json:"name"`
64+
Type string `json:"type"`
65+
License string `json:"license"`
66+
Version string `json:"version"`
67+
Require map[string]string `json:"require"`
68+
Extra shopwareBundleComposerJsonExtra `json:"extra"`
69+
Suggest map[string]string `json:"suggest"`
70+
Autoload composerAutoload `json:"autoload"`
6671
}
6772

6873
type shopwareBundleComposerJsonExtra struct {
@@ -78,11 +83,31 @@ func (p ShopwareBundle) GetRootDir() string {
7883
return path.Join(p.path, "src")
7984
}
8085

86+
func (p ShopwareBundle) GetSourceDirs() []string {
87+
var result []string
88+
89+
for _, val := range p.Composer.Autoload.Psr4 {
90+
result = append(result, path.Join(p.path, val))
91+
}
92+
93+
return result
94+
}
95+
8196
// GetResourcesDir returns the resources directory of the shopware bundle.
8297
func (p ShopwareBundle) GetResourcesDir() string {
8398
return path.Join(p.GetRootDir(), "Resources")
8499
}
85100

101+
func (p ShopwareBundle) GetResourcesDirs() []string {
102+
var result []string
103+
104+
for _, val := range p.GetSourceDirs() {
105+
result = append(result, path.Join(val, "Resources"))
106+
}
107+
108+
return result
109+
}
110+
86111
func (p ShopwareBundle) GetName() (string, error) {
87112
return p.Composer.Extra.BundleName, nil
88113
}

extension/bundle_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ func TestCreateBundle(t *testing.T) {
6060
"type": "shopware-bundle",
6161
"extra": {
6262
"shopware-bundle-name": "TestBundle"
63+
},
64+
"autoload": {
65+
"psr-4": {
66+
"TestBundle\\": "src/"
67+
}
6368
}
6469
}
6570
`)
@@ -75,6 +80,7 @@ func TestCreateBundle(t *testing.T) {
7580
assert.Equal(t, path.Join(dir, "src"), bundle.GetRootDir())
7681
assert.Equal(t, dir, bundle.GetPath())
7782
assert.Equal(t, path.Join(dir, "src", "Resources"), bundle.GetResourcesDir())
83+
assert.Equal(t, path.Join(dir, "src", "Resources"), bundle.GetResourcesDirs()[0])
7884
assert.Equal(t, TypeShopwareBundle, bundle.GetType())
7985

8086
_, err = bundle.GetChangelog()

extension/platform.go

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,31 @@ func (p PlatformPlugin) GetRootDir() string {
3030
return path.Join(p.path, "src")
3131
}
3232

33+
func (p PlatformPlugin) GetSourceDirs() []string {
34+
var result []string
35+
36+
for _, val := range p.Composer.Autoload.Psr4 {
37+
result = append(result, path.Join(p.path, val))
38+
}
39+
40+
return result
41+
}
42+
3343
// GetResourcesDir returns the resources directory of the plugin.
3444
func (p PlatformPlugin) GetResourcesDir() string {
3545
return path.Join(p.GetRootDir(), "Resources")
3646
}
3747

48+
func (p PlatformPlugin) GetResourcesDirs() []string {
49+
var result []string
50+
51+
for _, val := range p.GetSourceDirs() {
52+
result = append(result, path.Join(val, "Resources"))
53+
}
54+
55+
return result
56+
}
57+
3858
func newPlatformPlugin(path string) (*PlatformPlugin, error) {
3959
composerJsonFile := fmt.Sprintf("%s/composer.json", path)
4060
if _, err := os.Stat(composerJsonFile); err != nil {
@@ -273,14 +293,17 @@ func validatePHPFiles(c context.Context, ctx *ValidationContext) {
273293
logging.FromContext(c).Infof("PHP 7.2 is not supported for PHP linting, using 7.3 now")
274294
}
275295

276-
phpErrors, err := phplint.LintFolder(c, phpVersion, ctx.Extension.GetRootDir())
277-
if err != nil {
278-
ctx.AddWarning("php.linter", fmt.Sprintf("Could not lint php files: %s", err.Error()))
279-
return
280-
}
296+
for _, val := range ctx.Extension.GetSourceDirs() {
297+
phpErrors, err := phplint.LintFolder(c, phpVersion, val)
281298

282-
for _, error := range phpErrors {
283-
ctx.AddError("php.linter", fmt.Sprintf("%s: %s", error.File, error.Message))
299+
if err != nil {
300+
ctx.AddWarning("php.linter", fmt.Sprintf("Could not lint php files: %s", err.Error()))
301+
continue
302+
}
303+
304+
for _, error := range phpErrors {
305+
ctx.AddError("php.linter", fmt.Sprintf("%s: %s", error.File, error.Message))
306+
}
284307
}
285308
}
286309

extension/root.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,13 @@ type extensionMetadata struct {
100100
type Extension interface {
101101
GetName() (string, error)
102102
GetComposerName() (string, error)
103+
// Deprecated: use the list variation instead
103104
GetResourcesDir() string
105+
GetResourcesDirs() []string
104106

105107
// GetRootDir Returns the root folder where the code is located plugin -> src, app ->
106108
GetRootDir() string
109+
GetSourceDirs() []string
107110
GetVersion() (*version.Version, error)
108111
GetLicense() (string, error)
109112
GetShopwareVersionConstraint() (*version.Constraints, error)

extension/snippet_validator.go

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,13 @@ import (
1414

1515
func validateStorefrontSnippets(context *ValidationContext) {
1616
rootDir := context.Extension.GetRootDir()
17-
if err := validateStorefrontSnippetsByPath(rootDir, rootDir, context); err != nil {
18-
return
17+
18+
for _, val := range context.Extension.GetResourcesDirs() {
19+
storefrontFolder := path.Join(val, "snippet")
20+
21+
if err := validateStorefrontSnippetsByPath(storefrontFolder, rootDir, context); err != nil {
22+
return
23+
}
1924
}
2025

2126
for _, extraBundle := range context.Extension.GetExtensionConfig().Build.ExtraBundles {
@@ -27,15 +32,15 @@ func validateStorefrontSnippets(context *ValidationContext) {
2732
bundlePath = path.Join(bundlePath, extraBundle.Name)
2833
}
2934

30-
if err := validateStorefrontSnippetsByPath(bundlePath, rootDir, context); err != nil {
35+
storefrontFolder := path.Join(bundlePath, "Resources", "snippet")
36+
37+
if err := validateStorefrontSnippetsByPath(storefrontFolder, rootDir, context); err != nil {
3138
return
3239
}
3340
}
3441
}
3542

36-
func validateStorefrontSnippetsByPath(extensionRoot, rootDir string, context *ValidationContext) error {
37-
snippetFolder := path.Join(extensionRoot, "Resources", "snippet")
38-
43+
func validateStorefrontSnippetsByPath(snippetFolder, rootDir string, context *ValidationContext) error {
3944
if _, err := os.Stat(snippetFolder); err != nil {
4045
return nil //nolint:nilerr
4146
}
@@ -110,8 +115,13 @@ func validateStorefrontSnippetsByPath(extensionRoot, rootDir string, context *Va
110115

111116
func validateAdministrationSnippets(context *ValidationContext) {
112117
rootDir := context.Extension.GetRootDir()
113-
if err := validateAdministrationByPath(rootDir, rootDir, context); err != nil {
114-
return
118+
119+
for _, val := range context.Extension.GetResourcesDirs() {
120+
adminFolder := path.Join(val, "app", "administration")
121+
122+
if err := validateAdministrationByPath(adminFolder, rootDir, context); err != nil {
123+
return
124+
}
115125
}
116126

117127
for _, extraBundle := range context.Extension.GetExtensionConfig().Build.ExtraBundles {
@@ -123,15 +133,15 @@ func validateAdministrationSnippets(context *ValidationContext) {
123133
bundlePath = path.Join(bundlePath, extraBundle.Name)
124134
}
125135

126-
if err := validateAdministrationByPath(bundlePath, rootDir, context); err != nil {
136+
adminFolder := path.Join(bundlePath, "Resources", "app", "administration")
137+
138+
if err := validateAdministrationByPath(adminFolder, rootDir, context); err != nil {
127139
return
128140
}
129141
}
130142
}
131143

132-
func validateAdministrationByPath(extensionRoot, rootDir string, context *ValidationContext) error {
133-
adminFolder := path.Join(extensionRoot, "Resources", "app", "administration")
134-
144+
func validateAdministrationByPath(adminFolder, rootDir string, context *ValidationContext) error {
135145
if _, err := os.Stat(adminFolder); err != nil {
136146
return nil //nolint:nilerr
137147
}

internal/packagist/composer_test.go

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ func TestReadComposerJsonDifferentRepositoryWritings(t *testing.T) {
188188
tempDir := t.TempDir()
189189
composerFile := filepath.Join(tempDir, "composer.json")
190190

191-
var content = `
191+
content := `
192192
{
193193
"repositories": [
194194
{
@@ -208,17 +208,19 @@ func TestReadComposerJsonDifferentRepositoryWritings(t *testing.T) {
208208
composer, err := ReadComposerJson(composerFile)
209209
assert.NoError(t, err)
210210
assert.Equal(t, composerFile, composer.path)
211-
assert.Equal(t, "vcs", composer.Repositories[0].Type)
212-
assert.Equal(t, "https://github.com/shopware/platform", composer.Repositories[0].URL)
213-
assert.Equal(t, "path", composer.Repositories[1].Type)
214-
assert.Equal(t, "custom/plugins", composer.Repositories[1].URL)
211+
212+
expectedRepos := []ComposerJsonRepository{
213+
{Type: "vcs", URL: "https://github.com/shopware/platform"},
214+
{Type: "path", URL: "custom/plugins"},
215+
}
216+
assert.ElementsMatch(t, expectedRepos, composer.Repositories)
215217
})
216218

217219
t.Run("repository map", func(t *testing.T) {
218220
tempDir := t.TempDir()
219221
composerFile := filepath.Join(tempDir, "composer.json")
220222

221-
var content = `
223+
content := `
222224
{
223225
"repositories": {
224226
"remote": {
@@ -238,9 +240,11 @@ func TestReadComposerJsonDifferentRepositoryWritings(t *testing.T) {
238240
composer, err := ReadComposerJson(composerFile)
239241
assert.NoError(t, err)
240242
assert.Equal(t, composerFile, composer.path)
241-
assert.Equal(t, "vcs", composer.Repositories[0].Type)
242-
assert.Equal(t, "https://github.com/shopware/platform", composer.Repositories[0].URL)
243-
assert.Equal(t, "path", composer.Repositories[1].Type)
244-
assert.Equal(t, "custom/plugins", composer.Repositories[1].URL)
243+
244+
expectedRepos := []ComposerJsonRepository{
245+
{Type: "vcs", URL: "https://github.com/shopware/platform"},
246+
{Type: "path", URL: "custom/plugins"},
247+
}
248+
assert.ElementsMatch(t, expectedRepos, composer.Repositories)
245249
})
246250
}

0 commit comments

Comments
 (0)