Skip to content

Commit

Permalink
fix: set the 'kcl.mod' path as workdir (#176)
Browse files Browse the repository at this point in the history
* feat: set the 'kcl.mod' path as workdir

* fix: fix e2e test case
  • Loading branch information
zong-zhe authored Sep 5, 2023
1 parent b9c6d23 commit d8f53cf
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 2 deletions.
2 changes: 2 additions & 0 deletions pkg/api/kpm_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ func RunPkgInPath(opts *opt.CompileOptions) (string, error) {
// no entry
opts.Merge(kcl.WithKFilenames(opts.PkgPath()))
}
// set the 'kcl.mod' path as the work dir.
opts.Merge(kcl.WithWorkDir(opts.PkgPath()))

// Calculate the absolute path of entry file described by '--input'.
compiler := runner.NewCompilerWithOpts(opts)
Expand Down
9 changes: 9 additions & 0 deletions pkg/api/kpm_run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,12 @@ func TestRunTar(t *testing.T) {
os.RemoveAll(untarPath)
}
}

func TestRunWithWorkdir(t *testing.T) {
pkgPath := getTestDir(filepath.Join("test_work_dir", "dev"))
opts := opt.DefaultCompileOptions()
opts.SetPkgPath(pkgPath)
result, err := RunPkgInPath(opts)
assert.Equal(t, err, nil)
assert.Equal(t, result, "base: base\nmain: main")
}
1 change: 1 addition & 0 deletions pkg/api/test_data/test_work_dir/base/base.k
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
base = "base"
8 changes: 8 additions & 0 deletions pkg/api/test_data/test_work_dir/dev/kcl.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "helloworld"
edition = "0.5.0"
version = "0.1.0"

[profile]
entries = ["../base/base.k", "main.k"]

Empty file.
1 change: 1 addition & 0 deletions pkg/api/test_data/test_work_dir/dev/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
main = "main"
4 changes: 3 additions & 1 deletion test/e2e/kpm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,9 @@ var _ = ginkgo.Describe("Kpm CLI Testing", func() {

CopyDir(filepath.Join(testDataRoot, ts.Name), filepath.Join(workspace, ts.Name))

stdout, stderr, err := ExecKpmWithWorkDir(ts.Input, filepath.Join(workspace, ts.Name))
input := ReplaceAllKeyByValue(ts.Input, "<workspace>", filepath.Join(workspace, ts.Name))

stdout, stderr, err := ExecKpmWithWorkDir(input, filepath.Join(workspace, ts.Name))

expectedStdout := ReplaceAllKeyByValue(ts.ExpectStdout, "<workspace>", workspace)
expectedStderr := ReplaceAllKeyByValue(ts.ExpectStderr, "<workspace>", workspace)
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
kpm run ./kcl1
kpm run <workspace>/kcl1

0 comments on commit d8f53cf

Please sign in to comment.