Skip to content

Commit 2b1e0b9

Browse files
authored
Merge pull request #428 from zong-zhe/fix-missing-print
fix: fix missing Print()
2 parents 2c16041 + 0141705 commit 2b1e0b9

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

pkg/client/client_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1939,3 +1939,19 @@ func TestRunInVendor(t *testing.T) {
19391939
assert.Equal(t, buf.String(), "")
19401940
assert.Equal(t, res.GetRawYamlResult(), "The_first_kcl_program: Hello World!")
19411941
}
1942+
1943+
func TestRunWithLogger(t *testing.T) {
1944+
pkgPath := getTestDir("test_run_with_logger")
1945+
kpmcli, err := NewKpmClient()
1946+
assert.Equal(t, err, nil)
1947+
1948+
logbuf := new(bytes.Buffer)
1949+
1950+
_, err = kpmcli.Run(
1951+
WithWorkDir(pkgPath),
1952+
WithLogger(logbuf),
1953+
)
1954+
1955+
assert.Equal(t, err, nil)
1956+
assert.Equal(t, logbuf.String(), "Hello, World!\n")
1957+
}

pkg/client/run.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ package client
6565
import (
6666
"errors"
6767
"fmt"
68+
"io"
6869
"os"
6970
"path/filepath"
7071

@@ -88,6 +89,16 @@ type RunOptions struct {
8889

8990
type RunOption func(*RunOptions) error
9091

92+
func WithLogger(l io.Writer) RunOption {
93+
return func(ro *RunOptions) error {
94+
if ro.Option == nil {
95+
ro.Option = kcl.NewOption()
96+
}
97+
ro.Merge(kcl.WithLogger(l))
98+
return nil
99+
}
100+
}
101+
91102
// WithWorkDir sets the work directory for running the kcl package.
92103
func WithWorkDir(workDir string) RunOption {
93104
return func(ro *RunOptions) error {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
print("Hello, World!")

0 commit comments

Comments
 (0)