1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-12 10:55:20 +02:00

Add functions to infer build tool (#1210)

This commit is contained in:
Florian Wilhelm 2020-02-23 16:54:50 +01:00 committed by GitHub
parent 79475732f9
commit c15f6a03d2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 83 additions and 0 deletions

View File

@ -0,0 +1,36 @@
package piperutils
import "path/filepath"
// ProjectStructure describes a directory containing source code
type ProjectStructure struct {
directory string
}
// UsesMta returns `true` if the project structure directory contains typical files for mta projects (mta.yaml, mta.yml), `false` otherwise
func (projectStructure *ProjectStructure) UsesMta() bool {
return projectStructure.anyFileExists("mta.yaml", "mta.yml")
}
// UsesMaven returns `true` if the project structure directory contains a pom.xml file, false otherwise
func (projectStructure *ProjectStructure) UsesMaven() bool {
return projectStructure.anyFileExists("pom.xml")
}
// UsesNpm returns `true` if the project structure directory contains a package.json file, false otherwise
func (projectStructure *ProjectStructure) UsesNpm() bool {
return projectStructure.anyFileExists("package.json")
}
func (projectStructure *ProjectStructure) anyFileExists(candidates ...string) bool {
for i := 0; i < len(candidates); i++ {
exists, err := FileExists(filepath.Join(projectStructure.directory, candidates[i]))
if err != nil {
continue
}
if exists {
return true
}
}
return false
}

View File

@ -0,0 +1,47 @@
package piperutils
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestProjectWithOnlyMtaFile(t *testing.T) {
projectStructure := ProjectStructure{directory: "testdata/mta"}
resultMta := projectStructure.UsesMta()
assert.True(t, resultMta)
resultPom := projectStructure.UsesMaven()
assert.False(t, resultPom)
resultNpm := projectStructure.UsesNpm()
assert.False(t, resultNpm)
}
func TestProjectWithOnlyPomFile(t *testing.T) {
projectStructure := ProjectStructure{directory: "testdata/maven"}
resultMta := projectStructure.UsesMta()
assert.False(t, resultMta)
resultPom := projectStructure.UsesMaven()
assert.True(t, resultPom)
resultNpm := projectStructure.UsesNpm()
assert.False(t, resultNpm)
}
func TestProjectWithOnlyNpmFile(t *testing.T) {
projectStructure := ProjectStructure{directory: "testdata/npm"}
resultMta := projectStructure.UsesMta()
assert.False(t, resultMta)
resultPom := projectStructure.UsesMaven()
assert.False(t, resultPom)
resultNpm := projectStructure.UsesNpm()
assert.True(t, resultNpm)
}
func TestDirectryParameterIsEmptyAndNoProjectFilesAreInIt(t *testing.T) {
projectStructure := ProjectStructure{}
resultMta := projectStructure.UsesMta()
assert.False(t, resultMta)
resultPom := projectStructure.UsesMaven()
assert.False(t, resultPom)
resultNpm := projectStructure.UsesNpm()
assert.False(t, resultNpm)
}

0
pkg/piperutils/testdata/maven/pom.xml vendored Normal file
View File

0
pkg/piperutils/testdata/mta/mta.yaml vendored Normal file
View File

View File