tests: use proper context in test + add pull request workflow
This commit is contained in:
@@ -0,0 +1,39 @@
|
|||||||
|
name: Pull Request
|
||||||
|
on:
|
||||||
|
pull_request:
|
||||||
|
branches:
|
||||||
|
- main
|
||||||
|
jobs:
|
||||||
|
test-and-lint:
|
||||||
|
name: Test, Format & Lint
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- name: Checkout code
|
||||||
|
uses: actions/checkout@v6
|
||||||
|
- name: Set up Go
|
||||||
|
uses: actions/setup-go@v6
|
||||||
|
with:
|
||||||
|
go-version: "1.25"
|
||||||
|
cache: true
|
||||||
|
- name: Check formatting
|
||||||
|
run: |
|
||||||
|
# Fails if any files are not formatted correctly
|
||||||
|
if [ -n "$(gofmt -l .)" ]; then
|
||||||
|
echo "Go code is not formatted:"
|
||||||
|
gofmt -d .
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
- name: Run go vet
|
||||||
|
run: go vet ./...
|
||||||
|
- name: Run linter
|
||||||
|
uses: golangci/golangci-lint-action@v9
|
||||||
|
with:
|
||||||
|
version: latest
|
||||||
|
- name: Run tests
|
||||||
|
run: go test -v ./... -coverprofile=coverage.out
|
||||||
|
- name Generate coverage report
|
||||||
|
run: go tool cover -html=coverage.out -o coverage.html
|
||||||
|
- name: Upload test coverage
|
||||||
|
uses: actions/upload-artifact@v6
|
||||||
|
with:
|
||||||
|
name: coverage.html
|
||||||
@@ -143,7 +143,7 @@ func (c *Client) handleAPIError(resp *http.Response) error {
|
|||||||
// Returns:
|
// Returns:
|
||||||
// - *http.Request: The prepared HTTP request
|
// - *http.Request: The prepared HTTP request
|
||||||
// - error: An error if the context is nil or request creation fails
|
// - error: An error if the context is nil or request creation fails
|
||||||
func (c *Client) newRequest(ctx context.Context, method, path string, body any) (*http.Request, error) {
|
func (c *Client) newRequest(ctx context.Context, method, path string, _ any) (*http.Request, error) {
|
||||||
if ctx == nil {
|
if ctx == nil {
|
||||||
return nil, fmt.Errorf("context must not be nil")
|
return nil, fmt.Errorf("context must not be nil")
|
||||||
}
|
}
|
||||||
|
|||||||
+8
-6
@@ -14,7 +14,9 @@ func TestGet(t *testing.T) {
|
|||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
})
|
})
|
||||||
|
|
||||||
err := client.Ping.Get(nil)
|
var nilContext context.Context = nil
|
||||||
|
|
||||||
|
err := client.Ping.Get(nilContext)
|
||||||
|
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Errorf("Expected error for nil context, got nil")
|
t.Errorf("Expected error for nil context, got nil")
|
||||||
@@ -26,7 +28,7 @@ func TestGet(t *testing.T) {
|
|||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
})
|
})
|
||||||
|
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
ctx, cancel := context.WithCancel(t.Context())
|
||||||
|
|
||||||
cancel()
|
cancel()
|
||||||
|
|
||||||
@@ -44,7 +46,7 @@ func TestGet(t *testing.T) {
|
|||||||
onspring.WithHTTPClient(&http.Client{Transport: &ErrorTransport{}}),
|
onspring.WithHTTPClient(&http.Client{Transport: &ErrorTransport{}}),
|
||||||
)
|
)
|
||||||
|
|
||||||
err := client.Ping.Get(context.Background())
|
err := client.Ping.Get(t.Context())
|
||||||
|
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Errorf("Expected network error, got nil")
|
t.Errorf("Expected network error, got nil")
|
||||||
@@ -62,7 +64,7 @@ func TestGet(t *testing.T) {
|
|||||||
onspring.WithHTTPClient(client.HTTPClient()),
|
onspring.WithHTTPClient(client.HTTPClient()),
|
||||||
)
|
)
|
||||||
|
|
||||||
err := invalidClient.Ping.Get(context.Background())
|
err := invalidClient.Ping.Get(t.Context())
|
||||||
|
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Errorf("Expected request creation error, got nil")
|
t.Errorf("Expected request creation error, got nil")
|
||||||
@@ -82,7 +84,7 @@ func TestGet(t *testing.T) {
|
|||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
})
|
})
|
||||||
|
|
||||||
err := client.Ping.Get(context.Background())
|
err := client.Ping.Get(t.Context())
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Expected no error, got %v", err)
|
t.Errorf("Expected no error, got %v", err)
|
||||||
@@ -94,7 +96,7 @@ func TestGet(t *testing.T) {
|
|||||||
w.WriteHeader(http.StatusInternalServerError)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
})
|
})
|
||||||
|
|
||||||
err := client.Ping.Get(context.Background())
|
err := client.Ping.Get(t.Context())
|
||||||
|
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Errorf("Expected error, got nil")
|
t.Errorf("Expected error, got nil")
|
||||||
|
|||||||
Reference in New Issue
Block a user