Skip to content

Commit

Permalink
fix: configuration is working again
Browse files Browse the repository at this point in the history
  • Loading branch information
qvalentin committed Mar 17, 2024
1 parent e92fdc4 commit 74cedbc
Show file tree
Hide file tree
Showing 11 changed files with 795 additions and 26 deletions.
10 changes: 10 additions & 0 deletions .mockery.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
with-expecter: true
packages:
go.lsp.dev/protocol:
# place your package-specific config here
config:
interfaces:
# select the interfaces you want mocked
Client:
# Modify package-level config for this specific interface (if applicable)
config:
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ test:
@$(GO) test ./... -v -race -tags=integration

coverage:
@$(GO) test -coverprofile=.coverage ./internal/... && go tool cover -html=.coverage
@$(GO) test -coverprofile=.coverage -tags=integration ./internal/... && go tool cover -html=.coverage

.PHONY: build-release
build-release:
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ require (
github.com/shopspring/decimal v1.2.0 // indirect
github.com/spf13/cast v1.5.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/objx v0.5.2 // indirect
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f // indirect
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
go.lsp.dev/pkg v0.0.0-20210717090340-384b27a52fb2 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
Expand Down
5 changes: 2 additions & 3 deletions internal/adapter/yamlls/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@ import (
)

// Configuration implements protocol.Client.
func (y Connector) Configuration(ctx context.Context, params *protocol.ConfigurationParams) (result []interface{}, err error) {
func (y Connector) Configuration(_ context.Context, _ *protocol.ConfigurationParams) (result []interface{}, err error) {
settings := []interface{}{y.config.YamllsSettings}
return settings, nil
}

func (y Connector) DidChangeConfiguration() (err error) {
ctx := context.Background()
func (y Connector) DidChangeConfiguration(ctx context.Context) (err error) {
return y.server.DidChangeConfiguration(ctx, &protocol.DidChangeConfigurationParams{})
}
8 changes: 4 additions & 4 deletions internal/adapter/yamlls/initization.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"go.lsp.dev/uri"
)

func (yamllsConnector Connector) CallInitialize(workspaceURI uri.URI) error {
func (yamllsConnector Connector) CallInitialize(ctx context.Context, workspaceURI uri.URI) error {
if yamllsConnector.server == nil {
return nil
}
Expand All @@ -21,13 +21,13 @@ func (yamllsConnector Connector) CallInitialize(workspaceURI uri.URI) error {
},
}

_, err := yamllsConnector.server.Initialize(context.Background(), &params)
_, err := yamllsConnector.server.Initialize(ctx, &params)
if err != nil {
return err
}
err = yamllsConnector.server.DidChangeConfiguration(context.Background(), &lsp.DidChangeConfigurationParams{})
err = yamllsConnector.server.DidChangeConfiguration(ctx, &lsp.DidChangeConfigurationParams{})
if err != nil {
return err
}
return yamllsConnector.server.Initialized(context.Background(), &lsp.InitializedParams{})
return yamllsConnector.server.Initialized(ctx, &lsp.InitializedParams{})
}
3 changes: 2 additions & 1 deletion internal/adapter/yamlls/integration_test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package yamlls

import (
"context"
"encoding/json"
"os"
"strings"
Expand Down Expand Up @@ -60,7 +61,7 @@ func getYamlLsConnector(t *testing.T, config util.YamllsConfiguration) (*Connect
t.Fatal("Could not connect to yaml-language-server")
}

yamllsConnector.CallInitialize(uri.File(dir))
yamllsConnector.CallInitialize(context.Background(), uri.File(dir))

return yamllsConnector, documents, diagnosticsChan
}
Expand Down
38 changes: 26 additions & 12 deletions internal/handler/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package handler

import (
"context"
"encoding/json"

// "reflect"

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

Expand All @@ -14,30 +15,43 @@ func (h *langHandler) DidChangeConfiguration(ctx context.Context, params *lsp.Di
return nil
}

func (h *langHandler) RetrieveWorkspaceConfiguration(ctx context.Context) {
logger.Println("Calling workspace/configuration")
result := []util.HelmlsConfiguration{util.DefaultConfig}

func (h *langHandler) retrieveWorkspaceConfiguration(ctx context.Context) {
logger.Debug("Calling workspace/configuration")
configurationParams := lsp.ConfigurationParams{
Items: []lsp.ConfigurationItem{{Section: "helm-ls"}},
}
result := h.helmlsConfig

rawResult, err := h.client.Configuration(ctx, &configurationParams)

if err != nil {
logger.Println("Error calling workspace/configuration", err)
} else {
logger.Println("Workspace configuration:", rawResult)
h.initializationWithConfig(ctx)
return
}

if len(result) == 0 {
logger.Debug("Raw Workspace configuration:", rawResult)

if len(rawResult) == 0 {
logger.Println("Workspace configuration is empty")
h.initializationWithConfig(ctx)
return
}

jsonResult, err := json.Marshal(rawResult[0])
if err != nil {
logger.Println("Error marshalling workspace/configuration", err)
h.initializationWithConfig(ctx)
return
}
err = json.Unmarshal(jsonResult, &result)
if err != nil {
logger.Println("Error unmarshalling workspace/configuration", err)
h.initializationWithConfig(ctx)
return
}

// TODO: use retrieved config
h.helmlsConfig = util.DefaultConfig
h.helmlsConfig = result

logger.Println("Workspace configuration:", h.helmlsConfig)
h.initializationWithConfig()
h.initializationWithConfig(ctx)
}
96 changes: 96 additions & 0 deletions internal/handler/configuration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package handler

import (
"context"
"errors"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"

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

var (
configurationParams = lsp.ConfigurationParams{Items: []lsp.ConfigurationItem{{Section: "helm-ls"}}}
handler = &langHandler{
helmlsConfig: util.DefaultConfig,
chartStore: charts.NewChartStore(uri.File("/"), charts.NewChart),
}
)

func TestConfigurationWorks(t *testing.T) {
mockClient := mocks.NewMockClient(t)
handler.client = mockClient

userConfig := []interface{}{map[string]interface{}{
"LogLevel": "debug",
// disable yamlls to avoid configuring it in the test
"yamlls": map[string]interface{}{"enabled": false},
}}
mockClient.EXPECT().Configuration(mock.Anything, &configurationParams).Return(userConfig, nil)
handler.retrieveWorkspaceConfiguration(context.Background())

expectedConfig := util.DefaultConfig
expectedConfig.LogLevel = "debug"
expectedConfig.YamllsConfiguration.Enabled = false
assert.Equal(t, expectedConfig, handler.helmlsConfig)
}

func TestConfigurationWorksForEmptyConfig(t *testing.T) {
mockClient := mocks.NewMockClient(t)
handler.client = mockClient
// disable yamlls to avoid configuring it in the test
handler.helmlsConfig.YamllsConfiguration.Enabled = false

userConfig := []interface{}{}
mockClient.EXPECT().Configuration(mock.Anything, &configurationParams).Return(userConfig, nil)
handler.retrieveWorkspaceConfiguration(context.Background())

expectedConfig := util.DefaultConfig
expectedConfig.YamllsConfiguration.Enabled = false
assert.Equal(t, expectedConfig, handler.helmlsConfig)
}

func TestConfigurationWorksForError(t *testing.T) {
mockClient := mocks.NewMockClient(t)
handler.client = mockClient

// disable yamlls to avoid configuring it in the test
handler.helmlsConfig.YamllsConfiguration.Enabled = false

userConfig := []interface{}{map[string]interface{}{
"LogLevel": "debug",
}}
mockClient.EXPECT().Configuration(mock.Anything, &configurationParams).Return(userConfig, errors.New("error"))
handler.retrieveWorkspaceConfiguration(context.Background())

expectedConfig := util.DefaultConfig
expectedConfig.YamllsConfiguration.Enabled = false
assert.Equal(t, expectedConfig, handler.helmlsConfig)
}

func TestConfigurationWorksForJsonError(t *testing.T) {
mockClient := mocks.NewMockClient(t)
handler.client = mockClient

// disable yamlls to avoid configuring it in the test
handler.helmlsConfig.YamllsConfiguration.Enabled = false

userConfig := []interface{}{map[string]interface{}{
"LogLevel": "debug",
"test": func() {
return
},
}}
mockClient.EXPECT().Configuration(mock.Anything, &configurationParams).Return(userConfig, nil)
handler.retrieveWorkspaceConfiguration(context.Background())

expectedConfig := util.DefaultConfig
expectedConfig.YamllsConfiguration.Enabled = false
assert.Equal(t, expectedConfig, handler.helmlsConfig)
}
10 changes: 5 additions & 5 deletions internal/handler/initialization.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,21 +50,21 @@ func (h *langHandler) Initialize(ctx context.Context, params *lsp.InitializePara
}

func (h *langHandler) Initialized(ctx context.Context, _ *lsp.InitializedParams) (err error) {
go h.RetrieveWorkspaceConfiguration(ctx)
go h.retrieveWorkspaceConfiguration(ctx)
return nil
}

func (h *langHandler) initializationWithConfig() {
func (h *langHandler) initializationWithConfig(ctx context.Context) {
configureLogLevel(h.helmlsConfig)
h.chartStore.SetValuesFilesConfig(h.helmlsConfig.ValuesFilesConfig)
configureYamlls(h)
configureYamlls(ctx, h)
}

func configureYamlls(h *langHandler) {
func configureYamlls(ctx context.Context, h *langHandler) {
config := h.helmlsConfig
if config.YamllsConfiguration.Enabled {
h.yamllsConnector = yamlls.NewConnector(config.YamllsConfiguration, h.client, h.documents)
err := h.yamllsConnector.CallInitialize(h.chartStore.RootURI)
err := h.yamllsConnector.CallInitialize(ctx, h.chartStore.RootURI)
if err != nil {
logger.Error("Error initializing yamlls", err)
}
Expand Down
Loading

0 comments on commit 74cedbc

Please sign in to comment.