Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

introduce table driven test in TestCommandGet #140

Merged
merged 1 commit into from
Apr 30, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
149 changes: 82 additions & 67 deletions commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,82 +145,97 @@ func withFakeGitBackend(t *testing.T, block func(string, *_cloneArgs, *_updateAr

func TestCommandGet(t *testing.T) {
RegisterTestingT(t)

app := newApp()

withFakeGitBackend(t, func(tmpRoot string, cloneArgs *_cloneArgs, updateArgs *_updateArgs) {
localDir := filepath.Join(tmpRoot, "github.com", "motemen", "ghq-test-repo")

app.Run([]string{"", "get", "motemen/ghq-test-repo"})

Expect(cloneArgs.remote.String()).To(Equal("https://github.com/motemen/ghq-test-repo"))
Expect(filepath.ToSlash(cloneArgs.local)).To(Equal(filepath.ToSlash(localDir)))
Expect(cloneArgs.shallow).To(Equal(false))
})

withFakeGitBackend(t, func(tmpRoot string, cloneArgs *_cloneArgs, updateArgs *_updateArgs) {
localDir := filepath.Join(tmpRoot, "github.com", "motemen", "ghq-test-repo")

app.Run([]string{"", "get", "-p", "motemen/ghq-test-repo"})

Expect(cloneArgs.remote.String()).To(Equal("ssh://git@github.com/motemen/ghq-test-repo"))
Expect(filepath.ToSlash(cloneArgs.local)).To(Equal(filepath.ToSlash(localDir)))
})

withFakeGitBackend(t, func(tmpRoot string, cloneArgs *_cloneArgs, updateArgs *_updateArgs) {
localDir := filepath.Join(tmpRoot, "github.com", "motemen", "ghq-test-repo")
// mark as "already cloned", the condition may change later
os.MkdirAll(filepath.Join(localDir, ".git"), 0755)

app.Run([]string{"", "get", "-u", "motemen/ghq-test-repo"})

Expect(updateArgs.local).To(Equal(localDir))
})

withFakeGitBackend(t, func(tmpRoot string, cloneArgs *_cloneArgs, updateArgs *_updateArgs) {
localDir := filepath.Join(tmpRoot, "github.com", "motemen", "ghq-test-repo")
app.Run([]string{"", "get", "-p", "motemen/ghq-test-repo"})

Expect(cloneArgs.remote.String()).To(Equal("ssh://git@github.com/motemen/ghq-test-repo"))
Expect(filepath.ToSlash(cloneArgs.local)).To(Equal(filepath.ToSlash(localDir)))
Expect(cloneArgs.shallow).To(Equal(false))
})

withFakeGitBackend(t, func(tmpRoot string, cloneArgs *_cloneArgs, updateArgs *_updateArgs) {
localDir := filepath.Join(tmpRoot, "github.com", "motemen", "ghq-test-repo")
testCases := []struct {
name string
f func(string, *_cloneArgs, *_updateArgs)
}{
{
name: "simple",
f: func(tmpRoot string, cloneArgs *_cloneArgs, updateArgs *_updateArgs) {
localDir := filepath.Join(tmpRoot, "github.com", "motemen", "ghq-test-repo")

app.Run([]string{"", "get", "motemen/ghq-test-repo"})

Expect(cloneArgs.remote.String()).To(Equal("https://github.com/motemen/ghq-test-repo"))
Expect(filepath.ToSlash(cloneArgs.local)).To(Equal(filepath.ToSlash(localDir)))
Expect(cloneArgs.shallow).To(Equal(false))
},
},
{
name: "p option",
f: func(tmpRoot string, cloneArgs *_cloneArgs, updateArgs *_updateArgs) {
localDir := filepath.Join(tmpRoot, "github.com", "motemen", "ghq-test-repo")

app.Run([]string{"", "get", "-shallow", "motemen/ghq-test-repo"})
app.Run([]string{"", "get", "-p", "motemen/ghq-test-repo"})

Expect(cloneArgs.remote.String()).To(Equal("https://github.com/motemen/ghq-test-repo"))
Expect(filepath.ToSlash(cloneArgs.local)).To(Equal(filepath.ToSlash(localDir)))
Expect(cloneArgs.shallow).To(Equal(true))
})

withFakeGitBackend(t, func(tmpRoot string, cloneArgs *_cloneArgs, updateArgs *_updateArgs) {
localDir := filepath.Join(tmpRoot, "github.com", "motemen")
os.MkdirAll(localDir, 0755)
wd, _ := os.Getwd()
defer os.Chdir(wd)
os.Chdir(localDir)
Expect(cloneArgs.remote.String()).To(Equal("ssh://git@github.com/motemen/ghq-test-repo"))
Expect(filepath.ToSlash(cloneArgs.local)).To(Equal(filepath.ToSlash(localDir)))
Expect(cloneArgs.shallow).To(Equal(false))
},
},
{
name: "already cloned with -u",
f: func(tmpRoot string, cloneArgs *_cloneArgs, updateArgs *_updateArgs) {
localDir := filepath.Join(tmpRoot, "github.com", "motemen", "ghq-test-repo")
// mark as "already cloned", the condition may change later
os.MkdirAll(filepath.Join(localDir, ".git"), 0755)

app.Run([]string{"", "get", "-u", "." + string(filepath.Separator) + "ghq-test-repo"})
app.Run([]string{"", "get", "-u", "motemen/ghq-test-repo"})

Expect(cloneArgs.remote.String()).To(Equal("https://github.com/motemen/ghq-test-repo"))
Expect(cloneArgs.local).To(Equal(filepath.Join(localDir, "ghq-test-repo")))
})
Expect(updateArgs.local).To(Equal(localDir))
},
},
{
name: "shallow",
f: func(tmpRoot string, cloneArgs *_cloneArgs, updateArgs *_updateArgs) {
localDir := filepath.Join(tmpRoot, "github.com", "motemen", "ghq-test-repo")

withFakeGitBackend(t, func(tmpRoot string, cloneArgs *_cloneArgs, updateArgs *_updateArgs) {
localDir := filepath.Join(tmpRoot, "github.com", "motemen", "ghq-test-repo")
os.MkdirAll(localDir, 0755)
wd, _ := os.Getwd()
defer os.Chdir(wd)
os.Chdir(localDir)
app.Run([]string{"", "get", "-shallow", "motemen/ghq-test-repo"})

app.Run([]string{"", "get", "-u", ".." + string(filepath.Separator) + "ghq-another-test-repo"})
Expect(cloneArgs.remote.String()).To(Equal("https://github.com/motemen/ghq-test-repo"))
Expect(filepath.ToSlash(cloneArgs.local)).To(Equal(filepath.ToSlash(localDir)))
Expect(cloneArgs.shallow).To(Equal(true))
},
},
{
name: "dot slach ./",
f: func(tmpRoot string, cloneArgs *_cloneArgs, updateArgs *_updateArgs) {
localDir := filepath.Join(tmpRoot, "github.com", "motemen")
os.MkdirAll(localDir, 0755)
wd, _ := os.Getwd()
defer os.Chdir(wd)
os.Chdir(localDir)

app.Run([]string{"", "get", "-u", "." + string(filepath.Separator) + "ghq-test-repo"})

Expect(cloneArgs.remote.String()).To(Equal("https://github.com/motemen/ghq-test-repo"))
Expect(cloneArgs.local).To(Equal(filepath.Join(localDir, "ghq-test-repo")))
},
},
{
name: "dot dot slash ../",
f: func(tmpRoot string, cloneArgs *_cloneArgs, updateArgs *_updateArgs) {
localDir := filepath.Join(tmpRoot, "github.com", "motemen", "ghq-test-repo")
os.MkdirAll(localDir, 0755)
wd, _ := os.Getwd()
defer os.Chdir(wd)
os.Chdir(localDir)

app.Run([]string{"", "get", "-u", ".." + string(filepath.Separator) + "ghq-another-test-repo"})

Expect(cloneArgs.remote.String()).To(Equal("https://github.com/motemen/ghq-another-test-repo"))
Expect(cloneArgs.local).To(Equal(filepath.Join(tmpRoot, "github.com", "motemen", "ghq-another-test-repo")))
},
},
}

Expect(cloneArgs.remote.String()).To(Equal("https://github.com/motemen/ghq-another-test-repo"))
Expect(cloneArgs.local).To(Equal(filepath.Join(tmpRoot, "github.com", "motemen", "ghq-another-test-repo")))
})
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
withFakeGitBackend(t, tc.f)
})
}
}

func TestCommandList(t *testing.T) {
Expand Down