Skip to content

Commit

Permalink
wip: integra test
Browse files Browse the repository at this point in the history
  • Loading branch information
qvalentin committed Nov 19, 2024
1 parent 3f37bfa commit 0a444ce
Show file tree
Hide file tree
Showing 15 changed files with 140 additions and 75 deletions.
2 changes: 1 addition & 1 deletion internal/adapter/yamlls/completion_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func TestYamllsCompletionIntegration(t *testing.T) {
tt := tt1
t.Run(tt.desc, func(t *testing.T) {
t.Parallel()
yamllsConnector, documents, _ := getYamlLsConnector(t, config)
yamllsConnector, documents, _ := getYamlLsConnector(t, config, &DefaultCustomHandler)
openFile(t, documents, tt.file, yamllsConnector)

assert.EventuallyWithT(t, func(c *assert.CollectT) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func NewCustomSchemaProviderHandler(provider CustomSchemaProvider) jsonrpc2.Hand
return reply(ctx, nil, nil)
}

logger.Println("YamlHandler: custom/schema/request", req.Params())
logger.Println("YamlHandler: custom/schema/request", string(req.Params()))

if len(params) == 0 {
return reply(ctx, nil, nil)
Expand Down
79 changes: 79 additions & 0 deletions internal/adapter/yamlls/custom_schema_provider_integration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
//go:build integration

package yamlls

import (
"context"
"os"
"path"
"testing"
"time"

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

var TEST_JSON_SCHEMA = `
{
"$id": "https://example.com/address.schema.json",
"$schema": "https://json-schema.org/draft/2020-12/schema",
"description": "An address similar to http://microformats.org/wiki/h-card",
"type": "object",
"properties": {
"postOfficeBox": {
"type": "string"
},
"countryName": {
"type": "string"
}
}
}
`

func TestYamllsCustomSchemaProviderIntegration(t *testing.T) {
config := util.DefaultConfig.YamllsConfiguration
config.Path = "yamlls-debug.sh"

// tempDir := t.TempDir()
tempDir := "/data/data/com.termux/files/usr/tmp/"
schemaFile := path.Join(tempDir, "schema.json")
// write schema
err := os.WriteFile(schemaFile, []byte(TEST_JSON_SCHEMA), 0o644)
assert.NoError(t, err)

testFile := path.Join(tempDir, "test.yaml")
err = os.WriteFile(testFile, []byte("c"), 0o644)
assert.NoError(t, err)

customHandler := NewCustomSchemaHandler(
NewCustomSchemaProviderHandler(
func(ctx context.Context, URI uri.URI) (uri.URI, error) {
t.Log("Calling Schema provider")
return "http://localhost:8000/schema.json", nil
}))

yamllsConnector, documents, _ := getYamlLsConnector(t, config, customHandler)
openFile(t, documents, testFile, yamllsConnector)

assert.EventuallyWithT(t, func(c *assert.CollectT) {
result, _ := yamllsConnector.CallCompletion(context.Background(), &lsp.CompletionParams{
TextDocumentPositionParams: lsp.TextDocumentPositionParams{
TextDocument: lsp.TextDocumentIdentifier{
URI: uri.File(testFile),
},
Position: lsp.Position{
Line: 0,
Character: 1,
},
},
})
assert.NotNil(c, result)
if result == nil {
t.Log("result is nil")
return
}
t.Log("reuslt is", result)
}, time.Second*20, time.Second*2)
}
4 changes: 2 additions & 2 deletions internal/adapter/yamlls/diagnostics_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func TestYamllsDiagnosticsIntegration(t *testing.T) {
Enable: false,
}
config.YamllsSettings = yamllsSettings
yamllsConnector, documents, diagnosticsChan := getYamlLsConnector(t, config)
yamllsConnector, documents, diagnosticsChan := getYamlLsConnector(t, config, &DefaultCustomHandler)

didOpenChan := make(chan string)
go readTestFiles(TEST_DATA_DIR, didOpenChan, doneReadingFilesChan)
Expand Down Expand Up @@ -119,7 +119,7 @@ func TestYamllsDiagnosticsIntegrationWithSchema(t *testing.T) {
diagnosticsChan := make(chan lsp.PublishDiagnosticsParams)

config := util.DefaultConfig.YamllsConfiguration
yamllsConnector, documents, diagnosticsChan := getYamlLsConnector(t, config)
yamllsConnector, documents, diagnosticsChan := getYamlLsConnector(t, config, &DefaultCustomHandler)
file := filepath.Join("..", "..", "..", "testdata", "example", "templates", "service.yaml")
openFile(t, documents, file, yamllsConnector)

Expand Down
2 changes: 1 addition & 1 deletion internal/adapter/yamlls/hover_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func TestYamllsHoverIntegration(t *testing.T) {
tt := tt1
t.Run(tt.desc, func(t *testing.T) {
t.Parallel()
yamllsConnector, documents, _ := getYamlLsConnector(t, config)
yamllsConnector, documents, _ := getYamlLsConnector(t, config, &DefaultCustomHandler)
openFile(t, documents, tt.file, yamllsConnector)

assert.Eventually(t, func() bool {
Expand Down
2 changes: 0 additions & 2 deletions internal/adapter/yamlls/initization.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ func (yamllsConnector Connector) CallInitialize(ctx context.Context, workspaceUR
return err
}

yamllsConnector.customHandler.PostInitialize(ctx, yamllsConnector.conn)

defer func() {
yamllsConnector.customHandler.PostInitialize(ctx, yamllsConnector.conn)

Check failure on line 41 in internal/adapter/yamlls/initization.go

View workflow job for this annotation

GitHub Actions / lint (1.22.7, ubuntu-latest)

Error return value is not checked (errcheck)
}()
Expand Down
4 changes: 2 additions & 2 deletions internal/adapter/yamlls/integration_test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,15 @@ func (proc readWriteCloseMock) Close() error {
return nil
}

func getYamlLsConnector(t *testing.T, config util.YamllsConfiguration) (*Connector, *document.DocumentStore, chan lsp.PublishDiagnosticsParams) {
func getYamlLsConnector(t *testing.T, config util.YamllsConfiguration, customHandler *CustomHandler) (*Connector, *document.DocumentStore, chan lsp.PublishDiagnosticsParams) {
dir := t.TempDir()
documents := document.NewDocumentStore()
diagnosticsChan := make(chan lsp.PublishDiagnosticsParams)
con := jsonrpc2.NewConn(jsonrpc2.NewStream(readWriteCloseMock{diagnosticsChan}))
zapLogger, _ := zap.NewProduction()
client := protocol.ClientDispatcher(con, zapLogger)

yamllsConnector := NewConnector(context.Background(), config, client, documents, DefaultCustomHandler)
yamllsConnector := NewConnector(context.Background(), config, client, documents, customHandler)

if yamllsConnector.server == nil {
t.Fatal("Could not connect to yaml-language-server")
Expand Down
2 changes: 1 addition & 1 deletion internal/adapter/yamlls/symbol_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func TestYamllsDocumentSymoblIntegration(t *testing.T) {
tt := tt1
t.Run(tt.file, func(t *testing.T) {
t.Parallel()
yamllsConnector, documents, _ := getYamlLsConnector(t, config)
yamllsConnector, documents, _ := getYamlLsConnector(t, config, &DefaultCustomHandler)
openFile(t, documents, tt.file, yamllsConnector)

assert.EventuallyWithT(t, func(c *assert.CollectT) {
Expand Down
4 changes: 2 additions & 2 deletions internal/adapter/yamlls/yamlls.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ type Connector struct {
conn jsonrpc2.Conn
documents *document.DocumentStore
client protocol.Client
customHandler CustomHandler
customHandler *CustomHandler
EnabledForFilesGlobObject glob.Glob
}

func NewConnector(ctx context.Context,
yamllsConfiguration util.YamllsConfiguration,
client protocol.Client,
documents *document.DocumentStore,
customHandler CustomHandler,
customHandler *CustomHandler,
) *Connector {
yamllsCmd := exec.Command(yamllsConfiguration.Path, "--stdio")

Expand Down
19 changes: 18 additions & 1 deletion internal/charts/chart_for_document.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package charts
import (
"fmt"
"os"
"path"
"path/filepath"
"strings"

Expand All @@ -17,7 +18,10 @@ func (s *ChartStore) GetChartForDoc(uri lsp.DocumentURI) (*Chart, error) {
return chart, nil
}

chart, err := s.getChartFromFilesystemForTemplates(uri.Filename())
chart, err := s.getChartFromFilesystemForNonTemplates(uri.Filename())
if err != nil {
chart, err = s.getChartFromFilesystemForTemplates(uri.Filename())
}
if err != nil {
return chart, ErrChartNotFound{
URI: uri,
Expand Down Expand Up @@ -46,13 +50,26 @@ func (s *ChartStore) GetChartOrParentForDoc(uri lsp.DocumentURI) (*Chart, error)

func (s *ChartStore) getChartFromCache(uri lsp.DocumentURI) *Chart {
for chartURI, chart := range s.Charts {
// template files
if strings.HasPrefix(uri.Filename(), filepath.Join(chartURI.Filename(), "template")) {
return chart
}
// values.yaml files etc.
if path.Dir(uri.Filename()) == chartURI.Filename() {
return chart
}
}
return nil
}

func (s *ChartStore) getChartFromFilesystemForNonTemplates(path string) (*Chart, error) {
directory := filepath.Dir(path)
if isChartDirectory(directory) {
return s.newChart(uri.File(directory), s.valuesFilesConfig), nil
}
return nil, ErrChartNotFound{}
}

func (s *ChartStore) getChartFromFilesystemForTemplates(path string) (*Chart, error) {
directory := filepath.Dir(path)
if filepath.Base(directory) == "templates" {
Expand Down
30 changes: 30 additions & 0 deletions internal/charts/chart_for_document_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,3 +155,33 @@ func TestGetChartForDocumentWorksForChartWithDependencies(t *testing.T) {
assert.NotNil(t, chartStore.Charts[uri.File(filepath.Join(rootDir, "charts", "subchartexample"))])
assert.NotNil(t, chartStore.Charts[uri.File(filepath.Join(rootDir, "charts", charts.DependencyCacheFolder, "common"))])
}

func TestGetChartForDocumentWorksForValuesFile(t *testing.T) {
var (
rootDir = "../../testdata/dependenciesExample/"
chartStore = charts.NewChartStore(uri.File(rootDir), charts.NewChart, addChartCallback)
)

result1, error := chartStore.GetChartForDoc(uri.File(filepath.Join(rootDir, "values.yaml")))
assert.NoError(t, error)

assert.Len(t, result1.HelmChart.Dependencies(), 2)
assert.Len(t, chartStore.Charts, 3)

assert.NotNil(t, chartStore.Charts[uri.File(rootDir)])
}

func TestGetChartForDocumentWorksForValuesFileWithCache(t *testing.T) {
var (
rootDir = "../../testdata/dependenciesExample/"
chartStore = charts.NewChartStore(uri.File(rootDir), charts.NewChart, addChartCallback)
)

result1, error := chartStore.GetChartForDoc(uri.File(filepath.Join(rootDir, "values.yaml")))
assert.NoError(t, error)
assert.NotNil(t, chartStore.Charts[uri.File(rootDir)])

result2, error := chartStore.GetChartForDoc(uri.File(filepath.Join(rootDir, "values.yaml")))

assert.Same(t, result1, result2)
}
2 changes: 1 addition & 1 deletion internal/handler/template_handler/configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func (h *TemplateHandler) configureYamlls(ctx context.Context, helmlsConfig util
config := helmlsConfig
if config.YamllsConfiguration.Enabled {
h.configureYamlsEnabledGlob(helmlsConfig)
h.setYamllsConnector(yamlls.NewConnector(ctx, config.YamllsConfiguration, h.client, h.documents, yamlls.DefaultCustomHandler))
h.setYamllsConnector(yamlls.NewConnector(ctx, config.YamllsConfiguration, h.client, h.documents, &yamlls.DefaultCustomHandler))
err := h.yamllsConnector.CallInitialize(ctx, h.chartStore.RootURI)
if err != nil {
logger.Error("Error initializing yamlls", err)
Expand Down
2 changes: 1 addition & 1 deletion internal/handler/yaml_handler/configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func (h *YamlHandler) configureYamlls(ctx context.Context, helmlsConfig util.Hel
config.YamllsConfiguration,
h.client,
h.documents,
*yamlls.NewCustomSchemaHandler(
yamlls.NewCustomSchemaHandler(
yamlls.NewCustomSchemaProviderHandler(h.CustomSchemaProvider),
),
)
Expand Down
39 changes: 1 addition & 38 deletions internal/handler/yaml_handler/yaml_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ func (h *YamlHandler) CustomSchemaProvider(ctx context.Context, URI uri.URI) (ur
chart, err := h.chartStore.GetChartForDoc(URI)
if err != nil {
logger.Error(err)
// we can ignore the error, providing a wrong schema is still useful
}
schemaFilePath, err := jsonschema.CreateJsonSchemaForChart(chart)
if err != nil {
Expand All @@ -64,41 +65,3 @@ func (h *YamlHandler) CustomSchemaProvider(ctx context.Context, URI uri.URI) (ur
}
return uri.File(schemaFilePath), nil
}

func (h *YamlHandler) CustomHandler(ctx context.Context, reply jsonrpc2.Replier, req jsonrpc2.Request) error {
switch req.Method() {
case "custom/schema/request":

params := []string{}
jsonBytes, err := req.Params().MarshalJSON()
if err != nil {
logger.Error(err)
return reply(ctx, nil, nil)
}

err = json.Unmarshal(jsonBytes, &params)
if err != nil {
logger.Error(err)
return reply(ctx, nil, nil)
}

logger.Println("YamlHandler: custom/schema/request", req.Params())

if len(params) == 0 {
return reply(ctx, nil, nil)
}
chart, err := h.chartStore.GetChartForDoc(uri.New(params[0]))
if err != nil {
logger.Error(err)
}
schemaFilePath, err := jsonschema.CreateJsonSchemaForChart(chart)
if err != nil {
logger.Error(err)
return reply(ctx, nil, nil)
}

return reply(ctx, uri.New(schemaFilePath), nil)
}

return jsonrpc2.MethodNotFoundHandler(ctx, reply, req)
}
22 changes: 0 additions & 22 deletions internal/json_schema/json_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@ import (
var logger = log.GetLogger()

func CreateJsonSchemaForChart(chart *charts.Chart) (string, error) {
// reflector := jsonschema.Reflector{
// ExpandedStruct: true,
// AllowAdditionalProperties: true,
// }

schema, err := GenerateJSONSchema(chart.ValuesFiles.MainValuesFile.Values)

bytes, err := json.Marshal(schema)
Expand All @@ -26,8 +21,6 @@ func CreateJsonSchemaForChart(chart *charts.Chart) (string, error) {
return "", err
}

// create a tmp file and write the schema

file, err := os.CreateTemp("", base64.StdEncoding.EncodeToString([]byte(chart.RootURI.Filename())))
if err != nil {
logger.Error(err)
Expand All @@ -42,18 +35,3 @@ func CreateJsonSchemaForChart(chart *charts.Chart) (string, error) {

return file.Name(), nil
}

// func GenerateSchemaFromData(data interface{}) error {
// jsonBytes, err := json.Marshal(data)
// if err != nil {
// return nil, err
// }
//
// documentLoader := gojsonschema.NewStringLoader(string(jsonBytes))
// schema, err := gojsonschema.NewSchema(documentLoader)
// if err != nil {
// return nil, err
// }
//
// return schema.Root(), nil
// }

0 comments on commit 0a444ce

Please sign in to comment.