1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2025-11-06 09:09:19 +02:00

GPP Init Stage needs to consider mta yaml path (#3305)

mtaBuild - support config.source to yaml path
This commit is contained in:
Roland Stengel
2021-12-14 14:43:02 +01:00
committed by GitHub
parent 6f06ce0f56
commit 47d8d2c357
12 changed files with 405 additions and 54 deletions

View File

@@ -196,7 +196,7 @@ func mavenBuildMetadata() config.StepData {
{
Name: "pomPath",
ResourceRef: []config.ResourceReference{},
Scope: []string{"PARAMETERS", "STEPS"},
Scope: []string{"PARAMETERS", "STAGES", "STEPS"},
Type: "string",
Mandatory: false,
Aliases: []config.Alias{},

View File

@@ -8,6 +8,7 @@ import (
"net/http"
"os"
"path"
"path/filepath"
"strconv"
"strings"
"text/template"
@@ -160,7 +161,7 @@ func runMtaBuild(config mtaBuildOptions,
err = utils.SetNpmRegistries(config.DefaultNpmRegistry)
mtaYamlFile := "mta.yaml"
mtaYamlFile := filepath.Join(getSourcePath(config), "mta.yaml")
mtaYamlFileExists, err := utils.FileExists(mtaYamlFile)
if err != nil {
@@ -199,16 +200,9 @@ func runMtaBuild(config mtaBuildOptions,
if len(config.Extensions) != 0 {
call = append(call, fmt.Sprintf("--extensions=%s", config.Extensions))
}
if config.Source != "" && config.Source != "./" {
call = append(call, "--source", config.Source)
} else {
call = append(call, "--source", "./")
}
if config.Target != "" && config.Target != "./" {
call = append(call, "--target", config.Target)
} else {
call = append(call, "--target", "./")
}
call = append(call, "--source", getSourcePath(config))
call = append(call, "--target", getAbsPath(getMtarFileRoot(config)))
if config.Jobs > 0 {
call = append(call, "--mode=verbose")
@@ -255,7 +249,8 @@ func runMtaBuild(config mtaBuildOptions,
}
commonPipelineEnvironment.custom.buildSettingsInfo = buildSettingsInfo
commonPipelineEnvironment.mtarFilePath = mtarName
commonPipelineEnvironment.mtarFilePath = filepath.ToSlash(getMtarFilePath(config, mtarName))
commonPipelineEnvironment.custom.mtaBuildToolDesc = filepath.ToSlash(mtaYamlFile)
if config.InstallArtifacts {
// install maven artifacts in local maven repo because `mbt build` executes `mvn package -B`
@@ -500,3 +495,49 @@ func getMtaID(mtaYamlFile string, utils mtaBuildUtils) (string, error) {
return id, nil
}
// the "source" path locates the project's root
func getSourcePath(config mtaBuildOptions) string {
path := config.Source
if path == "" {
path = "./"
}
return filepath.FromSlash(path)
}
// target defines a subfolder of the project's root
func getTargetPath(config mtaBuildOptions) string {
path := config.Target
if path == "" {
path = "./"
}
return filepath.FromSlash(path)
}
// the "mtar" path resides below the project's root
// path=<config.source>/<config.target>/<mtarname>
func getMtarFileRoot(config mtaBuildOptions) string {
sourcePath := getSourcePath(config)
targetPath := getTargetPath(config)
return filepath.FromSlash(filepath.Join(sourcePath, targetPath))
}
func getMtarFilePath(config mtaBuildOptions, mtarName string) string {
root := getMtarFileRoot(config)
if root == "" || root == filepath.FromSlash("./") {
return mtarName
}
return filepath.FromSlash(filepath.Join(root, mtarName))
}
func getAbsPath(path string) string {
abspath, err := filepath.Abs(path)
// ignore error, pass customers path value in case of trouble
if err != nil {
abspath = path
}
return filepath.FromSlash(abspath)
}

View File

@@ -43,6 +43,7 @@ type mtaBuildOptions struct {
type mtaBuildCommonPipelineEnvironment struct {
mtarFilePath string
custom struct {
mtaBuildToolDesc string
mtarPublishedURL string
buildSettingsInfo string
}
@@ -55,6 +56,7 @@ func (p *mtaBuildCommonPipelineEnvironment) persist(path, resourceName string) {
value interface{}
}{
{category: "", name: "mtarFilePath", value: p.mtarFilePath},
{category: "custom", name: "mtaBuildToolDesc", value: p.custom.mtaBuildToolDesc},
{category: "custom", name: "mtarPublishedUrl", value: p.custom.mtarPublishedURL},
{category: "custom", name: "buildSettingsInfo", value: p.custom.buildSettingsInfo},
}
@@ -426,6 +428,7 @@ func mtaBuildMetadata() config.StepData {
Type: "piperEnvironment",
Parameters: []map[string]interface{}{
{"Name": "mtarFilePath"},
{"Name": "custom/mtaBuildToolDesc"},
{"Name": "custom/mtarPublishedUrl"},
{"Name": "custom/buildSettingsInfo"},
},

View File

@@ -3,6 +3,7 @@ package cmd
import (
"errors"
"net/http"
"os"
"path/filepath"
"testing"
@@ -46,7 +47,7 @@ func newMtaBuildTestUtilsBundle() *mtaBuildTestUtilsBundle {
return &utilsBundle
}
func TestMarBuild(t *testing.T) {
func TestMtaBuild(t *testing.T) {
cpe := mtaBuildCommonPipelineEnvironment{}
@@ -171,7 +172,7 @@ func TestMarBuild(t *testing.T) {
if assert.Len(t, utilsMock.Calls, 1) {
assert.Equal(t, "mbt", utilsMock.Calls[0].Exec)
assert.Equal(t, []string{"build", "--mtar", "myName.mtar", "--platform", "CF", "--source", "./", "--target", "./"}, utilsMock.Calls[0].Params)
assert.Equal(t, []string{"build", "--mtar", "myName.mtar", "--platform", "CF", "--source", filepath.FromSlash("./"), "--target", filepath.FromSlash(_ignoreError(os.Getwd()))}, utilsMock.Calls[0].Params)
}
assert.Equal(t, "myName.mtar", cpe.mtarFilePath)
})
@@ -193,9 +194,13 @@ func TestMarBuild(t *testing.T) {
if assert.Len(t, utilsMock.Calls, 1) {
assert.Equal(t, "mbt", utilsMock.Calls[0].Exec)
assert.Equal(t, []string{"build", "--mtar", "myName.mtar", "--platform", "CF", "--source", "mySourcePath/", "--target", "myTargetPath/"}, utilsMock.Calls[0].Params)
assert.Equal(t, []string{"build", "--mtar", "myName.mtar", "--platform", "CF",
"--source", filepath.FromSlash("mySourcePath/"),
"--target", filepath.Join(_ignoreError(os.Getwd()), filepath.FromSlash("mySourcePath/myTargetPath/"))},
utilsMock.Calls[0].Params)
}
assert.Equal(t, "myName.mtar", cpe.mtarFilePath)
assert.Equal(t, "mySourcePath/myTargetPath/myName.mtar", cpe.mtarFilePath)
assert.Equal(t, "mySourcePath/mta.yaml", cpe.custom.mtaBuildToolDesc)
})
})
@@ -278,3 +283,121 @@ func TestMarBuild(t *testing.T) {
})
})
}
func TestMtaBuildSourceDir(t *testing.T) {
t.Run("getSourcePath", func(t *testing.T) {
t.Parallel()
t.Run("getPath dir unset", func(t *testing.T) {
options := mtaBuildOptions{Source: "", Target: ""}
assert.Equal(t, filepath.FromSlash("./"), getSourcePath(options))
assert.Equal(t, filepath.FromSlash("./"), getTargetPath(options))
})
t.Run("getPath source set", func(t *testing.T) {
options := mtaBuildOptions{Source: "spath", Target: ""}
assert.Equal(t, filepath.FromSlash("spath"), getSourcePath(options))
assert.Equal(t, filepath.FromSlash("./"), getTargetPath(options))
})
t.Run("getPath target set", func(t *testing.T) {
options := mtaBuildOptions{Source: "", Target: "tpath"}
assert.Equal(t, filepath.FromSlash("./"), getSourcePath(options))
assert.Equal(t, filepath.FromSlash("tpath"), getTargetPath(options))
})
t.Run("getPath dir set to relative path", func(t *testing.T) {
options := mtaBuildOptions{Source: "spath", Target: "tpath"}
assert.Equal(t, filepath.FromSlash("spath"), getSourcePath(options))
assert.Equal(t, filepath.FromSlash("tpath"), getTargetPath(options))
})
t.Run("getPath dir ends with seperator", func(t *testing.T) {
options := mtaBuildOptions{Source: "spath/", Target: "tpath/"}
assert.Equal(t, filepath.FromSlash("spath/"), getSourcePath(options))
assert.Equal(t, filepath.FromSlash("tpath/"), getTargetPath(options))
})
t.Run("getPath dir set to absolute path", func(t *testing.T) {
sourcePath := filepath.Join(_ignoreError(os.Getwd()), "spath")
targetPath := filepath.Join(_ignoreError(os.Getwd()), "tpath")
options := mtaBuildOptions{Source: sourcePath, Target: targetPath}
assert.Equal(t, filepath.FromSlash(sourcePath), getSourcePath(options))
assert.Equal(t, filepath.FromSlash(targetPath), getTargetPath(options))
})
})
t.Run("find build tool descriptor from configuration", func(t *testing.T) {
t.Parallel()
cpe := mtaBuildCommonPipelineEnvironment{}
t.Run("default mta.yaml", func(t *testing.T) {
utilsMock := newMtaBuildTestUtilsBundle()
utilsMock.AddFile("mta.yaml", []byte("already there"))
_ = runMtaBuild(mtaBuildOptions{ApplicationName: "myApp"}, &cpe, utilsMock)
assert.False(t, utilsMock.HasWrittenFile("mta.yaml"))
})
t.Run("create mta.yaml from config.source", func(t *testing.T) {
utilsMock := newMtaBuildTestUtilsBundle()
utilsMock.AddFile("package.json", []byte("{\"name\": \"myName\", \"version\": \"1.2.3\"}"))
_ = runMtaBuild(mtaBuildOptions{ApplicationName: "myApp", Source: "create"}, &cpe, utilsMock)
assert.True(t, utilsMock.HasWrittenFile("create/mta.yaml"))
})
t.Run("read yaml from config.source", func(t *testing.T) {
utilsMock := newMtaBuildTestUtilsBundle()
utilsMock.AddFile("path/mta.yaml", []byte("already there"))
_ = runMtaBuild(mtaBuildOptions{ApplicationName: "myApp", Source: "path"}, &cpe, utilsMock)
assert.False(t, utilsMock.HasWrittenFile("path/mta.yaml"))
})
})
}
func TestMtaBuildMtar(t *testing.T) {
t.Run("getMtarName", func(t *testing.T) {
t.Parallel()
t.Run("mtar name from yaml", func(t *testing.T) {
utilsMock := newMtaBuildTestUtilsBundle()
utilsMock.AddFile("mta.yaml", []byte("ID: \"nameFromMtar\""))
assert.Equal(t, filepath.FromSlash("nameFromMtar.mtar"), _ignoreError(getMtarName(mtaBuildOptions{MtarName: ""}, "mta.yaml", utilsMock)))
})
t.Run("mtar name from config", func(t *testing.T) {
utilsMock := newMtaBuildTestUtilsBundle()
utilsMock.AddFile("mta.yaml", []byte("ID: \"nameFromMtar\""))
assert.Equal(t, filepath.FromSlash("nameFromConfig.mtar"), _ignoreError(getMtarName(mtaBuildOptions{MtarName: "nameFromConfig.mtar"}, "mta.yaml", utilsMock)))
})
})
t.Run("getMtarFilePath", func(t *testing.T) {
t.Parallel()
t.Run("plain mtar name", func(t *testing.T) {
assert.Equal(t, "mta.mtar", getMtarFilePath(mtaBuildOptions{Source: "", Target: ""}, "mta.mtar"))
})
t.Run("plain mtar name from default", func(t *testing.T) {
assert.Equal(t, "mta.mtar", getMtarFilePath(mtaBuildOptions{Source: "./", Target: "./"}, "mta.mtar"))
})
t.Run("source path", func(t *testing.T) {
assert.Equal(t, filepath.FromSlash("source/mta.mtar"), getMtarFilePath(mtaBuildOptions{Source: "source", Target: ""}, "mta.mtar"))
})
t.Run("target path", func(t *testing.T) {
assert.Equal(t, filepath.FromSlash("target/mta.mtar"), getMtarFilePath(mtaBuildOptions{Source: "", Target: "target"}, "mta.mtar"))
})
t.Run("source and target path", func(t *testing.T) {
assert.Equal(t, filepath.FromSlash("source/target/mta.mtar"), getMtarFilePath(mtaBuildOptions{Source: "source", Target: "target"}, "mta.mtar"))
})
})
}
func _ignoreError(s string, e error) string {
return s
}