Skip to content

Commit

Permalink
fix: rm kcl.mod.lock gen when there is no dependencies in kcl.mod
Browse files Browse the repository at this point in the history
Signed-off-by: zongz <zongzhe1024@163.com>
  • Loading branch information
zong-zhe committed Aug 9, 2024
1 parent bbd3a1c commit 9d27b18
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 17 deletions.
2 changes: 1 addition & 1 deletion pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ func (c *KpmClient) resolvePkgDeps(kclPkg *pkg.KclPkg, lockDeps *pkg.Dependencie
}

// Generate file kcl.mod.lock.
if !kclPkg.NoSumCheck || !update {
if kclPkg.ModFile.Dependencies.Deps.Len() > 0 && !kclPkg.NoSumCheck || !update {
err := kclPkg.LockDepsVersion()
if err != nil {
return err
Expand Down
24 changes: 24 additions & 0 deletions pkg/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1965,3 +1965,27 @@ func TestRunWithLogger(t *testing.T) {
assert.Equal(t, err, nil)
assert.Equal(t, logbuf.String(), "Hello, World!\n")
}

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

pkgSource, err := downloader.NewSourceFromStr(pkgPath)
assert.Equal(t, err, nil)

v := NewVisitor(*pkgSource, kpmcli)
err = v.Visit(pkgSource, func(p *pkg.KclPkg) error {
assert.Contains(t, p.GetPkgName(), "vPkg_")
_, err = os.Stat(filepath.Join(pkgPath, "kcl.mod"))
assert.Equal(t, os.IsNotExist(err), true)
_, err = os.Stat(filepath.Join(pkgPath, "kcl.mod.lock"))
assert.Equal(t, os.IsNotExist(err), true)
return nil
})
assert.Equal(t, err, nil)
_, err = os.Stat(filepath.Join(pkgPath, "kcl.mod"))
assert.Equal(t, os.IsNotExist(err), true)
_, err = os.Stat(filepath.Join(pkgPath, "kcl.mod.lock"))
assert.Equal(t, os.IsNotExist(err), true)
}
1 change: 1 addition & 0 deletions pkg/client/test_data/test_virtual_pkg_visitor/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The_first_kcl_program = 'Hello World!'
16 changes: 0 additions & 16 deletions pkg/client/visitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,22 +77,6 @@ func (vpv *VirtualPkgVisitor) Visit(s *downloader.Source, v visitFunc) error {
return err
}

// If the source path does not contain a kcl.mod file, create a virtual kcl.mod file.
vKclModPath := filepath.Join(sourcePath, constants.KCL_MOD)
if !utils.DirExists(vKclModPath) {
// After the visitFunc is executed, clean the virtual kcl.mod file.
defer func() error {
vKclModLockPath := filepath.Join(sourcePath, constants.KCL_MOD_LOCK)
if utils.DirExists(vKclModLockPath) {
err := os.RemoveAll(vKclModLockPath)
if err != nil {
return err
}
}
return nil
}()

}
initOpts := opt.InitOptions{
Name: "vPkg_" + uuid.New().String(),
InitPath: sourcePath,
Expand Down

0 comments on commit 9d27b18

Please sign in to comment.