|
| 1 | +package github |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + forge "github.com/git-pkgs/forge" |
| 7 | + "net/http" |
| 8 | + "net/http/httptest" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/google/go-github/v82/github" |
| 12 | +) |
| 13 | + |
| 14 | +func TestGitHubListForks(t *testing.T) { |
| 15 | + mux := http.NewServeMux() |
| 16 | + mux.HandleFunc("GET /api/v3/repos/octocat/hello-world/forks", func(w http.ResponseWriter, r *http.Request) { |
| 17 | + _ = json.NewEncoder(w).Encode([]*github.Repository{ |
| 18 | + {FullName: ptr("alice/hello-world"), Name: ptr("hello-world"), Fork: ptrBool(true), Owner: &github.User{Login: ptr("alice")}}, |
| 19 | + {FullName: ptr("bob/hello-world"), Name: ptr("hello-world"), Fork: ptrBool(true), Owner: &github.User{Login: ptr("bob")}}, |
| 20 | + }) |
| 21 | + }) |
| 22 | + |
| 23 | + srv := httptest.NewServer(mux) |
| 24 | + defer srv.Close() |
| 25 | + |
| 26 | + c := github.NewClient(nil) |
| 27 | + c, _ = c.WithEnterpriseURLs(srv.URL+"/api/v3", srv.URL+"/api/v3") |
| 28 | + s := &gitHubRepoService{client: c} |
| 29 | + |
| 30 | + forks, err := s.ListForks(context.Background(), "octocat", "hello-world", forge.ListForksOpts{}) |
| 31 | + if err != nil { |
| 32 | + t.Fatalf("unexpected error: %v", err) |
| 33 | + } |
| 34 | + if len(forks) != 2 { |
| 35 | + t.Fatalf("expected 2 forks, got %d", len(forks)) |
| 36 | + } |
| 37 | + assertEqual(t, "forks[0].FullName", "alice/hello-world", forks[0].FullName) |
| 38 | + assertEqual(t, "forks[1].FullName", "bob/hello-world", forks[1].FullName) |
| 39 | +} |
| 40 | + |
| 41 | +func TestGitHubListForksNotFound(t *testing.T) { |
| 42 | + mux := http.NewServeMux() |
| 43 | + mux.HandleFunc("GET /api/v3/repos/octocat/nope/forks", func(w http.ResponseWriter, r *http.Request) { |
| 44 | + w.WriteHeader(http.StatusNotFound) |
| 45 | + _ = json.NewEncoder(w).Encode(map[string]string{"message": "Not Found"}) |
| 46 | + }) |
| 47 | + |
| 48 | + srv := httptest.NewServer(mux) |
| 49 | + defer srv.Close() |
| 50 | + |
| 51 | + c := github.NewClient(nil) |
| 52 | + c, _ = c.WithEnterpriseURLs(srv.URL+"/api/v3", srv.URL+"/api/v3") |
| 53 | + s := &gitHubRepoService{client: c} |
| 54 | + |
| 55 | + _, err := s.ListForks(context.Background(), "octocat", "nope", forge.ListForksOpts{}) |
| 56 | + if err != forge.ErrNotFound { |
| 57 | + t.Fatalf("expected forge.ErrNotFound, got %v", err) |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +func TestGitHubListForksWithLimit(t *testing.T) { |
| 62 | + mux := http.NewServeMux() |
| 63 | + mux.HandleFunc("GET /api/v3/repos/octocat/hello-world/forks", func(w http.ResponseWriter, r *http.Request) { |
| 64 | + _ = json.NewEncoder(w).Encode([]*github.Repository{ |
| 65 | + {FullName: ptr("alice/hello-world"), Name: ptr("hello-world"), Fork: ptrBool(true), Owner: &github.User{Login: ptr("alice")}}, |
| 66 | + {FullName: ptr("bob/hello-world"), Name: ptr("hello-world"), Fork: ptrBool(true), Owner: &github.User{Login: ptr("bob")}}, |
| 67 | + {FullName: ptr("carol/hello-world"), Name: ptr("hello-world"), Fork: ptrBool(true), Owner: &github.User{Login: ptr("carol")}}, |
| 68 | + }) |
| 69 | + }) |
| 70 | + |
| 71 | + srv := httptest.NewServer(mux) |
| 72 | + defer srv.Close() |
| 73 | + |
| 74 | + c := github.NewClient(nil) |
| 75 | + c, _ = c.WithEnterpriseURLs(srv.URL+"/api/v3", srv.URL+"/api/v3") |
| 76 | + s := &gitHubRepoService{client: c} |
| 77 | + |
| 78 | + forks, err := s.ListForks(context.Background(), "octocat", "hello-world", forge.ListForksOpts{Limit: 2}) |
| 79 | + if err != nil { |
| 80 | + t.Fatalf("unexpected error: %v", err) |
| 81 | + } |
| 82 | + if len(forks) != 2 { |
| 83 | + t.Fatalf("expected 2 forks (limit), got %d", len(forks)) |
| 84 | + } |
| 85 | +} |
0 commit comments