Skip to content

Commit 3f5bde9

Browse files
committed
Add more test to branch.GenerateName
1 parent 1590e1c commit 3f5bde9

File tree

5 files changed

+87
-58
lines changed

5 files changed

+87
-58
lines changed

main.go

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,9 @@ import (
44
"flag"
55
"fmt"
66
"github.com/rm3l/gh-dev-branch/pkg/branch"
7+
"github.com/rm3l/gh-dev-branch/pkg/issue"
78
"log"
89
"os"
9-
"strconv"
10-
11-
"github.com/rm3l/gh-dev-branch/pkg/issue"
1210
)
1311

1412
func main() {
@@ -30,30 +28,22 @@ func main() {
3028
os.Exit(1)
3129
}
3230

33-
var issueNum int
3431
iss := os.Args[1]
3532
if iss == "-h" || iss == "-help" || iss == "--help" {
3633
flag.Usage()
3734
os.Exit(1)
3835
} else {
39-
var err error
40-
issueNum, err = strconv.Atoi(iss)
41-
if err != nil {
42-
//goland:noinspection GoUnhandledErrorResult
43-
fmt.Fprintln(os.Stderr, "issue must be a number")
44-
os.Exit(1)
45-
}
46-
4736
// Ignore errors since flag.CommandLine is set for ExitOnError.
4837
_ = flag.CommandLine.Parse(os.Args[2:])
4938
}
5039

51-
issueInfo, err := issue.Lookup(os.Stderr, repo, issueNum)
40+
ghIssueFinder := issue.NewGHIssueFinder()
41+
issueInfo, err := ghIssueFinder.FindById(os.Stderr, repo, iss)
5242
if err != nil {
5343
log.Fatalln(err)
5444
}
5545

56-
branchName, err := branch.GenerateName(issueInfo.Number, issueInfo.Title)
46+
branchName, err := branch.GenerateName(issueInfo.Id, issueInfo.Title)
5747
if err != nil {
5848
log.Fatalln(err)
5949
}

pkg/branch/branch.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ const _replacementCharacter = "-"
1111

1212
var _regexp = regexp.MustCompile("[^A-Za-z0-9.]+")
1313

14-
func GenerateName(index int, title string) (string, error) {
15-
if index <= 0 {
16-
return "", fmt.Errorf("invalid negative or zero index: %d", index)
14+
func GenerateName(id string, title string) (string, error) {
15+
if id == "" || strings.TrimSpace(id) == "" {
16+
return "", errors.New("id must not be blank")
1717
}
1818
if title == "" || strings.TrimSpace(title) == "" {
1919
return "", errors.New("title must not be blank")
@@ -24,7 +24,7 @@ func GenerateName(index int, title string) (string, error) {
2424
}
2525
generatedName := strings.ToLower(
2626
_regexp.ReplaceAllString(
27-
fmt.Sprintf("%d-%s", index, titleWithoutDotSuffix),
27+
fmt.Sprintf("%s-%s", id, titleWithoutDotSuffix),
2828
_replacementCharacter))
29-
return strings.TrimSuffix(generatedName, _replacementCharacter), nil
29+
return strings.TrimPrefix(strings.TrimSuffix(generatedName, _replacementCharacter), _replacementCharacter), nil
3030
}

pkg/branch/branch_test.go

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77

88
func TestGenerateName(t *testing.T) {
99
type args struct {
10-
num int
10+
id string
1111
title string
1212
}
1313
for _, tt := range []struct {
@@ -18,27 +18,45 @@ func TestGenerateName(t *testing.T) {
1818
want string
1919
}{
2020
{
21-
name: "negative index",
21+
name: "id empty",
2222
args: args{
23-
num: -1,
23+
id: "",
2424
title: "a-title",
2525
},
2626
want: "",
2727
wantErr: true,
2828
},
2929
{
30-
name: "zero index",
30+
name: "id blank",
3131
args: args{
32-
num: 0,
32+
id: " ",
3333
title: "a-title",
3434
},
3535
want: "",
3636
wantErr: true,
3737
},
38+
{
39+
name: "id starting with an hyphen -",
40+
args: args{
41+
id: "-1",
42+
title: "a-title",
43+
},
44+
want: "1-a-title",
45+
wantErr: false,
46+
},
47+
{
48+
name: "any other id",
49+
args: args{
50+
id: "myId",
51+
title: "a-title",
52+
},
53+
want: "myid-a-title",
54+
wantErr: false,
55+
},
3856
{
3957
name: "empty title",
4058
args: args{
41-
num: 33,
59+
id: "33",
4260
title: "",
4361
},
4462
want: "",
@@ -47,7 +65,7 @@ func TestGenerateName(t *testing.T) {
4765
{
4866
name: "blank title",
4967
args: args{
50-
num: 33,
68+
id: "33",
5169
title: " ",
5270
},
5371
want: "",
@@ -56,7 +74,7 @@ func TestGenerateName(t *testing.T) {
5674
{
5775
name: "dot title",
5876
args: args{
59-
num: 33,
77+
id: "33",
6078
title: ".",
6179
},
6280
want: "",
@@ -65,7 +83,7 @@ func TestGenerateName(t *testing.T) {
6583
{
6684
name: "branch name should not end with replacement if title already ends with it",
6785
args: args{
68-
num: 1,
86+
id: "1",
6987
title: "-",
7088
},
7189
want: "1",
@@ -74,7 +92,7 @@ func TestGenerateName(t *testing.T) {
7492
{
7593
name: "branch name should not end with replacement if title already ends with it",
7694
args: args{
77-
num: 1,
95+
id: "1",
7896
title: "something-ending-with-hyphen-",
7997
},
8098
want: "1-something-ending-with-hyphen",
@@ -83,7 +101,7 @@ func TestGenerateName(t *testing.T) {
83101
{
84102
name: "branch name should not have non-alphanumeric character",
85103
args: args{
86-
num: 777,
104+
id: "777",
87105
title: "[Feature request] an awesome feature for `tool`",
88106
},
89107
want: "777-feature-request-an-awesome-feature-for-tool",
@@ -92,7 +110,7 @@ func TestGenerateName(t *testing.T) {
92110
{
93111
name: "ending dot should be ignored",
94112
args: args{
95-
num: 1234,
113+
id: "1234",
96114
title: "Bug when running something.",
97115
},
98116
want: "1234-bug-when-running-something",
@@ -101,15 +119,15 @@ func TestGenerateName(t *testing.T) {
101119
{
102120
name: "branch name should be all lowercase",
103121
args: args{
104-
num: 2345,
122+
id: "2345",
105123
title: "THIS IS AN IMPROVEMENT SUGGESTION :-) !!!",
106124
},
107125
want: "2345-this-is-an-improvement-suggestion",
108126
wantErr: false,
109127
},
110128
} {
111129
t.Run(tt.name, func(t *testing.T) {
112-
got, err := GenerateName(tt.num, tt.title)
130+
got, err := GenerateName(tt.id, tt.title)
113131
if (err != nil) != tt.wantErr {
114132
t.Errorf("got err: %v, but wanted err set to %v", err, tt.wantErr)
115133
}

pkg/issue/github.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package issue
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"github.com/cli/go-gh"
7+
"io"
8+
"strconv"
9+
)
10+
11+
type GHIssueFinder struct{}
12+
13+
type _GhIssueInfo struct {
14+
Number int `json:"number"`
15+
Title string `json:"title"`
16+
}
17+
18+
func NewGHIssueFinder() *GHIssueFinder {
19+
return &GHIssueFinder{}
20+
}
21+
22+
func (g GHIssueFinder) FindById(w io.Writer, repo string, id string) (Info, error) {
23+
args := []string{"issue", "view", id, "--json", "number,title"}
24+
if repo != "" {
25+
args = append(args, "-R", repo)
26+
}
27+
var issueInfo _GhIssueInfo
28+
stdOut, stdErr, err := gh.Exec(args...)
29+
if err != nil {
30+
return Info{}, err
31+
}
32+
if stdErrStr := stdErr.String(); stdErrStr != "" {
33+
//goland:noinspection GoUnhandledErrorResult
34+
fmt.Fprintln(w, stdErrStr)
35+
}
36+
err = json.Unmarshal(stdOut.Bytes(), &issueInfo)
37+
return Info{
38+
Id: strconv.Itoa(issueInfo.Number),
39+
Title: issueInfo.Title,
40+
}, err
41+
}

pkg/issue/issue.go

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,12 @@
11
package issue
22

3-
import (
4-
"encoding/json"
5-
"fmt"
6-
"github.com/cli/go-gh"
7-
"io"
8-
"strconv"
9-
)
3+
import "io"
104

115
type Info struct {
12-
Number int `json:"number"`
13-
Title string `json:"title"`
6+
Id string
7+
Title string
148
}
159

16-
func Lookup(w io.Writer, repo string, issue int) (Info, error) {
17-
args := []string{"issue", "view", strconv.Itoa(issue), "--json", "number,title"}
18-
if repo != "" {
19-
args = append(args, "-R", repo)
20-
}
21-
var issueInfo Info
22-
stdOut, stdErr, err := gh.Exec(args...)
23-
if err != nil {
24-
return issueInfo, err
25-
}
26-
if stdErrStr := stdErr.String(); stdErrStr != "" {
27-
//goland:noinspection GoUnhandledErrorResult
28-
fmt.Fprintln(w, stdErrStr)
29-
}
30-
err = json.Unmarshal(stdOut.Bytes(), &issueInfo)
31-
return issueInfo, err
10+
type Finder interface {
11+
FindById(w io.Writer, repo string, issue string) (Info, error)
3212
}

0 commit comments

Comments
 (0)