Skip to content

Commit

Permalink
adjust file structure
Browse files Browse the repository at this point in the history
  • Loading branch information
Songmu committed Dec 18, 2019
1 parent 7d3f129 commit 80fa054
Show file tree
Hide file tree
Showing 13 changed files with 495 additions and 451 deletions.
35 changes: 35 additions & 0 deletions cmd_get.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package main

import (
"os"

"github.com/urfave/cli"
)

func doGet(c *cli.Context) error {
var (
argURL = c.Args().Get(0)
andLook = c.Bool("look")
)
g := &getter{
update: c.Bool("update"),
shallow: c.Bool("shallow"),
ssh: c.Bool("p"),
vcs: c.String("vcs"),
silent: c.Bool("silent"),
branch: c.String("branch"),
}

if argURL == "" {
cli.ShowCommandHelp(c, "get")
os.Exit(1)
}

if err := g.get(argURL); err != nil {
return err
}
if andLook {
return doLook(c)
}
return nil
}
159 changes: 159 additions & 0 deletions cmd_get_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
package main

import (
"os"
"path/filepath"
"testing"
)

func TestCommandGet(t *testing.T) {
app := newApp()

testCases := []struct {
name string
scenario func(*testing.T, string, *_cloneArgs, *_updateArgs)
}{
{
name: "simple",
scenario: func(t *testing.T, 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 := "https://github.com/motemen/ghq-test-repo"
if cloneArgs.remote.String() != expect {
t.Errorf("got: %s, expect: %s", cloneArgs.remote, expect)
}
if filepath.ToSlash(cloneArgs.local) != filepath.ToSlash(localDir) {
t.Errorf("got: %s, expect: %s", filepath.ToSlash(cloneArgs.local), filepath.ToSlash(localDir))
}
if cloneArgs.shallow {
t.Errorf("cloneArgs.shallow should be false")
}
if cloneArgs.branch != "" {
t.Errorf("cloneArgs.branch should be empty")
}
},
},
{
name: "-p option",
scenario: func(t *testing.T, 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 := "ssh://git@github.com/motemen/ghq-test-repo"
if cloneArgs.remote.String() != expect {
t.Errorf("got: %s, expect: %s", cloneArgs.remote, expect)
}
if filepath.ToSlash(cloneArgs.local) != filepath.ToSlash(localDir) {
t.Errorf("got: %s, expect: %s", filepath.ToSlash(cloneArgs.local), filepath.ToSlash(localDir))
}
if cloneArgs.shallow {
t.Errorf("cloneArgs.shallow should be false")
}
},
},
{
name: "already cloned with -u",
scenario: func(t *testing.T, 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"})

if updateArgs.local != localDir {
t.Errorf("got: %s, expect: %s", updateArgs.local, localDir)
}
},
},
{
name: "shallow",
scenario: func(t *testing.T, 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"})

expect := "https://github.com/motemen/ghq-test-repo"
if cloneArgs.remote.String() != expect {
t.Errorf("got: %s, expect: %s", cloneArgs.remote, expect)
}
if filepath.ToSlash(cloneArgs.local) != filepath.ToSlash(localDir) {
t.Errorf("got: %s, expect: %s", filepath.ToSlash(cloneArgs.local), filepath.ToSlash(localDir))
}
if !cloneArgs.shallow {
t.Errorf("cloneArgs.shallow should be true")
}
},
},
{
name: "dot slash ./",
scenario: func(t *testing.T, tmpRoot string, cloneArgs *_cloneArgs, updateArgs *_updateArgs) {
localDir := filepath.Join(tmpRoot, "github.com", "motemen")
os.MkdirAll(localDir, 0755)
wd, _ := os.Getwd()
os.Chdir(localDir)
defer os.Chdir(wd)

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

expect := "https://github.com/motemen/ghq-test-repo"
if cloneArgs.remote.String() != expect {
t.Errorf("got: %s, expect: %s", cloneArgs.remote, expect)
}
expectDir := filepath.Join(localDir, "ghq-test-repo")
if cloneArgs.local != expectDir {
t.Errorf("got: %s, expect: %s", cloneArgs.local, expectDir)
}
},
},
{
name: "dot dot slash ../",
scenario: func(t *testing.T, tmpRoot string, cloneArgs *_cloneArgs, updateArgs *_updateArgs) {
localDir := filepath.Join(tmpRoot, "github.com", "motemen", "ghq-test-repo")
os.MkdirAll(localDir, 0755)
wd, _ := os.Getwd()
os.Chdir(localDir)
defer os.Chdir(wd)

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

expect := "https://github.com/motemen/ghq-another-test-repo"
if cloneArgs.remote.String() != expect {
t.Errorf("got: %s, expect: %s", cloneArgs.remote, expect)
}
expectDir := filepath.Join(tmpRoot, "github.com", "motemen", "ghq-another-test-repo")
if cloneArgs.local != expectDir {
t.Errorf("got: %s, expect: %s", cloneArgs.local, expectDir)
}
},
},
{
name: "specific branch",
scenario: func(t *testing.T, tmpRoot string, cloneArgs *_cloneArgs, updateArgs *_updateArgs) {
localDir := filepath.Join(tmpRoot, "github.com", "motemen", "ghq-test-repo")

expectBranch := "hello"
app.Run([]string{"", "get", "-shallow", "-b", expectBranch, "motemen/ghq-test-repo"})

expect := "https://github.com/motemen/ghq-test-repo"
if cloneArgs.remote.String() != expect {
t.Errorf("got: %s, expect: %s", cloneArgs.remote, expect)
}
if filepath.ToSlash(cloneArgs.local) != filepath.ToSlash(localDir) {
t.Errorf("got: %s, expect: %s", filepath.ToSlash(cloneArgs.local), filepath.ToSlash(localDir))
}
if cloneArgs.branch != expectBranch {
t.Errorf("got: %q, expect: %q", cloneArgs.branch, expectBranch)
}
},
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
withFakeGitBackend(t, tc.scenario)
})
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
10 changes: 10 additions & 0 deletions list_test.go → cmd_list_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"flag"
"os"
"path/filepath"
"runtime"
Expand All @@ -12,6 +13,15 @@ import (
"golang.org/x/xerrors"
)

func flagSet(name string, flags []cli.Flag) *flag.FlagSet {
set := flag.NewFlagSet(name, flag.ContinueOnError)

for _, f := range flags {
f.Apply(set)
}
return set
}

func TestCommandList(t *testing.T) {
_, _, err := capture(func() {
app := cli.NewApp()
Expand Down
85 changes: 85 additions & 0 deletions cmd_look.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package main

import (
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"sync"

"github.com/motemen/ghq/cmdutil"
"github.com/urfave/cli"
)

func detectShell() string {
shell := os.Getenv("SHELL")
if shell != "" {
return shell
}
if runtime.GOOS == "windows" {
return os.Getenv("COMSPEC")
}
return "/bin/sh"
}

func doLook(c *cli.Context) error {
name := c.Args().First()

if name == "" {
cli.ShowCommandHelp(c, "look")
os.Exit(1)
}

var (
reposFound []*LocalRepository
mu sync.Mutex
)
if err := walkAllLocalRepositories(func(repo *LocalRepository) {
if repo.Matches(name) {
mu.Lock()
reposFound = append(reposFound, repo)
mu.Unlock()
}
}); err != nil {
return err
}

if len(reposFound) == 0 {
if url, err := newURL(name); err == nil {
repo, err := LocalRepositoryFromURL(url)
if err != nil {
return err
}
_, err = os.Stat(repo.FullPath)

// if the directory exists
if err == nil {
reposFound = append(reposFound, repo)
}
}
}

switch len(reposFound) {
case 0:
return fmt.Errorf("No repository found")
case 1:
repo := reposFound[0]
cmd := exec.Command(detectShell())
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Dir = repo.FullPath
cmd.Env = append(os.Environ(), "GHQ_LOOK="+filepath.ToSlash(repo.RelPath))
return cmdutil.RunCommand(cmd, true)
default:
b := &strings.Builder{}
b.WriteString("More than one repositories are found; Try more precise name\n")
for _, repo := range reposFound {
b.WriteString(fmt.Sprintf(" - %s\n", strings.Join(repo.PathParts, "/")))
}
return errors.New(b.String())
}
}
60 changes: 60 additions & 0 deletions cmd_look_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package main

import (
"fmt"
"os"
"os/exec"
"path/filepath"
"reflect"
"strings"
"testing"

"github.com/motemen/ghq/cmdutil"
)

func TestDoLook(t *testing.T) {
withFakeGitBackend(t, func(t *testing.T, tmproot string, _ *_cloneArgs, _ *_updateArgs) {
os.MkdirAll(filepath.Join(tmproot, "github.com", "motemen", "ghq", ".git"), 0755)
os.MkdirAll(filepath.Join(tmproot, "github.com", "motemen", "gobump", ".git"), 0755)
os.MkdirAll(filepath.Join(tmproot, "github.com", "Songmu", "gobump", ".git"), 0755)
defer func(orig func(cmd *exec.Cmd) error) {
cmdutil.CommandRunner = orig
}(cmdutil.CommandRunner)
var lastCmd *exec.Cmd
cmdutil.CommandRunner = func(cmd *exec.Cmd) error {
lastCmd = cmd
return nil
}
sh := detectShell()

err := newApp().Run([]string{"", "look", "https://github.com/motemen/ghq"})
if err != nil {
t.Errorf("error should be nil, but: %s", err)
}

if !reflect.DeepEqual(lastCmd.Args, []string{sh}) {
t.Errorf("lastCmd.Args: got: %v, expect: %v", lastCmd.Args, []string{sh})
}
dir := filepath.Join(tmproot, "github.com", "motemen", "ghq")
if filepath.Clean(lastCmd.Dir) != dir {
t.Errorf("lastCmd.Dir: got: %s, expect: %s", lastCmd.Dir, dir)
}
gotEnv := lastCmd.Env[len(lastCmd.Env)-1]
expectEnv := "GHQ_LOOK=github.com/motemen/ghq"
if gotEnv != expectEnv {
t.Errorf("lastCmd.Env[len(lastCmd.Env)-1]: got: %s, expect: %s", gotEnv, expectEnv)
}

err = newApp().Run([]string{"", "look", "github.com/motemen/_unknown"})
expect := "No repository found"
if !strings.HasPrefix(fmt.Sprintf("%s", err), expect) {
t.Errorf("error should has prefix %q, but: %s", expect, err)
}

err = newApp().Run([]string{"", "look", "gobump"})
expect = "More than one repositories are found; Try more precise name"
if !strings.HasPrefix(fmt.Sprintf("%s", err), expect) {
t.Errorf("error should has prefix %q, but: %s", expect, err)
}
})
}
30 changes: 30 additions & 0 deletions cmd_root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package main

import (
"fmt"

"github.com/urfave/cli"
)

func doRoot(c *cli.Context) error {
var (
w = c.App.Writer
all = c.Bool("all")
)
if all {
roots, err := localRepositoryRoots()
if err != nil {
return err
}
for _, root := range roots {
fmt.Fprintln(w, root)
}
return nil
}
root, err := primaryLocalRepositoryRoot()
if err != nil {
return err
}
fmt.Fprintln(w, root)
return nil
}
Loading

0 comments on commit 80fa054

Please sign in to comment.