@@ -12,9 +12,8 @@ import (
1212)
1313
1414type TestStore struct {
15- repositories map [string ]gitea.Repository
1615 projects map [string ]gitea.Organization
17- projects2repos map [string ][] * gitea.Repository
16+ projects2repos map [string ]map [ string ] gitea.Repository
1817 teams map [int64 ][]string
1918}
2019
@@ -26,9 +25,8 @@ type testGiteaClient struct {
2625
2726func NewTestStore () * TestStore {
2827 return & TestStore {
29- repositories : make (map [string ]gitea.Repository ),
3028 projects : make (map [string ]gitea.Organization ),
31- projects2repos : make (map [string ][] * gitea.Repository ),
29+ projects2repos : make (map [string ]map [ string ] gitea.Repository ),
3230 teams : make (map [int64 ][]string ),
3331 }
3432}
@@ -55,15 +53,14 @@ func (tc testGiteaClient) AddRepoTopic(string, string, string) (*gitea.Response,
5553}
5654func (tc testGiteaClient ) GetOrg (orgname string ) (* gitea.Organization , * gitea.Response , error ) {
5755 if org , ok := tc .testStore .projects [orgname ]; ok {
58- httpResp := http.Response {StatusCode : 200 }
59- return & org , & gitea.Response {Response : & httpResp }, nil
56+ return & org , & gitea.Response {Response : & httpResp200 }, nil
6057 }
6158 return nil , nil , nil
6259}
6360func (tc testGiteaClient ) CreateOrg (opt gitea.CreateOrgOption ) (* gitea.Organization , * gitea.Response , error ) {
6461 org := gitea.Organization {UserName : opt .Name }
6562 tc .testStore .projects [opt .Name ] = org
66- tc .testStore .projects2repos [opt .Name ] = make ([] * gitea.Repository , 0 )
63+ tc .testStore .projects2repos [opt .Name ] = make (map [ string ] gitea.Repository )
6764 return & org , nil , nil
6865}
6966func (tc testGiteaClient ) GetUserInfo (string ) (* gitea.User , * gitea.Response , error ) {
@@ -82,26 +79,26 @@ func (tc testGiteaClient) AddTeamMember(id int64, username string) (*gitea.Respo
8279 return nil , nil
8380}
8481func (tc testGiteaClient ) GetRepo (owner , reponame string ) (* gitea.Repository , * gitea.Response , error ) {
85- if repo , ok := tc .testStore .repositories [reponame ]; ok {
86- return & repo , nil , nil
82+ if repo , ok := tc .testStore .projects2repos [ owner ] [reponame ]; ok {
83+ return & repo , & gitea. Response { Response : & httpResp200 } , nil
8784 }
88- return nil , nil , nil
89-
85+ return nil , & gitea.Response {Response : & httpResp404 }, nil
9086}
9187func (tc testGiteaClient ) CreateOrgRepo (org string , repo gitea.CreateRepoOption ) (* gitea.Repository , * gitea.Response , error ) {
9288 r := gitea.Repository {Name : repo .Name }
93- tc .testStore .repositories [repo .Name ] = r
94- tc .testStore .projects2repos [org ] = append (tc .testStore .projects2repos [org ], & r )
89+ tc .testStore .projects2repos [org ][repo .Name ] = r
9590 return & r , nil , nil
9691}
9792func (tc testGiteaClient ) ListRepoHooks (string , string , gitea.ListHooksOptions ) ([]* gitea.Hook , * gitea.Response , error ) {
9893 return nil , nil , nil
9994}
10095func (tc testGiteaClient ) ListOrgRepos (org string , opt gitea.ListOrgReposOptions ) ([]* gitea.Repository , * gitea.Response , error ) {
101- if repos , ok := tc .testStore .projects2repos [org ]; ok {
102- return repos , nil , nil
96+ repos := make ([]* gitea.Repository , 0 )
97+ for _ , repo := range tc .testStore .projects2repos [org ] {
98+ r := repo
99+ repos = append (repos , & r )
103100 }
104- return nil , nil , nil
101+ return repos , nil , nil
105102}
106103
107104func (tc testGiteaClient ) CreateRepoHook (string , string , gitea.CreateHookOption ) (* gitea.Hook , * gitea.Response , error ) {
@@ -113,18 +110,26 @@ func (tc testGiteaClient) ListRepoTopics(org, repo string, opt gitea.ListRepoTop
113110func (tc testGiteaClient ) ListMyOrgs (gitea.ListOrgsOptions ) ([]* gitea.Organization , * gitea.Response , error ) {
114111 allOrgs := make ([]* gitea.Organization , 0 )
115112 for _ , org := range tc .testStore .projects {
116- allOrgs = append (allOrgs , & org )
113+ o := org
114+ allOrgs = append (allOrgs , & o )
117115 }
118116 return allOrgs , nil , nil
119117}
120118
119+ func (tc testGiteaClient ) DeleteRepo (owner , repo string ) (* gitea.Response , error ) {
120+ delete (tc .testStore .projects2repos [owner ], repo )
121+ return nil , nil
122+ }
123+
121124var (
122125 project1 = "test-project1"
123126 project2 = "test-project2"
124127 name = "test"
125128 testURL = "http://gitea.example.io"
126129 testListenerStringURL = "tekton-listener"
127130 testListenerURL = & testListenerStringURL
131+ httpResp200 = http.Response {StatusCode : 200 }
132+ httpResp404 = http.Response {StatusCode : 404 }
128133)
129134
130135func getTestCodeset () * domain.Codeset {
@@ -205,6 +210,40 @@ func TestGetRepository(t *testing.T) {
205210 }
206211}
207212
213+ func TestDeleteRepository (t * testing.T ) {
214+
215+ testGiteaAdminClient := newTestGiteaAdminClient (NewTestStore ())
216+
217+ // Reading repo that was not added should throw error
218+ _ , err := testGiteaAdminClient .GetRepository (project1 , name )
219+
220+ assertError (t , err , errRepoNotFound )
221+
222+ // Prepare new repo
223+ testGiteaAdminClient .PrepareRepository (getTestCodeset (), testListenerURL )
224+
225+ // Get the repo now
226+ _ , err = testGiteaAdminClient .GetRepository (project1 , name )
227+ if err != nil {
228+ t .Errorf ("Error geting repository that was just created" )
229+ }
230+
231+ err = testGiteaAdminClient .DeleteRepository (project1 , name )
232+ if err != nil {
233+ t .Errorf ("Error deleting repository" )
234+ }
235+
236+ c , err := testGiteaAdminClient .GetRepository (project1 , name )
237+ if c != nil {
238+ t .Errorf ("Repository still present after deleting" )
239+ }
240+
241+ err = testGiteaAdminClient .DeleteRepository (project1 , name )
242+ if err != nil {
243+ t .Errorf ("Error: deleting non existent repository should not fail" )
244+ }
245+ }
246+
208247func TestGetRepositories (t * testing.T ) {
209248
210249 testGiteaAdminClient := newTestGiteaAdminClient (NewTestStore ())
0 commit comments