1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2025-01-18 05:18:24 +02:00

Add mtaBuild step parameters: target, source (#2858)

* Add mtaBuild step parameters: target, source

* Add unit test for mtaBuild with custom source

* Simplify mtaBuild parameter defaults for source, target

* Fix mtaBuild default values and path in unit test

* Only append custom target and source params in mtaBuild

* Set mtaBuild source and target to ./ when not customized

* mtaBuild unit test: fix expected params sequence

Co-authored-by: Christopher Fenner <26137398+CCFenner@users.noreply.github.com>
Co-authored-by: Thorsten Duda <thorsten.duda@sap.com>
This commit is contained in:
Tom Bendrath 2021-06-14 16:06:47 +02:00 committed by GitHub
parent f9bfb037a0
commit 3520b36558
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 78 additions and 8 deletions

View File

@ -189,7 +189,16 @@ func runMtaBuild(config mtaBuildOptions,
if len(config.Extensions) != 0 {
call = append(call, fmt.Sprintf("--extensions=%s", config.Extensions))
}
call = append(call, "--target", "./")
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", "./")
}
if err = addNpmBinToPath(utils); err != nil {
return err

View File

@ -21,6 +21,8 @@ type mtaBuildOptions struct {
Extensions string `json:"extensions,omitempty"`
Platform string `json:"platform,omitempty"`
ApplicationName string `json:"applicationName,omitempty"`
Source string `json:"source,omitempty"`
Target string `json:"target,omitempty"`
DefaultNpmRegistry string `json:"defaultNpmRegistry,omitempty"`
ProjectSettingsFile string `json:"projectSettingsFile,omitempty"`
GlobalSettingsFile string `json:"globalSettingsFile,omitempty"`
@ -133,6 +135,8 @@ func addMtaBuildFlags(cmd *cobra.Command, stepConfig *mtaBuildOptions) {
cmd.Flags().StringVar(&stepConfig.Extensions, "extensions", os.Getenv("PIPER_extensions"), "The path to the extension descriptor file.")
cmd.Flags().StringVar(&stepConfig.Platform, "platform", `CF`, "The target platform to which the mtar can be deployed.")
cmd.Flags().StringVar(&stepConfig.ApplicationName, "applicationName", os.Getenv("PIPER_applicationName"), "The name of the application which is being built. If the parameter has been provided and no `mta.yaml` exists, the `mta.yaml` will be automatically generated using this parameter and the information (`name` and `version`) from 'package.json` before the actual build starts.")
cmd.Flags().StringVar(&stepConfig.Source, "source", `./`, "The path to the MTA project.")
cmd.Flags().StringVar(&stepConfig.Target, "target", `./`, "The folder for the generated `MTAR` file. If the parameter has been provided, the `MTAR` file is saved in the root of the folder provided by the argument.")
cmd.Flags().StringVar(&stepConfig.DefaultNpmRegistry, "defaultNpmRegistry", os.Getenv("PIPER_defaultNpmRegistry"), "Url to the npm registry that should be used for installing npm dependencies.")
cmd.Flags().StringVar(&stepConfig.ProjectSettingsFile, "projectSettingsFile", os.Getenv("PIPER_projectSettingsFile"), "Path or url to the mvn settings file that should be used as project settings file.")
cmd.Flags().StringVar(&stepConfig.GlobalSettingsFile, "globalSettingsFile", os.Getenv("PIPER_globalSettingsFile"), "Path or url to the mvn settings file that should be used as global settings file")
@ -184,6 +188,22 @@ func mtaBuildMetadata() config.StepData {
Mandatory: false,
Aliases: []config.Alias{},
},
{
Name: "source",
ResourceRef: []config.ResourceReference{},
Scope: []string{"PARAMETERS", "STAGES", "STEPS"},
Type: "string",
Mandatory: false,
Aliases: []config.Alias{},
},
{
Name: "target",
ResourceRef: []config.ResourceReference{},
Scope: []string{"PARAMETERS", "STAGES", "STEPS"},
Type: "string",
Mandatory: false,
Aliases: []config.Alias{},
},
{
Name: "defaultNpmRegistry",
ResourceRef: []config.ResourceReference{},

View File

@ -65,7 +65,7 @@ func TestMarBuild(t *testing.T) {
t.Run("Provide default npm registry", func(t *testing.T) {
utilsMock := newMtaBuildTestUtilsBundle()
options := mtaBuildOptions{ApplicationName: "myApp", Platform: "CF", DefaultNpmRegistry: "https://example.org/npm", MtarName: "myName"}
options := mtaBuildOptions{ApplicationName: "myApp", Platform: "CF", DefaultNpmRegistry: "https://example.org/npm", MtarName: "myName", Source: "./", Target: "./"}
utilsMock.AddFile("package.json", []byte("{\"name\": \"myName\", \"version\": \"1.2.3\"}"))
@ -94,7 +94,7 @@ func TestMarBuild(t *testing.T) {
utilsMock := newMtaBuildTestUtilsBundle()
options := mtaBuildOptions{ApplicationName: "myApp", Platform: "CF", MtarName: "myName"}
options := mtaBuildOptions{ApplicationName: "myApp", Platform: "CF", MtarName: "myName", Source: "./", Target: "./"}
utilsMock.AddFile("package.json", []byte("{\"name\": \"myName\", \"version\": \"1.2.3\"}"))
@ -161,7 +161,7 @@ func TestMarBuild(t *testing.T) {
cpe.mtarFilePath = ""
options := mtaBuildOptions{ApplicationName: "myApp", Platform: "CF", MtarName: "myName.mtar"}
options := mtaBuildOptions{ApplicationName: "myApp", Platform: "CF", MtarName: "myName.mtar", Source: "./", Target: "./"}
utilsMock.AddFile("package.json", []byte("{\"name\": \"myName\", \"version\": \"1.2.3\"}"))
@ -171,11 +171,34 @@ 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", "--target", "./"}, utilsMock.Calls[0].Params)
assert.Equal(t, []string{"build", "--mtar", "myName.mtar", "--platform", "CF", "--source", "./", "--target", "./"}, utilsMock.Calls[0].Params)
}
assert.Equal(t, "myName.mtar", cpe.mtarFilePath)
})
t.Run("Source and target related tests", func(t *testing.T) {
t.Run("Mta build mbt toolset with custom source and target paths", func(t *testing.T) {
utilsMock := newMtaBuildTestUtilsBundle()
cpe.mtarFilePath = ""
options := mtaBuildOptions{ApplicationName: "myApp", Platform: "CF", MtarName: "myName.mtar", Source: "mySourcePath/", Target: "myTargetPath/"}
utilsMock.AddFile("package.json", []byte("{\"name\": \"myName\", \"version\": \"1.2.3\"}"))
err := runMtaBuild(options, &cpe, utilsMock)
assert.Nil(t, err)
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, "myName.mtar", cpe.mtarFilePath)
})
})
t.Run("M2Path related tests", func(t *testing.T) {
t.Run("Mta build mbt toolset with m2Path", func(t *testing.T) {
@ -183,7 +206,7 @@ func TestMarBuild(t *testing.T) {
utilsMock.CurrentDir = "root_folder/workspace"
cpe.mtarFilePath = ""
options := mtaBuildOptions{ApplicationName: "myApp", Platform: "CF", MtarName: "myName.mtar", M2Path: ".pipeline/local_repo"}
options := mtaBuildOptions{ApplicationName: "myApp", Platform: "CF", MtarName: "myName.mtar", Source: "./", Target: "./", M2Path: ".pipeline/local_repo"}
utilsMock.AddFile("mta.yaml", []byte("ID: \"myNameFromMtar\""))
@ -201,7 +224,7 @@ func TestMarBuild(t *testing.T) {
utilsMock := newMtaBuildTestUtilsBundle()
utilsMock.AddFile("mta.yaml", []byte("ID: \"myNameFromMtar\""))
options := mtaBuildOptions{ApplicationName: "myApp", GlobalSettingsFile: "/opt/maven/settings.xml", Platform: "CF", MtarName: "myName"}
options := mtaBuildOptions{ApplicationName: "myApp", GlobalSettingsFile: "/opt/maven/settings.xml", Platform: "CF", MtarName: "myName", Source: "./", Target: "./"}
err := runMtaBuild(options, &cpe, utilsMock)
@ -216,7 +239,7 @@ func TestMarBuild(t *testing.T) {
utilsMock := newMtaBuildTestUtilsBundle()
utilsMock.AddFile("mta.yaml", []byte("ID: \"myNameFromMtar\""))
options := mtaBuildOptions{ApplicationName: "myApp", ProjectSettingsFile: "/my/project/settings.xml", Platform: "CF", MtarName: "myName"}
options := mtaBuildOptions{ApplicationName: "myApp", ProjectSettingsFile: "/my/project/settings.xml", Platform: "CF", MtarName: "myName", Source: "./", Target: "./"}
err := runMtaBuild(options, &cpe, utilsMock)

View File

@ -48,6 +48,24 @@ spec:
- STEPS
mandatory: false
default:
- name: source
type: string
description: "The path to the MTA project."
scope:
- PARAMETERS
- STAGES
- STEPS
mandatory: false
default: "./"
- name: target
type: string
description: "The folder for the generated `MTAR` file. If the parameter has been provided, the `MTAR` file is saved in the root of the folder provided by the argument."
scope:
- PARAMETERS
- STAGES
- STEPS
mandatory: false
default: "./"
- name: defaultNpmRegistry
type: string
description: "Url to the npm registry that should be used for installing npm dependencies."