Skip to content

Commit a54e7dc

Browse files
authored
Merge pull request #46 from nametake/add-tests
Added integration tests
2 parents 7417728 + 0de8002 commit a54e7dc

File tree

12 files changed

+241
-0
lines changed

12 files changed

+241
-0
lines changed

.github/workflows/go.yaml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
name: Go
3+
on:
4+
push:
5+
jobs:
6+
test:
7+
runs-on: ubuntu-latest
8+
steps:
9+
- uses: actions/checkout@v4
10+
- uses: actions/setup-go@v5
11+
- name: Install golangci-lint
12+
run: go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
13+
- name: Run test
14+
run: go test ./...

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@ module github.com/nametake/golangci-lint-langserver
33
go 1.23.4
44

55
require github.com/sourcegraph/jsonrpc2 v0.0.0-20191222043438-96c4efab7ee2
6+
7+
require github.com/google/go-cmp v0.6.0

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
2+
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
13
github.com/gorilla/websocket v1.4.1 h1:q7AeDBpnBk8AogcD4DSag/Ukw/KV+YhzLj2bP5HvKCM=
24
github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
35
github.com/sourcegraph/jsonrpc2 v0.0.0-20191222043438-96c4efab7ee2 h1:5VGNYxMxzZ8Jb2bARgVl1DNg8vpcd9S8b4MbbjWQ8/w=

handler_test.go

Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
package main
2+
3+
import (
4+
"os/exec"
5+
"path/filepath"
6+
"testing"
7+
8+
"github.com/google/go-cmp/cmp"
9+
)
10+
11+
func pt(s string) *string {
12+
return &s
13+
}
14+
15+
func TestLangHandler_lint_Integration(t *testing.T) {
16+
if _, err := exec.LookPath("golangci-lint"); err != nil {
17+
t.Fatal("golangci-lint is not installed in this environment")
18+
}
19+
20+
tests := []struct {
21+
name string
22+
h *langHandler
23+
filePath string
24+
want []Diagnostic
25+
}{
26+
{
27+
name: "no config file",
28+
h: &langHandler{
29+
logger: newStdLogger(false),
30+
command: []string{"golangci-lint", "run", "--out-format", "json", "--issues-exit-code=1"},
31+
rootDir: filepath.Dir("./testdata/noconfig"),
32+
},
33+
filePath: "./testdata/noconfig/main.go",
34+
want: []Diagnostic{
35+
{
36+
Range: Range{
37+
Start: Position{
38+
Line: 3,
39+
Character: 4,
40+
},
41+
End: Position{
42+
Line: 3,
43+
Character: 4,
44+
},
45+
},
46+
Severity: DSWarning,
47+
Code: nil,
48+
Source: pt("unused"),
49+
Message: "unused: var `foo` is unused",
50+
RelatedInformation: nil,
51+
},
52+
},
53+
},
54+
{
55+
name: "nolintername option works as expected",
56+
h: &langHandler{
57+
logger: newStdLogger(false),
58+
command: []string{"golangci-lint", "run", "--out-format", "json", "--issues-exit-code=1"},
59+
rootDir: filepath.Dir("./testdata/nolintername"),
60+
noLinterName: true,
61+
},
62+
filePath: "./testdata/nolintername/main.go",
63+
want: []Diagnostic{
64+
{
65+
Range: Range{
66+
Start: Position{
67+
Line: 3,
68+
Character: 4,
69+
},
70+
End: Position{
71+
Line: 3,
72+
Character: 4,
73+
},
74+
},
75+
Severity: DSWarning,
76+
Code: nil,
77+
Source: pt("unused"),
78+
Message: "var `foo` is unused",
79+
RelatedInformation: nil,
80+
},
81+
},
82+
},
83+
{
84+
name: "config file is loaded successfully",
85+
h: &langHandler{
86+
logger: newStdLogger(false),
87+
command: []string{"golangci-lint", "run", "--out-format", "json", "--issues-exit-code=1"},
88+
rootDir: filepath.Dir("./testdata/nolintername"),
89+
},
90+
filePath: "./testdata/loadconfig/main.go",
91+
want: []Diagnostic{
92+
{
93+
Range: Range{
94+
Start: Position{
95+
Line: 8,
96+
Character: 0,
97+
},
98+
End: Position{
99+
Line: 8,
100+
Character: 0,
101+
},
102+
},
103+
Severity: DSWarning,
104+
Code: nil,
105+
Source: pt("wsl"),
106+
Message: "wsl: block should not end with a whitespace (or comment)",
107+
RelatedInformation: nil,
108+
},
109+
},
110+
},
111+
{
112+
name: "multiple files in rootDir",
113+
h: &langHandler{
114+
logger: newStdLogger(false),
115+
command: []string{"golangci-lint", "run", "--out-format", "json", "--issues-exit-code=1"},
116+
rootDir: filepath.Dir("./testdata/multifile"),
117+
},
118+
filePath: "./testdata/multifile/bar.go",
119+
want: []Diagnostic{
120+
{
121+
Range: Range{
122+
Start: Position{
123+
Line: 3,
124+
Character: 4,
125+
},
126+
End: Position{
127+
Line: 3,
128+
Character: 4,
129+
},
130+
},
131+
Severity: DSWarning,
132+
Code: nil,
133+
Source: pt("unused"),
134+
Message: "unused: var `bar` is unused",
135+
RelatedInformation: nil,
136+
},
137+
},
138+
},
139+
{
140+
name: "nested directories in rootDir",
141+
h: &langHandler{
142+
logger: newStdLogger(false),
143+
command: []string{"golangci-lint", "run", "--out-format", "json", "--issues-exit-code=1"},
144+
rootDir: filepath.Dir("./testdata/nesteddir"),
145+
},
146+
filePath: "./testdata/nesteddir/bar/bar.go",
147+
want: []Diagnostic{
148+
{
149+
Range: Range{
150+
Start: Position{
151+
Line: 3,
152+
Character: 4,
153+
},
154+
End: Position{
155+
Line: 3,
156+
Character: 4,
157+
},
158+
},
159+
Severity: DSWarning,
160+
Code: nil,
161+
Source: pt("unused"),
162+
Message: "unused: var `bar` is unused",
163+
RelatedInformation: nil,
164+
},
165+
},
166+
},
167+
}
168+
169+
for _, tt := range tests {
170+
t.Run(tt.name, func(t *testing.T) {
171+
testFilePath, err := filepath.Abs(tt.filePath)
172+
if err != nil {
173+
t.Fatalf("filepath.Abs() returned unexpected error: %v", err)
174+
}
175+
testURI := DocumentURI("file://" + testFilePath)
176+
diagnostics, err := tt.h.lint(testURI)
177+
if err != nil {
178+
t.Fatalf("lint() returned unexpected error: %v", err)
179+
}
180+
if diff := cmp.Diff(tt.want, diagnostics); diff != "" {
181+
t.Errorf("lint() mismatch (-want +got):\n%s", diff)
182+
}
183+
})
184+
}
185+
}

testdata/loadconfig/.golangci.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
linters:
2+
enable:
3+
- wsl
4+
disable:
5+
- unused

testdata/loadconfig/main.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package main
2+
3+
// unused: var `foo` is unused
4+
var foo = "foo"
5+
6+
func Bar() {
7+
_ = foo
8+
9+
}

testdata/multifile/bar.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package main
2+
3+
// unused: var `bar` is unused
4+
var bar = "bar"

testdata/multifile/main.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package main
2+
3+
// unused: var `foo` is unused
4+
var foo = "foo"

testdata/nesteddir/bar/bar.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package bar
2+
3+
// unused: var `bar` is unused
4+
var bar = "bar"

testdata/nesteddir/main.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package main
2+
3+
// unused: var `foo` is unused
4+
var foo = "foo"

testdata/noconfig/main.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package main
2+
3+
// unused: var `foo` is unused
4+
var foo = "foo"

testdata/nolintername/main.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package main
2+
3+
// var `foo` is unused
4+
var foo = "foo"

0 commit comments

Comments
 (0)