Skip to content

Commit

Permalink
Merge pull request #428 from zong-zhe/fix-missing-print
Browse files Browse the repository at this point in the history
fix: fix missing Print()
  • Loading branch information
Peefy authored Aug 2, 2024
2 parents 2c16041 + 0141705 commit 2b1e0b9
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
16 changes: 16 additions & 0 deletions pkg/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1939,3 +1939,19 @@ func TestRunInVendor(t *testing.T) {
assert.Equal(t, buf.String(), "")
assert.Equal(t, res.GetRawYamlResult(), "The_first_kcl_program: Hello World!")
}

func TestRunWithLogger(t *testing.T) {
pkgPath := getTestDir("test_run_with_logger")
kpmcli, err := NewKpmClient()
assert.Equal(t, err, nil)

logbuf := new(bytes.Buffer)

_, err = kpmcli.Run(
WithWorkDir(pkgPath),
WithLogger(logbuf),
)

assert.Equal(t, err, nil)
assert.Equal(t, logbuf.String(), "Hello, World!\n")
}
11 changes: 11 additions & 0 deletions pkg/client/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ package client
import (
"errors"
"fmt"
"io"
"os"
"path/filepath"

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

type RunOption func(*RunOptions) error

func WithLogger(l io.Writer) RunOption {
return func(ro *RunOptions) error {
if ro.Option == nil {
ro.Option = kcl.NewOption()
}
ro.Merge(kcl.WithLogger(l))
return nil
}
}

// WithWorkDir sets the work directory for running the kcl package.
func WithWorkDir(workDir string) RunOption {
return func(ro *RunOptions) error {
Expand Down
1 change: 1 addition & 0 deletions pkg/client/test_data/test_run_with_logger/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
print("Hello, World!")

0 comments on commit 2b1e0b9

Please sign in to comment.