Skip to content

Commit

Permalink
fix: prevent panic on empty or unsupported template context
Browse files Browse the repository at this point in the history
fixes #81
  • Loading branch information
qvalentin committed May 19, 2024
1 parent c50176c commit fafbe46
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 4 deletions.
1 change: 1 addition & 0 deletions internal/handler/completion_main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ func TestCompletionMainSingleLines(t *testing.T) {
{`Test completion on {{ range .Values.ingress.hosts }} {{ .^ }} {{ end }}`, []string{"host", "paths"}, []string{}, nil},
{`Test completion on {{ range .Values.ingress.hosts }} {{ .ho^ }} {{ end }}`, []string{"host", "paths"}, []string{}, nil},
{`Test completion on {{ range .Values.ingress.hosts }} {{ range .paths }} {{ .^ }} {{ end }} {{ end }}`, []string{"pathType", "path"}, []string{}, nil},
{`Test completion on {{ root := . }} {{ $root.test.^ }}`, []string{}, []string{}, errors.New("[$root test ] is no valid template context for helm")},
}

for _, tt := range testCases {
Expand Down
10 changes: 10 additions & 0 deletions internal/handler/hover_main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package handler

import (
"context"
"errors"
"fmt"
"os"
"path/filepath"
Expand Down Expand Up @@ -113,6 +114,15 @@ func TestHoverMain(t *testing.T) {
expected: fmt.Sprintf("### %s\n%s\n\n", filepath.Join("..", "..", "testdata", "example", "values.yaml"), "1"),
expectedError: nil,
},
{
desc: "Test hover on template context with variables",
position: lsp.Position{
Line: 74,
Character: 50,
},
expected: "",
expectedError: errors.New("no template context found"),
},
}
for _, tt := range testCases {
t.Run(tt.desc, func(t *testing.T) {
Expand Down
5 changes: 3 additions & 2 deletions internal/language_features/built_in_objects.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (f *BuiltInObjectsFeature) AppropriateForNode() bool {

func (f *BuiltInObjectsFeature) References() (result []lsp.Location, err error) {
templateContext, err := f.getTemplateContext()
if err != nil {
if err != nil || len(templateContext) == 0 {
return []lsp.Location{}, err
}

Expand All @@ -67,7 +67,8 @@ func (f *BuiltInObjectsFeature) getDefinitionLocations(templateContext lsplocal.
}

func (f *BuiltInObjectsFeature) Hover() (string, error) {
templateContext, _ := f.getTemplateContext()
templateContext, err := f.getTemplateContext()

Check failure on line 70 in internal/language_features/built_in_objects.go

View workflow job for this annotation

GitHub Actions / lint (1.21.5, ubuntu-latest)

ineffectual assignment to err (ineffassign)

docs, err := f.builtInOjectDocsLookup(templateContext[0], helmdocs.BuiltInObjects)
return docs.Doc, err
}
Expand Down
7 changes: 5 additions & 2 deletions internal/language_features/template_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (f *TemplateContextFeature) AppropriateForNode() bool {

func (f *TemplateContextFeature) References() (result []lsp.Location, err error) {
templateContext, err := f.getTemplateContext()
if err != nil {
if err != nil || len(templateContext) == 0 {
return []lsp.Location{}, err
}

Expand All @@ -45,7 +45,7 @@ func (f *TemplateContextFeature) References() (result []lsp.Location, err error)

func (f *TemplateContextFeature) Definition() (result []lsp.Location, err error) {
templateContext, err := f.getTemplateContext()
if err != nil {
if err != nil || len(templateContext) == 0 {
return []lsp.Location{}, err
}
return f.getDefinitionLocations(templateContext), nil
Expand Down Expand Up @@ -81,6 +81,9 @@ func (f *TemplateContextFeature) getDefinitionLocations(templateContext lsplocal

func (f *TemplateContextFeature) Hover() (string, error) {
templateContext, err := f.getTemplateContext()
if err != nil || len(templateContext) == 0 {
return "", err
}

switch templateContext[0] {
case "Values":
Expand Down
11 changes: 11 additions & 0 deletions testdata/example/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,14 @@ spec:
{{- end }}
{{- end }}
hosts: {{ .Values.ingress.hosts }}

{{- $root := . -}}
{{- range $type, $config := $root.Values.deployments }}
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app-{{ $type }}
spec:
replicas: {{ $config.hpa.minReplicas }}
---
{{- end }}

0 comments on commit fafbe46

Please sign in to comment.