-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(yamlls): more integration tests
- Loading branch information
Showing
4 changed files
with
281 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package yamlls | ||
|
||
import ( | ||
"context" | ||
"strings" | ||
"testing" | ||
"time" | ||
|
||
"github.com/mrjosh/helm-ls/internal/util" | ||
"github.com/stretchr/testify/assert" | ||
lsp "go.lsp.dev/protocol" | ||
"go.lsp.dev/uri" | ||
) | ||
|
||
func TestYamllsHoverIntegration(t *testing.T) { | ||
config := util.DefaultConfig.YamllsConfiguration | ||
|
||
testCases := []struct { | ||
desc string | ||
file string | ||
position lsp.Position | ||
|
||
word string | ||
expected string | ||
}{ | ||
{ | ||
desc: "test hover on deployment.yaml", | ||
file: "../../../testdata/example/templates/deployment.yaml", | ||
position: lsp.Position{ | ||
Line: 0, | ||
Character: 0, | ||
}, | ||
word: "apiVersion", | ||
expected: "APIVersion defines the versioned schema of this representation of an object", | ||
}, | ||
{ | ||
desc: "test hover on deployment.yaml, template", | ||
file: "../../../testdata/example/templates/deployment.yaml", | ||
position: lsp.Position{ | ||
Line: 13, | ||
Character: 3, | ||
}, | ||
word: "template", | ||
expected: "Template describes the pods that will be created", | ||
}, | ||
{ | ||
desc: "test hover on deployment.yaml with value", | ||
file: "../../../testdata/example/templates/deployment.yaml", | ||
position: lsp.Position{ | ||
Line: 1, | ||
Character: 13, | ||
}, | ||
word: "kind", | ||
expected: "Kind is a string value representing the REST resource this object represents", | ||
}, | ||
{ | ||
desc: "test hover on file without templates", | ||
file: "../../../testdata/example/templates/deployment-no-templates.yaml", | ||
position: lsp.Position{ | ||
Line: 0, | ||
Character: 1, | ||
}, | ||
word: "", | ||
expected: "APIVersion defines the versioned schema of this representation of an object", | ||
}, | ||
} | ||
for _, tt1 := range testCases { | ||
tt := tt1 | ||
t.Run(tt.desc, func(t *testing.T) { | ||
t.Parallel() | ||
yamllsConnector, documents, _ := getYamlLsConnector(t, config) | ||
openFile(t, documents, tt.file, yamllsConnector) | ||
|
||
assert.Eventually(t, func() bool { | ||
result, err := yamllsConnector.CallHover(context.Background(), lsp.HoverParams{ | ||
TextDocumentPositionParams: lsp.TextDocumentPositionParams{ | ||
TextDocument: lsp.TextDocumentIdentifier{ | ||
URI: uri.File(tt.file), | ||
}, | ||
Position: tt.position, | ||
}, | ||
}, tt.word) | ||
return err == nil && strings.Contains(result.Contents.Value, tt.expected) | ||
}, time.Second*10, time.Second/2) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package yamlls | ||
|
||
import ( | ||
"encoding/json" | ||
"os" | ||
"strings" | ||
"testing" | ||
|
||
lsplocal "github.com/mrjosh/helm-ls/internal/lsp" | ||
"github.com/mrjosh/helm-ls/internal/util" | ||
"go.lsp.dev/jsonrpc2" | ||
"go.lsp.dev/protocol" | ||
lsp "go.lsp.dev/protocol" | ||
"go.lsp.dev/uri" | ||
"go.uber.org/zap" | ||
) | ||
|
||
type jsonRpcDiagnostics struct { | ||
Params lsp.PublishDiagnosticsParams `json:"params"` | ||
Jsonrpc string `json:"jsonrpc"` | ||
Method string `json:"method"` | ||
} | ||
|
||
type readWriteCloseMock struct { | ||
diagnosticsChan chan lsp.PublishDiagnosticsParams | ||
} | ||
|
||
func (proc readWriteCloseMock) Read(p []byte) (int, error) { | ||
return 1, nil | ||
} | ||
|
||
func (proc readWriteCloseMock) Write(p []byte) (int, error) { | ||
if strings.HasPrefix(string(p), "Content-Length: ") { | ||
return 1, nil | ||
} | ||
var diagnostics jsonRpcDiagnostics | ||
json.NewDecoder(strings.NewReader(string(p))).Decode(&diagnostics) | ||
|
||
proc.diagnosticsChan <- diagnostics.Params | ||
return 1, nil | ||
} | ||
|
||
func (proc readWriteCloseMock) Close() error { | ||
return nil | ||
} | ||
|
||
func getYamlLsConnector(t *testing.T, config util.YamllsConfiguration) (*Connector, *lsplocal.DocumentStore, chan lsp.PublishDiagnosticsParams) { | ||
dir := t.TempDir() | ||
documents := lsplocal.NewDocumentStore() | ||
diagnosticsChan := make(chan lsp.PublishDiagnosticsParams) | ||
con := jsonrpc2.NewConn(jsonrpc2.NewStream(readWriteCloseMock{diagnosticsChan})) | ||
zapLogger, _ := zap.NewProduction() | ||
client := protocol.ClientDispatcher(con, zapLogger) | ||
|
||
yamllsConnector := NewConnector(config, client, documents) | ||
|
||
if yamllsConnector.server == nil { | ||
t.Fatal("Could not connect to yaml-language-server") | ||
} | ||
|
||
yamllsConnector.CallInitialize(uri.File(dir)) | ||
|
||
return yamllsConnector, documents, diagnosticsChan | ||
} | ||
|
||
func openFile(t *testing.T, documents *lsplocal.DocumentStore, path string, yamllsConnector *Connector) { | ||
fileURI := uri.File(path) | ||
|
||
content, err := os.ReadFile(path) | ||
if err != nil { | ||
t.Fatal("Could not read test file", err) | ||
} | ||
d := lsp.DidOpenTextDocumentParams{ | ||
TextDocument: lsp.TextDocumentItem{ | ||
URI: fileURI, | ||
LanguageID: "", | ||
Version: 0, | ||
Text: string(content), | ||
}, | ||
} | ||
documents.DidOpen(&d, util.DefaultConfig) | ||
tree := lsplocal.ParseAst(nil, d.TextDocument.Text) | ||
yamllsConnector.DocumentDidOpen(tree, d) | ||
} |
Oops, something went wrong.