Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
qvalentin committed Apr 7, 2024
1 parent cbf9523 commit 86f2ed1
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 97 deletions.
7 changes: 6 additions & 1 deletion internal/handler/definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,14 @@ func (h *langHandler) Definition(ctx context.Context, params *lsp.DefinitionPara
func (h *langHandler) definitionAstParsing(genericDocumentUseCase languagefeatures.GenericDocumentUseCase, chart *charts.Chart, doc *lsplocal.Document, position lsp.Position) ([]lsp.Location, error) {

Check failure on line 37 in internal/handler/definition.go

View workflow job for this annotation

GitHub Actions / lint (1.21.5, ubuntu-latest)

unused-parameter: parameter 'position' seems to be unused, consider removing or renaming it as _ (revive)
var (
relevantChildNode = genericDocumentUseCase.Node
parentType = relevantChildNode.Parent().Type()
parentNode = relevantChildNode.Parent()
parentType string
)

if parentNode != nil {
parentType = parentNode.Type()
}

nodeType := relevantChildNode.Type()
switch nodeType {
case gotemplate.NodeTypeIdentifier:
Expand Down
107 changes: 63 additions & 44 deletions internal/handler/definition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ package handler

import (
"context"
"fmt"
"reflect"
"testing"

"github.com/mrjosh/helm-ls/internal/adapter/yamlls"
"github.com/mrjosh/helm-ls/internal/charts"
languagefeatures "github.com/mrjosh/helm-ls/internal/language_features"
lsplocal "github.com/mrjosh/helm-ls/internal/lsp"
gotemplate "github.com/mrjosh/helm-ls/internal/tree-sitter/gotemplate"
sitter "github.com/smacker/go-tree-sitter"
"github.com/mrjosh/helm-ls/internal/util"
"github.com/stretchr/testify/assert"
lsp "go.lsp.dev/protocol"
"go.lsp.dev/uri"
yamlv3 "gopkg.in/yaml.v3"
Expand All @@ -33,7 +32,7 @@ var testFileContent = `
`

var (
testDocumentTemplateURI = uri.URI("file:///test.yaml")
testDocumentTemplateURI = uri.URI("file:///templates/test.yaml")
testValuesURI = uri.URI("file:///values.yaml")
testOtherValuesURI = uri.URI("file:///values.other.yaml")
valuesContent = `
Expand All @@ -51,22 +50,12 @@ func genericDefinitionTest(t *testing.T, position lsp.Position, expectedLocation
if err != nil {
t.Fatal(err)
}
handler := &langHandler{
linterName: "helm-lint",
connPool: nil,
documents: nil,
}

parser := sitter.NewParser()
parser.SetLanguage(gotemplate.GetLanguage())
tree, _ := parser.ParseCtx(context.Background(), nil, []byte(testFileContent))
doc := &lsplocal.Document{
Content: testFileContent,
URI: testDocumentTemplateURI,
Ast: tree,
}
documents := lsplocal.NewDocumentStore()
fileURI := testDocumentTemplateURI
rootUri := uri.File("/")

location, err := handler.definitionAstParsing(languagefeatures.GenericDocumentUseCase{}, &charts.Chart{
chart := &charts.Chart{
ChartMetadata: &charts.ChartMetadata{},
ValuesFiles: &charts.ValuesFiles{
MainValuesFile: &charts.ValuesFile{
Expand All @@ -77,15 +66,33 @@ func genericDefinitionTest(t *testing.T, position lsp.Position, expectedLocation
AdditionalValuesFiles: []*charts.ValuesFile{},
},
RootURI: "",
}, doc, position)

if err != nil && err.Error() != expectedError.Error() {
t.Errorf("expected %v, got %v", expectedError, err)
}

if reflect.DeepEqual(location, expectedLocations) == false {
t.Errorf("expected %v, got %v", expectedLocations, location)
d := lsp.DidOpenTextDocumentParams{
TextDocument: lsp.TextDocumentItem{
URI: fileURI,
LanguageID: "",
Version: 0,
Text: string(testFileContent),
},
}
documents.DidOpen(&d, util.DefaultConfig)
chartStore := charts.NewChartStore(rootUri, charts.NewChart)
chartStore.Charts = map[uri.URI]*charts.Chart{rootUri: chart}
h := &langHandler{
chartStore: chartStore,
documents: documents,
yamllsConnector: &yamlls.Connector{},
}

location, err := h.Definition(context.TODO(), &lsp.DefinitionParams{
TextDocumentPositionParams: lsp.TextDocumentPositionParams{
TextDocument: lsp.TextDocumentIdentifier{URI: fileURI},
Position: position,
},
})

assert.Equal(t, expectedError, err)
assert.Equal(t, expectedLocations, location)
}

// Input:
Expand All @@ -106,8 +113,7 @@ func TestDefinitionVariable(t *testing.T) {
}

func TestDefinitionNotImplemented(t *testing.T) {
genericDefinitionTest(t, lsp.Position{Line: 1, Character: 1}, []lsp.Location{},
fmt.Errorf("Definition not implemented for node type %s", "{{"))
genericDefinitionTest(t, lsp.Position{Line: 1, Character: 1}, nil, nil)
}

// Input:
Expand Down Expand Up @@ -220,22 +226,12 @@ func genericDefinitionTestMultipleValuesFiles(t *testing.T, position lsp.Positio
if err != nil {
t.Fatal(err)
}
handler := &langHandler{
linterName: "helm-lint",
connPool: nil,
documents: nil,
}

parser := sitter.NewParser()
parser.SetLanguage(gotemplate.GetLanguage())
tree, _ := parser.ParseCtx(context.Background(), nil, []byte(testFileContent))
doc := &lsplocal.Document{
Content: testFileContent,
URI: testDocumentTemplateURI,
Ast: tree,
}
documents := lsplocal.NewDocumentStore()
fileURI := testDocumentTemplateURI
rootUri := uri.File("/")

location, err := handler.definitionAstParsing(languagefeatures.GenericDocumentUseCase{}, &charts.Chart{
chart := &charts.Chart{
ChartMetadata: &charts.ChartMetadata{},
ValuesFiles: &charts.ValuesFiles{
MainValuesFile: &charts.ValuesFile{
Values: make(map[string]interface{}),
Expand All @@ -251,7 +247,30 @@ func genericDefinitionTestMultipleValuesFiles(t *testing.T, position lsp.Positio
},
},
RootURI: "",
}, doc, position)
}
d := lsp.DidOpenTextDocumentParams{
TextDocument: lsp.TextDocumentItem{
URI: fileURI,
LanguageID: "",
Version: 0,
Text: string(testFileContent),
},
}
documents.DidOpen(&d, util.DefaultConfig)
chartStore := charts.NewChartStore(rootUri, charts.NewChart)
chartStore.Charts = map[uri.URI]*charts.Chart{rootUri: chart}
h := &langHandler{
chartStore: chartStore,
documents: documents,
yamllsConnector: &yamlls.Connector{},
}

location, err := h.Definition(context.TODO(), &lsp.DefinitionParams{
TextDocumentPositionParams: lsp.TextDocumentPositionParams{
TextDocument: lsp.TextDocumentIdentifier{URI: fileURI},
Position: position,
},
})

if err != nil && err.Error() != expectedError.Error() {
t.Errorf("expected %v, got %v", expectedError, err)
Expand Down
2 changes: 1 addition & 1 deletion internal/handler/hover.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (h *langHandler) Hover(ctx context.Context, params *lsp.HoverParams) (resul
if ct == gotemplate.NodeTypeDot {
word = lspinternal.TraverseIdentifierPathUp(currentNode, doc)
}
if pt == gotemplate.NodeTypeArgumentList {
if pt == gotemplate.NodeTypeArgumentList && ct == gotemplate.NodeTypeInterpretedStringLiteral {
includesCallFeature := languagefeatures.NewIncludesCallFeature(genericDocumentUseCase)
response, err := includesCallFeature.Hover()
return util.BuildHoverResponse(response, wordRange), err
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,24 @@ package languagefeatures

import (
"fmt"
"strings"

lsp "go.lsp.dev/protocol"

lsplocal "github.com/mrjosh/helm-ls/internal/lsp"
"github.com/mrjosh/helm-ls/internal/util"
)

type ValuesFeature struct {
type TemplateContextFeature struct {
GenericDocumentUseCase
}

func NewValuesFeature(genericDocumentUseCase GenericDocumentUseCase) *ValuesFeature {
return &ValuesFeature{
func NewValuesFeature(genericDocumentUseCase GenericDocumentUseCase) *TemplateContextFeature {
return &TemplateContextFeature{
GenericDocumentUseCase: genericDocumentUseCase,
}
}

func (f *ValuesFeature) References() (result []lsp.Location, err error) {
func (f *TemplateContextFeature) References() (result []lsp.Location, err error) {
includeName, err := f.getTemplateContext()
if err != nil {
return []lsp.Location{}, err
Expand All @@ -30,18 +29,18 @@ func (f *ValuesFeature) References() (result []lsp.Location, err error) {
return locations, nil
}

func (f *ValuesFeature) getTemplateContext() (lsplocal.TemplateContext, error) {
func (f *TemplateContextFeature) getTemplateContext() (lsplocal.TemplateContext, error) {
templateContext := f.GenericDocumentUseCase.Document.SymbolTable.GetTemplateContext(lsplocal.GetRangeForNode(f.Node))
if len(templateContext) == 0 || templateContext == nil {
return lsplocal.TemplateContext{}, fmt.Errorf("no template context found")
}
return templateContext, nil
}

func (f *ValuesFeature) getReferenceLocations(templateContext lsplocal.TemplateContext) []lsp.Location {
func (f *TemplateContextFeature) getReferenceLocations(templateContext lsplocal.TemplateContext) []lsp.Location {
locations := []lsp.Location{}
for _, doc := range f.GenericDocumentUseCase.DocumentStore.GetAllDocs() {
referenceRanges := doc.SymbolTable.GetValues(templateContext)
referenceRanges := doc.SymbolTable.GetTemplateContextRanges(templateContext)
for _, referenceRange := range referenceRanges {
locations = append(locations, util.RangeToLocation(doc.URI, referenceRange))
}
Expand All @@ -50,7 +49,7 @@ func (f *ValuesFeature) getReferenceLocations(templateContext lsplocal.TemplateC
return locations
}

func (f *ValuesFeature) Hover() (string, error) {
func (f *TemplateContextFeature) Hover() (string, error) {
templateContext, err := f.getTemplateContext()
return strings.Join(templateContext, " "), err
return templateContext.Format(), err
}
14 changes: 9 additions & 5 deletions internal/lsp/symbol_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import (

type TemplateContext []string

func (t TemplateContext) Format() string {
return strings.Join(t, ".")
}

type SymbolTable struct {
contexts map[string][]sitter.Range
contextsReversed map[sitter.Range]TemplateContext
Expand All @@ -26,15 +30,15 @@ func NewSymbolTable(ast *sitter.Tree, content []byte) *SymbolTable {
return s
}

func (s *SymbolTable) AddValue(templateContext []string, pointRange sitter.Range) {
s.contexts[strings.Join(templateContext, ".")] = append(s.contexts[strings.Join(templateContext, ".")], pointRange)
func (s *SymbolTable) AddTemplateContext(templateContext TemplateContext, pointRange sitter.Range) {
s.contexts[templateContext.Format()] = append(s.contexts[strings.Join(templateContext, ".")], pointRange)
sliceCopy := make(TemplateContext, len(templateContext))
copy(sliceCopy, templateContext)
s.contextsReversed[pointRange] = sliceCopy
}

func (s *SymbolTable) GetValues(path []string) []sitter.Range {
return s.contexts[strings.Join(path, ".")]
func (s *SymbolTable) GetTemplateContextRanges(templateContext TemplateContext) []sitter.Range {
return s.contexts[templateContext.Format()]
}

func (s *SymbolTable) GetTemplateContext(pointRange sitter.Range) TemplateContext {
Expand Down Expand Up @@ -64,7 +68,7 @@ func (s *SymbolTable) parseTree(ast *sitter.Tree, content []byte) {
v := Visitors{
symbolTable: s,
visitors: []Visitor{
NewValuesVisitor(s, content),
NewTemplateContextVisitor(s, content),
NewIncludeDefinitionsVisitor(s, content),
},
}
Expand Down
22 changes: 11 additions & 11 deletions internal/lsp/symbol_table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,14 @@ func TestSymbolTableForValues(t *testing.T) {
path: []string{"Test"},
startPoint: sitter.Point{
Row: 5,
Column: 3,
Column: 4,
},
},
{
path: []string{"Test"},
startPoint: sitter.Point{
Row: 20,
Column: 3,
Column: 4,
},
},
{
Expand All @@ -104,7 +104,7 @@ func TestSymbolTableForValues(t *testing.T) {
path: []string{"list"},
startPoint: sitter.Point{
Row: 8,
Column: 9,
Column: 10,
},
},
{
Expand All @@ -118,7 +118,7 @@ func TestSymbolTableForValues(t *testing.T) {
path: []string{"list[]", "listinner"},
startPoint: sitter.Point{
Row: 10,
Column: 4,
Column: 5,
},
},
{
Expand All @@ -132,14 +132,14 @@ func TestSymbolTableForValues(t *testing.T) {
path: []string{"list[]", "nested"},
startPoint: sitter.Point{
Row: 12,
Column: 10,
Column: 11,
},
},
{
path: []string{"list[]", "nested[]", "nestedinList"},
startPoint: sitter.Point{
Row: 13,
Column: 5,
Column: 6,
},
},
{
Expand All @@ -153,13 +153,13 @@ func TestSymbolTableForValues(t *testing.T) {
path: []string{"Values", "dollar[]", "nestedinList"},
startPoint: sitter.Point{
Row: 16,
Column: 5,
Column: 6,
},
},
}

for _, v := range expected {
values := symbolTable.GetValues(v.path)
values := symbolTable.GetTemplateContextRanges(v.path)
points := []sitter.Point{}
for _, v := range values {
points = append(points, v.StartPoint)
Expand Down Expand Up @@ -187,14 +187,14 @@ func TestSymbolTableForValuesTestFile(t *testing.T) {
{
path: []string{"Values", "ingress"},
startPoint: sitter.Point{
Row: 5,
Column: 3,
Row: 0x47,
Column: 0x18,
},
},
}

for _, v := range expected {
values := symbolTable.GetValues(v.path)
values := symbolTable.GetTemplateContextRanges(v.path)
points := []sitter.Point{}
for _, v := range values {
points = append(points, v.StartPoint)
Expand Down
Loading

0 comments on commit 86f2ed1

Please sign in to comment.