Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancement: Supports adding third-party dependencies from git repo with the version field #430

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -829,6 +829,14 @@ func (c *KpmClient) AddDepToPkg(kclPkg *pkg.KclPkg, d *pkg.Dependency) error {
return err
}

if dep, ok := kclPkg.Dependencies.Deps.Get(d.Name); ok {
if dep1, ok := kclPkg.ModFile.Dependencies.Deps.Get(d.Name); ok {
if dep1.Git != nil {
dep1.Git.Version = dep.Version
}
}
}

return err
}

Expand Down
138 changes: 138 additions & 0 deletions pkg/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1270,6 +1270,144 @@ func TestAddWithGitCommit(t *testing.T) {
}()
}

func TestAddWithGitTag(t *testing.T) {
pkgPath := getTestDir("add_with_git_tag")

testPkgPath := ""
if runtime.GOOS == "windows" {
testPkgPath = filepath.Join(pkgPath, "test_pkg_win")
} else {
testPkgPath = filepath.Join(pkgPath, "test_pkg")
}

testPkgPathModBak := filepath.Join(testPkgPath, "kcl.mod.bak")
testPkgPathMod := filepath.Join(testPkgPath, "kcl.mod")
testPkgPathModExpect := filepath.Join(testPkgPath, "kcl.mod.expect")
testPkgPathModLock := filepath.Join(testPkgPath, "kcl.mod.lock")
testPkgPathModLockBak := filepath.Join(testPkgPath, "kcl.mod.lock.bak")
testPkgPathModLockExpect := filepath.Join(testPkgPath, "kcl.mod.lock.expect")

err := copy.Copy(testPkgPathModBak, testPkgPathMod)
assert.Equal(t, err, nil)
err = copy.Copy(testPkgPathModLockBak, testPkgPathModLock)
assert.Equal(t, err, nil)

kpmcli, err := NewKpmClient()
assert.Equal(t, err, nil)
kclPkg, err := kpmcli.LoadPkgFromPath(testPkgPath)
assert.Equal(t, err, nil)

opts := opt.AddOptions{
LocalPath: testPkgPath,
RegistryOpts: opt.RegistryOptions{
Git: &opt.GitOptions{
Url: "https://github.com/kcl-lang/flask-demo-kcl-manifests.git",
Tag: "v0.1.0",
},
},
}
kpmcli.SetLogWriter(nil)
_, err = kpmcli.AddDepWithOpts(kclPkg, &opts)

assert.Equal(t, err, nil)

modContent, err := os.ReadFile(testPkgPathMod)
modContentStr := strings.ReplaceAll(string(modContent), "\r\n", "")
modContentStr = strings.ReplaceAll(modContentStr, "\n", "")
assert.Equal(t, err, nil)

modExpectContent, err := os.ReadFile(testPkgPathModExpect)
modExpectContentStr := strings.ReplaceAll(string(modExpectContent), "\r\n", "")
modExpectContentStr = strings.ReplaceAll(modExpectContentStr, "\n", "")

assert.Equal(t, err, nil)
assert.Equal(t, modContentStr, modExpectContentStr)

modLockContent, err := os.ReadFile(testPkgPathModLock)
modLockContentStr := strings.ReplaceAll(string(modLockContent), "\r\n", "")
modLockContentStr = strings.ReplaceAll(modLockContentStr, "\n", "")
assert.Equal(t, err, nil)
modLockExpectContent, err := os.ReadFile(testPkgPathModLockExpect)
modLockExpectContentStr := strings.ReplaceAll(string(modLockExpectContent), "\r\n", "")
modLockExpectContentStr = strings.ReplaceAll(modLockExpectContentStr, "\n", "")
assert.Equal(t, err, nil)
assert.Equal(t, modLockContentStr, modLockExpectContentStr)

defer func() {
_ = os.Remove(testPkgPathMod)
_ = os.Remove(testPkgPathModLock)
}()
}

func TestAddWithGitBranch(t *testing.T) {
pkgPath := getTestDir("add_with_git_branch")

testPkgPath := ""
if runtime.GOOS == "windows" {
testPkgPath = filepath.Join(pkgPath, "test_pkg_win")
} else {
testPkgPath = filepath.Join(pkgPath, "test_pkg")
}

testPkgPathModBak := filepath.Join(testPkgPath, "kcl.mod.bak")
testPkgPathMod := filepath.Join(testPkgPath, "kcl.mod")
testPkgPathModExpect := filepath.Join(testPkgPath, "kcl.mod.expect")
testPkgPathModLock := filepath.Join(testPkgPath, "kcl.mod.lock")
testPkgPathModLockBak := filepath.Join(testPkgPath, "kcl.mod.lock.bak")
testPkgPathModLockExpect := filepath.Join(testPkgPath, "kcl.mod.lock.expect")

err := copy.Copy(testPkgPathModBak, testPkgPathMod)
assert.Equal(t, err, nil)
err = copy.Copy(testPkgPathModLockBak, testPkgPathModLock)
assert.Equal(t, err, nil)

kpmcli, err := NewKpmClient()
assert.Equal(t, err, nil)
kclPkg, err := kpmcli.LoadPkgFromPath(testPkgPath)
assert.Equal(t, err, nil)

opts := opt.AddOptions{
LocalPath: testPkgPath,
RegistryOpts: opt.RegistryOptions{
Git: &opt.GitOptions{
Url: "https://github.com/kcl-lang/flask-demo-kcl-manifests.git",
Branch: "main",
},
},
}
kpmcli.SetLogWriter(nil)
_, err = kpmcli.AddDepWithOpts(kclPkg, &opts)

assert.Equal(t, err, nil)

modContent, err := os.ReadFile(testPkgPathMod)
modContentStr := strings.ReplaceAll(string(modContent), "\r\n", "")
modContentStr = strings.ReplaceAll(modContentStr, "\n", "")
assert.Equal(t, err, nil)

modExpectContent, err := os.ReadFile(testPkgPathModExpect)
modExpectContentStr := strings.ReplaceAll(string(modExpectContent), "\r\n", "")
modExpectContentStr = strings.ReplaceAll(modExpectContentStr, "\n", "")

assert.Equal(t, err, nil)
assert.Equal(t, modContentStr, modExpectContentStr)

modLockContent, err := os.ReadFile(testPkgPathModLock)
modLockContentStr := strings.ReplaceAll(string(modLockContent), "\r\n", "")
modLockContentStr = strings.ReplaceAll(modLockContentStr, "\n", "")
assert.Equal(t, err, nil)
modLockExpectContent, err := os.ReadFile(testPkgPathModLockExpect)
modLockExpectContentStr := strings.ReplaceAll(string(modLockExpectContent), "\r\n", "")
modLockExpectContentStr = strings.ReplaceAll(modLockExpectContentStr, "\n", "")
assert.Equal(t, err, nil)
assert.Equal(t, modLockContentStr, modLockExpectContentStr)

defer func() {
_ = os.Remove(testPkgPathMod)
_ = os.Remove(testPkgPathModLock)
}()
}

func TestLoadPkgFormOci(t *testing.T) {
type testCase struct {
Reg string
Expand Down
6 changes: 6 additions & 0 deletions pkg/client/test_data/add_with_git_branch/test_pkg/kcl.mod.bak
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "with_sum_check"
edition = "0.0.1"
version = "0.0.1"

[dependencies]
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "with_sum_check"
edition = "0.0.1"
version = "0.0.1"

[dependencies]
flask-demo-kcl-manifests = { git = "https://github.com/kcl-lang/flask-demo-kcl-manifests.git", branch = "main", version = "0.0.1" }
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[dependencies]
[dependencies.flask-demo-kcl-manifests]
name = "flask-demo-kcl-manifests"
full_name = "flask_manifests_0.0.1"
version = "0.0.1"
url = "https://github.com/kcl-lang/flask-demo-kcl-manifests.git"
branch = "main"
1 change: 1 addition & 0 deletions pkg/client/test_data/add_with_git_branch/test_pkg/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The_first_kcl_program = 'Hello World!'
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "with_sum_check"
edition = "0.0.1"
version = "0.0.1"

[dependencies]
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "with_sum_check"
edition = "0.0.1"
version = "0.0.1"

[dependencies]
flask-demo-kcl-manifests = { git = "https://github.com/kcl-lang/flask-demo-kcl-manifests.git", branch = "main", version = "0.0.1" }
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[dependencies]
[dependencies.flask-demo-kcl-manifests]
name = "flask-demo-kcl-manifests"
full_name = "flask_manifests_0.0.1"
version = "0.0.1"
url = "https://github.com/kcl-lang/flask-demo-kcl-manifests.git"
branch = "main"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The_first_kcl_program = 'Hello World!'
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ edition = "0.0.1"
version = "0.0.1"

[dependencies]
flask-demo-kcl-manifests = { git = "https://github.com/kcl-lang/flask-demo-kcl-manifests.git", commit = "ade147b" }
flask-demo-kcl-manifests = { git = "https://github.com/kcl-lang/flask-demo-kcl-manifests.git", commit = "ade147b", version = "0.0.1" }
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ edition = "0.0.1"
version = "0.0.1"

[dependencies]
flask-demo-kcl-manifests = { git = "https://github.com/kcl-lang/flask-demo-kcl-manifests.git", commit = "ade147b" }
flask-demo-kcl-manifests = { git = "https://github.com/kcl-lang/flask-demo-kcl-manifests.git", commit = "ade147b", version = "0.0.1" }
6 changes: 6 additions & 0 deletions pkg/client/test_data/add_with_git_tag/test_pkg/kcl.mod.bak
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "with_sum_check"
edition = "0.0.1"
version = "0.0.1"

[dependencies]
7 changes: 7 additions & 0 deletions pkg/client/test_data/add_with_git_tag/test_pkg/kcl.mod.expect
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "with_sum_check"
edition = "0.0.1"
version = "0.0.1"

[dependencies]
flask-demo-kcl-manifests = { git = "https://github.com/kcl-lang/flask-demo-kcl-manifests.git", tag = "v0.1.0", version = "0.0.1" }
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[dependencies]
[dependencies.flask-demo-kcl-manifests]
name = "flask-demo-kcl-manifests"
full_name = "flask_manifests_0.0.1"
version = "0.0.1"
url = "https://github.com/kcl-lang/flask-demo-kcl-manifests.git"
git_tag = "v0.1.0"
1 change: 1 addition & 0 deletions pkg/client/test_data/add_with_git_tag/test_pkg/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The_first_kcl_program = 'Hello World!'
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "with_sum_check"
edition = "0.0.1"
version = "0.0.1"

[dependencies]
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "with_sum_check"
edition = "0.0.1"
version = "0.0.1"

[dependencies]
flask-demo-kcl-manifests = { git = "https://github.com/kcl-lang/flask-demo-kcl-manifests.git", tag = "v0.1.0", version = "0.0.1" }
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[dependencies]
[dependencies.flask-demo-kcl-manifests]
name = "flask-demo-kcl-manifests"
full_name = "flask_manifests_0.0.1"
Copy link
Contributor

@zong-zhe zong-zhe Aug 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is probably the main reason for your test failure, and it is also where the PR is not yet complete, currently full_name is used to generate the path of the local dependency. So full_name should be <name>_<git_tag>, <name>_<git_commit> or <name>_<git_branch>, Instead of <name>_<version>, the problem was caused by the old local dependency file storage structure, and finish #384 work was more helpful in solving the problem.

I suggest stopping this PR for a while to concentrate on #384

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// GenDepFullName will generate the full name of a dependency by its name and version
// based on the '<package_name>_<package_tag>' format.
func (dep *Dependency) GenDepFullName() string {
    dep.FullName = fmt.Sprintf(PKG_NAME_PATTERN, dep.Name, dep.Version)
    return dep.FullName
}

Can this issue be resolved by changing the function mentioned above?

Copy link
Contributor

@zong-zhe zong-zhe Aug 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes to this method may affect other features at this stage, resulting in more test failures and more work. And this part of the work will be meaningless after #384 done. Therefore, we can try to stop this PR temporarily. Finish this PR after you finish #384

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I got your point now @zong-zhe

version = "0.0.1"
url = "https://github.com/kcl-lang/flask-demo-kcl-manifests.git"
git_tag = "v0.1.0"
1 change: 1 addition & 0 deletions pkg/client/test_data/add_with_git_tag/test_pkg_win/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The_first_kcl_program = 'Hello World!'
3 changes: 3 additions & 0 deletions pkg/package/toml.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,9 @@ func (dep *Dependencies) MarshalLockTOML() (string, error) {
if !ok {
break
}
if dep.Source.Git != nil {
dep.Source.Git.Version = ""
}
marshaledDeps[depKey] = dep
}

Expand Down
Loading