From 9e863d6759840760d21e889b78af6266bb69def7 Mon Sep 17 00:00:00 2001 From: zongz Date: Mon, 27 May 2024 11:25:52 +0800 Subject: [PATCH] feat: add StoreModLockFile and StoreModFile to store modfile and lockfile to local system Signed-off-by: zongz --- pkg/api/kpm_pkg.go | 10 ++++ pkg/api/kpm_run_test.go | 50 +++++++++++++++++++ .../store_mod_and_lock/dep1_0.0.1/kcl.mod | 7 +++ .../dep1_0.0.1/kcl.mod.lock | 9 ++++ .../test_data/store_mod_and_lock/expect.mod | 7 +++ .../store_mod_and_lock/expect.mod.lock | 9 ++++ 6 files changed, 92 insertions(+) create mode 100644 pkg/api/test_data/store_mod_and_lock/dep1_0.0.1/kcl.mod create mode 100644 pkg/api/test_data/store_mod_and_lock/dep1_0.0.1/kcl.mod.lock create mode 100644 pkg/api/test_data/store_mod_and_lock/expect.mod create mode 100644 pkg/api/test_data/store_mod_and_lock/expect.mod.lock diff --git a/pkg/api/kpm_pkg.go b/pkg/api/kpm_pkg.go index 357976fb..b50b7f3d 100644 --- a/pkg/api/kpm_pkg.go +++ b/pkg/api/kpm_pkg.go @@ -67,6 +67,16 @@ func (pkg *KclPackage) UpdateDependencyInPath(pkg_path string) error { return kpmcli.ResolvePkgDepsMetadata(pkg.pkg, true) } +// StoreModFile stores the kcl.mod file of the package to local file system. +func (pkg *KclPackage) StoreModFile() error { + return pkg.pkg.ModFile.StoreModFile() +} + +// StoreModLockFile stores the kcl.mod.lock file of the package to local file system. +func (pkg *KclPackage) StoreModLockFile() error { + return pkg.pkg.LockDepsVersion() +} + // GetPkgName returns the name of the package. func (pkg *KclPackage) GetPkgName() string { return pkg.pkg.GetPkgName() diff --git a/pkg/api/kpm_run_test.go b/pkg/api/kpm_run_test.go index 46b17718..a5bd07c9 100644 --- a/pkg/api/kpm_run_test.go +++ b/pkg/api/kpm_run_test.go @@ -11,6 +11,7 @@ import ( "github.com/stretchr/testify/assert" "kcl-lang.io/kcl-go/pkg/kcl" "kcl-lang.io/kpm/pkg/opt" + pkg "kcl-lang.io/kpm/pkg/package" "kcl-lang.io/kpm/pkg/utils" ) @@ -254,3 +255,52 @@ func TestRunWithOptsWithNoLog(t *testing.T) { assert.Equal(t, buf.String(), "") } + +func TestStoreModAndModLockFile(t *testing.T) { + testPath := getTestDir("store_mod_and_lock") + + testDep := pkg.Dependency{ + Name: "dep1", + FullName: "dep1_0.0.1", + Version: "0.0.1", + Sum: "sLr3e6W4RPrXYyswdOSiKqkHes1QHX2tk6SwxAPDqqo=", + LocalFullPath: filepath.Join(testPath, "dep1_0.0.1"), + Source: pkg.Source{ + Oci: &pkg.Oci{ + Reg: "ghcr.io", + Repo: "kcl-lang/dep1", + Tag: "0.0.1", + }, + }, + } + + testModFile := pkg.ModFile{ + Pkg: pkg.Package{ + Name: "test", + Version: "0.0.1", + Edition: "0.0.1", + }, + HomePath: filepath.Join(testPath, "dep1_0.0.1"), + Dependencies: pkg.Dependencies{ + Deps: map[string]pkg.Dependency{ + "dep1": testDep, + }, + }, + } + + testPkg := pkg.KclPkg{ + ModFile: testModFile, + HomePath: filepath.Join(testPath, "dep1_0.0.1"), + Dependencies: testModFile.Dependencies, + } + + testPackage := KclPackage{ + pkg: &testPkg, + } + + err := testPackage.StoreModFile() + assert.Equal(t, err, nil) + + err = testPackage.StoreModLockFile() + assert.Equal(t, err, nil) +} diff --git a/pkg/api/test_data/store_mod_and_lock/dep1_0.0.1/kcl.mod b/pkg/api/test_data/store_mod_and_lock/dep1_0.0.1/kcl.mod new file mode 100644 index 00000000..8f80cb27 --- /dev/null +++ b/pkg/api/test_data/store_mod_and_lock/dep1_0.0.1/kcl.mod @@ -0,0 +1,7 @@ +[package] +name = "test" +edition = "0.0.1" +version = "0.0.1" + +[dependencies] +dep1 = { oci = "oci://ghcr.io/kcl-lang/dep1", tag = "0.0.1" } diff --git a/pkg/api/test_data/store_mod_and_lock/dep1_0.0.1/kcl.mod.lock b/pkg/api/test_data/store_mod_and_lock/dep1_0.0.1/kcl.mod.lock new file mode 100644 index 00000000..319dd15e --- /dev/null +++ b/pkg/api/test_data/store_mod_and_lock/dep1_0.0.1/kcl.mod.lock @@ -0,0 +1,9 @@ +[dependencies] + [dependencies.dep1] + name = "dep1" + full_name = "dep1_0.0.1" + version = "0.0.1" + sum = "sLr3e6W4RPrXYyswdOSiKqkHes1QHX2tk6SwxAPDqqo=" + reg = "ghcr.io" + repo = "kcl-lang/dep1" + oci_tag = "0.0.1" diff --git a/pkg/api/test_data/store_mod_and_lock/expect.mod b/pkg/api/test_data/store_mod_and_lock/expect.mod new file mode 100644 index 00000000..8f80cb27 --- /dev/null +++ b/pkg/api/test_data/store_mod_and_lock/expect.mod @@ -0,0 +1,7 @@ +[package] +name = "test" +edition = "0.0.1" +version = "0.0.1" + +[dependencies] +dep1 = { oci = "oci://ghcr.io/kcl-lang/dep1", tag = "0.0.1" } diff --git a/pkg/api/test_data/store_mod_and_lock/expect.mod.lock b/pkg/api/test_data/store_mod_and_lock/expect.mod.lock new file mode 100644 index 00000000..319dd15e --- /dev/null +++ b/pkg/api/test_data/store_mod_and_lock/expect.mod.lock @@ -0,0 +1,9 @@ +[dependencies] + [dependencies.dep1] + name = "dep1" + full_name = "dep1_0.0.1" + version = "0.0.1" + sum = "sLr3e6W4RPrXYyswdOSiKqkHes1QHX2tk6SwxAPDqqo=" + reg = "ghcr.io" + repo = "kcl-lang/dep1" + oci_tag = "0.0.1"