From 9a3cd8583a3244398615aa65a8881d94a1fae109 Mon Sep 17 00:00:00 2001 From: jakezhu9 Date: Wed, 25 Oct 2023 17:14:00 +0800 Subject: [PATCH] fix: correct import data format for lists in yaml Signed-off-by: jakezhu9 --- pkg/tools/gen/genkcl_test.go | 37 +++++++++++++++---- pkg/tools/gen/templates/kcl/data.gotmpl | 4 +- .../gen/testdata/yaml/{ => basic}/expect.k | 0 .../gen/testdata/yaml/{ => basic}/input.yaml | 0 pkg/tools/gen/testdata/yaml/list/expect.k | 31 ++++++++++++++++ pkg/tools/gen/testdata/yaml/list/input.yaml | 22 +++++++++++ 6 files changed, 85 insertions(+), 9 deletions(-) rename pkg/tools/gen/testdata/yaml/{ => basic}/expect.k (100%) rename pkg/tools/gen/testdata/yaml/{ => basic}/input.yaml (100%) create mode 100644 pkg/tools/gen/testdata/yaml/list/expect.k create mode 100644 pkg/tools/gen/testdata/yaml/list/input.yaml diff --git a/pkg/tools/gen/genkcl_test.go b/pkg/tools/gen/genkcl_test.go index 59132f32..d65a41e9 100644 --- a/pkg/tools/gen/genkcl_test.go +++ b/pkg/tools/gen/genkcl_test.go @@ -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) { diff --git a/pkg/tools/gen/templates/kcl/data.gotmpl b/pkg/tools/gen/templates/kcl/data.gotmpl index 914429c8..ece47777 100644 --- a/pkg/tools/gen/templates/kcl/data.gotmpl +++ b/pkg/tools/gen/templates/kcl/data.gotmpl @@ -15,10 +15,10 @@ {{- end }} {{- " }\n" }} {{- else }} - {{- indentLines (formatValue .) " " }} + {{- indentLines (formatValue .) " " }}{{- "\n" }} {{- end }} {{- end }} {{- "]" }} {{- else }} - {{- formatValue .Value }} + {{- formatValue .Value }} {{- end }} diff --git a/pkg/tools/gen/testdata/yaml/expect.k b/pkg/tools/gen/testdata/yaml/basic/expect.k similarity index 100% rename from pkg/tools/gen/testdata/yaml/expect.k rename to pkg/tools/gen/testdata/yaml/basic/expect.k diff --git a/pkg/tools/gen/testdata/yaml/input.yaml b/pkg/tools/gen/testdata/yaml/basic/input.yaml similarity index 100% rename from pkg/tools/gen/testdata/yaml/input.yaml rename to pkg/tools/gen/testdata/yaml/basic/input.yaml diff --git a/pkg/tools/gen/testdata/yaml/list/expect.k b/pkg/tools/gen/testdata/yaml/list/expect.k new file mode 100644 index 00000000..804f0eb8 --- /dev/null +++ b/pkg/tools/gen/testdata/yaml/list/expect.k @@ -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}}" + ] + } +] diff --git a/pkg/tools/gen/testdata/yaml/list/input.yaml b/pkg/tools/gen/testdata/yaml/list/input.yaml new file mode 100644 index 00000000..13d07840 --- /dev/null +++ b/pkg/tools/gen/testdata/yaml/list/input.yaml @@ -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}}"