Skip to content

Commit 3caf7c3

Browse files
committed
Add test cases
1 parent 6943f9e commit 3caf7c3

File tree

5 files changed

+82
-9
lines changed

5 files changed

+82
-9
lines changed

issue.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"fmt"
1919
"go/ast"
2020
"os"
21+
"strconv"
2122
)
2223

2324
// Score type used by severity and confidence values
@@ -36,7 +37,7 @@ type Issue struct {
3637
What string `json:"details"` // Human readable explanation
3738
File string `json:"file"` // File name we found it in
3839
Code string `json:"code"` // Impacted code line
39-
Line int `json:"line"` // Line number in file
40+
Line string `json:"line"` // Line number in file
4041
}
4142

4243
// MetaData is embedded in all GAS rules. The Severity, Confidence and What message
@@ -85,7 +86,12 @@ func NewIssue(ctx *Context, node ast.Node, desc string, severity Score, confiden
8586
var code string
8687
fobj := ctx.FileSet.File(node.Pos())
8788
name := fobj.Name()
88-
line := fobj.Line(node.Pos())
89+
90+
start, end := fobj.Line(node.Pos()), fobj.Line(node.End())
91+
line := strconv.Itoa(start)
92+
if start != end {
93+
line = fmt.Sprintf("%d-%d", start, end)
94+
}
8995

9096
if file, err := os.Open(fobj.Name()); err == nil {
9197
defer file.Close()

issue_test.go

Lines changed: 68 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,47 @@
11
package gas_test
22

33
import (
4+
"go/ast"
5+
6+
"github.com/GoASTScanner/gas"
7+
"github.com/GoASTScanner/gas/rules"
8+
"github.com/GoASTScanner/gas/testutils"
49
. "github.com/onsi/ginkgo"
510
. "github.com/onsi/gomega"
611
)
712

813
var _ = Describe("Issue", func() {
914

1015
Context("when creating a new issue", func() {
11-
It("should provide a code snippet for the specified ast.Node", func() {
12-
Expect(1).Should(Equal(2))
13-
Fail("Not implemented")
16+
It("should create a code snippet from the specified ast.Node", func() {
17+
var target *ast.BasicLit
18+
source := `package main
19+
const foo = "bar"
20+
func main(){
21+
println(foo)
22+
}
23+
`
24+
pkg := testutils.NewTestPackage()
25+
defer pkg.Close()
26+
pkg.AddFile("foo.go", source)
27+
ctx := pkg.CreateContext("foo.go")
28+
v := testutils.NewMockVisitor()
29+
v.Callback = func(n ast.Node, ctx *gas.Context) bool {
30+
if node, ok := n.(*ast.BasicLit); ok {
31+
target = node
32+
return false
33+
}
34+
return true
35+
}
36+
v.Context = ctx
37+
ast.Walk(v, ctx.Root)
38+
Expect(target).ShouldNot(BeNil())
39+
40+
issue := gas.NewIssue(ctx, target, "", gas.High, gas.High)
41+
Expect(issue).ShouldNot(BeNil())
42+
Expect(issue.Code).Should(MatchRegexp(`"bar"`))
43+
Expect(issue.Line).Should(Equal(2))
44+
1445
})
1546

1647
It("should return an error if specific context is not able to be obtained", func() {
@@ -21,6 +52,40 @@ var _ = Describe("Issue", func() {
2152
Fail("Not implemented")
2253
})
2354

55+
It("should provide accurate line and file information for multi-line statements", func() {
56+
var target *ast.BinaryExpr
57+
58+
source := `package main
59+
import "os"
60+
func main(){`
61+
source += "q := `SELECT * FROM table WHERE` + \n os.Args[1] + `= ?` // nolint: gas\n"
62+
source += `println(q)}`
63+
64+
pkg := testutils.NewTestPackage()
65+
defer pkg.Close()
66+
pkg.AddFile("foo.go", source)
67+
ctx := pkg.CreateContext("foo.go")
68+
v := testutils.NewMockVisitor()
69+
v.Callback = func(n ast.Node, ctx *gas.Context) bool {
70+
if node, ok := n.(*ast.BinaryExpr); ok {
71+
target = node
72+
}
73+
return true
74+
}
75+
v.Context = ctx
76+
ast.Walk(v, ctx.Root)
77+
Expect(target).ShouldNot(BeNil())
78+
79+
// Use SQL rule to check binary expr
80+
cfg := gas.NewConfig()
81+
rule, _ := rules.NewSqlStrConcat(cfg)
82+
issue, err := rule.Match(target, ctx)
83+
Expect(err).ShouldNot(HaveOccurred())
84+
Expect(issue).ShouldNot(BeNil())
85+
Expect(issue.File).Should(MatchRegexp("foo.go"))
86+
Expect(issue.Line).Should(MatchRegexp("3-4"))
87+
})
88+
2489
It("should maintain the provided severity score", func() {
2590
Fail("Not implemented")
2691
})
@@ -29,9 +94,6 @@ var _ = Describe("Issue", func() {
2994
Fail("Not implemented")
3095
})
3196

32-
It("should correctly record `unsafe` import as not considered a package", func() {
33-
Fail("Not implemented")
34-
})
3597
})
3698

3799
})

resolve_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ var _ = Describe("Resolve ast node to concrete value", func() {
1515
var basicLiteral *ast.BasicLit
1616

1717
pkg := testutils.NewTestPackage()
18+
defer pkg.Close()
1819
pkg.AddFile("foo.go", `package main; const foo = "bar"; func main(){}`)
1920
ctx := pkg.CreateContext("foo.go")
2021
v := testutils.NewMockVisitor()
@@ -34,6 +35,7 @@ var _ = Describe("Resolve ast node to concrete value", func() {
3435
It("should successfully resolve identifier", func() {
3536
var ident *ast.Ident
3637
pkg := testutils.NewTestPackage()
38+
defer pkg.Close()
3739
pkg.AddFile("foo.go", `package main; var foo string = "bar"; func main(){}`)
3840
ctx := pkg.CreateContext("foo.go")
3941
v := testutils.NewMockVisitor()
@@ -53,6 +55,7 @@ var _ = Describe("Resolve ast node to concrete value", func() {
5355
It("should successfully resolve assign statement", func() {
5456
var assign *ast.AssignStmt
5557
pkg := testutils.NewTestPackage()
58+
defer pkg.Close()
5659
pkg.AddFile("foo.go", `package main; const x = "bar"; func main(){ y := x; println(y) }`)
5760
ctx := pkg.CreateContext("foo.go")
5861
v := testutils.NewMockVisitor()
@@ -73,6 +76,7 @@ var _ = Describe("Resolve ast node to concrete value", func() {
7376
It("should successfully resolve a binary statement", func() {
7477
var target *ast.BinaryExpr
7578
pkg := testutils.NewTestPackage()
79+
defer pkg.Close()
7680
pkg.AddFile("foo.go", `package main; const (x = "bar"; y = "baz"); func main(){ z := x + y; println(z) }`)
7781
ctx := pkg.CreateContext("foo.go")
7882
v := testutils.NewMockVisitor()

rule_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ var _ = Describe("Rule", func() {
4646
What: `Some explanation of the thing`,
4747
File: "main.go",
4848
Code: `#include <stdio.h> int main(){ puts("hello world"); }`,
49-
Line: 42,
49+
Line: "42",
5050
},
5151
err: nil,
5252
callback: func(n ast.Node, ctx *gas.Context) bool { return true },

rules/rules_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ var _ = Describe("gas rules", func() {
3232
for n, sample := range samples {
3333
analyzer.Reset()
3434
pkg := testutils.NewTestPackage()
35+
defer pkg.Close()
3536
pkg.AddFile(fmt.Sprintf("sample_%d.go", n), sample.Code)
3637
pkg.Build()
3738
e := analyzer.Process(pkg.Path)

0 commit comments

Comments
 (0)