Skip to content
This repository has been archived by the owner on Nov 22, 2022. It is now read-only.

Commit

Permalink
test: adapt tests to new changes
Browse files Browse the repository at this point in the history
  • Loading branch information
profclems committed Jan 10, 2021
1 parent ca2cab2 commit c177751
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 107 deletions.
4 changes: 2 additions & 2 deletions commands/issue/delete/issue_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ func NewCmdDelete(f *cmdutils.Factory) *cobra.Command {

for _, issue := range issues {
if f.IO.IsErrTTY && f.IO.IsaTTY {
fmt.Fprintf(f.IO.StdOut, "- Deleting Issue #%d\n", issue.IID)
fmt.Fprintf(f.IO.StdErr, "- Deleting Issue #%d\n", issue.IID)
}

err := api.DeleteIssue(apiClient, repo.FullName(), issue.IID)
if err != nil {
return err
}

fmt.Fprintln(f.IO.StdOut, utils.GreenCheck(), "Issue Deleted")
fmt.Fprintln(f.IO.StdErr, utils.GreenCheck(), "Issue Deleted")
}
return nil
},
Expand Down
50 changes: 22 additions & 28 deletions commands/issue/delete/issue_delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,14 @@ import (

"github.com/profclems/glab/internal/utils"

"github.com/acarl005/stripansi"
"github.com/profclems/glab/commands/cmdtest"
"github.com/profclems/glab/pkg/api"
"github.com/stretchr/testify/assert"
"github.com/xanzy/go-gitlab"
)

// TODO: test by mocking the appropriate api function
func TestMain(m *testing.M) {
cmdtest.InitTest(m, "mr_delete_test")
cmdtest.InitTest(m, "issue_delete_test")
}

func TestNewCmdDelete(t *testing.T) {
Expand All @@ -29,6 +27,14 @@ func TestNewCmdDelete(t *testing.T) {
}
return nil
}
api.GetIssue = func(client *gitlab.Client, projectID interface{}, issueID int) (*gitlab.Issue, error) {
if projectID == "" || projectID == "WRONG_REPO" || projectID == "expected_err" {
return nil, fmt.Errorf("error expected")
}
return &gitlab.Issue{
IID: issueID,
}, nil
}

tests := []struct {
name string
Expand All @@ -41,59 +47,47 @@ func TestNewCmdDelete(t *testing.T) {
name: "delete",
args: []string{"0", "-R", "NAMESPACE/WRONG_REPO"},
wantErr: true,

assertFunc: func(t *testing.T, out string, err string) {
assert.Contains(t, err, "error expected")
},
},
{
name: "id exists",
args: []string{"1"},
wantErr: false,
assertFunc: func(t *testing.T, out string, err string) {
assert.Contains(t, out, "✓ Issue Deleted\n")
assert.Contains(t, err, "✓ Issue Deleted\n")
},
},
{
name: "delete on different repo",
args: []string{"12", "-R", "profclems/glab"},
wantErr: false,
assertFunc: func(t *testing.T, out string, err string) {
assert.Contains(t, out, "✓ Issue Deleted\n")
assertFunc: func(t *testing.T, out string, stderr string) {
assert.Contains(t, stderr, "✓ Issue Deleted\n")
},
},
}

io, _, stdout, stderr := utils.IOTest()
f := cmdtest.StubFactory("")
f.IO = io
f.IO.IsaTTY = true
f.IO.IsErrTTY = true
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {

cmd := NewCmdDelete(f)
io, _, stdout, stderr := utils.IOTest()
f := cmdtest.StubFactory("")
f.IO = io
f.IO.IsaTTY = true
f.IO.IsErrTTY = true

cmd.Flags().StringP("repo", "R", "", "")
cmd := NewCmdDelete(f)

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cmd.Flags().StringP("repo", "R", "", "")

cli := strings.Join(tt.args, " ")
t.Log(cli)
_, err := cmdtest.RunCommand(cmd, cli)
if !tt.wantErr {
assert.Nil(t, err)
tt.assertFunc(t, stdout.String(), stderr.String())
} else {
assert.NotNil(t, err)
stderr.WriteString(err.Error()) // write err to stderr
}

out := stripansi.Strip(stdout.String())
outErr := stripansi.Strip(stderr.String())

tt.assertFunc(t, out, outErr)
assert.Contains(t, outErr, tt.errMsg)
stderr.Reset()
stdout.Reset()
})
}

Expand Down
9 changes: 3 additions & 6 deletions commands/issue/subscribe/issue_subscribe.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ func NewCmdSubscribe(f *cmdutils.Factory) *cobra.Command {
Aliases: []string{"sub"},
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
out := f.IO.StdOut
var err error

apiClient, err := f.HttpClient()
if err != nil {
return err
Expand All @@ -33,16 +30,16 @@ func NewCmdSubscribe(f *cmdutils.Factory) *cobra.Command {

for _, issue := range issues {
if f.IO.IsErrTTY && f.IO.IsaTTY {
fmt.Fprintf(out, "- Subscribing to Issue #%d in %s\n", issue.IID, utils.Cyan(repo.FullName()))
fmt.Fprintf(f.IO.StdErr, "- Subscribing to Issue #%d in %s\n", issue.IID, utils.Cyan(repo.FullName()))
}

issue, err := api.SubscribeToIssue(apiClient, repo.FullName(), issue.IID, nil)
if err != nil {
return err
}

fmt.Fprintln(out, utils.GreenCheck(), "Subscribed")
fmt.Fprintln(out, issueutils.DisplayIssue(issue))
fmt.Fprintln(f.IO.StdErr, utils.GreenCheck(), "Subscribed")
fmt.Fprintln(f.IO.StdOut, issueutils.DisplayIssue(issue))
}
return nil
},
Expand Down
53 changes: 20 additions & 33 deletions commands/issue/subscribe/issue_subscribe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (

"github.com/profclems/glab/internal/utils"

"github.com/acarl005/stripansi"
"github.com/profclems/glab/commands/cmdtest"
"github.com/profclems/glab/pkg/api"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -39,40 +38,34 @@ func TestNewCmdSubscribe(t *testing.T) {
}

testCases := []struct {
Name string
Issue string
ExpectedMsg []string
wantErr bool
Name string
Issue string
stderr string
wantErr bool
}{
{
Name: "Issue Exists",
Issue: "1",
ExpectedMsg: []string{"- Subscribing to Issue #1 in glab-cli/test", "✓ Subscribed to issue #1"},
Name: "Issue Exists",
Issue: "1",
stderr: "- Subscribing to Issue #1 in glab-cli/test\n✓ Subscribed\n",
},
{
Name: "Issue on another repo",
Issue: "1 -R profclems/glab",
ExpectedMsg: []string{"- Subscribing to Issue #1 in profclems/glab", "✓ Subscribed to issue #1"},
},
{
Name: "Issue Does Not Exist",
Issue: "0",
ExpectedMsg: []string{"- Subscribing to Issue #0 in glab-cli/test", "error expected"},
wantErr: true,
Name: "Issue Does Not Exist",
Issue: "0",
stderr: "- Subscribing to Issue #0 in glab-cli/test\nerror expected\n",
wantErr: true,
},
}

io, _, stdout, stderr := utils.IOTest()
f := cmdtest.StubFactory("https://gitlab.com/glab-cli/test")
f.IO = io
f.IO.IsaTTY = true
f.IO.IsErrTTY = true

cmd := NewCmdSubscribe(f)
cmd.Flags().StringP("repo", "R", "", "")

for _, tc := range testCases {
t.Run(tc.Name, func(t *testing.T) {
io, _, _, stderr := utils.IOTest()
f := cmdtest.StubFactory("https://gitlab.com/glab-cli/test")
f.IO = io
f.IO.IsaTTY = true
f.IO.IsErrTTY = true

cmd := NewCmdSubscribe(f)
cmd.Flags().StringP("repo", "R", "", "")

_, err := cmdtest.RunCommand(cmd, tc.Issue)
if tc.wantErr {
Expand All @@ -81,13 +74,7 @@ func TestNewCmdSubscribe(t *testing.T) {
} else {
require.NoError(t, err)
}

out := stripansi.Strip(stdout.String())

for _, msg := range tc.ExpectedMsg {
assert.Contains(t, out, msg)
assert.Contains(t, stderr.String(), "")
}
assert.Equal(t, tc.stderr, stderr.String())
})
}

Expand Down
9 changes: 3 additions & 6 deletions commands/issue/unsubscribe/issue_unsubscribe.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ func NewCmdUnsubscribe(f *cmdutils.Factory) *cobra.Command {
Aliases: []string{"unsub"},
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
var err error
out := f.IO.StdOut

apiClient, err := f.HttpClient()
if err != nil {
return err
Expand All @@ -34,16 +31,16 @@ func NewCmdUnsubscribe(f *cmdutils.Factory) *cobra.Command {

for _, issue := range issues {
if f.IO.IsaTTY && f.IO.IsErrTTY {
fmt.Fprintf(out, "- Unsubscribing from Issue #%d in %s\n", issue.IID, utils.Cyan(repo.FullName()))
fmt.Fprintf(f.IO.StdErr, "- Unsubscribing from Issue #%d in %s\n", issue.IID, utils.Cyan(repo.FullName()))
}

issue, err := api.UnsubscribeFromIssue(apiClient, repo.FullName(), issue.IID, nil)
if err != nil {
return err
}

fmt.Fprintln(out, utils.Red("✔"), "Unsubscribed")
fmt.Fprintln(out, issueutils.DisplayIssue(issue))
fmt.Fprintln(f.IO.StdErr, utils.Red("✔"), "Unsubscribed")
fmt.Fprintln(f.IO.StdOut, issueutils.DisplayIssue(issue))
}
return nil
},
Expand Down
52 changes: 20 additions & 32 deletions commands/issue/unsubscribe/issue_unsubscribe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (

"github.com/profclems/glab/internal/utils"

"github.com/acarl005/stripansi"
"github.com/profclems/glab/commands/cmdtest"
"github.com/profclems/glab/pkg/api"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -39,40 +38,34 @@ func TestNewCmdUnsubscribe(t *testing.T) {
}

testCases := []struct {
Name string
Issue string
ExpectedMsg []string
wantErr bool
Name string
Issue string
stderr string
wantErr bool
}{
{
Name: "Issue Exists",
Issue: "1",
ExpectedMsg: []string{"- Unsubscribing from Issue #1 in glab-cli/test", "✔ Unsubscribed from issue #1"},
Name: "Issue Exists",
Issue: "1",
stderr: "- Unsubscribing from Issue #1 in glab-cli/test\n✔ Unsubscribed\n",
},
{
Name: "Issue on another repo",
Issue: "1 -R profclems/glab",
ExpectedMsg: []string{"- Unsubscribing from Issue #1 in profclems/glab", "✔ Unsubscribed from issue #1"},
},
{
Name: "Issue Does Not Exist",
Issue: "0",
ExpectedMsg: []string{"- Unsubscribing from Issue #0 in glab-cli/test", "error expected"},
wantErr: true,
Name: "Issue Does Not Exist",
Issue: "0",
stderr: "- Unsubscribing from Issue #0 in glab-cli/test\nerror expected\n",
wantErr: true,
},
}

io, _, stdout, stderr := utils.IOTest()
f := cmdtest.StubFactory("https://gitlab.com/glab-cli/test")
f.IO = io
f.IO.IsaTTY = true
f.IO.IsErrTTY = true

cmd := NewCmdUnsubscribe(f)
cmd.Flags().StringP("repo", "R", "", "")

for _, tc := range testCases {
t.Run(tc.Name, func(t *testing.T) {
io, _, _, stderr := utils.IOTest()
f := cmdtest.StubFactory("https://gitlab.com/glab-cli/test")
f.IO = io
f.IO.IsaTTY = true
f.IO.IsErrTTY = true

cmd := NewCmdUnsubscribe(f)
cmd.Flags().StringP("repo", "R", "", "")

_, err := cmdtest.RunCommand(cmd, tc.Issue)
if tc.wantErr {
Expand All @@ -82,12 +75,7 @@ func TestNewCmdUnsubscribe(t *testing.T) {
require.NoError(t, err)
}

out := stripansi.Strip(stdout.String())

for _, msg := range tc.ExpectedMsg {
assert.Contains(t, out, msg)
assert.Contains(t, stderr.String(), "")
}
assert.Equal(t, tc.stderr, stderr.String())
})
}

Expand Down

0 comments on commit c177751

Please sign in to comment.