2013-07-05 03:18:54 +04:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
2015-02-10 03:58:17 +03:00
|
|
|
"fmt"
|
2013-12-10 10:47:15 +04:00
|
|
|
"os"
|
2015-02-10 03:58:17 +03:00
|
|
|
"path/filepath"
|
2013-07-05 03:18:54 +04:00
|
|
|
"testing"
|
2015-02-10 03:58:17 +03:00
|
|
|
|
2016-01-20 06:37:48 +03:00
|
|
|
"github.com/bmizerany/assert"
|
2015-02-10 03:58:17 +03:00
|
|
|
"github.com/github/hub/github"
|
2013-07-05 03:18:54 +04:00
|
|
|
)
|
|
|
|
|
2015-02-12 02:46:03 +03:00
|
|
|
func setupInitContext() {
|
2014-04-08 08:19:57 +04:00
|
|
|
os.Setenv("HUB_PROTOCOL", "git")
|
2016-10-29 02:38:41 +03:00
|
|
|
os.Setenv("HUB_CONFIG", "")
|
2013-12-17 19:07:26 +04:00
|
|
|
github.CreateTestConfigs("jingweno", "123")
|
2015-02-10 03:58:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestEmptyParams(t *testing.T) {
|
2015-02-12 02:46:03 +03:00
|
|
|
setupInitContext()
|
2013-07-05 03:18:54 +04:00
|
|
|
|
|
|
|
args := NewArgs([]string{"init"})
|
|
|
|
err := transformInitArgs(args)
|
|
|
|
|
|
|
|
assert.Equal(t, nil, err)
|
|
|
|
assert.Equal(t, true, args.IsParamsEmpty())
|
2015-02-10 03:58:17 +03:00
|
|
|
}
|
2013-07-05 03:18:54 +04:00
|
|
|
|
2015-02-10 03:58:17 +03:00
|
|
|
func TestFlagToAddRemote(t *testing.T) {
|
2015-02-12 02:46:03 +03:00
|
|
|
setupInitContext()
|
2013-07-05 03:18:54 +04:00
|
|
|
|
2015-02-10 03:58:17 +03:00
|
|
|
args := NewArgs([]string{"init", "-g", "--quiet"})
|
|
|
|
err := transformInitArgs(args)
|
2013-07-05 03:18:54 +04:00
|
|
|
assert.Equal(t, nil, err)
|
|
|
|
|
|
|
|
commands := args.Commands()
|
|
|
|
assert.Equal(t, 2, len(commands))
|
2015-02-10 03:58:17 +03:00
|
|
|
assert.Equal(t, "git init --quiet", 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",
|
|
|
|
filepath.Join(currentDir, ".git"),
|
|
|
|
filepath.Base(currentDir),
|
|
|
|
)
|
|
|
|
assert.Equal(t, expected, commands[1].String())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInitInAnotherDir(t *testing.T) {
|
2015-02-12 02:46:03 +03:00
|
|
|
setupInitContext()
|
2015-02-10 03:58:17 +03:00
|
|
|
|
2015-02-12 02:46:52 +03:00
|
|
|
args := NewArgs([]string{"init", "-g", "--template", "mytpl", "--shared=umask", "my project"})
|
2015-02-10 03:58:17 +03:00
|
|
|
err := transformInitArgs(args)
|
|
|
|
assert.Equal(t, nil, err)
|
|
|
|
|
|
|
|
commands := args.Commands()
|
|
|
|
assert.Equal(t, 2, len(commands))
|
2015-02-12 02:46:52 +03:00
|
|
|
assert.Equal(t, "git init --template mytpl --shared=umask my project", commands[0].String())
|
2015-02-10 03:58:17 +03:00
|
|
|
|
|
|
|
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",
|
|
|
|
filepath.Join(currentDir, "my project", ".git"),
|
|
|
|
"my-project",
|
|
|
|
)
|
|
|
|
assert.Equal(t, expected, commands[1].String())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSeparateGitDir(t *testing.T) {
|
2015-02-12 02:46:03 +03:00
|
|
|
setupInitContext()
|
2015-02-10 03:58:17 +03:00
|
|
|
|
|
|
|
args := NewArgs([]string{"init", "-g", "--separate-git-dir", "/tmp/where-i-play.git", "my/playground"})
|
|
|
|
err := transformInitArgs(args)
|
|
|
|
assert.Equal(t, nil, err)
|
|
|
|
|
|
|
|
commands := args.Commands()
|
|
|
|
assert.Equal(t, 2, len(commands))
|
|
|
|
assert.Equal(t, "git init --separate-git-dir /tmp/where-i-play.git my/playground", 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",
|
|
|
|
filepath.Join(currentDir, "my", "playground", ".git"),
|
|
|
|
"playground",
|
|
|
|
)
|
|
|
|
assert.Equal(t, expected, commands[1].String())
|
2013-07-05 03:18:54 +04:00
|
|
|
}
|