MCPcopy Index your code
hub / github.com/coder/coder / TestFetchBranchDiff

Function TestFetchBranchDiff

coderd/externalauth/gitprovider/github_test.go:880–964  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

878}
879
880func TestFetchBranchDiff(t *testing.T) {
881 t.Parallel()
882
883 const smallDiff = "diff --git a/file.go b/file.go\n--- a/file.go\n+++ b/file.go\n@@ -1 +1 @@\n-old\n+new\n"
884
885 t.Run("OK", func(t *testing.T) {
886 t.Parallel()
887
888 srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
889 if strings.Contains(r.URL.Path, "/compare/") {
890 w.Header().Set("Content-Type", "text/plain")
891 _, _ = w.Write([]byte(smallDiff))
892 return
893 }
894 // Repo metadata.
895 w.Header().Set("Content-Type", "application/json")
896 _, _ = w.Write([]byte(`{"default_branch":"main"}`))
897 }))
898 defer srv.Close()
899
900 gp, err := gitprovider.New("github", srv.URL+"/api/v3", srv.Client())
901 require.NoError(t, err)
902
903 require.NotNil(t, gp)
904
905 diff, err := gp.FetchBranchDiff(
906 context.Background(),
907 "test-token",
908 gitprovider.BranchRef{Owner: "org", Repo: "repo", Branch: "feat"},
909 )
910 require.NoError(t, err)
911 assert.Equal(t, smallDiff, diff)
912 })
913
914 t.Run("EmptyDefaultBranch", func(t *testing.T) {
915 t.Parallel()
916
917 srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
918 w.Header().Set("Content-Type", "application/json")
919 _, _ = w.Write([]byte(`{"default_branch":""}`))
920 }))
921 defer srv.Close()
922
923 gp, err := gitprovider.New("github", srv.URL+"/api/v3", srv.Client())
924 require.NoError(t, err)
925
926 require.NotNil(t, gp)
927
928 _, err = gp.FetchBranchDiff(
929 context.Background(),
930 "test-token",
931 gitprovider.BranchRef{Owner: "org", Repo: "repo", Branch: "feat"},
932 )
933 require.Error(t, err)
934 assert.Contains(t, err.Error(), "default branch is empty")
935 })
936
937 t.Run("DiffTooLarge", func(t *testing.T) {

Callers

nothing calls this directly

Calls 11

CloseMethod · 0.95
ClientMethod · 0.95
NewFunction · 0.92
RunMethod · 0.65
SetMethod · 0.65
WriteMethod · 0.65
FetchBranchDiffMethod · 0.65
ContainsMethod · 0.45
HeaderMethod · 0.45
EqualMethod · 0.45
ErrorMethod · 0.45

Tested by

no test coverage detected