Skip to content

Commit 2dd4120

Browse files
authored
Merge pull request #274 from zong-zhe/fix-win-bug
fix: fix symlink bug on windows
2 parents 53aece1 + 2c7dc2d commit 2dd4120

File tree

5 files changed

+91
-20
lines changed

5 files changed

+91
-20
lines changed

pkg/client/client.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"os"
88
"path/filepath"
99
"reflect"
10+
"runtime"
1011
"strings"
1112

1213
"github.com/BurntSushi/toml"
@@ -770,7 +771,7 @@ func (c *KpmClient) Download(dep *pkg.Dependency, localPath string) (*pkg.Depend
770771
// Creating symbolic links in a global cache is not an optimal solution.
771772
// This allows kclvm to locate the package by default.
772773
// This feature is unstable and will be removed soon.
773-
err = utils.CreateSymlink(dep.LocalFullPath, filepath.Join(filepath.Dir(localPath), dep.Name))
774+
err = createDepRef(dep.LocalFullPath, filepath.Join(filepath.Dir(localPath), dep.Name))
774775
if err != nil {
775776
return nil, err
776777
}
@@ -793,7 +794,7 @@ func (c *KpmClient) Download(dep *pkg.Dependency, localPath string) (*pkg.Depend
793794
// Creating symbolic links in a global cache is not an optimal solution.
794795
// This allows kclvm to locate the package by default.
795796
// This feature is unstable and will be removed soon.
796-
err = utils.CreateSymlink(dep.LocalFullPath, filepath.Join(filepath.Dir(localPath), dep.Name))
797+
err = createDepRef(dep.LocalFullPath, filepath.Join(filepath.Dir(localPath), dep.Name))
797798
if err != nil {
798799
return nil, err
799800
}
@@ -1361,3 +1362,14 @@ func check(dep pkg.Dependency, newDepPath string) bool {
13611362

13621363
return dep.Sum == sum
13631364
}
1365+
1366+
// createDepRef will create a dependency reference for the dependency saved on the local filesystem.
1367+
// On the unix-like system, it will create a symbolic link.
1368+
// On the windows system, it will create a junction.
1369+
func createDepRef(depName, refName string) error {
1370+
if runtime.GOOS == "windows" {
1371+
return copy.Copy(depName, refName)
1372+
} else {
1373+
return utils.CreateSymlink(depName, refName)
1374+
}
1375+
}

pkg/client/client_test.go

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@ func TestDownloadOci(t *testing.T) {
4949
err := os.MkdirAll(testPath, 0755)
5050
assert.Equal(t, err, nil)
5151

52+
defer func() {
53+
err := os.RemoveAll(getTestDir("download"))
54+
if err != nil {
55+
t.Errorf("Failed to remove directory: %v", err)
56+
}
57+
}()
58+
5259
depFromOci := pkg.Dependency{
5360
Name: "k8s",
5461
Version: "1.27",
@@ -75,15 +82,39 @@ func TestDownloadOci(t *testing.T) {
7582
assert.Equal(t, dep.LocalFullPath, testPath)
7683

7784
// Check whether the tar downloaded by `kpm add` has been deleted.
78-
assert.Equal(t, utils.DirExists(filepath.Join(testPath, "k8s_1.27.tar")), false)
85+
downloadPath := getTestDir("download")
86+
assert.Equal(t, utils.DirExists(filepath.Join(downloadPath, "k8s_1.27.tar")), false)
87+
88+
assert.Equal(t, utils.DirExists(filepath.Join(downloadPath, "k8s_1.27")), true)
89+
assert.Equal(t, utils.DirExists(filepath.Join(downloadPath, "k8s")), true)
90+
91+
// Check whether the reference and the dependency have the same hash.
92+
hashDep, err := utils.HashDir(filepath.Join(downloadPath, "k8s_1.27"))
93+
assert.Equal(t, err, nil)
94+
95+
depRefPath := filepath.Join(downloadPath, "k8s")
96+
info, err := os.Lstat(depRefPath)
97+
assert.Equal(t, err, nil)
98+
99+
if info.Mode()&os.ModeSymlink != 0 {
100+
depRefPath, err = os.Readlink(depRefPath)
101+
assert.Equal(t, err, nil)
102+
}
79103

80-
err = os.RemoveAll(getTestDir("download"))
104+
hashRef, err := utils.HashDir(depRefPath)
81105
assert.Equal(t, err, nil)
106+
assert.Equal(t, hashDep, hashRef)
82107
}
83108

84109
// TestDownloadLatestOci tests the case that the version is empty.
85110
func TestDownloadLatestOci(t *testing.T) {
86111
testPath := filepath.Join(getTestDir("download"), "a_random_name")
112+
defer func() {
113+
err := os.RemoveAll(getTestDir("download"))
114+
if err != nil {
115+
t.Errorf("Failed to remove directory: %v", err)
116+
}
117+
}()
87118
err := os.MkdirAll(testPath, 0755)
88119
assert.Equal(t, err, nil)
89120
depFromOci := pkg.Dependency{
@@ -115,8 +146,24 @@ func TestDownloadLatestOci(t *testing.T) {
115146
// Check whether the tar downloaded by `kpm add` has been deleted.
116147
assert.Equal(t, utils.DirExists(filepath.Join(testPath, "helloworld_0.1.1.tar")), false)
117148

118-
err = os.RemoveAll(getTestDir("download"))
149+
assert.Equal(t, utils.DirExists(filepath.Join(getTestDir("download"), "helloworld")), true)
150+
151+
// Check whether the reference and the dependency have the same hash.
152+
hashDep, err := utils.HashDir(dep.LocalFullPath)
153+
assert.Equal(t, err, nil)
154+
155+
depRefPath := filepath.Join(getTestDir("download"), "helloworld")
156+
info, err := os.Lstat(depRefPath)
157+
assert.Equal(t, err, nil)
158+
159+
if info.Mode()&os.ModeSymlink != 0 {
160+
depRefPath, err = os.Readlink(depRefPath)
161+
assert.Equal(t, err, nil)
162+
}
163+
164+
hashRef, err := utils.HashDir(depRefPath)
119165
assert.Equal(t, err, nil)
166+
assert.Equal(t, hashDep, hashRef)
120167
}
121168

122169
func TestDependencyGraph(t *testing.T) {

pkg/cmd/cmd_metadata.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Copyright 2023 The KCL Authors. All rights reserved.
2-
// Deprecated: The entire contents of this file will be deprecated.
2+
// Deprecated: The entire contents of this file will be deprecated.
33
// Please use the kcl cli - https://github.com/kcl-lang/cli.
44

55
package cmd

pkg/utils/utils_test.go

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -96,20 +96,6 @@ func TestUnTarDir(t *testing.T) {
9696
_ = os.RemoveAll(testSrc)
9797
}
9898

99-
func TestCreateSymbolLink(t *testing.T) {
100-
testDir := getTestDir("test_link")
101-
need_linked := filepath.Join(testDir, "need_be_linked_v1")
102-
linkPath := filepath.Join(testDir, "linked")
103-
104-
_ = os.Remove(linkPath)
105-
err := CreateSymlink(need_linked, linkPath)
106-
107-
linkTarget, _ := os.Readlink(linkPath)
108-
assert.Equal(t, err, nil)
109-
assert.Equal(t, linkTarget, need_linked)
110-
_ = os.Remove(linkPath)
111-
}
112-
11399
func TestDefaultKpmHome(t *testing.T) {
114100
homeDir, _ := os.UserHomeDir()
115101

pkg/utils/utils_unix_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//go:build linux || darwin
2+
// +build linux darwin
3+
4+
package utils
5+
6+
import (
7+
"os"
8+
"path/filepath"
9+
"testing"
10+
11+
"github.com/stretchr/testify/assert"
12+
)
13+
14+
func TestCreateSymbolLink(t *testing.T) {
15+
testDir := getTestDir("test_link")
16+
need_linked := filepath.Join(testDir, "need_be_linked_v1")
17+
linkPath := filepath.Join(testDir, "linked")
18+
19+
_ = os.Remove(linkPath)
20+
err := CreateSymlink(need_linked, linkPath)
21+
22+
linkTarget, _ := os.Readlink(linkPath)
23+
assert.Equal(t, err, nil)
24+
assert.Equal(t, linkTarget, need_linked)
25+
_ = os.Remove(linkPath)
26+
}

0 commit comments

Comments
 (0)