Skip to content

Commit

Permalink
fix(windows): support windows
Browse files Browse the repository at this point in the history
  • Loading branch information
qvalentin committed Feb 3, 2024
1 parent 0a96b2b commit 970d195
Show file tree
Hide file tree
Showing 31 changed files with 229 additions and 485 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/artifacts.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
with:
go-version: ${{ matrix.go-version }}
- name: Checkout code
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: build
run: |
make build-release
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ jobs:
steps:

- name: Install Go
uses: actions/setup-go@v2
uses: actions/setup-go@v4
with:
go-version: ${{ matrix.go-version }}

- name: Checkout code
uses: actions/checkout@v2
uses: actions/checkout@v4

- name: Lint
run: make lint
32 changes: 30 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ on:
branches:
- "master"
pull_request:
types: [synchronize]

jobs:

Expand All @@ -18,12 +19,39 @@ jobs:
steps:

- name: Install Go
uses: actions/setup-go@v2
uses: actions/setup-go@v4
with:
go-version: ${{ matrix.go-version }}

- name: Checkout code
uses: actions/checkout@v2
uses: actions/checkout@v4

- name: Run tests
run: make test

tests-windows:
name: tests
strategy:
matrix:
go-version: [1.19.1]
os: [windows-2019]
runs-on: ${{ matrix.os }}
steps:

- name: Install Go
uses: actions/setup-go@v4
with:
go-version: ${{ matrix.go-version }}

- name: Set up MinGW
uses: egor-tensin/setup-mingw@v2
with:
version: 12.2.0

- name: Checkout code
uses: actions/checkout@v4

- name: Run tests
run: make test
env:
CC: "cc"
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export GOBIN?=$(BIN)
export GO=$(shell which go)
export PACKAGE_NAME=github.com/mrjosh/helm-ls
export GOLANG_CROSS_VERSION=v1.20.6
export CGO_ENABLED=1

$(eval GIT_COMMIT=$(shell git rev-parse --short HEAD))
$(eval BRANCH_NAME=$(shell git symbolic-ref -q --short HEAD || git describe --tags --exact-match))
Expand Down
5 changes: 2 additions & 3 deletions cmds/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

"github.com/mrjosh/helm-ls/internal/charts"
locallsp "github.com/mrjosh/helm-ls/internal/lsp"
"github.com/mrjosh/helm-ls/internal/util"
"github.com/spf13/cobra"
"go.lsp.dev/uri"
)
Expand All @@ -20,14 +19,14 @@ func newLintCmd() *cobra.Command {
args = append(args, os.Getenv("PWD"))
}

rootPath := uri.New(util.FileURIScheme + args[0])
rootPath := uri.File(args[0])
chartStore := charts.NewChartStore(rootPath, charts.NewChart)
chart, err := chartStore.GetChartForURI(rootPath)
if err != nil {
return err
}

msgs, err := locallsp.GetDiagnostics(uri.New(args[0]), chart.ValuesFiles.MainValuesFile.Values)
msgs, err := locallsp.GetDiagnostics(rootPath, chart.ValuesFiles.MainValuesFile.Values)
if err != nil {
return err
}
Expand Down
1 change: 0 additions & 1 deletion internal/adapter/fs/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ func (fs *FileStorage) Canonical(path string) string {
}

func (fs *FileStorage) FileExists(path string) (bool, error) {

fi, err := fs.fileInfo(path)
if err != nil {
return false, err
Expand Down
6 changes: 3 additions & 3 deletions internal/adapter/yamlls/trimTemplate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,12 +307,12 @@ func TestTrimTemplate(t *testing.T) {
func testTrimTemplateWithTestData(t *testing.T, testData TrimTemplateTestData) {
doc := &lsplocal.Document{
Content: testData.documentText,
Ast: lsplocal.ParseAst(testData.documentText),
Ast: lsplocal.ParseAst(nil, testData.documentText),
}

var trimmed = trimTemplateForYamllsFromAst(doc.Ast, testData.documentText)
trimmed := trimTemplateForYamllsFromAst(doc.Ast, testData.documentText)

var result = trimmed == testData.trimmedText
result := trimmed == testData.trimmedText

if !result {
t.Errorf("Trimmed templated was not as expected but was %s ", trimmed)
Expand Down
36 changes: 21 additions & 15 deletions internal/charts/chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ type Chart struct {

func NewChart(rootURI uri.URI, valuesFilesConfig util.ValuesFilesConfig) *Chart {
return &Chart{
ValuesFiles: NewValuesFiles(rootURI, valuesFilesConfig.MainValuesFileName, valuesFilesConfig.LintOverlayValuesFileName, valuesFilesConfig.AdditionalValuesFilesGlobPattern),
ValuesFiles: NewValuesFiles(rootURI,
valuesFilesConfig.MainValuesFileName,
valuesFilesConfig.LintOverlayValuesFileName,
valuesFilesConfig.AdditionalValuesFilesGlobPattern),
ChartMetadata: NewChartMetadata(rootURI),
RootURI: rootURI,
ParentChart: newParentChart(rootURI),
Expand All @@ -32,20 +35,23 @@ type QueriedValuesFiles struct {
// ResolveValueFiles returns a list of all values files in the chart
// and all parent charts if the query tries to access global values
func (c *Chart) ResolveValueFiles(query []string, chartStore *ChartStore) []*QueriedValuesFiles {
if len(query) > 0 && query[0] == "global" {
parentChart := c.ParentChart.GetParentChart(chartStore)
if parentChart != nil {
return append([]*QueriedValuesFiles{{Selector: query, ValuesFiles: c.ValuesFiles}}, parentChart.ResolveValueFiles(query, chartStore)...)
}
ownResult := []*QueriedValuesFiles{{Selector: query, ValuesFiles: c.ValuesFiles}}
if len(query) == 0 {
return ownResult
}
chartName := c.ChartMetadata.Metadata.Name
if len(query) > 0 {
parentChart := c.ParentChart.GetParentChart(chartStore)
if parentChart != nil {
extendedQuery := append([]string{chartName}, query...)
return append([]*QueriedValuesFiles{{Selector: query, ValuesFiles: c.ValuesFiles}},
parentChart.ResolveValueFiles(extendedQuery, chartStore)...)
}

parentChart := c.ParentChart.GetParentChart(chartStore)
if parentChart == nil {
return ownResult
}

if query[0] == "global" {
return append(ownResult,
parentChart.ResolveValueFiles(query, chartStore)...)
}
return []*QueriedValuesFiles{{Selector: query, ValuesFiles: c.ValuesFiles}}

chartName := c.ChartMetadata.Metadata.Name
extendedQuery := append([]string{chartName}, query...)
return append(ownResult,
parentChart.ResolveValueFiles(extendedQuery, chartStore)...)
}
30 changes: 12 additions & 18 deletions internal/charts/chart_for_document.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,14 @@ func (s *ChartStore) GetChartForDoc(uri lsp.DocumentURI) (*Chart, error) {
return chart, nil
}

chart = s.getChartFromFilesystemForTemplates(uri.Filename())

if chart != nil {
s.Charts[chart.RootURI] = chart
return chart, nil
}

return nil, ErrChartNotFound{
URI: uri,
chart, err := s.getChartFromFilesystemForTemplates(uri.Filename())
s.Charts[chart.RootURI] = chart
if err != nil {
return chart, ErrChartNotFound{
URI: uri,
}
}
return chart, nil
}

func (s *ChartStore) getChartFromCache(uri lsp.DocumentURI) *Chart {
Expand All @@ -38,25 +36,21 @@ func (s *ChartStore) getChartFromCache(uri lsp.DocumentURI) *Chart {
return nil
}

func (s *ChartStore) getChartFromFilesystemForTemplates(path string) *Chart {
func (s *ChartStore) getChartFromFilesystemForTemplates(path string) (*Chart, error) {
directory := filepath.Dir(path)
if filepath.Base(directory) == "templates" {
templatesDir := directory
expectedChartDir := filepath.Dir(templatesDir)

// check if Chart.yaml exists
if isChartDirectory(expectedChartDir) {
return s.newChart(uri.New("file://"+expectedChartDir), s.valuesFilesConfig)
return s.newChart(uri.File(expectedChartDir), s.valuesFilesConfig), nil
}
}

rootDirectory := s.RootURI.Filename()
if directory == rootDirectory {
return nil
}

if directory == path {
return nil
if directory == rootDirectory || directory == path {
return s.newChart(uri.File(directory), s.valuesFilesConfig), ErrChartNotFound{}
}

return s.getChartFromFilesystemForTemplates(directory)
Expand All @@ -72,5 +66,5 @@ type ErrChartNotFound struct {
}

func (e ErrChartNotFound) Error() string {
return fmt.Sprintf("Chart not found for file: %s", e.URI)
return fmt.Sprintf("Chart not found for file: %s. Using fallback", e.URI)
}
35 changes: 22 additions & 13 deletions internal/charts/chart_for_document_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package charts_test
import (
"os"
"path/filepath"
"runtime"
"testing"

"github.com/mrjosh/helm-ls/internal/charts"
Expand All @@ -12,8 +13,8 @@ import (
)

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

chart := &charts.Chart{}
Expand Down Expand Up @@ -41,47 +42,55 @@ func TestGetChartForDocumentWorksForAlreadyAddedCharts(t *testing.T) {

result5, error := chartStore.GetChartForDoc("file:///tmp/directory/deployment.yaml")
assert.Error(t, error)
assert.Nil(t, result5)
assert.Equal(t, &charts.Chart{RootURI: uri.File("/tmp")}, result5)
}

func TestGetChartForDocumentWorksForNewToAddChart(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("Skipping test on windows because of https://github.com/golang/go/issues/51442")
}
var (
rootDir = t.TempDir()
expectedChartDirectory = filepath.Join(rootDir, "chart")
expectedChart = &charts.Chart{
RootURI: uri.New("file://" + expectedChartDirectory),
RootURI: uri.File(expectedChartDirectory),
}
newChartFunc = func(_ uri.URI, _ util.ValuesFilesConfig) *charts.Chart { return expectedChart }
chartStore = charts.NewChartStore(uri.New("file://"+rootDir), newChartFunc)
chartStore = charts.NewChartStore(uri.File(rootDir), newChartFunc)
err = os.MkdirAll(expectedChartDirectory, 0o755)
)
assert.NoError(t, err)
_, _ = os.Create(filepath.Join(expectedChartDirectory, "Chart.yaml"))
chartFile := filepath.Join(expectedChartDirectory, "Chart.yaml")
_, _ = os.Create(chartFile)

result1, error := chartStore.GetChartForDoc(uri.New("file://" + filepath.Join(expectedChartDirectory, "templates", "deployment.yaml")))
result1, error := chartStore.GetChartForDoc(uri.File(filepath.Join(expectedChartDirectory, "templates", "deployment.yaml")))
assert.NoError(t, error)
assert.Same(t, expectedChart, result1)

assert.Same(t, expectedChart, chartStore.Charts[uri.New("file://"+expectedChartDirectory)])
assert.Same(t, expectedChart, chartStore.Charts[uri.File(expectedChartDirectory)])
}

func TestGetChartForDocumentWorksForNewToAddChartWithNestedFile(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("Skipping test on windows because of https://github.com/golang/go/issues/51442")
}
var (
rootDir = t.TempDir()
expectedChartDirectory = filepath.Join(rootDir, "chart")
expectedChart = &charts.Chart{
RootURI: uri.New("file://" + expectedChartDirectory),
RootURI: uri.File(expectedChartDirectory),
}
newChartFunc = func(_ uri.URI, _ util.ValuesFilesConfig) *charts.Chart { return expectedChart }
chartStore = charts.NewChartStore(uri.New("file://"+rootDir), newChartFunc)
chartStore = charts.NewChartStore(uri.File(rootDir), newChartFunc)
err = os.MkdirAll(expectedChartDirectory, 0o755)
)
assert.NoError(t, err)
_, _ = os.Create(filepath.Join(expectedChartDirectory, "Chart.yaml"))
chartFile := filepath.Join(expectedChartDirectory, "Chart.yaml")
_, _ = os.Create(chartFile)

result1, error := chartStore.GetChartForDoc(uri.New("file://" + filepath.Join(expectedChartDirectory, "templates", "nested", "deployment.yaml")))
result1, error := chartStore.GetChartForDoc(uri.File(filepath.Join(expectedChartDirectory, "templates", "nested", "deployment.yaml")))
assert.NoError(t, error)
assert.Same(t, expectedChart, result1)

assert.Same(t, expectedChart, chartStore.Charts[uri.New("file://"+expectedChartDirectory)])
assert.Same(t, expectedChart, chartStore.Charts[uri.File(expectedChartDirectory)])
}
4 changes: 2 additions & 2 deletions internal/charts/chart_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (s *ChartStore) GetChartForURI(fileURI uri.URI) (*Chart, error) {
var chart *Chart
expectedChartDir := fileURI.Filename()
if isChartDirectory(expectedChartDir) {
chart = s.newChart(uri.New("file://"+expectedChartDir), s.valuesFilesConfig)
chart = s.newChart(uri.File(expectedChartDir), s.valuesFilesConfig)
}

if chart != nil {
Expand All @@ -59,7 +59,7 @@ func (s *ChartStore) GetChartForURI(fileURI uri.URI) (*Chart, error) {

func (s *ChartStore) ReloadValuesFile(file uri.URI) {
logger.Println("Reloading values file", file)
chart, err := s.GetChartForURI(uri.URI(util.FileURIScheme + filepath.Dir(file.Filename())))
chart, err := s.GetChartForURI(uri.File(filepath.Dir(file.Filename())))
if err != nil {
logger.Error("Error reloading values file", file, err)
return
Expand Down
Loading

0 comments on commit 970d195

Please sign in to comment.