This commit introduces a TDD testing framework for the `collect` commands. - A `TDD/` directory has been added to house the tests. - An environment variable `BORG_PLEXSUS=0` has been implemented to enable a mock mode, which prevents external network calls during testing. - The `collect` commands have been updated to use the command's output streams, allowing for output capturing in tests. - A `pkg/mocks` package has been added to provide mock implementations for testing. - The `.gitignore` file has been updated to exclude generated `.datanode` files.
38 lines
1 KiB
Go
38 lines
1 KiB
Go
package mocks
|
|
|
|
import (
|
|
"bytes"
|
|
"io"
|
|
"net/http"
|
|
)
|
|
|
|
// MockRoundTripper is a mock implementation of http.RoundTripper.
|
|
type MockRoundTripper struct {
|
|
Responses map[string]*http.Response
|
|
}
|
|
|
|
// RoundTrip implements the http.RoundTripper interface.
|
|
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)
|
|
resp.Body.Close() // close original body
|
|
resp.Body = io.NopCloser(bytes.NewReader(bodyBytes))
|
|
return resp, nil
|
|
}
|
|
return &http.Response{
|
|
StatusCode: http.StatusNotFound,
|
|
Body: io.NopCloser(bytes.NewBufferString("Not Found")),
|
|
Header: make(http.Header),
|
|
}, nil
|
|
}
|
|
|
|
// NewMockClient creates a new http.Client with a MockRoundTripper.
|
|
func NewMockClient(responses map[string]*http.Response) *http.Client {
|
|
return &http.Client{
|
|
Transport: &MockRoundTripper{
|
|
Responses: responses,
|
|
},
|
|
}
|
|
}
|