Skip to content

Commit

Permalink
callback instead of newChart
Browse files Browse the repository at this point in the history
  • Loading branch information
qvalentin committed Aug 24, 2024
1 parent ac8a70d commit e839bd9
Show file tree
Hide file tree
Showing 14 changed files with 49 additions and 41 deletions.
2 changes: 1 addition & 1 deletion cmds/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func newLintCmd() *cobra.Command {
}

rootPath := uri.File(args[0])
chartStore := charts.NewChartStore(rootPath, charts.NewChart)
chartStore := charts.NewChartStore(rootPath, charts.NewChart, func(chart *charts.Chart) {})
chart, err := chartStore.GetChartForURI(rootPath)
if err != nil {
return err
Expand Down
10 changes: 5 additions & 5 deletions internal/charts/chart_for_document_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
func TestGetChartForDocumentWorksForAlreadyAddedCharts(t *testing.T) {
chartStore := charts.NewChartStore("file:///tmp", func(uri uri.URI, _ util.ValuesFilesConfig) *charts.Chart {
return &charts.Chart{RootURI: uri}
})
}, addChartCallback)

chart := &charts.Chart{}
chartStore.Charts["file:///tmp/chart"] = chart
Expand Down Expand Up @@ -58,7 +58,7 @@ func TestGetChartForDocumentWorksForNewToAddChart(t *testing.T) {
HelmChart: &chart.Chart{},
}
newChartFunc = func(_ uri.URI, _ util.ValuesFilesConfig) *charts.Chart { return expectedChart }
chartStore = charts.NewChartStore(uri.File(rootDir), newChartFunc)
chartStore = charts.NewChartStore(uri.File(rootDir), newChartFunc, addChartCallback)
err = os.MkdirAll(expectedChartDirectory, 0o755)
)
assert.NoError(t, err)
Expand All @@ -84,7 +84,7 @@ func TestGetChartForDocumentWorksForNewToAddChartWithNestedFile(t *testing.T) {
HelmChart: &chart.Chart{},
}
newChartFunc = func(_ uri.URI, _ util.ValuesFilesConfig) *charts.Chart { return expectedChart }
chartStore = charts.NewChartStore(uri.File(rootDir), newChartFunc)
chartStore = charts.NewChartStore(uri.File(rootDir), newChartFunc, addChartCallback)
err = os.MkdirAll(expectedChartDirectory, 0o755)
)
assert.NoError(t, err)
Expand All @@ -101,7 +101,7 @@ func TestGetChartForDocumentWorksForNewToAddChartWithNestedFile(t *testing.T) {
func TestGetChartOrParentForDocWorks(t *testing.T) {
chartStore := charts.NewChartStore("file:///tmp", func(uri uri.URI, _ util.ValuesFilesConfig) *charts.Chart {
return &charts.Chart{RootURI: uri}
})
}, addChartCallback)

chart := &charts.Chart{}
chartStore.Charts["file:///tmp/chart"] = chart
Expand Down Expand Up @@ -142,7 +142,7 @@ func TestGetChartOrParentForDocWorks(t *testing.T) {
func TestGetChartForDocumentWorksForChartWithDependencies(t *testing.T) {
var (
rootDir = "../../testdata/dependenciesExample/"
chartStore = charts.NewChartStore(uri.File(rootDir), charts.NewChart)
chartStore = charts.NewChartStore(uri.File(rootDir), charts.NewChart, addChartCallback)
)

result1, error := chartStore.GetChartForDoc(uri.File(filepath.Join(rootDir, "templates", "deployment.yaml")))
Expand Down
5 changes: 4 additions & 1 deletion internal/charts/chart_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@ type ChartStore struct {
Charts map[uri.URI]*Chart
RootURI uri.URI
newChart func(uri.URI, util.ValuesFilesConfig) *Chart
addChartCallback func(chart *Chart)
valuesFilesConfig util.ValuesFilesConfig
}

func NewChartStore(rootURI uri.URI, newChart func(uri.URI, util.ValuesFilesConfig) *Chart) *ChartStore {
func NewChartStore(rootURI uri.URI, newChart func(uri.URI, util.ValuesFilesConfig) *Chart, addChartCallback func(chart *Chart)) *ChartStore {
return &ChartStore{
Charts: map[uri.URI]*Chart{},
RootURI: rootURI,
newChart: newChart,
addChartCallback: addChartCallback,
valuesFilesConfig: util.DefaultConfig.ValuesFilesConfig,
}
}
Expand All @@ -27,6 +29,7 @@ func NewChartStore(rootURI uri.URI, newChart func(uri.URI, util.ValuesFilesConfi
func (s *ChartStore) AddChart(chart *Chart) {
s.Charts[chart.RootURI] = chart
s.loadChartDependencies(chart)
s.addChartCallback(chart)
}

func (s *ChartStore) SetValuesFilesConfig(valuesFilesConfig util.ValuesFilesConfig) {
Expand Down
10 changes: 6 additions & 4 deletions internal/charts/chart_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"helm.sh/helm/v3/pkg/chartutil"
)

var addChartCallback = func(chart *Chart) {}

func TestSetValuesFilesConfigOverwrites(t *testing.T) {
valuesFilesConfig := util.ValuesFilesConfig{
MainValuesFileName: "value.yaml",
Expand All @@ -25,7 +27,7 @@ func TestSetValuesFilesConfigOverwrites(t *testing.T) {
_ = os.WriteFile(filepath.Join(tempDir, "value.yaml"), []byte("foo: main"), 0o644)
_ = os.WriteFile(filepath.Join(tempDir, "something.yaml"), []byte(valuesContent), 0o644)
_ = os.WriteFile(filepath.Join(tempDir, "values.other.yaml"), []byte(valuesContent), 0o644)
s := NewChartStore(uri.File(tempDir), NewChart)
s := NewChartStore(uri.File(tempDir), NewChart, addChartCallback)

chartOld, err := s.GetChartForURI(uri.File(tempDir))
assert.Equal(t, chartutil.Values{}, chartOld.ValuesFiles.MainValuesFile.Values)
Expand Down Expand Up @@ -54,7 +56,7 @@ func TestSetValuesFilesConfigDoesNotOverwrite(t *testing.T) {
_ = os.WriteFile(filepath.Join(tempDir, "something.yaml"), []byte(valuesContent), 0o644)
_ = os.WriteFile(filepath.Join(tempDir, "values.lint.yaml"), []byte(valuesContent), 0o644)
_ = os.WriteFile(filepath.Join(tempDir, "values.other.yaml"), []byte(valuesContent), 0o644)
s := NewChartStore(uri.File(tempDir), NewChart)
s := NewChartStore(uri.File(tempDir), NewChart, addChartCallback)

chart, err := s.GetChartForURI(uri.File(tempDir))
assert.NoError(t, err)
Expand All @@ -71,7 +73,7 @@ func TestGetChartForURIWhenChartYamlDoesNotExist(t *testing.T) {
tempDir := t.TempDir()

_ = os.WriteFile(filepath.Join(tempDir, "values.yaml"), []byte("foo: main"), 0o644)
s := NewChartStore(uri.File(tempDir), NewChart)
s := NewChartStore(uri.File(tempDir), NewChart, addChartCallback)

chart, err := s.GetChartForURI(uri.File(tempDir))
assert.Error(t, err)
Expand All @@ -90,7 +92,7 @@ func TestReloadValuesFiles(t *testing.T) {
RootURI: uri.File(tempDir),
ParentChart: ParentChart{},
}
s := NewChartStore(uri.File(tempDir), NewChart)
s := NewChartStore(uri.File(tempDir), NewChart, addChartCallback)
s.Charts[chart.RootURI] = chart

assert.Equal(t, "bar", chart.ValuesFiles.MainValuesFile.Values["foo"])
Expand Down
16 changes: 9 additions & 7 deletions internal/charts/chart_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import (
"github.com/stretchr/testify/assert"
)

var addChartCallback = func(chart *charts.Chart) {}

func TestNewChartsLoadsMetadata(t *testing.T) {
tempDir := t.TempDir()

Expand Down Expand Up @@ -87,7 +89,7 @@ func TestResolvesValuesFileOfParent(t *testing.T) {
HelmChart: &chart.Chart{},
}
newChartFunc := func(_ uri.URI, _ util.ValuesFilesConfig) *charts.Chart { return expectedChart }
chartStore := charts.NewChartStore(uri.File(tempDir), newChartFunc)
chartStore := charts.NewChartStore(uri.File(tempDir), newChartFunc, addChartCallback)

valueFiles := sut.ResolveValueFiles([]string{"global", "foo"}, chartStore)

Expand Down Expand Up @@ -124,7 +126,7 @@ func TestResolvesValuesFileOfParentByName(t *testing.T) {
HelmChart: &chart.Chart{},
}
newChartFunc := func(_ uri.URI, _ util.ValuesFilesConfig) *charts.Chart { return expectedChart }
chartStore := charts.NewChartStore(uri.File(tempDir), newChartFunc)
chartStore := charts.NewChartStore(uri.File(tempDir), newChartFunc, addChartCallback)

valueFiles := subchart.ResolveValueFiles([]string{"foo"}, chartStore)

Expand All @@ -138,7 +140,7 @@ func TestResolvesValuesFileOfParentByName(t *testing.T) {
func TestResolvesValuesFileOfDependencyWithGlobal(t *testing.T) {
var (
rootDir = "../../testdata/dependenciesExample"
chartStore = charts.NewChartStore(uri.File(rootDir), charts.NewChart)
chartStore = charts.NewChartStore(uri.File(rootDir), charts.NewChart, addChartCallback)
chart, err = chartStore.GetChartForDoc(uri.File(filepath.Join(rootDir, "templates", "deployment.yaml")))
valueFiles = chart.ResolveValueFiles([]string{"global"}, chartStore)
)
Expand All @@ -156,7 +158,7 @@ func TestResolvesValuesFileOfDependencyWithGlobal(t *testing.T) {
func TestResolvesValuesFileOfDependencyWithChartName(t *testing.T) {
var (
rootDir = "../../testdata/dependenciesExample"
chartStore = charts.NewChartStore(uri.File(rootDir), charts.NewChart)
chartStore = charts.NewChartStore(uri.File(rootDir), charts.NewChart, addChartCallback)
chart, err = chartStore.GetChartForDoc(uri.File(filepath.Join(rootDir, "templates", "deployment.yaml")))
valueFiles = chart.ResolveValueFiles([]string{"subchartexample", "foo"}, chartStore)
)
Expand All @@ -175,7 +177,7 @@ func TestResolvesValuesFileOfDependencyWithChartName(t *testing.T) {
func TestResolvesValuesFileOfDependencyWithOnlyChartName(t *testing.T) {
var (
rootDir = "../../testdata/dependenciesExample"
chartStore = charts.NewChartStore(uri.File(rootDir), charts.NewChart)
chartStore = charts.NewChartStore(uri.File(rootDir), charts.NewChart, addChartCallback)
chart, err = chartStore.GetChartForDoc(uri.File(filepath.Join(rootDir, "templates", "deployment.yaml")))
valueFiles = chart.ResolveValueFiles([]string{"subchartexample"}, chartStore)
)
Expand All @@ -194,7 +196,7 @@ func TestResolvesValuesFileOfDependencyWithOnlyChartName(t *testing.T) {
func TestResolvesValuesFileOfDependencyWithChartNameForPackedDependency(t *testing.T) {
var (
rootDir = "../../testdata/dependenciesExample"
chartStore = charts.NewChartStore(uri.File(rootDir), charts.NewChart)
chartStore = charts.NewChartStore(uri.File(rootDir), charts.NewChart, addChartCallback)
chart, err = chartStore.GetChartForDoc(uri.File(filepath.Join(rootDir, "templates", "deployment.yaml")))
valueFiles = chart.ResolveValueFiles([]string{"common", "exampleValue"}, chartStore)
)
Expand Down Expand Up @@ -238,7 +240,7 @@ func TestLoadsHelmChartWithDependecies(t *testing.T) {
func TestGetValueLocation(t *testing.T) {
chart := charts.NewChart(uri.File("../../testdata/dependenciesExample/"), util.ValuesFilesConfig{})

valueLocation, err := chart.GetValueLocation([]string{"Name"})
valueLocation, err := chart.GetMetadataLocation([]string{"Name"})
assert.NoError(t, err)

expected := lsp.Location{
Expand Down
2 changes: 1 addition & 1 deletion internal/handler/completion_main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ func completionTestCall(fileURI uri.URI, buf string, pos lsp.Position) (*lsp.Com
}
documents.DidOpen(&d, util.DefaultConfig)
h := &langHandler{
chartStore: charts.NewChartStore(uri.File("."), charts.NewChart),
chartStore: charts.NewChartStore(uri.File("."), charts.NewChart, func(chart *charts.Chart) {}),
documents: documents,
yamllsConnector: &yamlls.Connector{},
}
Expand Down
8 changes: 4 additions & 4 deletions internal/handler/configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestConfigurationWorks(t *testing.T) {
mockClient := mocks.NewMockClient(t)
handler := &langHandler{
helmlsConfig: util.DefaultConfig,
chartStore: charts.NewChartStore(uri.File("/"), charts.NewChart),
chartStore: charts.NewChartStore(uri.File("/"), charts.NewChart, addChartCallback),
}
handler.client = mockClient

Expand All @@ -43,7 +43,7 @@ func TestConfigurationWorksForEmptyConfig(t *testing.T) {
mockClient := mocks.NewMockClient(t)
handler := &langHandler{
helmlsConfig: util.DefaultConfig,
chartStore: charts.NewChartStore(uri.File("/"), charts.NewChart),
chartStore: charts.NewChartStore(uri.File("/"), charts.NewChart, addChartCallback),
}
handler.client = mockClient
// disable yamlls to avoid configuring it in the test
Expand All @@ -62,7 +62,7 @@ func TestConfigurationWorksForError(t *testing.T) {
mockClient := mocks.NewMockClient(t)
handler := &langHandler{
helmlsConfig: util.DefaultConfig,
chartStore: charts.NewChartStore(uri.File("/"), charts.NewChart),
chartStore: charts.NewChartStore(uri.File("/"), charts.NewChart, addChartCallback),
}
handler.client = mockClient

Expand All @@ -84,7 +84,7 @@ func TestConfigurationWorksForJsonError(t *testing.T) {
mockClient := mocks.NewMockClient(t)
handler := &langHandler{
helmlsConfig: util.DefaultConfig,
chartStore: charts.NewChartStore(uri.File("/"), charts.NewChart),
chartStore: charts.NewChartStore(uri.File("/"), charts.NewChart, addChartCallback),
}
handler.client = mockClient

Expand Down
3 changes: 2 additions & 1 deletion internal/handler/definition_chart_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,8 @@ func TestDefinitionChart(t *testing.T) {

chart := charts.NewChart(rootUri, util.DefaultConfig.ValuesFilesConfig)

chartStore := charts.NewChartStore(rootUri, charts.NewChart)
var addChartCallback = func(chart *charts.Chart) {}
chartStore := charts.NewChartStore(rootUri, charts.NewChart,addChartCallback)
_, err = chartStore.GetChartForURI(rootUri)
h := &langHandler{
chartStore: chartStore,
Expand Down
6 changes: 3 additions & 3 deletions internal/handler/definition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func genericDefinitionTest(t *testing.T, position lsp.Position, expectedLocation
},
}
documents.DidOpen(&d, util.DefaultConfig)
chartStore := charts.NewChartStore(rootUri, charts.NewChart)
chartStore := charts.NewChartStore(rootUri, charts.NewChart, addChartCallback)
chartStore.Charts = map[uri.URI]*charts.Chart{rootUri: testChart}
h := &langHandler{
chartStore: chartStore,
Expand Down Expand Up @@ -275,7 +275,7 @@ func genericDefinitionTestMultipleValuesFiles(t *testing.T, position lsp.Positio
},
}
documents.DidOpen(&d, util.DefaultConfig)
chartStore := charts.NewChartStore(rootUri, charts.NewChart)
chartStore := charts.NewChartStore(rootUri, charts.NewChart, addChartCallback)
chartStore.Charts = map[uri.URI]*charts.Chart{rootUri: chart}
h := &langHandler{
chartStore: chartStore,
Expand Down Expand Up @@ -368,7 +368,7 @@ func TestDefinitionSingleLine(t *testing.T) {
}
documents.DidOpen(&d, util.DefaultConfig)
h := &langHandler{
chartStore: charts.NewChartStore(rootUri, charts.NewChart),
chartStore: charts.NewChartStore(rootUri, charts.NewChart, addChartCallback),
documents: documents,
}

Expand Down
4 changes: 3 additions & 1 deletion internal/handler/hover_main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,10 @@ func TestHoverMain(t *testing.T) {
},
}
documents.DidOpen(&d, util.DefaultConfig)

addChartCallback := func(chart *charts.Chart) {}
h := &langHandler{
chartStore: charts.NewChartStore(uri.File("."), charts.NewChart),
chartStore: charts.NewChartStore(uri.File("."), charts.NewChart, addChartCallback),
documents: documents,
yamllsConnector: &yamlls.Connector{},
}
Expand Down
7 changes: 3 additions & 4 deletions internal/handler/initialization.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (h *langHandler) Initialize(ctx context.Context, params *lsp.InitializePara
}

logger.Debug("Initializing chartStore")
h.chartStore = charts.NewChartStore(workspaceURI, h.NewChartWithInitActions)
h.chartStore = charts.NewChartStore(workspaceURI, charts.NewChart, h.NewChartWithInitActions)

logger.Debug("Initializing done")
return &lsp.InitializeResult{
Expand Down Expand Up @@ -94,8 +94,7 @@ func configureLogLevel(helmlsConfig util.HelmlsConfiguration) {
}
}

func (h *langHandler) NewChartWithInitActions(rootURI uri.URI, valuesFilesConfig util.ValuesFilesConfig) *charts.Chart {
chart := h.NewChartWithWatchedFiles(rootURI, valuesFilesConfig)
func (h *langHandler) NewChartWithInitActions(chart *charts.Chart) {
h.NewChartWithWatchedFiles(chart)
go h.LoadDocsOnNewChart(chart)
return chart
}
8 changes: 5 additions & 3 deletions internal/handler/references_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import (
"go.lsp.dev/uri"
)

var addChartCallback = func(chart *charts.Chart) {}

func TestRefercesTemplateContext(t *testing.T) {
content := `
{{ .Values.test }}
Expand Down Expand Up @@ -94,7 +96,7 @@ func TestRefercesTemplateContext(t *testing.T) {
}
documents.DidOpen(&d, util.DefaultConfig)
h := &langHandler{
chartStore: charts.NewChartStore(uri.File("."), charts.NewChart),
chartStore: charts.NewChartStore(uri.File("."), charts.NewChart, addChartCallback),
documents: documents,
yamllsConnector: &yamlls.Connector{},
}
Expand Down Expand Up @@ -161,7 +163,7 @@ func TestRefercesTemplateContextWithTestFile(t *testing.T) {
}
documents.DidOpen(&d, util.DefaultConfig)
h := &langHandler{
chartStore: charts.NewChartStore(uri.File("."), charts.NewChart),
chartStore: charts.NewChartStore(uri.File("."), charts.NewChart, addChartCallback),
documents: documents,
yamllsConnector: &yamlls.Connector{},
}
Expand Down Expand Up @@ -219,7 +221,7 @@ func TestRefercesSingleLines(t *testing.T) {
}
documents.DidOpen(&d, util.DefaultConfig)
h := &langHandler{
chartStore: charts.NewChartStore(uri.File("."), charts.NewChart),
chartStore: charts.NewChartStore(uri.File("."), charts.NewChart, addChartCallback),
documents: documents,
yamllsConnector: &yamlls.Connector{},
}
Expand Down
7 changes: 2 additions & 5 deletions internal/handler/watched_files.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,21 @@ import (
"context"

"github.com/mrjosh/helm-ls/internal/charts"
"github.com/mrjosh/helm-ls/internal/util"
lsp "go.lsp.dev/protocol"
"go.lsp.dev/uri"

"go.lsp.dev/jsonrpc2"
)

func (h *langHandler) NewChartWithWatchedFiles(rootURI uri.URI, valuesFilesConfig util.ValuesFilesConfig) *charts.Chart {
logger.Debug("NewChartWithWatchedFiles", rootURI, valuesFilesConfig)
chart := charts.NewChart(rootURI, valuesFilesConfig)
func (h *langHandler) NewChartWithWatchedFiles(chart *charts.Chart) {
logger.Debug("NewChartWithWatchedFiles ", chart.RootURI)

uris := make([]uri.URI, 0)
for _, valuesFile := range chart.ValuesFiles.AllValuesFiles() {
uris = append(uris, valuesFile.URI)
}

go h.RegisterWatchedFiles(context.Background(), h.connPool, uris)
return chart
}

func (h *langHandler) RegisterWatchedFiles(ctx context.Context, conn jsonrpc2.Conn, files []uri.URI) {
Expand Down
2 changes: 1 addition & 1 deletion internal/language_features/template_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func (f *TemplateContextFeature) getDefinitionLocations(templateContext lsplocal
}
return locations
case "Chart":
location, _ := f.Chart.GetValueLocation(templateContext.Tail())
location, _ := f.Chart.GetMetadataLocation(templateContext.Tail())
return []lsp.Location{location}
}
return locations
Expand Down

0 comments on commit e839bd9

Please sign in to comment.