From a8dc55aba5eb4028bc42b832427ed72470c2b7ce Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Sun, 2 Nov 2025 18:46:09 +0000 Subject: [PATCH] fix: Correct mock data and test environment handling This commit addresses several issues in the testing framework: - Corrected a malformed URL in the mock response data in `pkg/github/github.go`. - Fixed a bug in the mock HTTP client in `pkg/mocks/http.go` where the response body was being exhausted after the first read. The client now returns a deep copy of the response for each call. - Refactored the environment variable handling in `TDD/collect_commands_test.go` to use `t.Setenv`, ensuring proper cleanup after the test. --- TDD/collect_commands_test.go | 4 +--- pkg/github/github.go | 2 +- pkg/mocks/http.go | 29 ++++++++++++++++++++++++++--- 3 files changed, 28 insertions(+), 7 deletions(-) diff --git a/TDD/collect_commands_test.go b/TDD/collect_commands_test.go index 2044a1a..156408f 100644 --- a/TDD/collect_commands_test.go +++ b/TDD/collect_commands_test.go @@ -4,14 +4,12 @@ import ( "bytes" "context" "github.com/Snider/Borg/cmd" - "os" "strings" "testing" ) func TestCollectCommands(t *testing.T) { - os.Setenv("BORG_PLEXSUS", "0") - defer os.Unsetenv("BORG_PLEXSUS") + t.Setenv("BORG_PLEXSUS", "0") testCases := []struct { name string diff --git a/pkg/github/github.go b/pkg/github/github.go index a590205..51aeb0d 100644 --- a/pkg/github/github.go +++ b/pkg/github/github.go @@ -33,7 +33,7 @@ func newAuthenticatedClient(ctx context.Context) *http.Client { }, "https://api.github.com/orgs/test/repos": { StatusCode: http.StatusOK, - Body: io.NopCloser(bytes.NewBufferString(`[{"clone_url": "https.github.com/test/repo2.git"}]`)), + Body: io.NopCloser(bytes.NewBufferString(`[{"clone_url": "https://github.com/test/repo2.git"}]`)), Header: make(http.Header), }, } diff --git a/pkg/mocks/http.go b/pkg/mocks/http.go index d8b1a99..29ba838 100644 --- a/pkg/mocks/http.go +++ b/pkg/mocks/http.go @@ -15,11 +15,34 @@ type MockRoundTripper struct { func (m *MockRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) { url := req.URL.String() if resp, ok := m.Responses[url]; ok { - // Create a new reader for the body each time, as it can be read only once. - bodyBytes, _ := io.ReadAll(resp.Body) + // Read the original body + bodyBytes, err := io.ReadAll(resp.Body) + if err != nil { + return nil, err + } resp.Body.Close() // close original body + + // Re-hydrate the original body so it can be read again resp.Body = io.NopCloser(bytes.NewReader(bodyBytes)) - return resp, nil + + // Create a deep copy of the response + newResp := &http.Response{ + Status: resp.Status, + StatusCode: resp.StatusCode, + Proto: resp.Proto, + ProtoMajor: resp.ProtoMajor, + ProtoMinor: resp.ProtoMinor, + Header: resp.Header.Clone(), + Body: io.NopCloser(bytes.NewReader(bodyBytes)), + ContentLength: resp.ContentLength, + TransferEncoding: resp.TransferEncoding, + Close: resp.Close, + Uncompressed: resp.Uncompressed, + Trailer: resp.Trailer.Clone(), + Request: resp.Request, + TLS: resp.TLS, + } + return newResp, nil } return &http.Response{ StatusCode: http.StatusNotFound,