Skip to content

Commit

Permalink
fix: replace all the hyphen to underscore in ModRelativePath (#538)
Browse files Browse the repository at this point in the history
  • Loading branch information
zong-zhe authored Nov 19, 2024
1 parent 7b13434 commit f007a54
Show file tree
Hide file tree
Showing 9 changed files with 129 additions and 12 deletions.
31 changes: 21 additions & 10 deletions pkg/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,17 @@ func TestWithGlobalLock(t *testing.T) {
test.RunTestWithGlobalLock(t, "TestPullWithModSpec", testPullWithModSpec)
test.RunTestWithGlobalLock(t, "testPullWithOnlySpec", testPullWithOnlySpec)
test.RunTestWithGlobalLock(t, "TestGraph", testGraph)
test.RunTestWithGlobalLock(t, "testCyclicDependency", testCyclicDependency)
test.RunTestWithGlobalLock(t, "testNewKpmClient", testNewKpmClient)
test.RunTestWithGlobalLock(t, "testLoadPkgFormOci", testLoadPkgFormOci)
test.RunTestWithGlobalLock(t, "testAddWithLocalPath", testAddWithLocalPath)
test.RunTestWithGlobalLock(t, "testRunLocalWithoutArgs", testRunLocalWithoutArgs)
test.RunTestWithGlobalLock(t, "TestRunLocalWithArgs", testRunLocalWithArgs)
test.RunTestWithGlobalLock(t, "testInsecureSkipTLSverifyOCIRegistry", testInsecureSkipTLSverifyOCIRegistry)
test.RunTestWithGlobalLock(t, "testRunWithInsecureSkipTLSverify", testRunWithInsecureSkipTLSverify)
test.RunTestWithGlobalLock(t, "TestAddDepsWithInsecureSkipTLSverify", testAddDepsWithInsecureSkipTLSverify)
test.RunTestWithGlobalLock(t, "testPushWithInsecureSkipTLSverify", testPushWithInsecureSkipTLSverify)
test.RunTestWithGlobalLock(t, "testMetadataOffline", testMetadataOffline)
}

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

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

func TestNewKpmClient(t *testing.T) {
func testNewKpmClient(t *testing.T) {
kpmcli, err := NewKpmClient()
assert.Equal(t, err, nil)
kpmhome, err := env.GetAbsPkgPath()
Expand Down Expand Up @@ -1108,7 +1119,7 @@ func testUpdateWithKclModlock(t *testing.T, kpmcli *KpmClient) {
}()
}

func TestMetadataOffline(t *testing.T) {
func testMetadataOffline(t *testing.T) {
kpmcli, err := NewKpmClient()
assert.Equal(t, err, nil)

Expand Down Expand Up @@ -1405,7 +1416,7 @@ func testAddWithGitCommit(t *testing.T) {
}()
}

func TestLoadPkgFormOci(t *testing.T) {
func testLoadPkgFormOci(t *testing.T) {
type testCase struct {
Reg string
Repo string
Expand Down Expand Up @@ -1452,7 +1463,7 @@ func TestLoadPkgFormOci(t *testing.T) {
}
}

func TestAddWithLocalPath(t *testing.T) {
func testAddWithLocalPath(t *testing.T) {

testpath := getTestDir("add_with_local_path")

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

func TestRunLocalWithoutArgs(t *testing.T) {
func testRunLocalWithoutArgs(t *testing.T) {
pkgPath := getTestDir("test_run_options")

kpmcli, err := NewKpmClient()
Expand Down Expand Up @@ -1844,7 +1855,7 @@ func TestRunLocalWithoutArgs(t *testing.T) {
}
}

func TestRunLocalWithArgs(t *testing.T) {
func testRunLocalWithArgs(t *testing.T) {
pkgPath := getTestDir("test_run_options")

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

func TestRunWithInsecureSkipTLSverify(t *testing.T) {
func testRunWithInsecureSkipTLSverify(t *testing.T) {

var buf bytes.Buffer

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

func TestAddDepsWithInsecureSkipTLSverify(t *testing.T) {
func testAddDepsWithInsecureSkipTLSverify(t *testing.T) {

var buf bytes.Buffer

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

func TestPushWithInsecureSkipTLSverify(t *testing.T) {
func testPushWithInsecureSkipTLSverify(t *testing.T) {
var buf bytes.Buffer

mux := http.NewServeMux()
Expand Down
2 changes: 1 addition & 1 deletion pkg/client/pull_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func testPullWithInsecureSkipTLSverify(t *testing.T) {
}()
}

func TestInsecureSkipTLSverifyOCIRegistry(t *testing.T) {
func testInsecureSkipTLSverifyOCIRegistry(t *testing.T) {
var buf bytes.Buffer

mux := http.NewServeMux()
Expand Down
18 changes: 17 additions & 1 deletion pkg/client/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ import (
"io"
"os"
"path/filepath"
"strings"

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

return sourcePath == pkgHome
return strings.EqualFold(sourcePath, pkgHome)
}

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

// Iterate all the kcl files and transform the '-' in mod relative path to '_'
for i, kfile := range o.KFilenameList {
if utils.IsModRelativePath(kfile) {
modName, entriesPath, err := utils.ParseModRelativePath(kfile)
if err != nil {
return err
}
if modName != "" {
modName = strings.ReplaceAll(modName, "-", "_")
}
modRelativePath := utils.GenerateModRelativePath(modName, entriesPath)
o.KFilenameList[i] = modRelativePath
}
}

return nil
}

Expand Down
28 changes: 28 additions & 0 deletions pkg/client/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,31 @@ func TestRun(t *testing.T) {
RunTestWithGlobalLockAndKpmCli(t, "TestRunOciWithSettingsFile", testRunOciWithSettingsFile)
RunTestWithGlobalLockAndKpmCli(t, "TestRunWithModSpecVersion", testRunWithModSpecVersion)
}
func TestRunWithHyphenEntries(t *testing.T) {
testFunc := func(t *testing.T, kpmcli *KpmClient) {
pkgPath := getTestDir("test_run_hyphen_entries")

res, err := kpmcli.Run(
WithRunSource(
&downloader.Source{
Local: &downloader.Local{
Path: pkgPath,
},
},
),
)

if err != nil {
t.Fatal(err)
}

expect, err := os.ReadFile(filepath.Join(pkgPath, "stdout"))
if err != nil {
t.Fatal(err)
}

assert.Equal(t, utils.RmNewline(res.GetRawYamlResult()), utils.RmNewline(string(expect)))
}

RunTestWithGlobalLockAndKpmCli(t, "testRunWithHyphenEntries", testFunc)
}
10 changes: 10 additions & 0 deletions pkg/client/test_data/test_run_hyphen_entries/kcl.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "test_run_hyphen_entries"
edition = "v0.10.0"
version = "0.0.1"

[dependencies]
flask_manifests = { git = "https://github.com/kcl-lang/flask-demo-kcl-manifests.git", commit = "ade147b", version = "0.0.1" }

[profile]
entries = ["main.k", "${flask-manifests:KCL_MOD}/main.k"]
7 changes: 7 additions & 0 deletions pkg/client/test_data/test_run_hyphen_entries/kcl.mod.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[dependencies]
[dependencies.flask_manifests]
name = "flask_manifests"
full_name = "flask_manifests_0.0.1"
version = "0.0.1"
url = "https://github.com/kcl-lang/flask-demo-kcl-manifests.git"
commit = "ade147b"
1 change: 1 addition & 0 deletions pkg/client/test_data/test_run_hyphen_entries/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The_first_kcl_program_current_mod = 'Hello Current Mod World!'
18 changes: 18 additions & 0 deletions pkg/client/test_data/test_run_hyphen_entries/stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
The_first_kcl_program_current_mod: Hello Current Mod World!
config:
name: flask-demo
replicas: 1
labels:
app: flask-demo
service:
type: NodePort
ports:
- port: 5000
protocol: TCP
targetPort: 5000
containers:
flaskdemo:
image: kcllang/flask_demo:8d31498e765ff67a2fa9933d4adffe067544b2fe
ports:
- protocol: TCP
containerPort: 5000
26 changes: 26 additions & 0 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,32 @@ func IsModRelativePath(s string) bool {
return re.MatchString(s)
}

// ParseModRelativePath parses the package name and path from a mod relative path
func ParseModRelativePath(s string) (string, string, error) {
re := regexp.MustCompile(`^\$\{([a-zA-Z0-9_-]+:)?KCL_MOD\}(.*)$`)
matches := re.FindStringSubmatch(s)
if len(matches) == 0 {
return "", "", fmt.Errorf("invalid mod relative path: %s", s)
}

// Extract package name and path
var pkgName, path string
if matches[1] != "" {
pkgName = matches[1][:len(matches[1])-1] // Remove the trailing colon
}
path = matches[2]

return pkgName, path, nil
}

// GenerateModRelativePath generates a mod relative path from the package name and path
func GenerateModRelativePath(pkgName, path string) string {
if pkgName != "" {
return fmt.Sprintf("${%s:KCL_MOD}%s", pkgName, path)
}
return fmt.Sprintf("${KCL_MOD}%s", path)
}

// MoveFile will move the file from 'src' to 'dest'.
// On windows, it will copy the file from 'src' to 'dest', and then delete the file under 'src'.
// On unix-like systems, it will rename the file from 'src' to 'dest'.
Expand Down

0 comments on commit f007a54

Please sign in to comment.