Skip to content

Commit 346a48b

Browse files
committed
Fix noctx lint errors
Replace bare net.Listen, exec.Command, http.Post, and http.NewRequest calls with their context-aware equivalents so the noctx linter passes.
1 parent f84750c commit 346a48b

File tree

6 files changed

+15
-8
lines changed

6 files changed

+15
-8
lines changed

cmd/dependabot/dependabot_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,14 @@ import (
1212
)
1313

1414
func TestDependabot(t *testing.T) {
15-
err := exec.Command("go", "build", ".").Run()
15+
ctx := context.Background()
16+
err := exec.CommandContext(ctx, "go", "build", ".").Run()
1617
if err != nil {
1718
t.Fatal("failed to build dependabot")
1819
}
1920
t.Cleanup(func() {
2021
os.Remove("dependabot")
2122
})
22-
23-
ctx := context.Background()
2423
engine := &script.Engine{
2524
Conds: scripttest.DefaultConds(),
2625
Cmds: Commands(),

cmd/dependabot/internal/cmd/update.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ func extractInput(cmd *cobra.Command, flags *UpdateFlags) (*model.Input, error)
169169
}
170170

171171
if hasServer {
172-
l, err := net.Listen("tcp", fmt.Sprintf("127.0.0.1:%d", flags.inputServerPort))
172+
l, err := (&net.ListenConfig{}).Listen(cmd.Context(), "tcp", fmt.Sprintf("127.0.0.1:%d", flags.inputServerPort))
173173
if err != nil {
174174
return nil, fmt.Errorf("failed to create listener: %w", err)
175175
}

cmd/dependabot/internal/cmd/update_test.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cmd
22

33
import (
4+
"context"
45
"fmt"
56
"net/http"
67
"os"
@@ -295,7 +296,12 @@ func Test_extractInput(t *testing.T) {
295296
// Retry the calls in case the server takes a bit to start up.
296297
for i := 0; i < 10; i++ {
297298
body := strings.NewReader(`{"job":{"package-manager":"go_modules"}}`)
298-
resp, err := http.Post("http://127.0.0.1:8080", "application/json", body)
299+
req, err := http.NewRequestWithContext(context.Background(), http.MethodPost, "http://127.0.0.1:8080", body)
300+
if err != nil {
301+
return
302+
}
303+
req.Header.Set("Content-Type", "application/json")
304+
resp, err := http.DefaultClient.Do(req)
299305
if err != nil {
300306
time.Sleep(10 * time.Millisecond)
301307
} else {

internal/server/api.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func NewAPI(expected []model.Output, writer io.Writer) *API {
5656
if os.Getenv("FAKE_API_PORT") != "" {
5757
port = os.Getenv("FAKE_API_PORT")
5858
}
59-
l, err := net.Listen("tcp", fakeAPIHost+":"+port)
59+
l, err := (&net.ListenConfig{}).Listen(context.Background(), "tcp", fakeAPIHost+":"+port)
6060
if err != nil {
6161
panic(err)
6262
}

internal/server/api_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package server
22

33
import (
44
"bytes"
5+
"context"
56
"crypto/sha256"
67
"encoding/base64"
78
"encoding/hex"
@@ -67,7 +68,7 @@ func TestAPI_CreatePullRequest_ReplacesBinaryWithHash(t *testing.T) {
6768

6869
url := "http://127.0.0.1:" + // use the API's port
6970
fmt.Sprintf("%d/create_pull_request", api.Port())
70-
req, err := http.NewRequest("POST", url, &body)
71+
req, err := http.NewRequestWithContext(context.Background(), "POST", url, &body)
7172
if err != nil {
7273
t.Fatalf("failed to create request: %v", err)
7374
}

internal/server/input_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package server
22

33
import (
44
"bytes"
5+
"context"
56
"fmt"
67
"net"
78
"net/http"
@@ -20,7 +21,7 @@ func TestInput(t *testing.T) {
2021
if os.Getenv("GOOS") == "darwin" {
2122
ip = "127.0.0.1"
2223
}
23-
l, err := net.Listen("tcp", ip+":0")
24+
l, err := (&net.ListenConfig{}).Listen(context.Background(), "tcp", ip+":0")
2425
if err != nil {
2526
t.Fatal("Failed to create listener: ", err.Error())
2627
}

0 commit comments

Comments
 (0)