From f25c78d0392332de97413188a09f43393328a283 Mon Sep 17 00:00:00 2001 From: dan9186 Date: Sun, 5 Apr 2026 08:32:38 -0700 Subject: [PATCH 1/4] update test client to allow returning errors --- client/testclient/client.go | 51 ++++++++++++++++++++----------------- 1 file changed, 27 insertions(+), 24 deletions(-) diff --git a/client/testclient/client.go b/client/testclient/client.go index 4f639cb..cd44a0e 100644 --- a/client/testclient/client.go +++ b/client/testclient/client.go @@ -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"] } From 6c2fed5ba0e0b0982204f73c28691c28d24d4ac1 Mon Sep 17 00:00:00 2001 From: dan9186 Date: Sun, 5 Apr 2026 08:40:59 -0700 Subject: [PATCH 2/4] expanding checkout tests --- cmd/checkout_test.go | 51 ++++++++++++++++++++++++++++++++++++++------ 1 file changed, 44 insertions(+), 7 deletions(-) diff --git a/cmd/checkout_test.go b/cmd/checkout_test.go index a6e3e7f..5f14b64 100644 --- a/cmd/checkout_test.go +++ b/cmd/checkout_test.go @@ -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") + }) } From beba27fddb0958c7431eace3df4ac4036e8cf447 Mon Sep 17 00:00:00 2001 From: dan9186 Date: Sun, 5 Apr 2026 08:41:12 -0700 Subject: [PATCH 3/4] adding clone tests --- cmd/clone_test.go | 99 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 cmd/clone_test.go diff --git a/cmd/clone_test.go b/cmd/clone_test.go new file mode 100644 index 0000000..d9d70f9 --- /dev/null +++ b/cmd/clone_test.go @@ -0,0 +1,99 @@ +package cmd + +import ( + "errors" + "testing" + + "github.com/gomicro/align/client/testclient" + "github.com/google/go-github/github" + "github.com/spf13/viper" + "github.com/stretchr/testify/assert" +) + +func TestClone(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 := cloneFunc(cloneCmd, []string{}) + assert.NoError(t, err) + + tc.AssertCommandsCalled(t, "GetRepos", "CloneRepos") + }) + + t.Run("calls expected commands with name arg", func(t *testing.T) { + tc := testclient.New() + clt = tc + + err := cloneFunc(cloneCmd, []string{"some-org"}) + assert.NoError(t, err) + + tc.AssertCommandsCalled(t, "GetRepos", "CloneRepos") + }) + + t.Run("returns error on get repos failure", func(t *testing.T) { + tc := testclient.New() + tc.Errors["GetRepos"] = errors.New("some get repos error") + clt = tc + + err := cloneFunc(cloneCmd, []string{}) + assert.ErrorContains(t, err, "get repos") + + tc.AssertCommandsCalled(t, "GetRepos") + }) + + t.Run("returns error on clone repos failure", func(t *testing.T) { + tc := testclient.New() + tc.Errors["CloneRepos"] = errors.New("some clone error") + clt = tc + + err := cloneFunc(cloneCmd, []string{}) + assert.ErrorContains(t, err, "clone repos") + + tc.AssertCommandsCalled(t, "GetRepos", "CloneRepos") + }) +} + +func TestFilterByTopics(t *testing.T) { + repoWithTopics := func(topics ...string) *github.Repository { + return &github.Repository{Topics: topics} + } + + t.Run("returns all repos when no topics filter", func(t *testing.T) { + repos := []*github.Repository{ + repoWithTopics("go", "cli"), + repoWithTopics("rust"), + } + + result := filterByTopics(repos, []string{}) + assert.Equal(t, repos, result) + }) + + t.Run("filters repos by single topic", func(t *testing.T) { + match := repoWithTopics("go", "cli") + noMatch := repoWithTopics("rust") + + result := filterByTopics([]*github.Repository{match, noMatch}, []string{"go"}) + assert.Equal(t, []*github.Repository{match}, result) + }) + + t.Run("filters repos requiring all topics present", func(t *testing.T) { + both := repoWithTopics("go", "cli") + onlyOne := repoWithTopics("go") + + result := filterByTopics([]*github.Repository{both, onlyOne}, []string{"go", "cli"}) + assert.Equal(t, []*github.Repository{both}, result) + }) + + t.Run("returns nil when no repos match", func(t *testing.T) { + repos := []*github.Repository{ + repoWithTopics("rust"), + } + + result := filterByTopics(repos, []string{"go"}) + assert.Nil(t, result) + }) +} From 7b4cbfeb2b2721a5a7f46f3dacfcaa0395b03e5a Mon Sep 17 00:00:00 2001 From: dan9186 Date: Sun, 5 Apr 2026 09:05:48 -0700 Subject: [PATCH 4/4] add some tests for the other commands --- cmd/add_test.go | 57 ++++++++++++++++++ cmd/branch_test.go | 100 +++++++++++++++++++++++++++++++ cmd/commit_test.go | 56 ++++++++++++++++++ cmd/diff_test.go | 43 ++++++++++++++ cmd/log_test.go | 43 ++++++++++++++ cmd/pull_test.go | 47 +++++++++++++++ cmd/push_test.go | 47 +++++++++++++++ cmd/remote/add_test.go | 47 +++++++++++++++ cmd/remote/remote_test.go | 46 +++++++++++++++ cmd/remote/remove_test.go | 47 +++++++++++++++ cmd/remote/set-url_test.go | 47 +++++++++++++++ cmd/status_test.go | 43 ++++++++++++++ cmd/tag_test.go | 117 +++++++++++++++++++++++++++++++++++++ 13 files changed, 740 insertions(+) create mode 100644 cmd/add_test.go create mode 100644 cmd/branch_test.go create mode 100644 cmd/commit_test.go create mode 100644 cmd/diff_test.go create mode 100644 cmd/log_test.go create mode 100644 cmd/pull_test.go create mode 100644 cmd/push_test.go create mode 100644 cmd/remote/add_test.go create mode 100644 cmd/remote/remote_test.go create mode 100644 cmd/remote/remove_test.go create mode 100644 cmd/remote/set-url_test.go create mode 100644 cmd/status_test.go create mode 100644 cmd/tag_test.go diff --git a/cmd/add_test.go b/cmd/add_test.go new file mode 100644 index 0000000..9f7d317 --- /dev/null +++ b/cmd/add_test.go @@ -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") + }) +} diff --git a/cmd/branch_test.go b/cmd/branch_test.go new file mode 100644 index 0000000..c93e95e --- /dev/null +++ b/cmd/branch_test.go @@ -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") + }) +} diff --git a/cmd/commit_test.go b/cmd/commit_test.go new file mode 100644 index 0000000..0202b2b --- /dev/null +++ b/cmd/commit_test.go @@ -0,0 +1,56 @@ +package cmd + +import ( + "errors" + "testing" + + "github.com/gomicro/align/client/testclient" + "github.com/spf13/viper" + "github.com/stretchr/testify/assert" +) + +func TestCommit(t *testing.T) { + viper.Set("verbose", true) + t.Cleanup(func() { viper.Set("verbose", false) }) + + t.Run("calls expected commands", func(t *testing.T) { + message = "test commit" + t.Cleanup(func() { message = "" }) + + tc := testclient.New() + clt = tc + + err := commitFunc(commitCmd, []string{}) + assert.NoError(t, err) + + tc.AssertCommandsCalled(t, "GetDirs", "CommitRepos") + }) + + t.Run("returns error on get dirs failure", func(t *testing.T) { + message = "test commit" + t.Cleanup(func() { message = "" }) + + tc := testclient.New() + tc.Errors["GetDirs"] = errors.New("some dirs error") + clt = tc + + err := commitFunc(commitCmd, []string{}) + assert.ErrorContains(t, err, "get dirs") + + tc.AssertCommandsCalled(t, "GetDirs") + }) + + t.Run("returns error on commit repos failure", func(t *testing.T) { + message = "test commit" + t.Cleanup(func() { message = "" }) + + tc := testclient.New() + tc.Errors["CommitRepos"] = errors.New("some commit error") + clt = tc + + err := commitFunc(commitCmd, []string{}) + assert.ErrorContains(t, err, "commit repos") + + tc.AssertCommandsCalled(t, "GetDirs", "CommitRepos") + }) +} diff --git a/cmd/diff_test.go b/cmd/diff_test.go new file mode 100644 index 0000000..0aceee1 --- /dev/null +++ b/cmd/diff_test.go @@ -0,0 +1,43 @@ +package cmd + +import ( + "errors" + "testing" + + "github.com/gomicro/align/client/testclient" + "github.com/stretchr/testify/assert" +) + +func TestDiff(t *testing.T) { + t.Run("calls expected commands", func(t *testing.T) { + tc := testclient.New() + clt = tc + + err := diffFunc(diffCmd, []string{}) + assert.NoError(t, err) + + tc.AssertCommandsCalled(t, "GetDirs", "DiffRepos") + }) + + 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 := diffFunc(diffCmd, []string{}) + assert.ErrorContains(t, err, "get dirs") + + tc.AssertCommandsCalled(t, "GetDirs") + }) + + t.Run("returns error on diff repos failure", func(t *testing.T) { + tc := testclient.New() + tc.Errors["DiffRepos"] = errors.New("some diff error") + clt = tc + + err := diffFunc(diffCmd, []string{}) + assert.ErrorContains(t, err, "diff repos") + + tc.AssertCommandsCalled(t, "GetDirs", "DiffRepos") + }) +} diff --git a/cmd/log_test.go b/cmd/log_test.go new file mode 100644 index 0000000..7a91088 --- /dev/null +++ b/cmd/log_test.go @@ -0,0 +1,43 @@ +package cmd + +import ( + "errors" + "testing" + + "github.com/gomicro/align/client/testclient" + "github.com/stretchr/testify/assert" +) + +func TestLog(t *testing.T) { + t.Run("calls expected commands", func(t *testing.T) { + tc := testclient.New() + clt = tc + + err := logFunc(logCmd, []string{}) + assert.NoError(t, err) + + tc.AssertCommandsCalled(t, "GetDirs", "LogRepos") + }) + + 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 := logFunc(logCmd, []string{}) + assert.ErrorContains(t, err, "get dirs") + + tc.AssertCommandsCalled(t, "GetDirs") + }) + + t.Run("returns error on log repos failure", func(t *testing.T) { + tc := testclient.New() + tc.Errors["LogRepos"] = errors.New("some log error") + clt = tc + + err := logFunc(logCmd, []string{}) + assert.ErrorContains(t, err, "log repos") + + tc.AssertCommandsCalled(t, "GetDirs", "LogRepos") + }) +} diff --git a/cmd/pull_test.go b/cmd/pull_test.go new file mode 100644 index 0000000..6975797 --- /dev/null +++ b/cmd/pull_test.go @@ -0,0 +1,47 @@ +package cmd + +import ( + "errors" + "testing" + + "github.com/gomicro/align/client/testclient" + "github.com/spf13/viper" + "github.com/stretchr/testify/assert" +) + +func TestPull(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 := pullFunc(pullCmd, []string{}) + assert.NoError(t, err) + + tc.AssertCommandsCalled(t, "GetDirs", "PullRepos") + }) + + 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 := pullFunc(pullCmd, []string{}) + assert.ErrorContains(t, err, "get dirs") + + tc.AssertCommandsCalled(t, "GetDirs") + }) + + t.Run("returns error on pull repos failure", func(t *testing.T) { + tc := testclient.New() + tc.Errors["PullRepos"] = errors.New("some pull error") + clt = tc + + err := pullFunc(pullCmd, []string{}) + assert.ErrorContains(t, err, "pull repos") + + tc.AssertCommandsCalled(t, "GetDirs", "PullRepos") + }) +} diff --git a/cmd/push_test.go b/cmd/push_test.go new file mode 100644 index 0000000..4b21960 --- /dev/null +++ b/cmd/push_test.go @@ -0,0 +1,47 @@ +package cmd + +import ( + "errors" + "testing" + + "github.com/gomicro/align/client/testclient" + "github.com/spf13/viper" + "github.com/stretchr/testify/assert" +) + +func TestPush(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 := pushFunc(pushCmd, []string{}) + assert.NoError(t, err) + + tc.AssertCommandsCalled(t, "GetDirs", "PushRepos") + }) + + 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 := pushFunc(pushCmd, []string{}) + assert.ErrorContains(t, err, "get dirs") + + tc.AssertCommandsCalled(t, "GetDirs") + }) + + t.Run("returns error on push repos failure", func(t *testing.T) { + tc := testclient.New() + tc.Errors["PushRepos"] = errors.New("some push error") + clt = tc + + err := pushFunc(pushCmd, []string{}) + assert.ErrorContains(t, err, "push repos") + + tc.AssertCommandsCalled(t, "GetDirs", "PushRepos") + }) +} diff --git a/cmd/remote/add_test.go b/cmd/remote/add_test.go new file mode 100644 index 0000000..6a6841f --- /dev/null +++ b/cmd/remote/add_test.go @@ -0,0 +1,47 @@ +package remote + +import ( + "errors" + "testing" + + "github.com/gomicro/align/client/testclient" + "github.com/spf13/viper" + "github.com/stretchr/testify/assert" +) + +func TestRemoteAdd(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{"origin", "https://github.com/org"}) + assert.NoError(t, err) + + tc.AssertCommandsCalled(t, "GetDirs", "Add") + }) + + 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{"origin", "https://github.com/org"}) + assert.ErrorContains(t, err, "get dirs") + + tc.AssertCommandsCalled(t, "GetDirs") + }) + + t.Run("returns error on add failure", func(t *testing.T) { + tc := testclient.New() + tc.Errors["Add"] = errors.New("some add error") + clt = tc + + err := addFunc(addCmd, []string{"origin", "https://github.com/org"}) + assert.ErrorContains(t, err, "add") + + tc.AssertCommandsCalled(t, "GetDirs", "Add") + }) +} diff --git a/cmd/remote/remote_test.go b/cmd/remote/remote_test.go new file mode 100644 index 0000000..ee01a7e --- /dev/null +++ b/cmd/remote/remote_test.go @@ -0,0 +1,46 @@ +package remote + +import ( + "errors" + "testing" + + "github.com/gomicro/align/client/testclient" + "github.com/stretchr/testify/assert" +) + +func TestRemote(t *testing.T) { + verbose = true + t.Cleanup(func() { verbose = false }) + + t.Run("calls expected commands", func(t *testing.T) { + tc := testclient.New() + clt = tc + + err := remoteFunc(RemoteCmd, []string{}) + assert.NoError(t, err) + + tc.AssertCommandsCalled(t, "GetDirs", "Remotes") + }) + + 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 := remoteFunc(RemoteCmd, []string{}) + assert.ErrorContains(t, err, "get dirs") + + tc.AssertCommandsCalled(t, "GetDirs") + }) + + t.Run("returns error on remotes failure", func(t *testing.T) { + tc := testclient.New() + tc.Errors["Remotes"] = errors.New("some remotes error") + clt = tc + + err := remoteFunc(RemoteCmd, []string{}) + assert.ErrorContains(t, err, "remotes") + + tc.AssertCommandsCalled(t, "GetDirs", "Remotes") + }) +} diff --git a/cmd/remote/remove_test.go b/cmd/remote/remove_test.go new file mode 100644 index 0000000..a679c49 --- /dev/null +++ b/cmd/remote/remove_test.go @@ -0,0 +1,47 @@ +package remote + +import ( + "errors" + "testing" + + "github.com/gomicro/align/client/testclient" + "github.com/spf13/viper" + "github.com/stretchr/testify/assert" +) + +func TestRemoteRemove(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 := removeFunc(removeCmd, []string{"origin"}) + assert.NoError(t, err) + + tc.AssertCommandsCalled(t, "GetDirs", "Remove") + }) + + 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 := removeFunc(removeCmd, []string{"origin"}) + assert.ErrorContains(t, err, "get dirs") + + tc.AssertCommandsCalled(t, "GetDirs") + }) + + t.Run("returns error on remove failure", func(t *testing.T) { + tc := testclient.New() + tc.Errors["Remove"] = errors.New("some remove error") + clt = tc + + err := removeFunc(removeCmd, []string{"origin"}) + assert.ErrorContains(t, err, "remove") + + tc.AssertCommandsCalled(t, "GetDirs", "Remove") + }) +} diff --git a/cmd/remote/set-url_test.go b/cmd/remote/set-url_test.go new file mode 100644 index 0000000..d1bdb3a --- /dev/null +++ b/cmd/remote/set-url_test.go @@ -0,0 +1,47 @@ +package remote + +import ( + "errors" + "testing" + + "github.com/gomicro/align/client/testclient" + "github.com/spf13/viper" + "github.com/stretchr/testify/assert" +) + +func TestRemoteSetURL(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 := setURLFunc(setURLCmd, []string{"origin", "https://github.com/org"}) + assert.NoError(t, err) + + tc.AssertCommandsCalled(t, "GetDirs", "SetURLs") + }) + + 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 := setURLFunc(setURLCmd, []string{"origin", "https://github.com/org"}) + assert.ErrorContains(t, err, "get dirs") + + tc.AssertCommandsCalled(t, "GetDirs") + }) + + t.Run("returns error on set url failure", func(t *testing.T) { + tc := testclient.New() + tc.Errors["SetURLs"] = errors.New("some set url error") + clt = tc + + err := setURLFunc(setURLCmd, []string{"origin", "https://github.com/org"}) + assert.ErrorContains(t, err, "set url") + + tc.AssertCommandsCalled(t, "GetDirs", "SetURLs") + }) +} diff --git a/cmd/status_test.go b/cmd/status_test.go new file mode 100644 index 0000000..e6a23b0 --- /dev/null +++ b/cmd/status_test.go @@ -0,0 +1,43 @@ +package cmd + +import ( + "errors" + "testing" + + "github.com/gomicro/align/client/testclient" + "github.com/stretchr/testify/assert" +) + +func TestStatus(t *testing.T) { + t.Run("calls expected commands", func(t *testing.T) { + tc := testclient.New() + clt = tc + + err := statusFunc(statusCmd, []string{}) + assert.NoError(t, err) + + tc.AssertCommandsCalled(t, "GetDirs", "StatusRepos") + }) + + 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 := statusFunc(statusCmd, []string{}) + assert.ErrorContains(t, err, "get dirs") + + tc.AssertCommandsCalled(t, "GetDirs") + }) + + t.Run("returns error on status repos failure", func(t *testing.T) { + tc := testclient.New() + tc.Errors["StatusRepos"] = errors.New("some status error") + clt = tc + + err := statusFunc(statusCmd, []string{}) + assert.ErrorContains(t, err, "status repos") + + tc.AssertCommandsCalled(t, "GetDirs", "StatusRepos") + }) +} diff --git a/cmd/tag_test.go b/cmd/tag_test.go new file mode 100644 index 0000000..98729f0 --- /dev/null +++ b/cmd/tag_test.go @@ -0,0 +1,117 @@ +package cmd + +import ( + "errors" + "testing" + + "github.com/gomicro/align/client/testclient" + "github.com/spf13/viper" + "github.com/stretchr/testify/assert" +) + +func TestTag(t *testing.T) { + viper.Set("verbose", true) + t.Cleanup(func() { viper.Set("verbose", false) }) + + t.Run("lists tags when no args", func(t *testing.T) { + tc := testclient.New() + clt = tc + + err := tagFunc(tagCmd, []string{}) + assert.NoError(t, err) + + tc.AssertCommandsCalled(t, "GetDirs", "ListTags") + }) + + t.Run("lists tags with list flag", func(t *testing.T) { + list = true + t.Cleanup(func() { list = false }) + + tc := testclient.New() + clt = tc + + err := tagFunc(tagCmd, []string{}) + assert.NoError(t, err) + + tc.AssertCommandsCalled(t, "GetDirs", "ListTags") + }) + + t.Run("creates tag", func(t *testing.T) { + message = "release" + t.Cleanup(func() { message = "" }) + + tc := testclient.New() + clt = tc + + err := tagFunc(tagCmd, []string{"v1.0.0"}) + assert.NoError(t, err) + + tc.AssertCommandsCalled(t, "GetDirs", "TagRepos") + }) + + t.Run("deletes tag", func(t *testing.T) { + del = true + t.Cleanup(func() { del = false }) + + tc := testclient.New() + clt = tc + + err := tagFunc(tagCmd, []string{"v1.0.0"}) + assert.NoError(t, err) + + tc.AssertCommandsCalled(t, "GetDirs", "TagRepos") + }) + + 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 := tagFunc(tagCmd, []string{}) + assert.ErrorContains(t, err, "get dirs") + + tc.AssertCommandsCalled(t, "GetDirs") + }) + + t.Run("returns error on list tags failure", func(t *testing.T) { + tc := testclient.New() + tc.Errors["ListTags"] = errors.New("some list tags error") + clt = tc + + err := tagFunc(tagCmd, []string{}) + assert.ErrorContains(t, err, "list tags") + + tc.AssertCommandsCalled(t, "GetDirs", "ListTags") + }) + + t.Run("returns error on tag repos failure", func(t *testing.T) { + message = "release" + t.Cleanup(func() { message = "" }) + + tc := testclient.New() + tc.Errors["TagRepos"] = errors.New("some tag error") + clt = tc + + err := tagFunc(tagCmd, []string{"v1.0.0"}) + assert.ErrorContains(t, err, "tagging") + + tc.AssertCommandsCalled(t, "GetDirs", "TagRepos") + }) + + t.Run("returns error when sign requires message", func(t *testing.T) { + sign = true + message = "" + t.Cleanup(func() { + sign = false + message = "" + }) + + tc := testclient.New() + clt = tc + + err := tagFunc(tagCmd, []string{"v1.0.0"}) + assert.ErrorContains(t, err, "--message is required") + + tc.AssertCommandsCalled(t, "GetDirs") + }) +}