Skip to content
This repository was archived by the owner on Jan 5, 2023. It is now read-only.

Commit 1ba1029

Browse files
author
Sauyon Lee
committed
Use comment-based tests for GoModExpr
1 parent 34837c1 commit 1ba1029

File tree

9 files changed

+100
-44
lines changed

9 files changed

+100
-44
lines changed

ql/test/library-tests/semmle/go/GoModExpr/ExcludeLines.expected

Lines changed: 0 additions & 2 deletions
This file was deleted.

ql/test/library-tests/semmle/go/GoModExpr/ExcludeLines.ql

Lines changed: 0 additions & 4 deletions
This file was deleted.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
missingRequire
2+
missingExclude
3+
missingReplace
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import go
2+
3+
/**
4+
* Holds if there exists a comment on the same line as `l`
5+
* that contains the substring "`kind`,`dep`,`ver`".
6+
*/
7+
predicate metadata(Locatable l, string kind, string mod, string dep, string ver) {
8+
exists(string f, int line, Comment c, string text |
9+
l.hasLocationInfo(f, line, _, _, _) and
10+
c.hasLocationInfo(f, line, _, _, _)
11+
|
12+
text = c.getText().regexpFind("\\b([^,\\s]+,[^,]+,[^,]+,[^,\\s]+)", _, _) and
13+
kind = text.regexpCapture("([^,]+),([^,]+),([^,]+),([^,]+)", 1) and
14+
mod = text.regexpCapture("([^,]+),([^,]+),([^,]+),([^,]+)", 2) and
15+
dep = text.regexpCapture("([^,]+),([^,]+),([^,]+),([^,]+)", 3) and
16+
ver = text.regexpCapture("([^,]+),([^,]+),([^,]+),([^,]+)", 4)
17+
)
18+
}
19+
20+
query predicate missingRequire(string mod, string dep, string ver, int line) {
21+
exists(Locatable l | metadata(l, "RequireLine", mod, dep, ver) |
22+
l.hasLocationInfo(_, line, _, _, _)
23+
) and
24+
not exists(GoModRequireLine req |
25+
req.getModulePath() = mod and
26+
req.getPath() = dep and
27+
req.getVersion() = ver and
28+
metadata(req, "RequireLine", mod, dep, ver) and
29+
req.hasLocationInfo(_, line, _, _, _)
30+
)
31+
}
32+
33+
query predicate missingExclude(string mod, string dep, string ver, int line) {
34+
exists(Locatable l | metadata(l, "ExcludeLine", mod, dep, ver) |
35+
l.hasLocationInfo(_, line, _, _, _)
36+
) and
37+
not exists(GoModExcludeLine exc |
38+
exc.getModulePath() = mod and
39+
exc.getPath() = dep and
40+
exc.getVersion() = ver and
41+
metadata(exc, "ExcludeLine", mod, dep, ver) and
42+
exc.hasLocationInfo(_, line, _, _, _)
43+
)
44+
}
45+
46+
/**
47+
* Holds if there exists a comment on the same line as `l`
48+
* that contains the substring "ReplaceLine,`mod`,`dep`,`dver`,`rep`,`rver`".
49+
*/
50+
predicate repmetadata(Locatable l, string mod, string dep, string dver, string rep, string rver) {
51+
exists(string f, int line, Comment c, string text |
52+
l.hasLocationInfo(f, line, _, _, _) and
53+
c.hasLocationInfo(f, line, _, _, _)
54+
|
55+
text = c.getText().regexpFind("\\b(ReplaceLine,[^,]*,[^,]*,[^,]*,[^,]*,[^,\\s]*)", _, _) and
56+
mod = text.regexpCapture("ReplaceLine,([^,]*),([^,]*),([^,]*),([^,]*),([^,]*)", 1) and
57+
dep = text.regexpCapture("ReplaceLine,([^,]*),([^,]*),([^,]*),([^,]*),([^,]*)", 2) and
58+
dver = text.regexpCapture("ReplaceLine,([^,]*),([^,]*),([^,]*),([^,]*),([^,]*)", 3) and
59+
rep = text.regexpCapture("ReplaceLine,([^,]*),([^,]*),([^,]*),([^,]*),([^,]*)", 4) and
60+
rver = text.regexpCapture("ReplaceLine,([^,]*),([^,]*),([^,]*),([^,]*),([^,]*)", 5)
61+
)
62+
}
63+
64+
query predicate missingReplace(string mod, string dep, string dver, string rep, string rver, int line) {
65+
exists(Locatable l | repmetadata(l, mod, dep, dver, rep, rver) |
66+
l.hasLocationInfo(_, line, _, _, _)
67+
) and
68+
not exists(GoModReplaceLine repl |
69+
(
70+
rver = repl.getReplacementVersion()
71+
or
72+
not exists(repl.getReplacementVersion()) and
73+
rver = ""
74+
) and
75+
(
76+
dver = repl.getOriginalVersion()
77+
or
78+
not exists(repl.getOriginalVersion()) and
79+
dver = ""
80+
)
81+
|
82+
repl.getModulePath() = mod and
83+
repl.getOriginalPath() = dep and
84+
repl.getReplacementPath() = rep and
85+
repmetadata(repl, mod, dep, dver, rep, rver) and
86+
repl.hasLocationInfo(_, line, _, _, _)
87+
)
88+
}

ql/test/library-tests/semmle/go/GoModExpr/ReplaceLines.expected

Lines changed: 0 additions & 2 deletions
This file was deleted.

ql/test/library-tests/semmle/go/GoModExpr/ReplaceLines.ql

Lines changed: 0 additions & 18 deletions
This file was deleted.

ql/test/library-tests/semmle/go/GoModExpr/RequireLines.expected

Lines changed: 0 additions & 5 deletions
This file was deleted.

ql/test/library-tests/semmle/go/GoModExpr/RequireLines.ql

Lines changed: 0 additions & 4 deletions
This file was deleted.

ql/test/library-tests/semmle/go/GoModExpr/go.mod

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,21 @@ module codeql-go-tests/gomod
33
go 1.14
44

55
require (
6-
github.com/github/codeql-go v1.23.2-0.20200302182241-5e71a04fdf30 // indirect
7-
golang.org/x/tools v0.0.0-20200109174759-ac4f524c1612 // indirect
6+
github.com/github/codeql-go v1.23.2-0.20200302182241-5e71a04fdf30 // indirect RequireLine,codeql-go-tests/gomod,github.com/github/codeql-go,v1.23.2-0.20200302182241-5e71a04fdf30
7+
golang.org/x/tools v0.0.0-20200109174759-ac4f524c1612 // indirect RequireLine,codeql-go-tests/gomod,golang.org/x/tools,v0.0.0-20200109174759-ac4f524c1612
88
)
99

10-
exclude github.com/github/codeql-go v1.23.1
10+
exclude github.com/github/codeql-go v1.23.1 // ExcludeLine,codeql-go-tests/gomod,github.com/github/codeql-go,v1.23.1
1111

12-
replace github.com/Masterminds/squirrel => ./squirrel
12+
replace github.com/Masterminds/squirrel => ./squirrel // ReplaceLine,codeql-go-tests/gomod,github.com/Masterminds/squirrel,,./squirrel,
1313

14-
replace github.com/Sirupsen/logrus v1.4.1 => github.com/sirupsen/logrus v1.4.1
14+
replace github.com/Sirupsen/logrus v1.4.1 => github.com/sirupsen/logrus v1.4.1 // ReplaceLine,codeql-go-tests/gomod,github.com/Sirupsen/logrus,v1.4.1,github.com/sirupsen/logrus,v1.4.1
1515

16-
require github.com/gorilla/mux v1.7.4 // indirect
16+
require github.com/gorilla/mux v1.7.4 // indirect RequireLine,codeql-go-tests/gomod,github.com/gorilla/mux,v1.7.4
1717

1818
require (
19-
github.com/Masterminds/squirrel v1.2.0 // indirect
20-
github.com/Sirupsen/logrus v1.4.1 // indirect
19+
github.com/Masterminds/squirrel v1.2.0 // indirect RequireLine,codeql-go-tests/gomod,github.com/Masterminds/squirrel,v1.2.0
20+
github.com/Sirupsen/logrus v1.4.1 // indirect RequireLine,codeql-go-tests/gomod,github.com/Sirupsen/logrus,v1.4.1
2121
)
2222

23-
exclude github.com/sirupsen/logrus v1.4.2
23+
exclude github.com/sirupsen/logrus v1.4.2 // ExcludeLine,codeql-go-tests/gomod,github.com/sirupsen/logrus,v1.4.2

0 commit comments

Comments
 (0)