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.
This commit is contained in:
google-labs-jules[bot] 2025-11-02 18:46:09 +00:00
parent 7adfff1d0d
commit a8dc55aba5
3 changed files with 28 additions and 7 deletions

View file

@ -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

View file

@ -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),
},
}

View file

@ -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,