Skip to content

Commit

Permalink
Merge pull request #337 from zong-zhe/add-store-api
Browse files Browse the repository at this point in the history
feat: add StoreModLockFile and StoreModFile to store modfile and lockfile to local system
  • Loading branch information
Peefy committed May 27, 2024
2 parents d20b1ac + 9e863d6 commit 7a4df5b
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 0 deletions.
10 changes: 10 additions & 0 deletions pkg/api/kpm_pkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
50 changes: 50 additions & 0 deletions pkg/api/kpm_run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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)
}
7 changes: 7 additions & 0 deletions pkg/api/test_data/store_mod_and_lock/dep1_0.0.1/kcl.mod
Original file line number Diff line number Diff line change
@@ -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" }
9 changes: 9 additions & 0 deletions pkg/api/test_data/store_mod_and_lock/dep1_0.0.1/kcl.mod.lock
Original file line number Diff line number Diff line change
@@ -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"
7 changes: 7 additions & 0 deletions pkg/api/test_data/store_mod_and_lock/expect.mod
Original file line number Diff line number Diff line change
@@ -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" }
9 changes: 9 additions & 0 deletions pkg/api/test_data/store_mod_and_lock/expect.mod.lock
Original file line number Diff line number Diff line change
@@ -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"

0 comments on commit 7a4df5b

Please sign in to comment.