Skip to content

Commit

Permalink
fix: correct import data format for lists in yaml
Browse files Browse the repository at this point in the history
Signed-off-by: jakezhu9 <jakezhu9@gmail.com>
  • Loading branch information
jakezhu9 committed Oct 25, 2023
1 parent f9ea93c commit 9a3cd85
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 9 deletions.
37 changes: 30 additions & 7 deletions pkg/tools/gen/genkcl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,17 +181,40 @@ func TestGenKclFromJson(t *testing.T) {
}

func TestGenKclFromYaml(t *testing.T) {
input := filepath.Join("testdata", "yaml", "input.yaml")
expectFilepath := filepath.Join("testdata", "yaml", "expect.k")
expect := readFileString(t, expectFilepath)
type testCase struct {
name string
input string
expect string
}
var cases []testCase

var buf bytes.Buffer
err := GenKcl(&buf, input, nil, &GenKclOptions{})
casesPath := filepath.Join("testdata", "yaml")
caseFiles, err := os.ReadDir(casesPath)
if err != nil {
t.Fatal(err)
}
result := buf.Bytes()
assert2.Equal(t, expect, string(bytes.ReplaceAll(result, []byte("\r\n"), []byte("\n"))))

for _, caseFile := range caseFiles {
input := filepath.Join(casesPath, caseFile.Name(), "input.yaml")
expectFilepath := filepath.Join(casesPath, caseFile.Name(), "expect.k")
cases = append(cases, testCase{
name: caseFile.Name(),
input: input,
expect: readFileString(t, expectFilepath),
})
}

for _, testcase := range cases {
t.Run(testcase.name, func(t *testing.T) {
var buf bytes.Buffer
err := GenKcl(&buf, testcase.input, nil, &GenKclOptions{})
if err != nil {
t.Fatal(err)
}
result := buf.Bytes()
assert2.Equal(t, testcase.expect, string(bytes.ReplaceAll(result, []byte("\r\n"), []byte("\n"))))
})
}
}

func readFileString(t testing.TB, p string) (content string) {
Expand Down
4 changes: 2 additions & 2 deletions pkg/tools/gen/templates/kcl/data.gotmpl
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
{{- end }}
{{- " }\n" }}
{{- else }}
{{- indentLines (formatValue .) " " }}
{{- indentLines (formatValue .) " " }}{{- "\n" }}
{{- end }}
{{- end }}
{{- "]" }}
{{- else }}
{{- formatValue .Value }}
{{- formatValue .Value }}
{{- end }}
File renamed without changes.
File renamed without changes.
31 changes: 31 additions & 0 deletions pkg/tools/gen/testdata/yaml/list/expect.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
"""
This file was generated by the KCL auto-gen tool. DO NOT EDIT.
Editing this file might prove futile when you re-run the KCL auto-gen generate command.
"""

before = {
hooks = [
"go mod tidy"
]
}
builds = [
{
id = "kcl"
main = "./kcl.go"
goos = [
"darwin"
"linux"
"windows"
]
goarch = [
"amd64"
"arm64"
]
env = [
"CGO_ENABLED=0"
]
ldflags = [
"-X kcl-lang.io/cli/pkg/version.version={{.Version}}"
]
}
]
22 changes: 22 additions & 0 deletions pkg/tools/gen/testdata/yaml/list/input.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# This is an example .goreleaser.yml file with some sane defaults.
# Make sure to check the documentation at http://goreleaser.com
before:
hooks:
# You may remove this if you don't use go modules.
- go mod tidy

# .goreleaser.yml
builds:
- id: "kcl"
main: ./kcl.go
goos:
- darwin
- linux
- windows
goarch:
- amd64
- arm64
env:
- CGO_ENABLED=0
ldflags:
- "-X kcl-lang.io/cli/pkg/version.version={{.Version}}"

0 comments on commit 9a3cd85

Please sign in to comment.