Skip to content

Commit 7f63635

Browse files
author
Tobias Meinhardt
committed
Wrote tests for git stash and stash pop && fix push cmd
1 parent 7ef005c commit 7f63635

File tree

10 files changed

+81
-20
lines changed

10 files changed

+81
-20
lines changed

git/logging.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func (s loggingService) StashPop() (err error) {
7878
defer func(begin time.Time) {
7979
s.logger.Log("method", "StashPop", "took", time.Since(begin), "err", err)
8080
}(time.Now())
81-
return s.next.Stash()
81+
return s.next.StashPop()
8282
}
8383

8484
// Commit added changes

git/native_adapter.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func (a nativeGitAdapter) Fetch() error {
7878

7979
// Add all changes
8080
func (a nativeGitAdapter) AddAll() error {
81-
cmd := a.exec.Command("git add .")
81+
cmd := a.exec.Command("git add -A")
8282
var stderr bytes.Buffer
8383
cmd.Stderr = &stderr
8484
err := cmd.Run()
@@ -90,7 +90,11 @@ func (a nativeGitAdapter) Stash() error {
9090
cmd := a.exec.Command("git stash")
9191
var stderr bytes.Buffer
9292
cmd.Stderr = &stderr
93-
err := cmd.Run()
93+
err := cmd.Start()
94+
if err != nil {
95+
errors.Wrap(err, stderr.String())
96+
}
97+
err = cmd.Wait()
9498
return errors.Wrap(err, stderr.String())
9599
}
96100

git/native_adapter_test.go

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func TestSetCICDOrigin(t *testing.T) {
2424
err := s.SetCICDOrigin(newOrigin)
2525
testenv.CheckForErrors(t, err)
2626

27-
stdout, err := local.Do("git remote get-url origin")
27+
stdout, _, err := local.Do("git remote get-url origin")
2828
testenv.CheckForErrors(t, err)
2929
if strings.TrimSpace(stdout.String()) != newOrigin {
3030
t.Errorf("origin should be %s but is %s", newOrigin, stdout.String())
@@ -109,3 +109,50 @@ func TestFetch(t *testing.T) {
109109
t.Errorf("branch should be '%s' but is '%s'", local2Branch, branchName)
110110
}
111111
}
112+
113+
func TestStash(t *testing.T) {
114+
local, _, teardown := testenv.SetupEnv(t)
115+
defer teardown()
116+
117+
s := setupNativeGitService(local.Folder)
118+
local.Do("touch test.file")
119+
stdout, _, _ := local.Do("git status | grep test.file")
120+
if stdout.String() == "" {
121+
t.Errorf("testfile lookup should NOT be empty")
122+
}
123+
124+
s.AddAll()
125+
s.Stash()
126+
127+
stdout, _, _ = local.Do("git status | grep test.file")
128+
if stdout.String() != "" {
129+
t.Errorf("testfile lookup should be empty")
130+
}
131+
}
132+
133+
func TestStashPop(t *testing.T) {
134+
local, _, teardown := testenv.SetupEnv(t)
135+
defer teardown()
136+
137+
s := setupNativeGitService(local.Folder)
138+
local.Do("touch test.file")
139+
stdout, _, _ := local.Do("git status | grep test.file")
140+
if stdout.String() == "" {
141+
t.Errorf("testfile lookup should NOT be empty")
142+
}
143+
144+
s.AddAll()
145+
s.Stash()
146+
147+
stdout, _, _ = local.Do("git status | grep test.file")
148+
if stdout.String() != "" {
149+
t.Errorf("testfile lookup should be empty")
150+
}
151+
152+
s.StashPop()
153+
154+
stdout, _, _ = local.Do("git status | grep test.file")
155+
if stdout.String() == "" {
156+
t.Errorf("testfile lookup should NOT be empty")
157+
}
158+
}

git/service.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,13 @@ type Service interface {
2424
RemoteBranchExists(branchName string) error
2525
}
2626

27-
type service struct {
27+
type NativeService interface {
2828
Service
29+
CMDExecutor() cmd.CmdExecutor
2930
}
3031

32+
type service struct{ Service }
33+
3134
func NewGoGitService() Service {
3235
return service{goGitAdapter{}}
3336
}

go.sum

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ github.com/coreos/go-etcd v2.0.0+incompatible/go.mod h1:Jez6KQU2B/sWsbdaef3ED8Nz
3030
github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk=
3131
github.com/coreos/go-systemd v0.0.0-20181012123002-c6f51f82210d/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
3232
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
33+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
3334
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3435
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
3536
github.com/dustin/go-humanize v0.0.0-20180713052910-9f541cc9db5d/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
@@ -382,6 +383,7 @@ github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw=
382383
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
383384
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
384385
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
386+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
385387
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
386388
github.com/prometheus/client_golang v0.8.0/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw=
387389
github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo=
@@ -450,6 +452,7 @@ github.com/src-d/gcfg v1.4.0/go.mod h1:p/UMsR43ujA89BJY9duynAwIpvqEujIH/jFlfL7jW
450452
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
451453
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
452454
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
455+
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
453456
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
454457
github.com/tarm/serial v0.0.0-20180830185346-98f6abe2eb07/go.mod h1:kDXzergiv9cbyO7IOYJZWg1U88JhDg3PB6klq9Hg2pA=
455458
github.com/tcnksm/ghr v0.12.0/go.mod h1:2YSVCxP2GsVRnE6R3bZxEiVnpJ9GUkotqlq2No0kTMU=

pkg/cli/cmd/push.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,19 @@ var pushCmd = &cobra.Command{
4040
currentBranch = cb
4141
}
4242

43-
err = g.Stash()
44-
util.CheckForError(err, "Stash")
45-
46-
err = g.Checkout(currentBranch)
47-
util.CheckForError(err, "Checkout")
48-
49-
err = g.StashPop()
50-
util.CheckForError(err, "StashPop")
51-
5243
if pushCmdOptions.AddAll {
5344
err = g.AddAll()
5445
util.CheckForError(err, "AddAll")
5546

47+
err = g.Stash()
48+
util.CheckForError(err, "Stash")
49+
50+
err = g.Checkout(currentBranch)
51+
util.CheckForError(err, "Checkout")
52+
53+
err = g.StashPop()
54+
util.CheckForError(err, "StashPop")
55+
5656
if pushCmdOptions.CommitMessage != "" {
5757
err = g.Commit(pushCmdOptions.CommitMessage)
5858
util.CheckForError(err, "Commit")

pkg/cli/cmd/util/common.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package util
22

33
import (
4-
"github.com/meinto/glow/cmd"
54
"errors"
65
"log"
76
"os"
87

8+
"github.com/meinto/glow/cmd"
9+
910
"github.com/meinto/glow/gitprovider"
1011

1112
kitlog "github.com/go-kit/kit/log"

testenv/command.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,17 @@ func NewCommand() *Command {
1515
return &Command{"/bin/bash"}
1616
}
1717

18-
func (c *Command) Do(str string, args ...interface{}) (bytes.Buffer, error) {
18+
func (c *Command) Do(str string, args ...interface{}) (stdout, stderr bytes.Buffer, err error) {
1919
formattedStr := str
2020
if strings.Contains(str, "%") {
2121
formattedStr = fmt.Sprintf(str, args...)
2222
}
2323
cmd := exec.Command(c.shell, "-c", formattedStr)
24-
var stdout, stderr bytes.Buffer
2524
cmd.Stdout = &stdout
2625
cmd.Stderr = &stderr
27-
return stdout, cmd.Run()
26+
err = cmd.Start()
27+
if err != nil {
28+
return bytes.Buffer{}, bytes.Buffer{}, err
29+
}
30+
return stdout, stderr, cmd.Wait()
2831
}

testenv/local_repository.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,6 @@ func (r *LocalRepository) Push(branch string) {
5050
}
5151

5252
func (r *LocalRepository) Exists(branch string) (bool, string) {
53-
stdout, _ := r.Do(fmt.Sprintf("git rev-parse --abbrev-ref %s", branch))
53+
stdout, _, _ := r.Do(fmt.Sprintf("git rev-parse --abbrev-ref %s", branch))
5454
return strings.TrimSpace(stdout.String()) == branch, stdout.String()
5555
}

testenv/repository.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func NewRepository(folder string) *Repository {
1717
}
1818
}
1919

20-
func (r *Repository) Do(str string, args ...interface{}) (bytes.Buffer, error) {
20+
func (r *Repository) Do(str string, args ...interface{}) (stdout, stderr bytes.Buffer, err error) {
2121
moveToDir := fmt.Sprintf("cd %s", r.Folder)
2222
return r.exec.Do(moveToDir+" && "+str, args...)
2323
}

0 commit comments

Comments
 (0)