Skip to content
Snippets Groups Projects
Commit 195890fb authored by Kamil Trzcinski's avatar Kamil Trzcinski
Browse files

Execute steps for build as-is, without joining and splitting them

parent 3daea6e1
No related branches found
No related tags found
No related merge requests found
......@@ -37,7 +37,16 @@ func GetRemoteLongRunningBuild() (JobResponse, error) {
return getRemoteBuildResponse("sleep 3600")
}
func getRemoteBuildResponse(commands string) (response JobResponse, err error) {
func GetMultilineBashBuild() (JobResponse, error) {
return getRemoteBuildResponse(`if true; then
bash \
--login \
-c 'echo Hello World'
fi
`)
}
func getRemoteBuildResponse(commands ...string) (response JobResponse, err error) {
response = JobResponse{
GitInfo: GitInfo{
RepoURL: repoRemoteURL,
......@@ -49,7 +58,7 @@ func getRemoteBuildResponse(commands string) (response JobResponse, err error) {
Steps: Steps{
Step{
Name: StepNameScript,
Script: StepScript{commands},
Script: commands,
When: StepWhenAlways,
AllowFailure: false,
},
......@@ -59,7 +68,7 @@ func getRemoteBuildResponse(commands string) (response JobResponse, err error) {
return
}
func getLocalBuildResponse(commands string) (response JobResponse, err error) {
func getLocalBuildResponse(commands ...string) (response JobResponse, err error) {
localRepoURL, err := getLocalRepoURL()
if err != nil {
return
......@@ -76,7 +85,7 @@ func getLocalBuildResponse(commands string) (response JobResponse, err error) {
Steps: Steps{
Step{
Name: StepNameScript,
Script: StepScript{commands},
Script: commands,
When: StepWhenAlways,
AllowFailure: false,
},
......
......@@ -442,3 +442,16 @@ func TestBuildWithDebugTrace(t *testing.T) {
assert.Regexp(t, `[^$] echo Hello World`, out)
})
}
func TestBuildMultilineCommand(t *testing.T) {
multilineBuild, err := common.GetMultilineBashBuild()
assert.NoError(t, err)
build, cleanup := newBuild(t, multilineBuild, "bash")
defer cleanup()
// The default build shouldn't have debug tracing enabled
out, err := runBuildReturningOutput(t, build)
assert.NoError(t, err)
assert.NotContains(t, out, "bash")
assert.Contains(t, out, "Hello World")
assert.Contains(t, out, "collapsed multi-line command")
}
......@@ -67,15 +67,15 @@ func (c *GitLabCiYamlParser) prepareJobInfo(job *common.JobResponse) (err error)
func (c *GitLabCiYamlParser) getCommands(commands interface{}) (common.StepScript, error) {
if lines, ok := commands.([]interface{}); ok {
text := ""
var steps common.StepScript
for _, line := range lines {
if lineText, ok := line.(string); ok {
text += lineText + "\n"
steps = append(steps, lineText)
} else {
return common.StepScript{}, errors.New("unsupported script")
}
}
return common.StepScript(strings.Split(text, "\n")), nil
return steps, nil
} else if text, ok := commands.(string); ok {
return common.StepScript(strings.Split(text, "\n")), nil
} else if commands != nil {
......@@ -116,9 +116,7 @@ func (c *GitLabCiYamlParser) prepareSteps(job *common.JobResponse) (err error) {
if err != nil {
return
}
for _, scriptLine := range script {
scriptCommands = append(scriptCommands, scriptLine)
}
scriptCommands = append(scriptCommands, script...)
afterScriptCommands, err = c.getCommands(c.jobConfig["after_script"])
if err != nil {
......
......@@ -339,12 +339,17 @@ func (b *AbstractShell) writeDownloadArtifactsScript(w ShellWriter, info common.
}
// Write the given string of commands using the provided ShellWriter object.
func (b *AbstractShell) writeCommands(w ShellWriter, commands string) {
commands = strings.TrimSpace(commands)
for _, command := range strings.Split(commands, "\n") {
func (b *AbstractShell) writeCommands(w ShellWriter, commands ...string) {
for _, command := range commands {
command = strings.TrimSpace(command)
if command != "" {
w.Notice("$ %s", command)
lines := strings.SplitN(command, "\n", 2)
if len(lines) > 1 {
// TODO: this should be collapsable once we introduce that in GitLab
w.Notice("$ %s # collapsed multi-line command", lines[0])
} else {
w.Notice("$ %s", lines[0])
}
} else {
w.EmptyLine()
}
......@@ -373,8 +378,7 @@ func (b *AbstractShell) writeUserScript(w ShellWriter, info common.ShellScriptIn
b.writeCommands(w, info.PreBuildScript)
}
commands := strings.Join(scriptStep.Script, "\n")
b.writeCommands(w, commands)
b.writeCommands(w, scriptStep.Script...)
if info.PostBuildScript != "" {
b.writeCommands(w, info.PostBuildScript)
......@@ -503,18 +507,7 @@ func (b *AbstractShell) writeAfterScript(w ShellWriter, info common.ShellScriptI
b.writeCdBuildDir(w, info)
w.Notice("Running after script...")
for _, command := range afterScriptStep.Script {
command = strings.TrimSpace(command)
if command != "" {
w.Notice("$ %s", command)
} else {
w.EmptyLine()
}
w.Line(command)
w.CheckForErrors()
}
b.writeCommands(w, afterScriptStep.Script...)
return nil
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment