提交 93537d45 编写于 作者: M Mislav Marohnić

Merge branch 'git-empty-args'

......@@ -21,7 +21,17 @@ type Cmd struct {
}
func (cmd Cmd) String() string {
return fmt.Sprintf("%s %s", cmd.Name, strings.Join(cmd.Args, " "))
args := make([]string, len(cmd.Args))
for i, a := range cmd.Args {
if strings.ContainsRune(a, '"') {
args[i] = fmt.Sprintf(`'%s'`, a)
} else if a == "" || strings.ContainsRune(a, '\'') || strings.ContainsRune(a, ' ') {
args[i] = fmt.Sprintf(`"%s"`, a)
} else {
args[i] = a
}
}
return fmt.Sprintf("%s %s", cmd.Name, strings.Join(args, " "))
}
// WithArg returns the current argument
......@@ -138,7 +148,7 @@ func NewWithArray(cmd []string) *Cmd {
func verboseLog(cmd *Cmd) {
if os.Getenv("HUB_VERBOSE") != "" {
msg := fmt.Sprintf("$ %s %s", cmd.Name, strings.Join(cmd.Args, " "))
msg := fmt.Sprintf("$ %s", cmd.String())
if ui.IsTerminal(os.Stderr) {
msg = fmt.Sprintf("\033[35m%s\033[0m", msg)
}
......
......@@ -18,3 +18,11 @@ func TestWithArg(t *testing.T) {
assert.Equal(t, "git", execCmd.Name)
assert.Equal(t, 4, len(execCmd.Args))
}
func Test_String(t *testing.T) {
c := Cmd{
Name: "echo",
Args: []string{"hi", "hello world", "don't", `"fake news"`},
}
assert.Equal(t, `echo hi "hello world" "don't" '"fake news"'`, c.String())
}
......@@ -92,9 +92,7 @@ func (a *Args) ToCmd() *cmd.Cmd {
}
for _, arg := range a.Params {
if arg != "" {
c.WithArg(arg)
}
c.WithArg(arg)
}
return c
......
......@@ -120,6 +120,12 @@ func TestArgs_GlobalFlags_Replaced(t *testing.T) {
assert.Equal(t, []string{"-a", "http://example.com"}, cmd.Args)
}
func TestArgs_ToCmd(t *testing.T) {
args := NewArgs([]string{"a", "", "b", ""})
cmd := args.ToCmd()
assert.Equal(t, []string{"a", "", "b", ""}, cmd.Args)
}
func TestArgs_GlobalFlags_BeforeAfterChain(t *testing.T) {
args := NewArgs([]string{"-c", "key=value", "-C", "dir", "status"})
args.Before("git", "remote", "add")
......
......@@ -57,13 +57,13 @@ func TestInitInAnotherDir(t *testing.T) {
commands := args.Commands()
assert.Equal(t, 2, len(commands))
assert.Equal(t, "git init --template mytpl --shared=umask my project", commands[0].String())
assert.Equal(t, "git init --template mytpl --shared=umask \"my project\"", commands[0].String())
currentDir, err := os.Getwd()
assert.Equal(t, nil, err)
expected := fmt.Sprintf(
"git --git-dir %s remote add origin git@github.com:jingweno/%s.git",
"git --git-dir \"%s\" remote add origin git@github.com:jingweno/%s.git",
filepath.Join(currentDir, "my project", ".git"),
"my-project",
)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册