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

Sparse checkout - kcl mod add --package #453

Merged
merged 29 commits into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
11632e5
download a repo success
officialasishkumar Aug 17, 2024
0b57c39
change kcl.mod and kcl.mod.lock names with pkg name
officialasishkumar Aug 18, 2024
b913fbb
fix duplication of download in kcl.mod
officialasishkumar Aug 18, 2024
5fc146e
merge package functionality to kcl mod run
officialasishkumar Aug 18, 2024
53b67a2
fix seg fault in test case
officialasishkumar Aug 18, 2024
56b3ff8
remove last changes
officialasishkumar Aug 18, 2024
a8e640f
fix seg fault in e2e
officialasishkumar Aug 18, 2024
af3a900
remove pkg flag from kpmClient
officialasishkumar Aug 19, 2024
a45ba80
add unit test for FindPackage and matchesPackageName
officialasishkumar Aug 19, 2024
a16e457
new unit test for parseopt
officialasishkumar Aug 19, 2024
e4a4dd2
add unit test for download with git and package flag
officialasishkumar Aug 19, 2024
64621be
update parseOpt unit test
officialasishkumar Aug 19, 2024
7558424
test folder for utils_test
officialasishkumar Aug 19, 2024
5f4b6d1
new unit test
officialasishkumar Aug 19, 2024
766ce39
fix seg fault
officialasishkumar Aug 19, 2024
4221eb3
add unit test for mod file when package flag is used
officialasishkumar Aug 19, 2024
ecf16e6
fix failing unit test
officialasishkumar Aug 19, 2024
3e9d372
fix failing unit test
officialasishkumar Aug 19, 2024
e06bb6c
remove the contents of the generated lock files
officialasishkumar Aug 19, 2024
8cdac96
revert
officialasishkumar Aug 19, 2024
c8ffe70
add windows test
officialasishkumar Aug 20, 2024
b36c95f
add test folder
officialasishkumar Aug 20, 2024
fee67ab
fix failing unit test
officialasishkumar Aug 20, 2024
b74e785
remove the filter
officialasishkumar Aug 20, 2024
f973335
add nosumcheck
officialasishkumar Aug 20, 2024
e0c8c5a
nosumcheck to true
officialasishkumar Aug 20, 2024
3bd9a77
remove sum from actual
officialasishkumar Aug 20, 2024
9b3de39
remove comments from client_test
officialasishkumar Aug 20, 2024
d3a0c63
remove comments from client_test
officialasishkumar Aug 20, 2024
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
23 changes: 16 additions & 7 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,17 +283,17 @@ const PKG_NAME_PATTERN = "%s_%s"
// 2. in the vendor subdirectory of the current package.
// 3. the dependency is from the local path.
func (c *KpmClient) getDepStorePath(search_path string, d *pkg.Dependency, isVendor bool) string {

storePkgName := d.GenPathSuffix()

if d.IsFromLocal() {
return d.GetLocalFullPath(search_path)
} else {
path := ""
if isVendor {
return filepath.Join(search_path, "vendor", storePkgName)
path = filepath.Join(search_path, "vendor", storePkgName)
} else {
return filepath.Join(c.homePath, storePkgName)
path = filepath.Join(c.homePath, storePkgName)
}
return path
}
}

Expand Down Expand Up @@ -747,7 +747,7 @@ func (c *KpmClient) AddDepWithOpts(kclPkg *pkg.KclPkg, opt *opt.AddOptions) (*pk
c.noSumCheck = opt.NoSumCheck
kclPkg.NoSumCheck = opt.NoSumCheck

// 1. get the name and version of the repository from the input arguments.
// 1. get the name and version of the repository/package from the input arguments.
d, err := pkg.ParseOpt(&opt.RegistryOpts)
if err != nil {
return nil, err
Expand Down Expand Up @@ -1101,7 +1101,16 @@ func (c *KpmClient) Download(dep *pkg.Dependency, homePath, localPath string) (*
return nil, err
}

dep.LocalFullPath = localPath
if dep.GetPackage() != "" {
localFullPath, err := utils.FindPackage(localPath, dep.GetPackage())
if err != nil {
return nil, err
}
dep.LocalFullPath = localFullPath
dep.Name = dep.GetPackage()
} else {
dep.LocalFullPath = localPath
}
// Creating symbolic links in a global cache is not an optimal solution.
// This allows kclvm to locate the package by default.
// This feature is unstable and will be removed soon.
Expand All @@ -1111,7 +1120,7 @@ func (c *KpmClient) Download(dep *pkg.Dependency, homePath, localPath string) (*
// }
dep.FullName = dep.GenDepFullName()

modFile, err := c.LoadModFile(localPath)
modFile, err := c.LoadModFile(dep.LocalFullPath)
if err != nil {
return nil, err
}
Expand Down
126 changes: 125 additions & 1 deletion pkg/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"log"
"os"
"path/filepath"
"regexp"
"runtime"
"sort"
"strings"
Expand Down Expand Up @@ -139,6 +140,129 @@ func TestDownloadLatestOci(t *testing.T) {
assert.Equal(t, utils.DirExists(filepath.Join(getTestDir("download"), "helloworld")), false)
}

func TestDownloadGitWithPackage(t *testing.T) {
testPath := filepath.Join(getTestDir("download"), "a_random_name")

defer func() {
err := os.RemoveAll(getTestDir("download"))
if err != nil {
t.Errorf("Failed to remove directory: %v", err)
}
}()

err := os.MkdirAll(testPath, 0755)
assert.Equal(t, err, nil)

depFromGit := pkg.Dependency{
Name: "k8s",
Version: "",
Source: downloader.Source{
Git: &downloader.Git{
Url: "https://github.com/kcl-lang/modules.git",
Commit: "bdd4d00a88bc3534ae50affa8328df2927fd2171",
Package: "add-ndots",
},
},
}

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

dep, err := kpmcli.Download(&depFromGit, "", testPath)

assert.Equal(t, err, nil)
assert.Equal(t, dep.Source.Git.Package, "add-ndots")
}

func TestModandLockFilesWithGitPackageDownload(t *testing.T) {
testPkgPath := getTestDir("test_mod_file_package")

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

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/modules.git",
Commit: "ee03122b5f45b09eb48694422fc99a0772f6bba8",
Package: "agent",
},
},
}

_, err = kpmcli.AddDepWithOpts(kclPkg, &opts)
assert.Equal(t, err, nil)

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

modContent, err := os.ReadFile(testPkgPathMod)
assert.Equal(t, err, nil)

modExpectContent, err := os.ReadFile(testPkgPathModExpect)
assert.Equal(t, err, nil)

modContentStr := string(modContent)
modExpectContentStr := string(modExpectContent)

for _, str := range []*string{&modContentStr, &modExpectContentStr} {
*str = strings.ReplaceAll(*str, " ", "")
*str = strings.ReplaceAll(*str, "\r\n", "")
*str = strings.ReplaceAll(*str, "\n", "")

sumRegex := regexp.MustCompile(`sum\s*=\s*"[^"]+"`)
*str = sumRegex.ReplaceAllString(*str, "")

*str = strings.TrimRight(*str, ", \t\r\n")
}

assert.Equal(t, modExpectContentStr, modContentStr)

modLockContent, err := os.ReadFile(testPkgPathModLock)
assert.Equal(t, err, nil)

modLockExpectContent, err := os.ReadFile(testPkgPathModLockExpect)
assert.Equal(t, err, nil)

modLockContentStr := string(modLockContent)
modLockExpectContentStr := string(modLockExpectContent)

for _, str := range []*string{&modLockContentStr, &modLockExpectContentStr} {
*str = strings.ReplaceAll(*str, " ", "")
*str = strings.ReplaceAll(*str, "\r\n", "")
*str = strings.ReplaceAll(*str, "\n", "")

sumRegex := regexp.MustCompile(`sum\s*=\s*"[^"]+"`)
*str = sumRegex.ReplaceAllString(*str, "")

*str = strings.TrimRight(*str, ", \t\r\n")
}

fmt.Println(modLockContentStr)

assert.Equal(t, modLockExpectContentStr, modLockContentStr)

defer func() {
err = os.Truncate(testPkgPathMod, 0)
assert.Equal(t, err, nil)

err = os.Truncate(testPkgPathModLock, 0)
assert.Equal(t, err, nil)
}()
}

func TestDependencyGraph(t *testing.T) {
testDir := getTestDir("test_dependency_graph")
assert.Equal(t, utils.DirExists(filepath.Join(testDir, "kcl.mod.lock")), false)
Expand Down Expand Up @@ -889,7 +1013,7 @@ func TestUpdateWithKclModlock(t *testing.T) {
err = kpmcli.UpdateDeps(kclPkg)
assert.Equal(t, err, nil)
got_lock_file := filepath.Join(dest_testDir, "kcl.mod.lock")
got_content, err := os.ReadFile(got_lock_file)
got_content, err := os.ReadFile(got_lock_file) // help
assert.Equal(t, err, nil)

expected_path := filepath.Join(dest_testDir, "expected")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[package]

[dependencies]
agent = { git = "https://github.com/kcl-lang/modules.git", commit = "ee03122b5f45b09eb48694422fc99a0772f6bba8", package = "agent" }
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[dependencies]
[dependencies.agent]
name = "agent"
full_name = "agent_ee03122b5f45b09eb48694422fc99a0772f6bba8"
version = "0.1.0"
url = "https://github.com/kcl-lang/modules.git"
commit = "ee03122b5f45b09eb48694422fc99a0772f6bba8"
package = "agent"
[dependencies.k8s]
name = "k8s"
full_name = "k8s_1.28"
version = "1.28"
Empty file.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[package]

[dependencies]
agent = { git = "https://github.com/kcl-lang/modules.git", commit = "ee03122b5f45b09eb48694422fc99a0772f6bba8", package = "agent" }
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[dependencies]
[dependencies.agent]
name = "agent"
full_name = "agent_ee03122b5f45b09eb48694422fc99a0772f6bba8"
version = "0.1.0"
url = "https://github.com/kcl-lang/modules.git"
commit = "ee03122b5f45b09eb48694422fc99a0772f6bba8"
package = "agent"
[dependencies.k8s]
name = "k8s"
full_name = "k8s_1.28"
version = "1.28"
Empty file.
Empty file.
17 changes: 14 additions & 3 deletions pkg/cmd/cmd_add.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ func NewAddCmd(kpmcli *client.KpmClient) *cli.Command {
Name: "rename",
Usage: "rename the package name in kcl.mod.lock",
},
&cli.StringSliceFlag{
Name: "package",
Usage: "package name to use in case of git",
},
},

Action: func(c *cli.Context) error {
Expand Down Expand Up @@ -198,6 +202,12 @@ func parseGitRegistryOptions(c *cli.Context) (*opt.RegistryOptions, *reporter.Kp
return nil, err
}

gitPackage, err := onlyOnceOption(c, "package")

if err != (*reporter.KpmEvent)(nil) {
return nil, err
}

if gitUrl == "" {
return nil, reporter.NewErrorEvent(reporter.InvalidGitUrl, fmt.Errorf("the argument 'git' is required"))
}
Expand All @@ -208,9 +218,10 @@ func parseGitRegistryOptions(c *cli.Context) (*opt.RegistryOptions, *reporter.Kp

return &opt.RegistryOptions{
Git: &opt.GitOptions{
Url: gitUrl,
Tag: gitTag,
Commit: gitCommit,
Url: gitUrl,
Tag: gitTag,
Commit: gitCommit,
Package: gitPackage,
},
}, nil
}
Expand Down
8 changes: 8 additions & 0 deletions pkg/downloader/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type Git struct {
Commit string `toml:"commit,omitempty"`
Tag string `toml:"git_tag,omitempty"`
Version string `toml:"version,omitempty"`
Package string `toml:"package,omitempty"`
}

type Registry struct {
Expand Down Expand Up @@ -211,6 +212,13 @@ func (git *Git) ToFilePath() (string, error) {
), nil
}

func (git *Git) GetPackage() string {
if(git == nil) {
return ""
}
return git.Package
}

func (oci *Oci) ToFilePath() (string, error) {
if oci == nil {
return "", fmt.Errorf("oci source is nil")
Expand Down
12 changes: 12 additions & 0 deletions pkg/downloader/toml.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ const TAG_PATTERN = "tag = \"%s\""
const GIT_COMMIT_PATTERN = "commit = \"%s\""
const GIT_BRANCH_PATTERN = "branch = \"%s\""
const VERSION_PATTERN = "version = \"%s\""
const GIT_PACKAGE = "package = \"%s\""
const SEPARATOR = ", "

func (git *Git) MarshalTOML() string {
Expand All @@ -94,6 +95,12 @@ func (git *Git) MarshalTOML() string {
sb.WriteString(SEPARATOR)
sb.WriteString(fmt.Sprintf(VERSION_PATTERN, git.Version))
}

if len(git.Package) != 0 {
sb.WriteString(SEPARATOR)
sb.WriteString(fmt.Sprintf(GIT_PACKAGE, git.Package))
}

return sb.String()
}

Expand Down Expand Up @@ -175,6 +182,7 @@ const GIT_URL_FLAG = "git"
const TAG_FLAG = "tag"
const GIT_COMMIT_FLAG = "commit"
const GIT_BRANCH_FLAG = "branch"
const GIT_PACKAGE_FLAG = "package"

func (git *Git) UnmarshalModTOML(data interface{}) error {
meta, ok := data.(map[string]interface{})
Expand All @@ -198,6 +206,10 @@ func (git *Git) UnmarshalModTOML(data interface{}) error {
git.Branch = v
}

if v, ok := meta[GIT_PACKAGE_FLAG].(string); ok {
git.Package = v
}

return nil
}

Expand Down
9 changes: 5 additions & 4 deletions pkg/opt/opt.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,10 +378,11 @@ func ParseLocalPathOptions(localPath string) (*LocalOptions, *reporter.KpmEvent)
}

type GitOptions struct {
Url string
Branch string
Commit string
Tag string
Url string
Branch string
Commit string
Tag string
Package string
}

func (opts *GitOptions) Validate() error {
Expand Down
Loading