Skip to content

Commit 2434f52

Browse files
authored
Add function to enable formatting with dry run (#558)
* chore: update kcl-lib dependency to 0.12.4 Signed-off-by: Jakob Beckmann <f4z3r-github@pm.me> * feat(format): add function with options to enable dry runs Relates-to: kcl-lang/kcl#2101 Signed-off-by: Jakob Beckmann <f4z3r-github@pm.me> * ci(linux-musl): update to Golang 1.26 Signed-off-by: Jakob Beckmann <f4z3r-github@pm.me> --------- Signed-off-by: Jakob Beckmann <f4z3r-github@pm.me>
1 parent 4f04419 commit 2434f52

7 files changed

Lines changed: 68 additions & 8 deletions

File tree

.github/workflows/main_linux_musl.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jobs:
1414
# Alpine Linux container configurations
1515
- os: alpine-latest
1616
runner: ubuntu-latest # Host runner for the container
17-
container: golang:1.24-alpine
17+
container: golang:1.26-alpine
1818
cgo_enabled: 1
1919

2020
runs-on: ${{ matrix.runner }}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ require (
2222
google.golang.org/grpc v1.82.1
2323
google.golang.org/protobuf v1.36.11
2424
gopkg.in/yaml.v3 v3.0.1
25-
kcl-lang.io/lib v0.12.3
25+
kcl-lang.io/lib v0.12.4
2626
)
2727

2828
require (

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,5 +116,5 @@ gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntN
116116
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
117117
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
118118
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
119-
kcl-lang.io/lib v0.12.3 h1:x/a4Nyl5Wa5gMrhu5dPLeZEho9ryXJXgHODXJ8xC9gk=
120-
kcl-lang.io/lib v0.12.3/go.mod h1:kK/P1DUXQD+HpdRuPMb4/f7U7Njr2q5VrihmDHjKtnw=
119+
kcl-lang.io/lib v0.12.4 h1:y6oa0KimtzZe+MaNd73p8b76hXcCLzD8lrZTf7gwGNc=
120+
kcl-lang.io/lib v0.12.4/go.mod h1:G2pPSIdu+cFb5OxiaxB0qBRIFFL598OZf1e5oiqWnns=

kcl.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ type (
6868
ListOptionsResult = loader.ListOptionsResult
6969
ParseProgramArgs = parser.ParseProgramArgs
7070
ParseProgramResult = parser.ParseProgramResult
71+
FormatPathOptions = format.FormatPathOptions
7172
)
7273

7374
// MustRun is like Run but panics if return any error.
@@ -169,6 +170,21 @@ func FormatPath(path string) (changedPaths []string, err error) {
169170
return format.FormatPath(path)
170171
}
171172

173+
// FormatPathWithOptions formats files from the given path with some options.
174+
// path:
175+
// if path is `.` or empty string, all KCL files in current directory will be formatted, not recursively
176+
// if path is `path/file.k`, the specified KCL file will be formatted
177+
// if path is `path/to/dir`, all KCL files in the specified dir will be formatted, not recursively
178+
// if path is `path/to/dir/...`, all KCL files in the specified dir will be formatted recursively
179+
//
180+
// the returned changedPaths are the changed file paths (relative path)
181+
func FormatPathWithOptions(
182+
path string,
183+
opts FormatPathOptions,
184+
) ([]string, error) {
185+
return format.FormatPathWithOptions(path, opts)
186+
}
187+
172188
// ListDepFiles return the depend files from the given path
173189
func ListDepFiles(workDir string, opt *ListDepFilesOption) (files []string, err error) {
174190
return list.ListDepFiles(workDir, opt)

pkg/tools/format/format_test.go

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,39 @@ func TestFormatPath(t *testing.T) {
9898
}
9999
}
100100

101+
func TestFormatPathWithOptions(t *testing.T) {
102+
successDir := filepath.Join("testdata", "success")
103+
104+
sourceFiles := findFiles(t, successDir, func(info fs.DirEntry) bool {
105+
return strings.HasSuffix(info.Name(), ".k")
106+
})
107+
var sourceFilesBackup []kclFile
108+
109+
for _, sourceFile := range sourceFiles {
110+
content, err := os.ReadFile(sourceFile)
111+
if err != nil {
112+
t.Fatalf("read source file content failed: %s", sourceFile)
113+
}
114+
sourceFilesBackup = append(sourceFilesBackup, kclFile{
115+
name: sourceFile,
116+
content: content,
117+
})
118+
}
119+
120+
_, err := FormatPathWithOptions(successDir, FormatPathOptions{DryRun: true})
121+
if err != nil {
122+
t.Fatalf("format path exec failed. %v", err)
123+
}
124+
125+
for _, sourceFile := range sourceFilesBackup {
126+
newContent, err := os.ReadFile(sourceFile.name)
127+
if err != nil {
128+
t.Fatalf("read source file content failed: %s", sourceFile.name)
129+
}
130+
assert.Equal(t, sourceFile.content, newContent, fmt.Sprintf("format path with dry run should not modify files, file: %s, expect: %s, get: %s", sourceFile.name, sourceFile.content, newContent))
131+
}
132+
}
133+
101134
type filterFile func(fs.DirEntry) bool
102135

103136
func findFiles(t testing.TB, testDir string, filter filterFile) (names []string) {
@@ -122,7 +155,7 @@ type kclFile struct {
122155

123156
func writeFile(t *testing.T, kclfiles []kclFile) {
124157
for _, backUpFile := range kclfiles {
125-
err := os.WriteFile(backUpFile.name, backUpFile.content, 0666)
158+
err := os.WriteFile(backUpFile.name, backUpFile.content, 0o666)
126159
if err != nil {
127160
t.Logf("write back formatted source file failed: %v", err)
128161
}

pkg/tools/format/kformat.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,25 @@ func FormatCode(code any) ([]byte, error) {
3636
return resp.Formatted, nil
3737
}
3838

39-
func FormatPath(path string) (changedPaths []string, err error) {
39+
type FormatPathOptions struct {
40+
DryRun bool
41+
}
42+
43+
func FormatPathWithOptions(
44+
path string,
45+
opts FormatPathOptions,
46+
) ([]string, error) {
4047
svc := kcl.Service()
4148
resp, err := svc.FormatPath(&gpyrpc.FormatPathArgs{
42-
Path: path,
49+
Path: path,
50+
DryRun: opts.DryRun,
4351
})
4452
if err != nil {
4553
return nil, err
4654
}
4755
return resp.ChangedPaths, nil
4856
}
57+
58+
func FormatPath(path string) ([]string, error) {
59+
return FormatPathWithOptions(path, FormatPathOptions{})
60+
}
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
schema Person:
22
name: str
3-

0 commit comments

Comments
 (0)