chore: support split from string text

This commit is contained in:
Bo-Yi Wu 2019-11-21 01:07:15 +08:00
parent 9ef0a47bc0
commit 390cd4dc80
2 changed files with 67 additions and 8 deletions

View File

@ -182,19 +182,23 @@ func (p Plugin) Exec() error {
}
func (p Plugin) scriptCommands() []string {
numCommands := len(p.Config.Script)
if p.Config.ScriptStop {
numCommands *= 2
}
commands := make([]string, numCommands)
scripts := []string{}
for _, cmd := range p.Config.Script {
if p.Config.ScriptStop {
commands = append(commands, "DRONE_SSH_PREV_COMMAND_EXIT_CODE=$? ; if [ $DRONE_SSH_PREV_COMMAND_EXIT_CODE -ne 0 ]; then exit $DRONE_SSH_PREV_COMMAND_EXIT_CODE; fi;")
scripts = append(scripts, strings.Split(cmd, "\n")...)
} else {
scripts = append(scripts, cmd)
}
}
commands := make([]string, 0)
for _, cmd := range scripts {
commands = append(commands, cmd)
if p.Config.ScriptStop {
commands = append(commands, "DRONE_SSH_PREV_COMMAND_EXIT_CODE=$? ; if [ $DRONE_SSH_PREV_COMMAND_EXIT_CODE -ne 0 ]; then exit $DRONE_SSH_PREV_COMMAND_EXIT_CODE; fi;")
}
}
return commands

View File

@ -2,7 +2,9 @@ package main
import (
"bytes"
"io"
"os"
"reflect"
"strings"
"testing"
"time"
@ -527,3 +529,56 @@ func TestEnvOutput(t *testing.T) {
func unindent(text string) string {
return strings.TrimSpace(strings.Replace(text, "\t", "", -1))
}
func TestPlugin_scriptCommands(t *testing.T) {
type fields struct {
Config Config
Writer io.Writer
}
tests := []struct {
name string
fields fields
want []string
}{
{
name: "normal testing",
fields: fields{
Config: Config{
Script: []string{"mkdir a", "mkdir b"},
},
},
want: []string{"mkdir a", "mkdir b"},
},
{
name: "script stop",
fields: fields{
Config: Config{
Script: []string{"mkdir a", "mkdir b"},
ScriptStop: true,
},
},
want: []string{"mkdir a", "DRONE_SSH_PREV_COMMAND_EXIT_CODE=$? ; if [ $DRONE_SSH_PREV_COMMAND_EXIT_CODE -ne 0 ]; then exit $DRONE_SSH_PREV_COMMAND_EXIT_CODE; fi;", "mkdir b", "DRONE_SSH_PREV_COMMAND_EXIT_CODE=$? ; if [ $DRONE_SSH_PREV_COMMAND_EXIT_CODE -ne 0 ]; then exit $DRONE_SSH_PREV_COMMAND_EXIT_CODE; fi;"},
},
{
name: "normal testing 2",
fields: fields{
Config: Config{
Script: []string{"mkdir a\nmkdir c", "mkdir b"},
ScriptStop: true,
},
},
want: []string{"mkdir a", "DRONE_SSH_PREV_COMMAND_EXIT_CODE=$? ; if [ $DRONE_SSH_PREV_COMMAND_EXIT_CODE -ne 0 ]; then exit $DRONE_SSH_PREV_COMMAND_EXIT_CODE; fi;", "mkdir c", "DRONE_SSH_PREV_COMMAND_EXIT_CODE=$? ; if [ $DRONE_SSH_PREV_COMMAND_EXIT_CODE -ne 0 ]; then exit $DRONE_SSH_PREV_COMMAND_EXIT_CODE; fi;", "mkdir b", "DRONE_SSH_PREV_COMMAND_EXIT_CODE=$? ; if [ $DRONE_SSH_PREV_COMMAND_EXIT_CODE -ne 0 ]; then exit $DRONE_SSH_PREV_COMMAND_EXIT_CODE; fi;"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
p := Plugin{
Config: tt.fields.Config,
Writer: tt.fields.Writer,
}
if got := p.scriptCommands(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("Plugin.scriptCommands() = %#v, want %#v", got, tt.want)
}
})
}
}