Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions .github/workflows/go.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
name: Go
on:
push:
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
- name: Install golangci-lint
run: go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
- name: Run test
run: go test ./...
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ module github.com/nametake/golangci-lint-langserver
go 1.23.4

require github.com/sourcegraph/jsonrpc2 v0.0.0-20191222043438-96c4efab7ee2

require github.com/google/go-cmp v0.6.0
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/gorilla/websocket v1.4.1 h1:q7AeDBpnBk8AogcD4DSag/Ukw/KV+YhzLj2bP5HvKCM=
github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/sourcegraph/jsonrpc2 v0.0.0-20191222043438-96c4efab7ee2 h1:5VGNYxMxzZ8Jb2bARgVl1DNg8vpcd9S8b4MbbjWQ8/w=
Expand Down
185 changes: 185 additions & 0 deletions handler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
package main

import (
"os/exec"
"path/filepath"
"testing"

"github.com/google/go-cmp/cmp"
)

func pt(s string) *string {
return &s
}

func TestLangHandler_lint_Integration(t *testing.T) {
if _, err := exec.LookPath("golangci-lint"); err != nil {
t.Fatal("golangci-lint is not installed in this environment")
}

tests := []struct {
name string
h *langHandler
filePath string
want []Diagnostic
}{
{
name: "no config file",
h: &langHandler{
logger: newStdLogger(false),
command: []string{"golangci-lint", "run", "--out-format", "json", "--issues-exit-code=1"},
rootDir: filepath.Dir("./testdata/noconfig"),
},
filePath: "./testdata/noconfig/main.go",
want: []Diagnostic{
{
Range: Range{
Start: Position{
Line: 3,
Character: 4,
},
End: Position{
Line: 3,
Character: 4,
},
},
Severity: DSWarning,
Code: nil,
Source: pt("unused"),
Message: "unused: var `foo` is unused",
RelatedInformation: nil,
},
},
},
{
name: "nolintername option works as expected",
h: &langHandler{
logger: newStdLogger(false),
command: []string{"golangci-lint", "run", "--out-format", "json", "--issues-exit-code=1"},
rootDir: filepath.Dir("./testdata/nolintername"),
noLinterName: true,
},
filePath: "./testdata/nolintername/main.go",
want: []Diagnostic{
{
Range: Range{
Start: Position{
Line: 3,
Character: 4,
},
End: Position{
Line: 3,
Character: 4,
},
},
Severity: DSWarning,
Code: nil,
Source: pt("unused"),
Message: "var `foo` is unused",
RelatedInformation: nil,
},
},
},
{
name: "config file is loaded successfully",
h: &langHandler{
logger: newStdLogger(false),
command: []string{"golangci-lint", "run", "--out-format", "json", "--issues-exit-code=1"},
rootDir: filepath.Dir("./testdata/nolintername"),
},
filePath: "./testdata/loadconfig/main.go",
want: []Diagnostic{
{
Range: Range{
Start: Position{
Line: 8,
Character: 0,
},
End: Position{
Line: 8,
Character: 0,
},
},
Severity: DSWarning,
Code: nil,
Source: pt("wsl"),
Message: "wsl: block should not end with a whitespace (or comment)",
RelatedInformation: nil,
},
},
},
{
name: "multiple files in rootDir",
h: &langHandler{
logger: newStdLogger(false),
command: []string{"golangci-lint", "run", "--out-format", "json", "--issues-exit-code=1"},
rootDir: filepath.Dir("./testdata/multifile"),
},
filePath: "./testdata/multifile/bar.go",
want: []Diagnostic{
{
Range: Range{
Start: Position{
Line: 3,
Character: 4,
},
End: Position{
Line: 3,
Character: 4,
},
},
Severity: DSWarning,
Code: nil,
Source: pt("unused"),
Message: "unused: var `bar` is unused",
RelatedInformation: nil,
},
},
},
{
name: "nested directories in rootDir",
h: &langHandler{
logger: newStdLogger(false),
command: []string{"golangci-lint", "run", "--out-format", "json", "--issues-exit-code=1"},
rootDir: filepath.Dir("./testdata/nesteddir"),
},
filePath: "./testdata/nesteddir/bar/bar.go",
want: []Diagnostic{
{
Range: Range{
Start: Position{
Line: 3,
Character: 4,
},
End: Position{
Line: 3,
Character: 4,
},
},
Severity: DSWarning,
Code: nil,
Source: pt("unused"),
Message: "unused: var `bar` is unused",
RelatedInformation: nil,
},
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
testFilePath, err := filepath.Abs(tt.filePath)
if err != nil {
t.Fatalf("filepath.Abs() returned unexpected error: %v", err)
}
testURI := DocumentURI("file://" + testFilePath)
diagnostics, err := tt.h.lint(testURI)
if err != nil {
t.Fatalf("lint() returned unexpected error: %v", err)
}
if diff := cmp.Diff(tt.want, diagnostics); diff != "" {
t.Errorf("lint() mismatch (-want +got):\n%s", diff)
}
})
}
}
5 changes: 5 additions & 0 deletions testdata/loadconfig/.golangci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
linters:
enable:
- wsl
disable:
- unused
9 changes: 9 additions & 0 deletions testdata/loadconfig/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package main

// unused: var `foo` is unused
var foo = "foo"

func Bar() {
_ = foo

}
4 changes: 4 additions & 0 deletions testdata/multifile/bar.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package main

// unused: var `bar` is unused
var bar = "bar"
4 changes: 4 additions & 0 deletions testdata/multifile/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package main

// unused: var `foo` is unused
var foo = "foo"
4 changes: 4 additions & 0 deletions testdata/nesteddir/bar/bar.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package bar

// unused: var `bar` is unused
var bar = "bar"
4 changes: 4 additions & 0 deletions testdata/nesteddir/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package main

// unused: var `foo` is unused
var foo = "foo"
4 changes: 4 additions & 0 deletions testdata/noconfig/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package main

// unused: var `foo` is unused
var foo = "foo"
4 changes: 4 additions & 0 deletions testdata/nolintername/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package main

// var `foo` is unused
var foo = "foo"
Loading