From 390cd4dc8020cb4f2b0e8aaa98222307a7d3f0b3 Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Thu, 21 Nov 2019 01:07:15 +0800 Subject: [PATCH] chore: support split from string text --- plugin.go | 20 ++++++++++-------- plugin_test.go | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 8 deletions(-) diff --git a/plugin.go b/plugin.go index 3a6fc88..8458e76 100644 --- a/plugin.go +++ b/plugin.go @@ -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 { + 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;") } - - commands = append(commands, cmd) } return commands diff --git a/plugin_test.go b/plugin_test.go index 2ffc89a..355b15c 100644 --- a/plugin_test.go +++ b/plugin_test.go @@ -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) + } + }) + } +}