Skip to content

Commit f007a54

Browse files
authored
fix: replace all the hyphen to underscore in ModRelativePath (#538)
1 parent 7b13434 commit f007a54

File tree

9 files changed

+129
-12
lines changed

9 files changed

+129
-12
lines changed

pkg/client/client_test.go

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,17 @@ func TestWithGlobalLock(t *testing.T) {
6161
test.RunTestWithGlobalLock(t, "TestPullWithModSpec", testPullWithModSpec)
6262
test.RunTestWithGlobalLock(t, "testPullWithOnlySpec", testPullWithOnlySpec)
6363
test.RunTestWithGlobalLock(t, "TestGraph", testGraph)
64+
test.RunTestWithGlobalLock(t, "testCyclicDependency", testCyclicDependency)
65+
test.RunTestWithGlobalLock(t, "testNewKpmClient", testNewKpmClient)
66+
test.RunTestWithGlobalLock(t, "testLoadPkgFormOci", testLoadPkgFormOci)
67+
test.RunTestWithGlobalLock(t, "testAddWithLocalPath", testAddWithLocalPath)
68+
test.RunTestWithGlobalLock(t, "testRunLocalWithoutArgs", testRunLocalWithoutArgs)
69+
test.RunTestWithGlobalLock(t, "TestRunLocalWithArgs", testRunLocalWithArgs)
70+
test.RunTestWithGlobalLock(t, "testInsecureSkipTLSverifyOCIRegistry", testInsecureSkipTLSverifyOCIRegistry)
71+
test.RunTestWithGlobalLock(t, "testRunWithInsecureSkipTLSverify", testRunWithInsecureSkipTLSverify)
72+
test.RunTestWithGlobalLock(t, "TestAddDepsWithInsecureSkipTLSverify", testAddDepsWithInsecureSkipTLSverify)
73+
test.RunTestWithGlobalLock(t, "testPushWithInsecureSkipTLSverify", testPushWithInsecureSkipTLSverify)
74+
test.RunTestWithGlobalLock(t, "testMetadataOffline", testMetadataOffline)
6475
}
6576

6677
// TestDownloadOci test download from oci registry.
@@ -347,7 +358,7 @@ func testDependencyGraph(t *testing.T) {
347358
assert.Equal(t, err, nil)
348359
}
349360

350-
func TestCyclicDependency(t *testing.T) {
361+
func testCyclicDependency(t *testing.T) {
351362
testDir := getTestDir("test_cyclic_dependency")
352363
assert.Equal(t, utils.DirExists(filepath.Join(testDir, "aaa")), true)
353364
assert.Equal(t, utils.DirExists(filepath.Join(testDir, "aaa/kcl.mod")), true)
@@ -972,7 +983,7 @@ func hasSubdirInTar(tarPath, subdir string) (bool, error) {
972983
return false, nil
973984
}
974985

975-
func TestNewKpmClient(t *testing.T) {
986+
func testNewKpmClient(t *testing.T) {
976987
kpmcli, err := NewKpmClient()
977988
assert.Equal(t, err, nil)
978989
kpmhome, err := env.GetAbsPkgPath()
@@ -1108,7 +1119,7 @@ func testUpdateWithKclModlock(t *testing.T, kpmcli *KpmClient) {
11081119
}()
11091120
}
11101121

1111-
func TestMetadataOffline(t *testing.T) {
1122+
func testMetadataOffline(t *testing.T) {
11121123
kpmcli, err := NewKpmClient()
11131124
assert.Equal(t, err, nil)
11141125

@@ -1405,7 +1416,7 @@ func testAddWithGitCommit(t *testing.T) {
14051416
}()
14061417
}
14071418

1408-
func TestLoadPkgFormOci(t *testing.T) {
1419+
func testLoadPkgFormOci(t *testing.T) {
14091420
type testCase struct {
14101421
Reg string
14111422
Repo string
@@ -1452,7 +1463,7 @@ func TestLoadPkgFormOci(t *testing.T) {
14521463
}
14531464
}
14541465

1455-
func TestAddWithLocalPath(t *testing.T) {
1466+
func testAddWithLocalPath(t *testing.T) {
14561467

14571468
testpath := getTestDir("add_with_local_path")
14581469

@@ -1804,7 +1815,7 @@ func testDependenciesOrder(t *testing.T) {
18041815
assert.Equal(t, utils.RmNewline(string(got)), utils.RmNewline(string(expect)))
18051816
}
18061817

1807-
func TestRunLocalWithoutArgs(t *testing.T) {
1818+
func testRunLocalWithoutArgs(t *testing.T) {
18081819
pkgPath := getTestDir("test_run_options")
18091820

18101821
kpmcli, err := NewKpmClient()
@@ -1844,7 +1855,7 @@ func TestRunLocalWithoutArgs(t *testing.T) {
18441855
}
18451856
}
18461857

1847-
func TestRunLocalWithArgs(t *testing.T) {
1858+
func testRunLocalWithArgs(t *testing.T) {
18481859
pkgPath := getTestDir("test_run_options")
18491860

18501861
kpmcli, err := NewKpmClient()
@@ -2115,7 +2126,7 @@ func TestVirtualPackageVisiter(t *testing.T) {
21152126
assert.Equal(t, os.IsNotExist(err), true)
21162127
}
21172128

2118-
func TestRunWithInsecureSkipTLSverify(t *testing.T) {
2129+
func testRunWithInsecureSkipTLSverify(t *testing.T) {
21192130

21202131
var buf bytes.Buffer
21212132

@@ -2154,7 +2165,7 @@ func TestRunWithInsecureSkipTLSverify(t *testing.T) {
21542165
assert.Equal(t, buf.String(), "Called Success\n")
21552166
}
21562167

2157-
func TestAddDepsWithInsecureSkipTLSverify(t *testing.T) {
2168+
func testAddDepsWithInsecureSkipTLSverify(t *testing.T) {
21582169

21592170
var buf bytes.Buffer
21602171

@@ -2207,7 +2218,7 @@ func TestAddDepsWithInsecureSkipTLSverify(t *testing.T) {
22072218
assert.Equal(t, buf.String(), "Called Success\n")
22082219
}
22092220

2210-
func TestPushWithInsecureSkipTLSverify(t *testing.T) {
2221+
func testPushWithInsecureSkipTLSverify(t *testing.T) {
22112222
var buf bytes.Buffer
22122223

22132224
mux := http.NewServeMux()

pkg/client/pull_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ func testPullWithInsecureSkipTLSverify(t *testing.T) {
8888
}()
8989
}
9090

91-
func TestInsecureSkipTLSverifyOCIRegistry(t *testing.T) {
91+
func testInsecureSkipTLSverifyOCIRegistry(t *testing.T) {
9292
var buf bytes.Buffer
9393

9494
mux := http.NewServeMux()

pkg/client/run.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ import (
6868
"io"
6969
"os"
7070
"path/filepath"
71+
"strings"
7172

7273
"kcl-lang.io/kcl-go/pkg/kcl"
7374
"kcl-lang.io/kpm/pkg/constants"
@@ -440,7 +441,7 @@ func (o *RunOptions) applyCompileOptions(source downloader.Source, kclPkg *pkg.K
440441
pkgHome = filepath.Join(workDir, pkgHome)
441442
}
442443

443-
return sourcePath == pkgHome
444+
return strings.EqualFold(sourcePath, pkgHome)
444445
}
445446

446447
// If the sources from cli is not empty, use the sources from cli.
@@ -503,6 +504,21 @@ func (o *RunOptions) applyCompileOptions(source downloader.Source, kclPkg *pkg.K
503504
}
504505
}
505506

507+
// Iterate all the kcl files and transform the '-' in mod relative path to '_'
508+
for i, kfile := range o.KFilenameList {
509+
if utils.IsModRelativePath(kfile) {
510+
modName, entriesPath, err := utils.ParseModRelativePath(kfile)
511+
if err != nil {
512+
return err
513+
}
514+
if modName != "" {
515+
modName = strings.ReplaceAll(modName, "-", "_")
516+
}
517+
modRelativePath := utils.GenerateModRelativePath(modName, entriesPath)
518+
o.KFilenameList[i] = modRelativePath
519+
}
520+
}
521+
506522
return nil
507523
}
508524

pkg/client/run_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,31 @@ func TestRun(t *testing.T) {
105105
RunTestWithGlobalLockAndKpmCli(t, "TestRunOciWithSettingsFile", testRunOciWithSettingsFile)
106106
RunTestWithGlobalLockAndKpmCli(t, "TestRunWithModSpecVersion", testRunWithModSpecVersion)
107107
}
108+
func TestRunWithHyphenEntries(t *testing.T) {
109+
testFunc := func(t *testing.T, kpmcli *KpmClient) {
110+
pkgPath := getTestDir("test_run_hyphen_entries")
111+
112+
res, err := kpmcli.Run(
113+
WithRunSource(
114+
&downloader.Source{
115+
Local: &downloader.Local{
116+
Path: pkgPath,
117+
},
118+
},
119+
),
120+
)
121+
122+
if err != nil {
123+
t.Fatal(err)
124+
}
125+
126+
expect, err := os.ReadFile(filepath.Join(pkgPath, "stdout"))
127+
if err != nil {
128+
t.Fatal(err)
129+
}
130+
131+
assert.Equal(t, utils.RmNewline(res.GetRawYamlResult()), utils.RmNewline(string(expect)))
132+
}
133+
134+
RunTestWithGlobalLockAndKpmCli(t, "testRunWithHyphenEntries", testFunc)
135+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[package]
2+
name = "test_run_hyphen_entries"
3+
edition = "v0.10.0"
4+
version = "0.0.1"
5+
6+
[dependencies]
7+
flask_manifests = { git = "https://github.com/kcl-lang/flask-demo-kcl-manifests.git", commit = "ade147b", version = "0.0.1" }
8+
9+
[profile]
10+
entries = ["main.k", "${flask-manifests:KCL_MOD}/main.k"]
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[dependencies]
2+
[dependencies.flask_manifests]
3+
name = "flask_manifests"
4+
full_name = "flask_manifests_0.0.1"
5+
version = "0.0.1"
6+
url = "https://github.com/kcl-lang/flask-demo-kcl-manifests.git"
7+
commit = "ade147b"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
The_first_kcl_program_current_mod = 'Hello Current Mod World!'
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
The_first_kcl_program_current_mod: Hello Current Mod World!
2+
config:
3+
name: flask-demo
4+
replicas: 1
5+
labels:
6+
app: flask-demo
7+
service:
8+
type: NodePort
9+
ports:
10+
- port: 5000
11+
protocol: TCP
12+
targetPort: 5000
13+
containers:
14+
flaskdemo:
15+
image: kcllang/flask_demo:8d31498e765ff67a2fa9933d4adffe067544b2fe
16+
ports:
17+
- protocol: TCP
18+
containerPort: 5000

pkg/utils/utils.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,32 @@ func IsModRelativePath(s string) bool {
307307
return re.MatchString(s)
308308
}
309309

310+
// ParseModRelativePath parses the package name and path from a mod relative path
311+
func ParseModRelativePath(s string) (string, string, error) {
312+
re := regexp.MustCompile(`^\$\{([a-zA-Z0-9_-]+:)?KCL_MOD\}(.*)$`)
313+
matches := re.FindStringSubmatch(s)
314+
if len(matches) == 0 {
315+
return "", "", fmt.Errorf("invalid mod relative path: %s", s)
316+
}
317+
318+
// Extract package name and path
319+
var pkgName, path string
320+
if matches[1] != "" {
321+
pkgName = matches[1][:len(matches[1])-1] // Remove the trailing colon
322+
}
323+
path = matches[2]
324+
325+
return pkgName, path, nil
326+
}
327+
328+
// GenerateModRelativePath generates a mod relative path from the package name and path
329+
func GenerateModRelativePath(pkgName, path string) string {
330+
if pkgName != "" {
331+
return fmt.Sprintf("${%s:KCL_MOD}%s", pkgName, path)
332+
}
333+
return fmt.Sprintf("${KCL_MOD}%s", path)
334+
}
335+
310336
// MoveFile will move the file from 'src' to 'dest'.
311337
// On windows, it will copy the file from 'src' to 'dest', and then delete the file under 'src'.
312338
// On unix-like systems, it will rename the file from 'src' to 'dest'.

0 commit comments

Comments
 (0)