2013-06-26 19:58:15 +04:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2014-04-08 08:04:09 +04:00
|
|
|
|
2016-01-20 06:37:48 +03:00
|
|
|
"github.com/bmizerany/assert"
|
2013-06-26 19:58:15 +04:00
|
|
|
)
|
|
|
|
|
2013-07-02 22:28:50 +04:00
|
|
|
func TestNewArgs(t *testing.T) {
|
|
|
|
args := NewArgs([]string{})
|
|
|
|
assert.Equal(t, "", args.Command)
|
2013-07-02 22:56:45 +04:00
|
|
|
assert.Equal(t, 0, args.ParamsSize())
|
2013-07-02 22:28:50 +04:00
|
|
|
|
|
|
|
args = NewArgs([]string{"command"})
|
|
|
|
assert.Equal(t, "command", args.Command)
|
2013-07-02 22:56:45 +04:00
|
|
|
assert.Equal(t, 0, args.ParamsSize())
|
2013-07-02 22:28:50 +04:00
|
|
|
|
|
|
|
args = NewArgs([]string{"command", "args"})
|
|
|
|
assert.Equal(t, "command", args.Command)
|
2013-07-02 22:56:45 +04:00
|
|
|
assert.Equal(t, 1, args.ParamsSize())
|
2014-04-08 08:04:09 +04:00
|
|
|
|
2014-10-23 01:02:11 +04:00
|
|
|
args = NewArgs([]string{"--version"})
|
2015-07-08 20:52:13 +03:00
|
|
|
assert.Equal(t, "--version", args.Command)
|
2014-10-23 01:02:11 +04:00
|
|
|
assert.Equal(t, 0, args.ParamsSize())
|
|
|
|
|
|
|
|
args = NewArgs([]string{"--help"})
|
2015-07-08 20:52:13 +03:00
|
|
|
assert.Equal(t, "--help", args.Command)
|
2014-10-23 01:02:11 +04:00
|
|
|
assert.Equal(t, 0, args.ParamsSize())
|
2013-07-02 22:28:50 +04:00
|
|
|
}
|
|
|
|
|
2013-12-10 13:04:36 +04:00
|
|
|
func TestArgs_Words(t *testing.T) {
|
2015-07-08 20:52:13 +03:00
|
|
|
args := NewArgs([]string{"merge", "--no-ff", "master", "-m", "message"})
|
2013-12-10 13:04:36 +04:00
|
|
|
a := args.Words()
|
|
|
|
|
2015-07-08 20:52:13 +03:00
|
|
|
assert.Equal(t, 2, len(a))
|
2013-12-10 13:04:36 +04:00
|
|
|
assert.Equal(t, "master", a[0])
|
2015-07-08 20:52:13 +03:00
|
|
|
assert.Equal(t, "message", a[1])
|
2013-12-10 13:04:36 +04:00
|
|
|
}
|
|
|
|
|
2013-12-11 01:29:09 +04:00
|
|
|
func TestArgs_Insert(t *testing.T) {
|
2013-07-11 03:29:26 +04:00
|
|
|
args := NewArgs([]string{"command", "1", "2", "3", "4"})
|
|
|
|
args.InsertParam(0, "foo")
|
|
|
|
|
|
|
|
assert.Equal(t, 5, args.ParamsSize())
|
|
|
|
assert.Equal(t, "foo", args.FirstParam())
|
|
|
|
|
|
|
|
args = NewArgs([]string{"command", "1", "2", "3", "4"})
|
|
|
|
args.InsertParam(3, "foo")
|
|
|
|
|
|
|
|
assert.Equal(t, 5, args.ParamsSize())
|
|
|
|
assert.Equal(t, "foo", args.Params[3])
|
2015-02-17 21:04:45 +03:00
|
|
|
|
|
|
|
args = NewArgs([]string{"checkout", "-b"})
|
|
|
|
args.InsertParam(1, "foo")
|
|
|
|
|
|
|
|
assert.Equal(t, 2, args.ParamsSize())
|
|
|
|
assert.Equal(t, "-b", args.Params[0])
|
|
|
|
assert.Equal(t, "foo", args.Params[1])
|
|
|
|
|
|
|
|
args = NewArgs([]string{"checkout"})
|
|
|
|
args.InsertParam(1, "foo")
|
|
|
|
|
|
|
|
assert.Equal(t, 1, args.ParamsSize())
|
|
|
|
assert.Equal(t, "foo", args.Params[0])
|
2013-07-11 03:29:26 +04:00
|
|
|
}
|
|
|
|
|
2013-12-11 01:29:09 +04:00
|
|
|
func TestArgs_Remove(t *testing.T) {
|
2013-07-02 22:28:50 +04:00
|
|
|
args := NewArgs([]string{"1", "2", "3", "4"})
|
2013-06-26 19:58:15 +04:00
|
|
|
|
2013-07-02 22:56:45 +04:00
|
|
|
item := args.RemoveParam(1)
|
2013-07-02 22:28:50 +04:00
|
|
|
assert.Equal(t, "3", item)
|
2013-07-02 22:56:45 +04:00
|
|
|
assert.Equal(t, 2, args.ParamsSize())
|
|
|
|
assert.Equal(t, "2", args.FirstParam())
|
|
|
|
assert.Equal(t, "4", args.GetParam(1))
|
2013-06-26 19:58:15 +04:00
|
|
|
}
|
2015-07-08 20:52:13 +03:00
|
|
|
|
|
|
|
func TestArgs_GlobalFlags(t *testing.T) {
|
|
|
|
args := NewArgs([]string{"-c", "key=value", "status", "-s", "-b"})
|
|
|
|
assert.Equal(t, "status", args.Command)
|
|
|
|
assert.Equal(t, []string{"-c", "key=value"}, args.GlobalFlags)
|
|
|
|
assert.Equal(t, []string{"-s", "-b"}, args.Params)
|
|
|
|
assert.Equal(t, false, args.Noop)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestArgs_GlobalFlags_Noop(t *testing.T) {
|
|
|
|
args := NewArgs([]string{"-c", "key=value", "--noop", "--literal-pathspecs", "status", "-s", "-b"})
|
|
|
|
assert.Equal(t, "status", args.Command)
|
|
|
|
assert.Equal(t, []string{"-c", "key=value", "--literal-pathspecs"}, args.GlobalFlags)
|
|
|
|
assert.Equal(t, []string{"-s", "-b"}, args.Params)
|
|
|
|
assert.Equal(t, true, args.Noop)
|
|
|
|
}
|
2015-07-09 07:07:10 +03:00
|
|
|
|
2015-07-09 07:35:34 +03:00
|
|
|
func TestArgs_GlobalFlags_NoopTwice(t *testing.T) {
|
|
|
|
args := NewArgs([]string{"--noop", "--bare", "--noop", "status"})
|
|
|
|
assert.Equal(t, "status", args.Command)
|
|
|
|
assert.Equal(t, []string{"--bare"}, args.GlobalFlags)
|
|
|
|
assert.Equal(t, 0, len(args.Params))
|
|
|
|
assert.Equal(t, true, args.Noop)
|
|
|
|
}
|
|
|
|
|
2015-07-09 07:07:10 +03:00
|
|
|
func TestArgs_GlobalFlags_Repeated(t *testing.T) {
|
|
|
|
args := NewArgs([]string{"-C", "mydir", "-c", "a=b", "--bare", "-c", "c=d", "-c", "e=f", "status"})
|
|
|
|
assert.Equal(t, "status", args.Command)
|
|
|
|
assert.Equal(t, []string{"-C", "mydir", "-c", "a=b", "--bare", "-c", "c=d", "-c", "e=f"}, args.GlobalFlags)
|
|
|
|
assert.Equal(t, 0, len(args.Params))
|
|
|
|
assert.Equal(t, false, args.Noop)
|
|
|
|
}
|
2016-02-15 07:42:40 +03:00
|
|
|
|
|
|
|
func TestArgs_GlobalFlags_Propagate(t *testing.T) {
|
|
|
|
args := NewArgs([]string{"-c", "key=value", "status"})
|
|
|
|
cmd := args.ToCmd()
|
|
|
|
assert.Equal(t, []string{"-c", "key=value", "status"}, cmd.Args)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestArgs_GlobalFlags_Replaced(t *testing.T) {
|
|
|
|
args := NewArgs([]string{"-c", "key=value", "status"})
|
|
|
|
args.Replace("open", "", "-a", "http://example.com")
|
|
|
|
cmd := args.ToCmd()
|
|
|
|
assert.Equal(t, "open", cmd.Name)
|
|
|
|
assert.Equal(t, []string{"-a", "http://example.com"}, cmd.Args)
|
|
|
|
}
|
2019-04-14 16:08:21 +03:00
|
|
|
|
|
|
|
func TestArgs_GlobalFlags_BeforeAfterChain(t *testing.T) {
|
|
|
|
args := NewArgs([]string{"-c", "key=value", "-C", "dir", "status"})
|
|
|
|
args.Before("git", "remote", "add")
|
|
|
|
args.After("git", "clean")
|
|
|
|
args.After("echo", "done!")
|
|
|
|
cmds := args.Commands()
|
|
|
|
assert.Equal(t, 4, len(cmds))
|
|
|
|
assert.Equal(t, "git -c key=value -C dir remote add", cmds[0].String())
|
|
|
|
assert.Equal(t, "git -c key=value -C dir status", cmds[1].String())
|
|
|
|
assert.Equal(t, "git -c key=value -C dir clean", cmds[2].String())
|
|
|
|
assert.Equal(t, "echo done!", cmds[3].String())
|
|
|
|
}
|