Skip to content

Commit 8ad0b44

Browse files
committed
chore: update tests
1 parent be87d66 commit 8ad0b44

File tree

3 files changed

+203
-102
lines changed

3 files changed

+203
-102
lines changed

pkg/golinters/noctx/noctx.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ func New() *goanalysis.Linter {
1212

1313
return goanalysis.NewLinter(
1414
a.Name,
15-
"Finds sending http request without context.Context",
15+
"Detects function and method with missing usage of context.Context",
1616
[]*analysis.Analyzer{a},
1717
nil,
1818
).WithLoadMode(goanalysis.LoadModeTypesInfo)

pkg/golinters/noctx/testdata/noctx.go

+101-51
Original file line numberDiff line numberDiff line change
@@ -3,132 +3,182 @@ package testdata
33

44
import (
55
"context"
6+
"database/sql"
67
"net/http"
78
)
89

910
var newRequestPkg = http.NewRequest
1011

11-
func Noctx() {
12-
const url = "http://example.com"
12+
func _() {
13+
const url = "https://example.com"
14+
1315
cli := &http.Client{}
1416

1517
ctx := context.Background()
16-
http.Get(url) // want `net/http\.Get must not be called`
17-
_ = http.Get // OK
18-
f := http.Get // OK
19-
f(url) // want `net/http\.Get must not be called`
20-
21-
http.Head(url) // want `net/http\.Head must not be called`
22-
http.Post(url, "", nil) // want `net/http\.Post must not be called`
23-
http.PostForm(url, nil) // want `net/http\.PostForm must not be called`
24-
25-
cli.Get(url) // want `\(\*net/http\.Client\)\.Get must not be called`
26-
_ = cli.Get // OK
27-
m := cli.Get // OK
28-
m(url) // want `\(\*net/http\.Client\)\.Get must not be called`
29-
30-
cli.Head(url) // want `\(\*net/http\.Client\)\.Head must not be called`
31-
cli.Post(url, "", nil) // want `\(\*net/http\.Client\)\.Post must not be called`
32-
cli.PostForm(url, nil) // want `\(\*net/http\.Client\)\.PostForm must not be called`
3318

34-
req, _ := http.NewRequest(http.MethodPost, url, nil) // want `should rewrite http.NewRequestWithContext or add \(\*Request\).WithContext`
19+
req, _ := http.NewRequest(http.MethodPost, url, nil) // want `net/http\.NewRequest must not be called. use net/http\.NewRequestWithContext`
3520
cli.Do(req)
3621

3722
req2, _ := http.NewRequestWithContext(ctx, http.MethodPost, url, nil) // OK
3823
cli.Do(req2)
3924

40-
req3, _ := http.NewRequest(http.MethodPost, url, nil) // OK
41-
req3 = req3.WithContext(ctx)
25+
req3, _ := http.NewRequest(http.MethodPost, url, nil) // want `net/http\.NewRequest must not be called. use net/http\.NewRequestWithContext`
26+
req3 = req3.WithContext(ctx) // want `\(\*net/http\.Request\)\.WithContext must not be called. use net/http\.NewRequestWithContext`
4227
cli.Do(req3)
4328

4429
f2 := func(req *http.Request, ctx context.Context) *http.Request {
4530
return req
4631
}
47-
req4, _ := http.NewRequest(http.MethodPost, url, nil) // want `should rewrite http.NewRequestWithContext or add \(\*Request\).WithContext`
32+
req4, _ := http.NewRequest(http.MethodPost, url, nil) // want `net/http\.NewRequest must not be called. use net/http\.NewRequestWithContext`
4833
req4 = f2(req4, ctx)
4934

50-
req41, _ := http.NewRequest(http.MethodPost, url, nil) // OK
51-
req41 = req41.WithContext(ctx)
35+
req41, _ := http.NewRequest(http.MethodPost, url, nil) // want `net/http\.NewRequest must not be called. use net/http\.NewRequestWithContext`
36+
req41 = req41.WithContext(ctx) // want `\(\*net/http\.Request\)\.WithContext must not be called. use net/http\.NewRequestWithContext`
5237
req41 = f2(req41, ctx)
5338

5439
newRequest := http.NewRequest
55-
req5, _ := newRequest(http.MethodPost, url, nil) // want `should rewrite http.NewRequestWithContext or add \(\*Request\).WithContext`
40+
req5, _ := newRequest(http.MethodPost, url, nil) // want `net/http\.NewRequest must not be called. use net/http\.NewRequestWithContext`
5641
cli.Do(req5)
5742

58-
req51, _ := newRequest(http.MethodPost, url, nil) // OK
59-
req51 = req51.WithContext(ctx)
43+
req51, _ := newRequest(http.MethodPost, url, nil) // want `net/http\.NewRequest must not be called. use net/http\.NewRequestWithContext`
44+
req51 = req51.WithContext(ctx) // want `\(\*net/http\.Request\)\.WithContext must not be called. use net/http\.NewRequestWithContext`
6045
cli.Do(req51)
6146

62-
req52, _ := newRequestPkg(http.MethodPost, url, nil) // want `should rewrite http.NewRequestWithContext or add \(\*Request\).WithContext`
47+
req52, _ := newRequestPkg(http.MethodPost, url, nil) // TODO: false negative `net/http\.NewRequest must not be called. use net/http\.NewRequestWithContext`
6348
cli.Do(req52)
6449

6550
type MyRequest = http.Request
6651
f3 := func(req *MyRequest, ctx context.Context) *MyRequest {
6752
return req
6853
}
69-
req6, _ := http.NewRequest(http.MethodPost, url, nil) // want `should rewrite http.NewRequestWithContext or add \(\*Request\).WithContext`
54+
req6, _ := http.NewRequest(http.MethodPost, url, nil) // want `net/http\.NewRequest must not be called. use net/http\.NewRequestWithContext`
7055
req6 = f3(req6, ctx)
7156

72-
req61, _ := http.NewRequest(http.MethodPost, url, nil) // OK
73-
req61 = req61.WithContext(ctx)
57+
req61, _ := http.NewRequest(http.MethodPost, url, nil) // want `net/http\.NewRequest must not be called. use net/http\.NewRequestWithContext`
58+
req61 = req61.WithContext(ctx) // want `\(\*net/http\.Request\)\.WithContext must not be called. use net/http\.NewRequestWithContext`
7459
req61 = f3(req61, ctx)
7560

7661
type MyRequest2 http.Request
7762
f4 := func(req *MyRequest2, ctx context.Context) *MyRequest2 {
7863
return req
7964
}
80-
req7, _ := http.NewRequest(http.MethodPost, url, nil) // want `should rewrite http.NewRequestWithContext or add \(\*Request\).WithContext`
65+
req7, _ := http.NewRequest(http.MethodPost, url, nil) // want `net/http\.NewRequest must not be called. use net/http\.NewRequestWithContext`
8166
req71 := MyRequest2(*req7)
8267
f4(&req71, ctx)
8368

84-
req72, _ := http.NewRequest(http.MethodPost, url, nil) // OK
85-
req72 = req72.WithContext(ctx)
69+
req72, _ := http.NewRequest(http.MethodPost, url, nil) // want `net/http\.NewRequest must not be called. use net/http\.NewRequestWithContext`
70+
req72 = req72.WithContext(ctx) // want `\(\*net/http\.Request\)\.WithContext must not be called. use net/http\.NewRequestWithContext`
8671
req73 := MyRequest2(*req7)
8772
f4(&req73, ctx)
8873

8974
req8, _ := func() (*http.Request, error) {
90-
return http.NewRequest(http.MethodPost, url, nil) // want `should rewrite http.NewRequestWithContext or add \(\*Request\).WithContext`
75+
return http.NewRequest(http.MethodPost, url, nil) // want `net/http\.NewRequest must not be called. use net/http\.NewRequestWithContext`
9176
}()
9277
cli.Do(req8)
9378

9479
req82, _ := func() (*http.Request, error) {
95-
req82, _ := http.NewRequest(http.MethodPost, url, nil) // OK
96-
req82 = req82.WithContext(ctx)
80+
req82, _ := http.NewRequest(http.MethodPost, url, nil) // want `net/http\.NewRequest must not be called. use net/http\.NewRequestWithContext`
81+
req82 = req82.WithContext(ctx) // want `\(\*net/http\.Request\)\.WithContext must not be called. use net/http\.NewRequestWithContext`
9782
return req82, nil
9883
}()
9984
cli.Do(req82)
10085

10186
f5 := func(req, req2 *http.Request, ctx context.Context) (*http.Request, *http.Request) {
10287
return req, req2
10388
}
104-
req9, _ := http.NewRequest(http.MethodPost, url, nil) // want `should rewrite http.NewRequestWithContext or add \(\*Request\).WithContext`
89+
req9, _ := http.NewRequest(http.MethodPost, url, nil) // want `net/http\.NewRequest must not be called. use net/http\.NewRequestWithContext`
10590
req9, _ = f5(req9, req9, ctx)
10691

107-
req91, _ := http.NewRequest(http.MethodPost, url, nil) // OK
108-
req91 = req91.WithContext(ctx)
92+
req91, _ := http.NewRequest(http.MethodPost, url, nil) // want `net/http\.NewRequest must not be called. use net/http\.NewRequestWithContext`
93+
req91 = req91.WithContext(ctx) // want `\(\*net/http\.Request\)\.WithContext must not be called. use net/http\.NewRequestWithContext`
10994
req9, _ = f5(req91, req91, ctx)
11095

111-
req10, _ := http.NewRequest(http.MethodPost, url, nil) // want `should rewrite http.NewRequestWithContext or add \(\*Request\).WithContext`
112-
req11, _ := http.NewRequest(http.MethodPost, url, nil) // want `should rewrite http.NewRequestWithContext or add \(\*Request\).WithContext`
96+
req10, _ := http.NewRequest(http.MethodPost, url, nil) // want `net/http\.NewRequest must not be called. use net/http\.NewRequestWithContext`
97+
req11, _ := http.NewRequest(http.MethodPost, url, nil) // want `net/http\.NewRequest must not be called. use net/http\.NewRequestWithContext`
11398
req10, req11 = f5(req10, req11, ctx)
11499

115-
req101, _ := http.NewRequest(http.MethodPost, url, nil) // want `should rewrite http.NewRequestWithContext or add \(\*Request\).WithContext`
116-
req111, _ := http.NewRequest(http.MethodPost, url, nil) // OK
117-
req111 = req111.WithContext(ctx)
100+
req101, _ := http.NewRequest(http.MethodPost, url, nil) // want `net/http\.NewRequest must not be called. use net/http\.NewRequestWithContext`
101+
req111, _ := http.NewRequest(http.MethodPost, url, nil) // want `net/http\.NewRequest must not be called. use net/http\.NewRequestWithContext`
102+
req111 = req111.WithContext(ctx) // want `\(\*net/http\.Request\)\.WithContext must not be called. use net/http\.NewRequestWithContext`
118103
req101, req111 = f5(req101, req111, ctx)
119104

120105
func() (*http.Request, *http.Request) {
121-
req12, _ := http.NewRequest(http.MethodPost, url, nil) // want `should rewrite http.NewRequestWithContext or add \(\*Request\).WithContext`
122-
req13, _ := http.NewRequest(http.MethodPost, url, nil) // want `should rewrite http.NewRequestWithContext or add \(\*Request\).WithContext`
106+
req12, _ := http.NewRequest(http.MethodPost, url, nil) // want `net/http\.NewRequest must not be called. use net/http\.NewRequestWithContext`
107+
req13, _ := http.NewRequest(http.MethodPost, url, nil) // want `net/http\.NewRequest must not be called. use net/http\.NewRequestWithContext`
123108
return req12, req13
124109
}()
125110

126111
func() (*http.Request, *http.Request) {
127-
req14, _ := http.NewRequest(http.MethodPost, url, nil) // want `should rewrite http.NewRequestWithContext or add \(\*Request\).WithContext`
128-
req15, _ := http.NewRequest(http.MethodPost, url, nil) // OK
129-
req15 = req15.WithContext(ctx)
112+
req14, _ := http.NewRequest(http.MethodPost, url, nil) // want `net/http\.NewRequest must not be called. use net/http\.NewRequestWithContext`
113+
req15, _ := http.NewRequest(http.MethodPost, url, nil) // want `net/http\.NewRequest must not be called. use net/http\.NewRequestWithContext`
114+
req15 = req15.WithContext(ctx) // want `\(\*net/http\.Request\)\.WithContext must not be called. use net/http\.NewRequestWithContext`
130115

131116
return req14, req15
132117
}()
118+
119+
req121, _ := http.NewRequest(http.MethodPost, url, nil) // want `net/http\.NewRequest must not be called. use net/http\.NewRequestWithContext`
120+
req121.AddCookie(&http.Cookie{Name: "k", Value: "v"})
121+
req121 = req121.WithContext(context.WithValue(req121.Context(), struct{}{}, 0)) // want `\(\*net/http\.Request\)\.WithContext must not be called. use net/http\.NewRequestWithContext`
122+
cli.Do(req121)
123+
}
124+
125+
func _() {
126+
const url = "http://example.com"
127+
cli := &http.Client{}
128+
129+
http.Get(url) // want `net/http\.Get must not be called. use net/http\.NewRequestWithContext and \(\*net/http.Client\)\.Do\(\*http.Request\)`
130+
_ = http.Get // OK
131+
f := http.Get // OK
132+
f(url) // want `net/http\.Get must not be called. use net/http\.NewRequestWithContext and \(\*net/http.Client\)\.Do\(\*http.Request\)`
133+
134+
http.Head(url) // want `net/http\.Head must not be called. use net/http\.NewRequestWithContext and \(\*net/http.Client\)\.Do\(\*http.Request\)`
135+
http.Post(url, "", nil) // want `net/http\.Post must not be called. use net/http\.NewRequestWithContext and \(\*net/http.Client\)\.Do\(\*http.Request\)`
136+
http.PostForm(url, nil) // want `net/http\.PostForm must not be called. use net/http\.NewRequestWithContext and \(\*net/http.Client\)\.Do\(\*http.Request\)`
137+
138+
cli.Get(url) // want `\(\*net/http\.Client\)\.Get must not be called. use \(\*net/http.Client\)\.Do\(\*http.Request\)`
139+
_ = cli.Get // OK
140+
m := cli.Get // OK
141+
m(url) // want `\(\*net/http\.Client\)\.Get must not be called. use \(\*net/http.Client\)\.Do\(\*http.Request\)`
142+
143+
cli.Head(url) // want `\(\*net/http\.Client\)\.Head must not be called. use \(\*net/http.Client\)\.Do\(\*http.Request\)`
144+
cli.Post(url, "", nil) // want `\(\*net/http\.Client\)\.Post must not be called. use \(\*net/http.Client\)\.Do\(\*http.Request\)`
145+
cli.PostForm(url, nil) // want `\(\*net/http\.Client\)\.PostForm must not be called. use \(\*net/http.Client\)\.Do\(\*http.Request\)`
133146
}
134147

148+
func _() {
149+
ctx := context.Background()
150+
151+
db, _ := sql.Open("noctx", "noctx://")
152+
153+
db.Exec("select * from testdata") // want `\(\*database/sql\.DB\)\.Exec must not be called. use \(\*database/sql\.DB\)\.ExecContext`
154+
db.ExecContext(ctx, "select * from testdata")
155+
156+
db.Ping() // want `\(\*database/sql\.DB\)\.Ping must not be called. use \(\*database/sql\.DB\)\.PingContext`
157+
db.PingContext(ctx)
158+
159+
db.Prepare("select * from testdata") // want `\(\*database/sql\.DB\)\.Prepare must not be called. use \(\*database/sql\.DB\)\.PrepareContext`
160+
db.PrepareContext(ctx, "select * from testdata")
161+
162+
db.Query("select * from testdata") // want `\(\*database/sql\.DB\)\.Query must not be called. use \(\*database/sql\.DB\)\.QueryContext`
163+
db.QueryContext(ctx, "select * from testdata")
164+
165+
db.QueryRow("select * from testdata") // want `\(\*database/sql\.DB\)\.QueryRow must not be called. use \(\*database/sql\.DB\)\.QueryRowContext`
166+
db.QueryRowContext(ctx, "select * from testdata")
167+
168+
// transactions
169+
170+
tx, _ := db.Begin()
171+
tx.Exec("select * from testdata") // want `\(\*database/sql\.Tx\)\.Exec must not be called. use \(\*database/sql\.Tx\)\.ExecContext`
172+
tx.ExecContext(ctx, "select * from testdata")
173+
174+
tx.Prepare("select * from testdata") // want `\(\*database/sql\.Tx\)\.Prepare must not be called. use \(\*database/sql\.Tx\)\.PrepareContext`
175+
tx.PrepareContext(ctx, "select * from testdata")
176+
177+
tx.Query("select * from testdata") // want `\(\*database/sql\.Tx\)\.Query must not be called. use \(\*database/sql\.Tx\)\.QueryContext`
178+
tx.QueryContext(ctx, "select * from testdata")
179+
180+
tx.QueryRow("select * from testdata") // want `\(\*database/sql\.Tx\)\.QueryRow must not be called. use \(\*database/sql\.Tx\)\.QueryRowContext`
181+
tx.QueryRowContext(ctx, "select * from testdata")
182+
183+
_ = tx.Commit()
184+
}

0 commit comments

Comments
 (0)