Skip to content
Merged
Show file tree
Hide file tree
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
51 changes: 27 additions & 24 deletions client/testclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,146 +10,149 @@ import (

type TestClient struct {
CommandsCalled []string
Errors map[string]error
}

func New() *TestClient {
return &TestClient{}
return &TestClient{
Errors: map[string]error{},
}
}

func (c *TestClient) CheckoutRepos(ctx context.Context, repoDirs []string, args ...string) error {
c.CommandsCalled = append(c.CommandsCalled, "CheckoutRepos")

return nil
return c.Errors["CheckoutRepos"]
}

func (c *TestClient) CommitRepos(ctx context.Context, dirs []string, args ...string) error {
c.CommandsCalled = append(c.CommandsCalled, "CommitRepos")

return nil
return c.Errors["CommitRepos"]
}

func (c *TestClient) CloneRepos(ctx context.Context, baseDir string) ([]*clientctx.Repository, error) {
c.CommandsCalled = append(c.CommandsCalled, "CloneRepos")

return nil, nil
return nil, c.Errors["CloneRepos"]
}

func (c *TestClient) GetBranchNames(ctx context.Context, dirs []string) ([]string, error) {
c.CommandsCalled = append(c.CommandsCalled, "GetBranchNames")

return nil, nil
return nil, c.Errors["GetBranchNames"]
}

func (c *TestClient) GetBranchAndTagNames(ctx context.Context, dirs []string) ([]string, error) {
c.CommandsCalled = append(c.CommandsCalled, "GetBranchAndTagNames")

return nil, nil
return nil, c.Errors["GetBranchAndTagNames"]
}

func (c *TestClient) GetDirs(ctx context.Context, dir string) ([]string, error) {
c.CommandsCalled = append(c.CommandsCalled, "GetDirs")

return nil, nil
return nil, c.Errors["GetDirs"]
}

func (c *TestClient) GetLogins(ctx context.Context) ([]string, error) {
c.CommandsCalled = append(c.CommandsCalled, "GetLogins")

return nil, nil
return nil, c.Errors["GetLogins"]
}

func (c *TestClient) GetRemoteNames(ctx context.Context, dirs []string) ([]string, error) {
c.CommandsCalled = append(c.CommandsCalled, "GetRemoteNames")

return nil, nil
return nil, c.Errors["GetRemoteNames"]
}

func (c *TestClient) GetTagNames(ctx context.Context, dirs []string) ([]string, error) {
c.CommandsCalled = append(c.CommandsCalled, "GetTagNames")

return nil, nil
return nil, c.Errors["GetTagNames"]
}

func (c *TestClient) GetRepos(ctx context.Context, name string) ([]*github.Repository, error) {
c.CommandsCalled = append(c.CommandsCalled, "GetRepos")

return nil, nil
return nil, c.Errors["GetRepos"]
}

func (c *TestClient) Branches(ctx context.Context, repoDirs []string, args ...string) error {
c.CommandsCalled = append(c.CommandsCalled, "Branches")

return nil
return c.Errors["Branches"]
}

func (c *TestClient) ListTags(ctx context.Context, repoDirs []string, args ...string) error {
c.CommandsCalled = append(c.CommandsCalled, "ListTags")

return nil
return c.Errors["ListTags"]
}

func (c *TestClient) PullRepos(ctx context.Context, repoDirs []string, args ...string) error {
c.CommandsCalled = append(c.CommandsCalled, "PullRepos")

return nil
return c.Errors["PullRepos"]
}

func (c *TestClient) PushRepos(ctx context.Context, repoDirs []string, args ...string) error {
c.CommandsCalled = append(c.CommandsCalled, "PushRepos")

return nil
return c.Errors["PushRepos"]
}

func (c *TestClient) Remotes(ctx context.Context, repoDirs []string, args ...string) error {
c.CommandsCalled = append(c.CommandsCalled, "Remotes")

return nil
return c.Errors["Remotes"]
}

func (c *TestClient) SetURLs(ctx context.Context, repoDirs []string, name, baseURL string) error {
c.CommandsCalled = append(c.CommandsCalled, "SetURLs")

return nil
return c.Errors["SetURLs"]
}

func (c *TestClient) Add(ctx context.Context, dirs []string, name, baseURL string) error {
c.CommandsCalled = append(c.CommandsCalled, "Add")

return nil
return c.Errors["Add"]
}

func (c *TestClient) Remove(ctx context.Context, dirs []string, name string) error {
c.CommandsCalled = append(c.CommandsCalled, "Remove")

return nil
return c.Errors["Remove"]
}

func (c *TestClient) StatusRepos(ctx context.Context, dirs []string, ignoreEmpty bool, args ...string) error {
c.CommandsCalled = append(c.CommandsCalled, "StatusRepos")

return nil
return c.Errors["StatusRepos"]
}

func (c *TestClient) StageFiles(ctx context.Context, dirs []string, args ...string) error {
c.CommandsCalled = append(c.CommandsCalled, "StageFiles")

return nil
return c.Errors["StageFiles"]
}

func (c *TestClient) TagRepos(ctx context.Context, repoDirs []string, args ...string) error {
c.CommandsCalled = append(c.CommandsCalled, "TagRepos")

return nil
return c.Errors["TagRepos"]
}

func (c *TestClient) DiffRepos(ctx context.Context, repoDirs []string, cfg *repos.DiffConfig) error {
c.CommandsCalled = append(c.CommandsCalled, "DiffRepos")

return nil
return c.Errors["DiffRepos"]
}

func (c *TestClient) LogRepos(ctx context.Context, repoDirs []string, ignoreEmtpy bool, args ...string) error {
c.CommandsCalled = append(c.CommandsCalled, "LogRepos")

return nil
return c.Errors["LogRepos"]
}
57 changes: 57 additions & 0 deletions cmd/add_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package cmd

import (
"errors"
"testing"

"github.com/gomicro/align/client/testclient"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
)

func TestAdd(t *testing.T) {
viper.Set("verbose", true)
t.Cleanup(func() { viper.Set("verbose", false) })

t.Run("calls expected commands", func(t *testing.T) {
tc := testclient.New()
clt = tc

err := addFunc(addCmd, []string{})
assert.NoError(t, err)

tc.AssertCommandsCalled(t, "GetDirs", "StageFiles")
})

t.Run("calls expected commands with file args", func(t *testing.T) {
tc := testclient.New()
clt = tc

err := addFunc(addCmd, []string{"file.go"})
assert.NoError(t, err)

tc.AssertCommandsCalled(t, "GetDirs", "StageFiles")
})

t.Run("returns error on get dirs failure", func(t *testing.T) {
tc := testclient.New()
tc.Errors["GetDirs"] = errors.New("some dirs error")
clt = tc

err := addFunc(addCmd, []string{})
assert.ErrorContains(t, err, "get dirs")

tc.AssertCommandsCalled(t, "GetDirs")
})

t.Run("returns error on stage files failure", func(t *testing.T) {
tc := testclient.New()
tc.Errors["StageFiles"] = errors.New("some stage error")
clt = tc

err := addFunc(addCmd, []string{})
assert.ErrorContains(t, err, "stage files")

tc.AssertCommandsCalled(t, "GetDirs", "StageFiles")
})
}
100 changes: 100 additions & 0 deletions cmd/branch_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package cmd

import (
"errors"
"testing"

"github.com/gomicro/align/client/testclient"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
)

func TestBranch(t *testing.T) {
viper.Set("verbose", true)
t.Cleanup(func() { viper.Set("verbose", false) })

t.Run("lists branches", func(t *testing.T) {
tc := testclient.New()
clt = tc

err := branchFunc(branchCmd, []string{})
assert.NoError(t, err)

tc.AssertCommandsCalled(t, "GetDirs", "Branches")
})

t.Run("deletes branch", func(t *testing.T) {
del = true
t.Cleanup(func() { del = false })

tc := testclient.New()
clt = tc

err := branchFunc(branchCmd, []string{"main"})
assert.NoError(t, err)

tc.AssertCommandsCalled(t, "GetDirs", "Branches")
})

t.Run("force deletes branch", func(t *testing.T) {
delForce = true
t.Cleanup(func() { delForce = false })

tc := testclient.New()
clt = tc

err := branchFunc(branchCmd, []string{"main"})
assert.NoError(t, err)

tc.AssertCommandsCalled(t, "GetDirs", "Branches")
})

t.Run("returns error when deleting without branch name", func(t *testing.T) {
del = true
t.Cleanup(func() { del = false })

tc := testclient.New()
clt = tc

err := branchFunc(branchCmd, []string{})
assert.ErrorContains(t, err, "branch name is required")

tc.AssertCommandsCalled(t, "GetDirs")
})

t.Run("returns error on get dirs failure", func(t *testing.T) {
tc := testclient.New()
tc.Errors["GetDirs"] = errors.New("some dirs error")
clt = tc

err := branchFunc(branchCmd, []string{})
assert.ErrorContains(t, err, "get dirs")

tc.AssertCommandsCalled(t, "GetDirs")
})

t.Run("returns error on branches list failure", func(t *testing.T) {
tc := testclient.New()
tc.Errors["Branches"] = errors.New("some branches error")
clt = tc

err := branchFunc(branchCmd, []string{})
assert.ErrorContains(t, err, "list")

tc.AssertCommandsCalled(t, "GetDirs", "Branches")
})

t.Run("returns error on branches delete failure", func(t *testing.T) {
del = true
t.Cleanup(func() { del = false })

tc := testclient.New()
tc.Errors["Branches"] = errors.New("some branches error")
clt = tc

err := branchFunc(branchCmd, []string{"main"})
assert.ErrorContains(t, err, "delete")

tc.AssertCommandsCalled(t, "GetDirs", "Branches")
})
}
51 changes: 44 additions & 7 deletions cmd/checkout_test.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,58 @@
package cmd

import (
"errors"
"testing"

"github.com/gomicro/align/client/testclient"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
)

func TestCheckout(t *testing.T) {
tc := testclient.New()
clt = tc
// Set verbose=true to skip uiprogress global state across subtests.
viper.Set("verbose", true)
t.Cleanup(func() { viper.Set("verbose", false) })

args := []string{}
t.Run("calls expected commands", func(t *testing.T) {
tc := testclient.New()
clt = tc

err := checkoutFunc(nil, args)
assert.NoError(t, err)
err := checkoutFunc(checkoutCmd, []string{"main"})
assert.NoError(t, err)

tc.AssertCommandsCalled(t, "GetDirs", "CheckoutRepos")
tc.ResetCommandsCalled()
tc.AssertCommandsCalled(t, "GetDirs", "CheckoutRepos")
})

t.Run("passes args to checkout", func(t *testing.T) {
tc := testclient.New()
clt = tc

err := checkoutFunc(checkoutCmd, []string{"my-feature-branch"})
assert.NoError(t, err)

tc.AssertCommandsCalled(t, "GetDirs", "CheckoutRepos")
})

t.Run("returns error on get dirs failure", func(t *testing.T) {
tc := testclient.New()
tc.Errors["GetDirs"] = errors.New("some dirs error")
clt = tc

err := checkoutFunc(checkoutCmd, []string{"main"})
assert.ErrorContains(t, err, "get dirs")

tc.AssertCommandsCalled(t, "GetDirs")
})

t.Run("returns error on checkout failure", func(t *testing.T) {
tc := testclient.New()
tc.Errors["CheckoutRepos"] = errors.New("some checkout error")
clt = tc

err := checkoutFunc(checkoutCmd, []string{"main"})
assert.ErrorContains(t, err, "checkout repos")

tc.AssertCommandsCalled(t, "GetDirs", "CheckoutRepos")
})
}
Loading
Loading