diff --git a/pkg/api/kpm_run.go b/pkg/api/kpm_run.go index 28aa30d8..7f607ab2 100644 --- a/pkg/api/kpm_run.go +++ b/pkg/api/kpm_run.go @@ -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) diff --git a/pkg/api/kpm_run_test.go b/pkg/api/kpm_run_test.go index 0f01e4a6..76568ae4 100644 --- a/pkg/api/kpm_run_test.go +++ b/pkg/api/kpm_run_test.go @@ -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") +} diff --git a/pkg/api/test_data/test_work_dir/base/base.k b/pkg/api/test_data/test_work_dir/base/base.k new file mode 100644 index 00000000..36c04d8f --- /dev/null +++ b/pkg/api/test_data/test_work_dir/base/base.k @@ -0,0 +1 @@ +base = "base" \ No newline at end of file diff --git a/pkg/api/test_data/test_work_dir/dev/kcl.mod b/pkg/api/test_data/test_work_dir/dev/kcl.mod new file mode 100644 index 00000000..38c7e776 --- /dev/null +++ b/pkg/api/test_data/test_work_dir/dev/kcl.mod @@ -0,0 +1,8 @@ +[package] +name = "helloworld" +edition = "0.5.0" +version = "0.1.0" + +[profile] +entries = ["../base/base.k", "main.k"] + diff --git a/pkg/api/test_data/test_work_dir/dev/kcl.mod.lock b/pkg/api/test_data/test_work_dir/dev/kcl.mod.lock new file mode 100644 index 00000000..e69de29b diff --git a/pkg/api/test_data/test_work_dir/dev/main.k b/pkg/api/test_data/test_work_dir/dev/main.k new file mode 100644 index 00000000..cc7380ab --- /dev/null +++ b/pkg/api/test_data/test_work_dir/dev/main.k @@ -0,0 +1 @@ +main = "main" \ No newline at end of file diff --git a/test/e2e/kpm_test.go b/test/e2e/kpm_test.go index 97f9671c..f09aeadc 100644 --- a/test/e2e/kpm_test.go +++ b/test/e2e/kpm_test.go @@ -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, "", filepath.Join(workspace, ts.Name)) + + stdout, stderr, err := ExecKpmWithWorkDir(input, filepath.Join(workspace, ts.Name)) expectedStdout := ReplaceAllKeyByValue(ts.ExpectStdout, "", workspace) expectedStderr := ReplaceAllKeyByValue(ts.ExpectStderr, "", workspace) diff --git a/test/e2e/test_suites/kpm/kpm_run/test_kpm_run_with_local_dep/test_suite.input b/test/e2e/test_suites/kpm/kpm_run/test_kpm_run_with_local_dep/test_suite.input index 08b5f923..41ece571 100644 --- a/test/e2e/test_suites/kpm/kpm_run/test_kpm_run_with_local_dep/test_suite.input +++ b/test/e2e/test_suites/kpm/kpm_run/test_kpm_run_with_local_dep/test_suite.input @@ -1 +1 @@ -kpm run ./kcl1 \ No newline at end of file +kpm run /kcl1 \ No newline at end of file