Skip to content

Commit

Permalink
Merge pull request #69 from mrjosh/fix/subchart-lints
Browse files Browse the repository at this point in the history
fix(linting): use parent chart for linting subchart files
  • Loading branch information
qvalentin authored Mar 30, 2024
2 parents e9c311f + 3b83154 commit 0e3cd8c
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 7 deletions.
16 changes: 16 additions & 0 deletions internal/charts/chart_for_document.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,22 @@ func (s *ChartStore) GetChartForDoc(uri lsp.DocumentURI) (*Chart, error) {
return chart, nil
}

func (s *ChartStore) GetChartOrParentForDoc(uri lsp.DocumentURI) (*Chart, error) {
chart, err := s.GetChartForDoc(uri)
if err != nil {
return chart, err
}

if chart.ParentChart.HasParent {
parentChart := chart.ParentChart.GetParentChartRecursive(s)
if parentChart == nil {
return chart, err
}
return parentChart, nil
}
return chart, nil
}

func (s *ChartStore) getChartFromCache(uri lsp.DocumentURI) *Chart {
for chartURI, chart := range s.Charts {
if strings.HasPrefix(uri.Filename(), filepath.Join(chartURI.Filename(), "template")) {
Expand Down
41 changes: 41 additions & 0 deletions internal/charts/chart_for_document_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,44 @@ func TestGetChartForDocumentWorksForNewToAddChartWithNestedFile(t *testing.T) {

assert.Same(t, expectedChart, chartStore.Charts[uri.File(expectedChartDirectory)])
}

func TestGetChartOrParentForDocWorks(t *testing.T) {
chartStore := charts.NewChartStore("file:///tmp", func(uri uri.URI, _ util.ValuesFilesConfig) *charts.Chart {
return &charts.Chart{RootURI: uri}
})

chart := &charts.Chart{}
chartStore.Charts["file:///tmp/chart"] = chart
subchart := &charts.Chart{
ValuesFiles: &charts.ValuesFiles{},
ChartMetadata: &charts.ChartMetadata{},
RootURI: "file:///tmp/chart/charts/subchart",
ParentChart: charts.ParentChart{
ParentChartURI: "file:///tmp/chart",
HasParent: true,
},
}
chartStore.Charts["file:///tmp/chart/charts/subchart"] = subchart
otherchart := &charts.Chart{}
chartStore.Charts["file:///tmp/otherChart"] = otherchart

result1, error := chartStore.GetChartOrParentForDoc("file:///tmp/chart/templates/deployment.yaml")
assert.NoError(t, error)
assert.Same(t, chart, result1)

result2, error := chartStore.GetChartOrParentForDoc("file:///tmp/chart/templates/directory/deployment.yaml")
assert.NoError(t, error)
assert.Same(t, chart, result2)

result3, error := chartStore.GetChartOrParentForDoc("file:///tmp/chart/charts/subchart/templates/directory/deployment.yaml")
assert.NoError(t, error)
assert.Same(t, chart, result3)

result4, error := chartStore.GetChartOrParentForDoc("file:///tmp/otherChart/templates/directory/deployment.yaml")
assert.NoError(t, error)
assert.Same(t, otherchart, result4)

result5, error := chartStore.GetChartOrParentForDoc("file:///tmp/directory/deployment.yaml")
assert.Error(t, error)
assert.Equal(t, &charts.Chart{RootURI: uri.File("/tmp")}, result5)
}
12 changes: 12 additions & 0 deletions internal/charts/parent_chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,15 @@ func (p *ParentChart) GetParentChart(chartStore *ChartStore) *Chart {
}
return chart
}

func (p *ParentChart) GetParentChartRecursive(chartStore *ChartStore) *Chart {
chart := p.GetParentChart(chartStore)
if chart == nil {
return nil
}
parentChart := chart.ParentChart.GetParentChartRecursive(chartStore)
if parentChart == nil {
return chart
}
return parentChart
}
9 changes: 2 additions & 7 deletions internal/handler/text_document.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,11 @@ func (h *langHandler) DidOpen(ctx context.Context, params *lsp.DidOpenTextDocume

h.yamllsConnector.DocumentDidOpen(doc.Ast, *params)

_, err = h.chartStore.GetChartForDoc(doc.URI)
if err != nil {
logger.Error("Error getting chart info for file", doc.URI, err)
}

doc, ok := h.documents.Get(params.TextDocument.URI)
if !ok {
return errors.New("Could not get document: " + params.TextDocument.URI.Filename())
}
chart, err := h.chartStore.GetChartForDoc(doc.URI)
chart, err := h.chartStore.GetChartOrParentForDoc(doc.URI)
if err != nil {
logger.Error("Error getting chart info for file", doc.URI, err)
}
Expand All @@ -45,7 +40,7 @@ func (h *langHandler) DidSave(ctx context.Context, params *lsp.DidSaveTextDocume
if !ok {
return errors.New("Could not get document: " + params.TextDocument.URI.Filename())
}
chart, err := h.chartStore.GetChartForDoc(doc.URI)
chart, err := h.chartStore.GetChartOrParentForDoc(doc.URI)
if err != nil {
logger.Error("Error getting chart info for file", doc.URI, err)
}
Expand Down

0 comments on commit 0e3cd8c

Please sign in to comment.