@@ -26,15 +26,181 @@ func TestGitHubRepo_notFound(t *testing.T) {
2626
2727 client := newTestClient (httpReg )
2828 repo , err := GitHubRepo (client , ghrepo .New ("OWNER" , "REPO" ))
29- if err == nil {
30- t .Fatal ("GitHubRepo did not return an error" )
31- }
32- if wants := "GraphQL: Could not resolve to a Repository with the name 'OWNER/REPO'." ; err .Error () != wants {
33- t .Errorf ("GitHubRepo error: want %q, got %q" , wants , err .Error ())
34- }
35- if repo != nil {
36- t .Errorf ("GitHubRepo: expected nil repo, got %v" , repo )
29+ require .EqualError (t , err , "GraphQL: Could not resolve to a Repository with the name 'OWNER/REPO'." )
30+ assert .Nil (t , repo )
31+ }
32+
33+ func TestGitHubRepo_success (t * testing.T ) {
34+ httpReg := & httpmock.Registry {}
35+ defer httpReg .Verify (t )
36+
37+ httpReg .Register (
38+ httpmock .GraphQL (`query RepositoryInfo\b` ),
39+ httpmock .StringResponse (`
40+ { "data": { "repository": {
41+ "id": "REPOID",
42+ "name": "REPO",
43+ "owner": {"login": "OWNER"},
44+ "hasIssuesEnabled": true,
45+ "description": "a cool repo",
46+ "hasWikiEnabled": true,
47+ "viewerPermission": "ADMIN",
48+ "defaultBranchRef": {"name": "main"},
49+ "parent": null,
50+ "mergeCommitAllowed": true,
51+ "rebaseMergeAllowed": true,
52+ "squashMergeAllowed": false
53+ } } }` ))
54+
55+ client := newTestClient (httpReg )
56+ repo , err := GitHubRepo (client , ghrepo .New ("OWNER" , "REPO" ))
57+ require .NoError (t , err )
58+ assert .Equal (t , & Repository {
59+ ID : "REPOID" ,
60+ Name : "REPO" ,
61+ Owner : RepositoryOwner {Login : "OWNER" },
62+ HasIssuesEnabled : true ,
63+ Description : "a cool repo" ,
64+ HasWikiEnabled : true ,
65+ ViewerPermission : "ADMIN" ,
66+ DefaultBranchRef : BranchRef {Name : "main" },
67+ MergeCommitAllowed : true ,
68+ RebaseMergeAllowed : true ,
69+ hostname : "github.com" ,
70+ }, repo )
71+ assert .True (t , repo .ViewerCanPush ())
72+ assert .True (t , repo .ViewerCanTriage ())
73+ }
74+
75+ func TestGitHubRepo_withParent (t * testing.T ) {
76+ httpReg := & httpmock.Registry {}
77+ defer httpReg .Verify (t )
78+
79+ httpReg .Register (
80+ httpmock .GraphQL (`query RepositoryInfo\b` ),
81+ httpmock .StringResponse (`
82+ { "data": { "repository": {
83+ "id": "REPOID",
84+ "name": "REPO",
85+ "owner": {"login": "OWNER"},
86+ "hasIssuesEnabled": true,
87+ "description": "",
88+ "hasWikiEnabled": false,
89+ "viewerPermission": "READ",
90+ "defaultBranchRef": {"name": "main"},
91+ "parent": {
92+ "id": "PARENTID",
93+ "name": "PARENT-REPO",
94+ "owner": {"login": "PARENT-OWNER"},
95+ "hasIssuesEnabled": true,
96+ "description": "parent repo",
97+ "hasWikiEnabled": true,
98+ "viewerPermission": "READ",
99+ "defaultBranchRef": {"name": "develop"}
100+ },
101+ "mergeCommitAllowed": false,
102+ "rebaseMergeAllowed": false,
103+ "squashMergeAllowed": true
104+ } } }` ))
105+
106+ client := newTestClient (httpReg )
107+ repo , err := GitHubRepo (client , ghrepo .New ("OWNER" , "REPO" ))
108+ require .NoError (t , err )
109+ wantParent := & Repository {
110+ ID : "PARENTID" ,
111+ Name : "PARENT-REPO" ,
112+ Owner : RepositoryOwner {Login : "PARENT-OWNER" },
113+ HasIssuesEnabled : true ,
114+ Description : "parent repo" ,
115+ HasWikiEnabled : true ,
116+ ViewerPermission : "READ" ,
117+ DefaultBranchRef : BranchRef {Name : "develop" },
118+ hostname : "github.com" ,
37119 }
120+ assert .Equal (t , & Repository {
121+ ID : "REPOID" ,
122+ Name : "REPO" ,
123+ Owner : RepositoryOwner {Login : "OWNER" },
124+ HasIssuesEnabled : true ,
125+ ViewerPermission : "READ" ,
126+ DefaultBranchRef : BranchRef {Name : "main" },
127+ Parent : wantParent ,
128+ SquashMergeAllowed : true ,
129+ hostname : "github.com" ,
130+ }, repo )
131+ assert .False (t , repo .ViewerCanPush ())
132+ assert .False (t , repo .ViewerCanTriage ())
133+ }
134+
135+ func TestIssueRepoInfo_notFound (t * testing.T ) {
136+ httpReg := & httpmock.Registry {}
137+ defer httpReg .Verify (t )
138+
139+ httpReg .Register (
140+ httpmock .GraphQL (`query IssueRepositoryInfo\b` ),
141+ httpmock .StringResponse (`{ "data": { "repository": null } }` ))
142+
143+ client := newTestClient (httpReg )
144+ repo , err := IssueRepoInfo (client , ghrepo .New ("OWNER" , "REPO" ))
145+ require .EqualError (t , err , "GraphQL: Could not resolve to a Repository with the name 'OWNER/REPO'." )
146+ assert .Nil (t , repo )
147+ }
148+
149+ func TestIssueRepoInfo_success (t * testing.T ) {
150+ httpReg := & httpmock.Registry {}
151+ defer httpReg .Verify (t )
152+
153+ httpReg .Register (
154+ httpmock .GraphQL (`query IssueRepositoryInfo\b` ),
155+ httpmock .StringResponse (`
156+ { "data": { "repository": {
157+ "id": "REPOID",
158+ "name": "REPO",
159+ "owner": {"login": "OWNER"},
160+ "hasIssuesEnabled": true,
161+ "viewerPermission": "WRITE"
162+ } } }` ))
163+
164+ client := newTestClient (httpReg )
165+ repo , err := IssueRepoInfo (client , ghrepo .New ("OWNER" , "REPO" ))
166+ require .NoError (t , err )
167+ assert .Equal (t , & Repository {
168+ ID : "REPOID" ,
169+ Name : "REPO" ,
170+ Owner : RepositoryOwner {Login : "OWNER" },
171+ HasIssuesEnabled : true ,
172+ ViewerPermission : "WRITE" ,
173+ hostname : "github.com" ,
174+ }, repo )
175+ assert .True (t , repo .ViewerCanTriage ())
176+ }
177+
178+ func TestIssueRepoInfo_issuesDisabled (t * testing.T ) {
179+ httpReg := & httpmock.Registry {}
180+ defer httpReg .Verify (t )
181+
182+ httpReg .Register (
183+ httpmock .GraphQL (`query IssueRepositoryInfo\b` ),
184+ httpmock .StringResponse (`
185+ { "data": { "repository": {
186+ "id": "REPOID",
187+ "name": "REPO",
188+ "owner": {"login": "OWNER"},
189+ "hasIssuesEnabled": false,
190+ "viewerPermission": "READ"
191+ } } }` ))
192+
193+ client := newTestClient (httpReg )
194+ repo , err := IssueRepoInfo (client , ghrepo .New ("OWNER" , "REPO" ))
195+ require .NoError (t , err )
196+ assert .Equal (t , & Repository {
197+ ID : "REPOID" ,
198+ Name : "REPO" ,
199+ Owner : RepositoryOwner {Login : "OWNER" },
200+ ViewerPermission : "READ" ,
201+ hostname : "github.com" ,
202+ }, repo )
203+ assert .False (t , repo .ViewerCanTriage ())
38204}
39205
40206func Test_RepoMetadata (t * testing.T ) {
0 commit comments