Skip to content

Commit

Permalink
Linting (#36)
Browse files Browse the repository at this point in the history
* lint: cosmetics

* lint: rename global variables

* gofmt

* lint: unchecked error

* lint: errcheck

* lint: cosmetics

* Specialize 'run' function to run 'git'
  • Loading branch information
arl authored Sep 1, 2020
1 parent 59b3b29 commit 247481f
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 66 deletions.
61 changes: 40 additions & 21 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"testing"
)

var updateGolden = flag.Bool("update", false, "update golden files")
var _updateGolden = flag.Bool("update", false, "update golden files")

// This test ensures that new features do not change gitmux output when used
// with a default configuration.
Expand All @@ -25,29 +25,35 @@ func TestOutputNonRegression(t *testing.T) {
if err != nil {
log.Fatal(err)
}

defer os.RemoveAll(tmpdir)

t.Logf("test working directory: %q", tmpdir)
cloneAndHack(t, tmpdir)

cmd := exec.Command("go", "run", ".", "-printcfg")

b, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("Command error: %s: %s\noutput: %s", cmdString(cmd), err, string(b))
}

defcfg := path.Join(tmpdir, "default.cfg")
if err := ioutil.WriteFile(defcfg, b, os.ModePerm); err != nil {
t.Fatalf("Can't write %q: %s", defcfg, err)
}

repodir := path.Join(tmpdir, "gitmux")

cmd = exec.Command("go", "run", ".", "-cfg", defcfg, repodir)

got, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("Command %q failed:\n%s\nerr: %s", cmdString(cmd), b, err)
}

goldenFile := path.Join("testdata", "default.output.golden")

if *updateGolden {
if *_updateGolden {
if err := ioutil.WriteFile(goldenFile, got, os.ModePerm); err != nil {
t.Fatalf("Can't update golden file %q: %s", goldenFile, err)
}
Expand All @@ -67,58 +73,71 @@ func cmdString(cmd *exec.Cmd) string {
return strings.Join(append([]string{cmd.Path}, cmd.Args...), " ")
}

func run(t *testing.T, name string, args ...string) {
func git(t *testing.T, args ...string) {
t.Helper()

cmd := exec.Command(name, args...)
out, err := cmd.CombinedOutput()
if err != nil {
cmd := exec.Command("git", args...)
if out, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("Command %q failed:\n%s\nerr: %s", cmdString(cmd), out, err)
}
}

func cloneAndHack(t *testing.T, dir string) {
t.Helper()

var popdir func() error
var err error

if popdir, err = pushdir(dir); err != nil {
popd1, err := pushdir(dir)
if err != nil {
t.Fatal(err)
}
defer popdir()

run(t, "git", "clone", "git://github.com/arl/gitmux.git")
defer func() {
if err := popd1(); err != nil {
t.Fatalf("popd1: %v", err)
}
}()

git(t, "clone", "git://github.com/arl/gitmux.git")

if popdir, err = pushdir("gitmux"); err != nil {
popd2, err := pushdir("gitmux")
if err != nil {
t.Fatal(err)
}
defer popdir()

defer func() {
if err := popd2(); err != nil {
t.Fatalf("popd2: %v", err)
}
}()

if err := ioutil.WriteFile("dummy", []byte("dummy"), os.ModePerm); err != nil {
t.Fatalf("write dummy: %s", err)
}
run(t, "git", "add", "dummy")
run(t, "git", "commit", "-m", "add dummy file")

git(t, "add", "dummy")
git(t, "commit", "-m", "add dummy file")

if err := ioutil.WriteFile("dummy2", []byte("dummy2"), os.ModePerm); err != nil {
t.Fatalf("write dummy2: %s", err)
}
run(t, "git", "add", "dummy2")
run(t, "git", "stash")

git(t, "add", "dummy2")
git(t, "stash")

if err := ioutil.WriteFile("file1", nil, os.ModePerm); err != nil {
t.Fatalf("write file1: %s", err)
}

if err := ioutil.WriteFile("file2", nil, os.ModePerm); err != nil {
t.Fatalf("write file2: %s", err)
}

if err := ioutil.WriteFile("file3", nil, os.ModePerm); err != nil {
t.Fatalf("write file3: %s", err)
}

run(t, "git", "add", "file1")
run(t, "git", "add", "file2")
git(t, "add", "file1")
git(t, "add", "file2")

if err := ioutil.WriteFile("file2", []byte("foo"), os.ModePerm); err != nil {
t.Fatalf("write file2: %s", err)
}
Expand Down
25 changes: 21 additions & 4 deletions format/tmux/formater.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,17 @@ func truncateBranchName(name string, maxlen int, isremote bool) string {
remoteName := ""
branchName := name

const (
idxRemote = 0
idxBranch = 1
numItems = 2
)

if isremote {
a := strings.SplitAfterN(name, "/", 2)
if len(a) == 2 {
remoteName = a[0]
branchName = a[1]
a := strings.SplitAfterN(name, "/", numItems)
if len(a) == numItems {
remoteName = a[idxRemote]
branchName = a[idxBranch]
}
}

Expand Down Expand Up @@ -136,8 +142,10 @@ func (f *Formater) Format(w io.Writer, st *gitstatus.Status) error {
truncateBranchName(f.st.LocalBranch, f.Options.BranchMaxLen, false))
f.flags()
_, err := f.b.WriteTo(w)

return err
}

f.format()
_, err := f.b.WriteTo(w)

Expand Down Expand Up @@ -165,6 +173,7 @@ func (f *Formater) format() {

func (f *Formater) specialState() {
f.clear()

switch f.st.State {
case gitstatus.Rebasing:
fmt.Fprintf(&f.b, "%s[rebase] ", f.Styles.State)
Expand All @@ -183,11 +192,13 @@ func (f *Formater) specialState() {
case gitstatus.Default:
fmt.Fprintf(&f.b, "%s%s", f.Styles.Branch, f.Symbols.Branch)
}

f.currentRef()
}

func (f *Formater) remote() {
f.clear()

if f.st.RemoteBranch != "" {
fmt.Fprintf(&f.b, "%s%s", f.Styles.Remote,
truncateBranchName(f.st.RemoteBranch, f.Options.BranchMaxLen, true))
Expand All @@ -197,6 +208,7 @@ func (f *Formater) remote() {

func (f *Formater) remoteBranch() {
f.clear()

if f.st.RemoteBranch != "" {
fmt.Fprintf(&f.b, "%s%s", f.Styles.Remote,
truncateBranchName(f.st.RemoteBranch, f.Options.BranchMaxLen, true))
Expand All @@ -207,8 +219,10 @@ func (f *Formater) divergence() {
f.clear()

pref := " "

if f.st.BehindCount != 0 {
fmt.Fprintf(&f.b, " %s%d", f.Symbols.Behind, f.st.BehindCount)

pref = ""
}

Expand All @@ -224,8 +238,10 @@ func (f *Formater) clear() {

func (f *Formater) currentRef() {
f.clear()

if f.st.IsDetached {
fmt.Fprintf(&f.b, "%s%s", f.Symbols.HashPrefix, f.st.HEAD)

return
}

Expand All @@ -238,6 +254,7 @@ func (f *Formater) flags() {

if f.st.IsClean {
fmt.Fprintf(&f.b, "%s%s", f.Styles.Clean, f.Symbols.Clean)

return
}

Expand Down
Loading

0 comments on commit 247481f

Please sign in to comment.