From 11632e5f9c326044d087ac39bf3886c571b93786 Mon Sep 17 00:00:00 2001 From: Asish Kumar Date: Sat, 17 Aug 2024 23:41:35 +0530 Subject: [PATCH 01/29] download a repo success Signed-off-by: Asish Kumar --- pkg/client/client.go | 24 ++++++++++++++++++++-- pkg/cmd/cmd_add.go | 21 ++++++++++++++++--- pkg/opt/opt.go | 9 +++++---- pkg/utils/utils.go | 48 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 93 insertions(+), 9 deletions(-) diff --git a/pkg/client/client.go b/pkg/client/client.go index ea3bd0fd..2e01ea48 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -52,6 +52,8 @@ type KpmClient struct { settings settings.Settings // The flag of whether to check the checksum of the package and update kcl.mod.lock. noSumCheck bool + // The package to use in case of multiple packages. + pkg string } // NewKpmClient will create a new kpm client with default settings. @@ -75,6 +77,16 @@ func NewKpmClient() (*KpmClient, error) { }, nil } +// SetPackage will set the 'package' to be used in case of multiple packages in a single repo. +func (c *KpmClient) SetPackage(pkgName string) { + c.pkg = pkgName +} + +// GetPackage will get the 'package' to be used in case of multiple packages in a single repo. +func (c *KpmClient) GetPackage() string { + return c.pkg +} + // SetNoSumCheck will set the 'noSumCheck' flag. func (c *KpmClient) SetNoSumCheck(noSumCheck bool) { c.noSumCheck = noSumCheck @@ -1101,7 +1113,15 @@ func (c *KpmClient) Download(dep *pkg.Dependency, homePath, localPath string) (* return nil, err } - dep.LocalFullPath = localPath + if c.GetPackage() != "" { + localFullPath, err := utils.FindPackage(localPath, c.GetPackage()) + if err != nil { + return nil, err + } + dep.LocalFullPath = localFullPath + } 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. @@ -1111,7 +1131,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 } diff --git a/pkg/cmd/cmd_add.go b/pkg/cmd/cmd_add.go index f0786759..e7ab687c 100644 --- a/pkg/cmd/cmd_add.go +++ b/pkg/cmd/cmd_add.go @@ -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 { @@ -94,6 +98,10 @@ func KpmAdd(c *cli.Context, kpmcli *client.KpmClient) error { return err } + if addOpts.RegistryOpts.Git.Package != "" { + kpmcli.SetPackage(addOpts.RegistryOpts.Git.Package) + } + if addOpts.RegistryOpts.Local != nil { absAddPath, err := filepath.Abs(addOpts.RegistryOpts.Local.Path) if err != nil { @@ -198,6 +206,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")) } @@ -208,9 +222,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 } diff --git a/pkg/opt/opt.go b/pkg/opt/opt.go index 9c863ca4..02309a55 100644 --- a/pkg/opt/opt.go +++ b/pkg/opt/opt.go @@ -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 { diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go index 14bc9b06..6c7cdff2 100644 --- a/pkg/utils/utils.go +++ b/pkg/utils/utils.go @@ -17,6 +17,7 @@ import ( "runtime" "strings" + "github.com/BurntSushi/toml" "github.com/distribution/reference" "github.com/moby/term" "github.com/otiai10/copy" @@ -599,3 +600,50 @@ func AbsTarPath(tarPath string) (string, error) { return absTarPath, nil } + +// FindPackage finds the package with the package name 'targetPackage' under the 'root' directory kcl.mod file. +func FindPackage(root, targetPackage string) (string, error) { + var result string + err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error { + if err != nil { + return err + } + if info.IsDir() { + kclModPath := filepath.Join(path, constants.KCL_MOD) + if _, err := os.Stat(kclModPath); err == nil { + if matchesPackageName(kclModPath, targetPackage) { + result = path + return filepath.SkipAll + } + } + } + return nil + }) + + if err != nil { + return "", err + } + if result == "" { + return "", fmt.Errorf("kcl.mod with package '%s' not found", targetPackage) + } + return result, nil +} + +// MatchesPackageName checks whether the package name in the kcl.mod file under 'kclModPath' is equal to 'targetPackage'. +func matchesPackageName(kclModPath, targetPackage string) bool { + type Package struct { + Name string `toml:"name"` + } + type ModFile struct { + Package Package `toml:"package"` + } + + var modFile ModFile + _, err := toml.DecodeFile(kclModPath, &modFile) + if err != nil { + fmt.Printf("Error parsing kcl.mod file: %v\n", err) + return false + } + + return modFile.Package.Name == targetPackage +} From 0b57c398d7571aea76f7d096a168f98493cfea92 Mon Sep 17 00:00:00 2001 From: Asish Kumar Date: Sun, 18 Aug 2024 12:42:29 +0530 Subject: [PATCH 02/29] change kcl.mod and kcl.mod.lock names with pkg name Signed-off-by: Asish Kumar --- pkg/client/client.go | 1 + pkg/downloader/source.go | 1 + pkg/downloader/toml.go | 12 ++++++++++++ pkg/package/modfile.go | 9 +++++---- 4 files changed, 19 insertions(+), 4 deletions(-) diff --git a/pkg/client/client.go b/pkg/client/client.go index 2e01ea48..0d2c0302 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -1119,6 +1119,7 @@ func (c *KpmClient) Download(dep *pkg.Dependency, homePath, localPath string) (* return nil, err } dep.LocalFullPath = localFullPath + dep.Name = c.GetPackage() } else { dep.LocalFullPath = localPath } diff --git a/pkg/downloader/source.go b/pkg/downloader/source.go index 5cec444e..e1b78bb2 100644 --- a/pkg/downloader/source.go +++ b/pkg/downloader/source.go @@ -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 { diff --git a/pkg/downloader/toml.go b/pkg/downloader/toml.go index 5c4d3f6c..565c4b34 100644 --- a/pkg/downloader/toml.go +++ b/pkg/downloader/toml.go @@ -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 { @@ -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() } @@ -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{}) @@ -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 } diff --git a/pkg/package/modfile.go b/pkg/package/modfile.go index b4fb3b49..ed61d592 100644 --- a/pkg/package/modfile.go +++ b/pkg/package/modfile.go @@ -482,10 +482,11 @@ func (deps *Dependencies) loadLockFile(filepath string) error { func ParseOpt(opt *opt.RegistryOptions) (*Dependency, error) { if opt.Git != nil { gitSource := downloader.Git{ - Url: opt.Git.Url, - Branch: opt.Git.Branch, - Commit: opt.Git.Commit, - Tag: opt.Git.Tag, + Url: opt.Git.Url, + Branch: opt.Git.Branch, + Commit: opt.Git.Commit, + Tag: opt.Git.Tag, + Package: opt.Git.Package, } gitRef, err := gitSource.GetValidGitReference() From b913fbb8abd442359f1b212face962140d68c7bf Mon Sep 17 00:00:00 2001 From: Asish Kumar Date: Sun, 18 Aug 2024 15:54:24 +0530 Subject: [PATCH 03/29] fix duplication of download in kcl.mod Signed-off-by: Asish Kumar --- pkg/client/client.go | 2 +- pkg/package/modfile.go | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/pkg/client/client.go b/pkg/client/client.go index 0d2c0302..a49ae209 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -759,7 +759,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 diff --git a/pkg/package/modfile.go b/pkg/package/modfile.go index ed61d592..0f72cf32 100644 --- a/pkg/package/modfile.go +++ b/pkg/package/modfile.go @@ -567,18 +567,29 @@ func ParseOpt(opt *opt.RegistryOptions) (*Dependency, error) { const PKG_NAME_PATTERN = "%s_%s" // ParseRepoFullNameFromGitSource will extract the kcl package name from the git url. +// If the package flag is passed then it will be used as the package name. func ParseRepoFullNameFromGitSource(gitSrc downloader.Git) (string, error) { ref, err := gitSrc.GetValidGitReference() if err != nil { return "", err } if len(ref) != 0 { + if len(gitSrc.Package) != 0 { + return fmt.Sprintf(PKG_NAME_PATTERN, gitSrc.Package, ref), nil + } return fmt.Sprintf(PKG_NAME_PATTERN, utils.ParseRepoNameFromGitUrl(gitSrc.Url), ref), nil } + if len(gitSrc.Package) != 0 { + return gitSrc.Package, nil + } return utils.ParseRepoNameFromGitUrl(gitSrc.Url), nil } // ParseRepoNameFromGitSource will extract the kcl package name from the git url. +// If the package flag is passed then it will be used func ParseRepoNameFromGitSource(gitSrc downloader.Git) string { + if gitSrc.Package != "" { + return gitSrc.Package + } return utils.ParseRepoNameFromGitUrl(gitSrc.Url) } From 5fc146ed2ebaa58333884cbe689920a53ac2dd4d Mon Sep 17 00:00:00 2001 From: Asish Kumar Date: Sun, 18 Aug 2024 16:21:09 +0530 Subject: [PATCH 04/29] merge package functionality to kcl mod run Signed-off-by: Asish Kumar --- pkg/client/client.go | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/pkg/client/client.go b/pkg/client/client.go index a49ae209..5337dd5d 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -295,17 +295,20 @@ 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) + } + if d.Source.Git.Package != "" { + path, _ = utils.FindPackage(path, d.Source.Git.Package) } + return path } } From 53b67a2c35a34713efe06735a8e474628de6c918 Mon Sep 17 00:00:00 2001 From: Asish Kumar Date: Sun, 18 Aug 2024 17:24:50 +0530 Subject: [PATCH 05/29] fix seg fault in test case Signed-off-by: Asish Kumar --- pkg/client/client.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/client/client.go b/pkg/client/client.go index 5337dd5d..d6b27bf1 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -305,8 +305,8 @@ func (c *KpmClient) getDepStorePath(search_path string, d *pkg.Dependency, isVen } else { path = filepath.Join(c.homePath, storePkgName) } - if d.Source.Git.Package != "" { - path, _ = utils.FindPackage(path, d.Source.Git.Package) + if c.GetPackage() != "" { + path, _ = utils.FindPackage(path, c.GetPackage()) } return path } From 56b3ff81a74e5a970012326813c669b03a13527b Mon Sep 17 00:00:00 2001 From: Asish Kumar Date: Sun, 18 Aug 2024 20:48:53 +0530 Subject: [PATCH 06/29] remove last changes Signed-off-by: Asish Kumar --- pkg/cmd/cmd_add.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pkg/cmd/cmd_add.go b/pkg/cmd/cmd_add.go index e7ab687c..193611ab 100644 --- a/pkg/cmd/cmd_add.go +++ b/pkg/cmd/cmd_add.go @@ -98,10 +98,6 @@ func KpmAdd(c *cli.Context, kpmcli *client.KpmClient) error { return err } - if addOpts.RegistryOpts.Git.Package != "" { - kpmcli.SetPackage(addOpts.RegistryOpts.Git.Package) - } - if addOpts.RegistryOpts.Local != nil { absAddPath, err := filepath.Abs(addOpts.RegistryOpts.Local.Path) if err != nil { @@ -143,6 +139,10 @@ func onlyOnceOption(c *cli.Context, name string) (string, *reporter.KpmEvent) { func parseAddOptions(c *cli.Context, kpmcli *client.KpmClient, localPath string) (*opt.AddOptions, error) { noSumCheck := c.Bool(FLAG_NO_SUM_CHECK) newPkgName := c.String("rename") + pkg := c.StringSlice("package") + if pkg[0] != "" { + kpmcli.SetPackage(pkg[0]) + } // parse from 'kpm add -git https://xxx/xxx.git -tag v0.0.1'. if c.NArg() == 0 { gitOpts, err := parseGitRegistryOptions(c) From a8e640ff99a407eda065830558f0918ccc9a39cf Mon Sep 17 00:00:00 2001 From: Asish Kumar Date: Sun, 18 Aug 2024 20:53:43 +0530 Subject: [PATCH 07/29] fix seg fault in e2e Signed-off-by: Asish Kumar --- pkg/cmd/cmd_add.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/cmd/cmd_add.go b/pkg/cmd/cmd_add.go index 193611ab..80128cf7 100644 --- a/pkg/cmd/cmd_add.go +++ b/pkg/cmd/cmd_add.go @@ -140,7 +140,7 @@ func parseAddOptions(c *cli.Context, kpmcli *client.KpmClient, localPath string) noSumCheck := c.Bool(FLAG_NO_SUM_CHECK) newPkgName := c.String("rename") pkg := c.StringSlice("package") - if pkg[0] != "" { + if len(pkg) >= 1 { kpmcli.SetPackage(pkg[0]) } // parse from 'kpm add -git https://xxx/xxx.git -tag v0.0.1'. From af3a90033472cb5a796dcd0638f71e50a434608d Mon Sep 17 00:00:00 2001 From: Asish Kumar Date: Mon, 19 Aug 2024 15:32:31 +0530 Subject: [PATCH 08/29] remove pkg flag from kpmClient Signed-off-by: Asish Kumar --- pkg/client/client.go | 22 +++++----------------- pkg/cmd/cmd_add.go | 4 ---- pkg/downloader/source.go | 7 +++++++ 3 files changed, 12 insertions(+), 21 deletions(-) diff --git a/pkg/client/client.go b/pkg/client/client.go index d6b27bf1..ea0bc8f2 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -52,8 +52,6 @@ type KpmClient struct { settings settings.Settings // The flag of whether to check the checksum of the package and update kcl.mod.lock. noSumCheck bool - // The package to use in case of multiple packages. - pkg string } // NewKpmClient will create a new kpm client with default settings. @@ -77,16 +75,6 @@ func NewKpmClient() (*KpmClient, error) { }, nil } -// SetPackage will set the 'package' to be used in case of multiple packages in a single repo. -func (c *KpmClient) SetPackage(pkgName string) { - c.pkg = pkgName -} - -// GetPackage will get the 'package' to be used in case of multiple packages in a single repo. -func (c *KpmClient) GetPackage() string { - return c.pkg -} - // SetNoSumCheck will set the 'noSumCheck' flag. func (c *KpmClient) SetNoSumCheck(noSumCheck bool) { c.noSumCheck = noSumCheck @@ -305,8 +293,8 @@ func (c *KpmClient) getDepStorePath(search_path string, d *pkg.Dependency, isVen } else { path = filepath.Join(c.homePath, storePkgName) } - if c.GetPackage() != "" { - path, _ = utils.FindPackage(path, c.GetPackage()) + if d.GetPackage() != "" { + path, _ = utils.FindPackage(path, d.GetPackage()) } return path } @@ -1116,13 +1104,13 @@ func (c *KpmClient) Download(dep *pkg.Dependency, homePath, localPath string) (* return nil, err } - if c.GetPackage() != "" { - localFullPath, err := utils.FindPackage(localPath, c.GetPackage()) + if dep.GetPackage() != "" { + localFullPath, err := utils.FindPackage(localPath, dep.GetPackage()) if err != nil { return nil, err } dep.LocalFullPath = localFullPath - dep.Name = c.GetPackage() + dep.Name = dep.GetPackage() } else { dep.LocalFullPath = localPath } diff --git a/pkg/cmd/cmd_add.go b/pkg/cmd/cmd_add.go index 80128cf7..30761bdf 100644 --- a/pkg/cmd/cmd_add.go +++ b/pkg/cmd/cmd_add.go @@ -139,10 +139,6 @@ func onlyOnceOption(c *cli.Context, name string) (string, *reporter.KpmEvent) { func parseAddOptions(c *cli.Context, kpmcli *client.KpmClient, localPath string) (*opt.AddOptions, error) { noSumCheck := c.Bool(FLAG_NO_SUM_CHECK) newPkgName := c.String("rename") - pkg := c.StringSlice("package") - if len(pkg) >= 1 { - kpmcli.SetPackage(pkg[0]) - } // parse from 'kpm add -git https://xxx/xxx.git -tag v0.0.1'. if c.NArg() == 0 { gitOpts, err := parseGitRegistryOptions(c) diff --git a/pkg/downloader/source.go b/pkg/downloader/source.go index e1b78bb2..2576bbee 100644 --- a/pkg/downloader/source.go +++ b/pkg/downloader/source.go @@ -212,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") From a45ba803c074b7cedc9c19d1014bbe60bc2aa00f Mon Sep 17 00:00:00 2001 From: Asish Kumar Date: Mon, 19 Aug 2024 17:15:54 +0530 Subject: [PATCH 09/29] add unit test for FindPackage and matchesPackageName Signed-off-by: Asish Kumar --- pkg/utils/utils_test.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/pkg/utils/utils_test.go b/pkg/utils/utils_test.go index 92d1d1d2..fc36bde2 100644 --- a/pkg/utils/utils_test.go +++ b/pkg/utils/utils_test.go @@ -302,3 +302,16 @@ func TestIsModRelativePath(t *testing.T) { assert.Equal(t, IsModRelativePath("${helloworld:KCL_MOD}/aaa"), true) assert.Equal(t, IsModRelativePath("xxx/xxx/xxx"), false) } + + +func TestFindPackage(t *testing.T) { + testDir := getTestDir("test_find_package") + correctAddress := filepath.Join(testDir, "test_2") + foundAddress, _ := FindPackage(testDir, "test_find_package") + assert.Equal(t, foundAddress, correctAddress) +} + +func TestMatchesPackageName(t *testing.T) { + address := filepath.Join(getTestDir("test_find_package"), "test_2", "kcl.mod") + assert.Equal(t, matchesPackageName(address, "test_find_package"), true) +} \ No newline at end of file From a16e457ceb8971ed3c9d3b6efce3e51bc5f9acc1 Mon Sep 17 00:00:00 2001 From: Asish Kumar Date: Mon, 19 Aug 2024 17:23:30 +0530 Subject: [PATCH 10/29] new unit test for parseopt Signed-off-by: Asish Kumar --- pkg/package/modfile_test.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/pkg/package/modfile_test.go b/pkg/package/modfile_test.go index 8d3425d1..56e5f333 100644 --- a/pkg/package/modfile_test.go +++ b/pkg/package/modfile_test.go @@ -146,6 +146,18 @@ func TestParseOpt(t *testing.T) { assert.Equal(t, dep.Branch, "") assert.Equal(t, dep.Commit, "") assert.Equal(t, dep.Git.Tag, "test_tag") + + dep, _ = ParseOpt(&opt.RegistryOptions{ + Git: &opt.GitOptions{ + Url: "test.git", + Branch: "", + Commit: "", + Tag: "test_tag", + Package: "k8s", + }, + }) + + assert.Equal(t, dep.Git.Package, "k8s") } func TestLoadModFileNotExist(t *testing.T) { From e4a4dd24454358410dd296eac2855176de42bccb Mon Sep 17 00:00:00 2001 From: Asish Kumar Date: Mon, 19 Aug 2024 18:24:49 +0530 Subject: [PATCH 11/29] add unit test for download with git and package flag Signed-off-by: Asish Kumar --- pkg/client/client_test.go | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/pkg/client/client_test.go b/pkg/client/client_test.go index b47161f5..e7c92bc8 100644 --- a/pkg/client/client_test.go +++ b/pkg/client/client_test.go @@ -139,6 +139,40 @@ 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 TestDependencyGraph(t *testing.T) { testDir := getTestDir("test_dependency_graph") assert.Equal(t, utils.DirExists(filepath.Join(testDir, "kcl.mod.lock")), false) From 64621be4a086ae9a8b761e8f71fde99b154012a4 Mon Sep 17 00:00:00 2001 From: Asish Kumar Date: Mon, 19 Aug 2024 18:27:57 +0530 Subject: [PATCH 12/29] update parseOpt unit test Signed-off-by: Asish Kumar --- pkg/package/modfile_test.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pkg/package/modfile_test.go b/pkg/package/modfile_test.go index 56e5f333..d340c183 100644 --- a/pkg/package/modfile_test.go +++ b/pkg/package/modfile_test.go @@ -157,6 +157,8 @@ func TestParseOpt(t *testing.T) { }, }) + assert.Equal(t, dep.Name, "k8s") + assert.Equal(t, dep.FullName, "k8s_test_tag") assert.Equal(t, dep.Git.Package, "k8s") } From 75584245d4906b717243d536180541276aeae2f5 Mon Sep 17 00:00:00 2001 From: Asish Kumar Date: Mon, 19 Aug 2024 18:38:42 +0530 Subject: [PATCH 13/29] test folder for utils_test Signed-off-by: Asish Kumar --- pkg/utils/test_data/test_find_package/test_1/kcl.mod | 4 ++++ pkg/utils/test_data/test_find_package/test_2/kcl.mod | 4 ++++ 2 files changed, 8 insertions(+) create mode 100644 pkg/utils/test_data/test_find_package/test_1/kcl.mod create mode 100644 pkg/utils/test_data/test_find_package/test_2/kcl.mod diff --git a/pkg/utils/test_data/test_find_package/test_1/kcl.mod b/pkg/utils/test_data/test_find_package/test_1/kcl.mod new file mode 100644 index 00000000..6b1923fc --- /dev/null +++ b/pkg/utils/test_data/test_find_package/test_1/kcl.mod @@ -0,0 +1,4 @@ +[package] +name = "test_find_package_false" +edition = "v0.1.0" +version = "0.0.1" \ No newline at end of file diff --git a/pkg/utils/test_data/test_find_package/test_2/kcl.mod b/pkg/utils/test_data/test_find_package/test_2/kcl.mod new file mode 100644 index 00000000..78e4cf8a --- /dev/null +++ b/pkg/utils/test_data/test_find_package/test_2/kcl.mod @@ -0,0 +1,4 @@ +[package] +name = "test_find_package" +edition = "v0.1.0" +version = "0.0.1" \ No newline at end of file From 5f4b6d13d557ba4c35d39d3b1ce328012c99e02a Mon Sep 17 00:00:00 2001 From: Asish Kumar Date: Tue, 20 Aug 2024 00:46:04 +0530 Subject: [PATCH 14/29] new unit test Signed-off-by: Asish Kumar --- pkg/client/client_test.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/pkg/client/client_test.go b/pkg/client/client_test.go index e7c92bc8..a920c5c7 100644 --- a/pkg/client/client_test.go +++ b/pkg/client/client_test.go @@ -173,6 +173,30 @@ func TestDownloadGitWithPackage(t *testing.T) { assert.Equal(t, dep.Source.Git.Package, "add-ndots") } +func TestModandLockFilesWithGitPackageDownload(t *testing.T) { + testPkgPath := getTestDir("test_mod_file_package") + + 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) +} + func TestDependencyGraph(t *testing.T) { testDir := getTestDir("test_dependency_graph") assert.Equal(t, utils.DirExists(filepath.Join(testDir, "kcl.mod.lock")), false) From 766ce395d3e1236981ad24fa7363eb8c1c0101c0 Mon Sep 17 00:00:00 2001 From: Asish Kumar Date: Tue, 20 Aug 2024 01:10:27 +0530 Subject: [PATCH 15/29] fix seg fault Signed-off-by: Asish Kumar --- pkg/client/client.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/pkg/client/client.go b/pkg/client/client.go index ea0bc8f2..946ddf51 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -293,9 +293,6 @@ func (c *KpmClient) getDepStorePath(search_path string, d *pkg.Dependency, isVen } else { path = filepath.Join(c.homePath, storePkgName) } - if d.GetPackage() != "" { - path, _ = utils.FindPackage(path, d.GetPackage()) - } return path } } From 4221eb3820b521fc0b6938058d943cad047627ba Mon Sep 17 00:00:00 2001 From: Asish Kumar Date: Tue, 20 Aug 2024 01:26:06 +0530 Subject: [PATCH 16/29] add unit test for mod file when package flag is used Signed-off-by: Asish Kumar --- pkg/client/client_test.go | 22 ++++++++++++++++++- .../test_mod_file_package/expect.mod | 4 ++++ .../test_mod_file_package/expect.mod.lock | 12 ++++++++++ .../test_data/test_mod_file_package/kcl.mod | 0 .../test_mod_file_package/kcl.mod.lock | 0 5 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 pkg/client/test_data/test_mod_file_package/expect.mod create mode 100644 pkg/client/test_data/test_mod_file_package/expect.mod.lock create mode 100644 pkg/client/test_data/test_mod_file_package/kcl.mod create mode 100644 pkg/client/test_data/test_mod_file_package/kcl.mod.lock diff --git a/pkg/client/client_test.go b/pkg/client/client_test.go index a920c5c7..89c7f15e 100644 --- a/pkg/client/client_test.go +++ b/pkg/client/client_test.go @@ -195,6 +195,26 @@ func TestModandLockFilesWithGitPackageDownload(t *testing.T) { _, err = kpmcli.AddDepWithOpts(kclPkg, &opts) assert.Equal(t, err, nil) + + got_lock_file := filepath.Join(testPkgPath, "kcl.mod.lock") + got_content, err := os.ReadFile(got_lock_file) + assert.Equal(t, err, nil) + + expected_path := filepath.Join(testPkgPath, "expect.mod.lock") + expected_content, err := os.ReadFile(expected_path) + + assert.Equal(t, err, nil) + assert.Equal(t, string(got_content), string(expected_content)) + + got_lock_file = filepath.Join(testPkgPath, "kcl.mod") + got_content, err = os.ReadFile(got_lock_file) + assert.Equal(t, err, nil) + + expected_path = filepath.Join(testPkgPath, "expect.mod") + expected_content, err = os.ReadFile(expected_path) + + assert.Equal(t, err, nil) + assert.Equal(t, string(got_content), string(expected_content)) } func TestDependencyGraph(t *testing.T) { @@ -947,7 +967,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") diff --git a/pkg/client/test_data/test_mod_file_package/expect.mod b/pkg/client/test_data/test_mod_file_package/expect.mod new file mode 100644 index 00000000..18fcce1e --- /dev/null +++ b/pkg/client/test_data/test_mod_file_package/expect.mod @@ -0,0 +1,4 @@ +[package] + +[dependencies] +agent = { git = "https://github.com/kcl-lang/modules.git", commit = "ee03122b5f45b09eb48694422fc99a0772f6bba8", package = "agent" } \ No newline at end of file diff --git a/pkg/client/test_data/test_mod_file_package/expect.mod.lock b/pkg/client/test_data/test_mod_file_package/expect.mod.lock new file mode 100644 index 00000000..f7821a7d --- /dev/null +++ b/pkg/client/test_data/test_mod_file_package/expect.mod.lock @@ -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" diff --git a/pkg/client/test_data/test_mod_file_package/kcl.mod b/pkg/client/test_data/test_mod_file_package/kcl.mod new file mode 100644 index 00000000..e69de29b diff --git a/pkg/client/test_data/test_mod_file_package/kcl.mod.lock b/pkg/client/test_data/test_mod_file_package/kcl.mod.lock new file mode 100644 index 00000000..e69de29b From ecf16e6fce3cc10867878684101907f229d6ad28 Mon Sep 17 00:00:00 2001 From: Asish Kumar Date: Tue, 20 Aug 2024 01:30:09 +0530 Subject: [PATCH 17/29] fix failing unit test Signed-off-by: Asish Kumar --- pkg/client/client_test.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/pkg/client/client_test.go b/pkg/client/client_test.go index 89c7f15e..1917717d 100644 --- a/pkg/client/client_test.go +++ b/pkg/client/client_test.go @@ -191,6 +191,7 @@ func TestModandLockFilesWithGitPackageDownload(t *testing.T) { Package: "agent", }, }, + NoSumCheck: false, } _, err = kpmcli.AddDepWithOpts(kclPkg, &opts) @@ -202,8 +203,8 @@ func TestModandLockFilesWithGitPackageDownload(t *testing.T) { expected_path := filepath.Join(testPkgPath, "expect.mod.lock") expected_content, err := os.ReadFile(expected_path) - assert.Equal(t, err, nil) + assert.Equal(t, string(got_content), string(expected_content)) got_lock_file = filepath.Join(testPkgPath, "kcl.mod") @@ -212,8 +213,8 @@ func TestModandLockFilesWithGitPackageDownload(t *testing.T) { expected_path = filepath.Join(testPkgPath, "expect.mod") expected_content, err = os.ReadFile(expected_path) - assert.Equal(t, err, nil) + assert.Equal(t, string(got_content), string(expected_content)) } From 3e9d372f946bbb29c6c861aca26a63912e511cf5 Mon Sep 17 00:00:00 2001 From: Asish Kumar Date: Tue, 20 Aug 2024 01:40:44 +0530 Subject: [PATCH 18/29] fix failing unit test Signed-off-by: Asish Kumar --- pkg/client/client_test.go | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/pkg/client/client_test.go b/pkg/client/client_test.go index 1917717d..b1c54ff1 100644 --- a/pkg/client/client_test.go +++ b/pkg/client/client_test.go @@ -168,7 +168,7 @@ func TestDownloadGitWithPackage(t *testing.T) { 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") } @@ -186,12 +186,11 @@ func TestModandLockFilesWithGitPackageDownload(t *testing.T) { LocalPath: testPkgPath, RegistryOpts: opt.RegistryOptions{ Git: &opt.GitOptions{ - Url: "https://github.com/kcl-lang/modules.git", - Commit: "ee03122b5f45b09eb48694422fc99a0772f6bba8", + Url: "https://github.com/kcl-lang/modules.git", + Commit: "ee03122b5f45b09eb48694422fc99a0772f6bba8", Package: "agent", }, }, - NoSumCheck: false, } _, err = kpmcli.AddDepWithOpts(kclPkg, &opts) @@ -201,11 +200,20 @@ func TestModandLockFilesWithGitPackageDownload(t *testing.T) { got_content, err := os.ReadFile(got_lock_file) assert.Equal(t, err, nil) + got_content_lines := strings.Split(string(got_content), "\n") + got_content_filtered := "" + for _, line := range got_content_lines { + if !strings.Contains(line, "sum") { + got_content_filtered += line + "\n" + } + } + got_content_filtered = strings.TrimSuffix(got_content_filtered, "\n") + expected_path := filepath.Join(testPkgPath, "expect.mod.lock") expected_content, err := os.ReadFile(expected_path) assert.Equal(t, err, nil) - assert.Equal(t, string(got_content), string(expected_content)) + assert.Equal(t, got_content_filtered, string(expected_content)) got_lock_file = filepath.Join(testPkgPath, "kcl.mod") got_content, err = os.ReadFile(got_lock_file) From e06bb6cbb51f2c23668d3894cf38e053af7b0e1b Mon Sep 17 00:00:00 2001 From: Asish Kumar Date: Tue, 20 Aug 2024 01:47:11 +0530 Subject: [PATCH 19/29] remove the contents of the generated lock files Signed-off-by: Asish Kumar --- pkg/client/client_test.go | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/pkg/client/client_test.go b/pkg/client/client_test.go index b1c54ff1..f2dbc503 100644 --- a/pkg/client/client_test.go +++ b/pkg/client/client_test.go @@ -215,8 +215,13 @@ func TestModandLockFilesWithGitPackageDownload(t *testing.T) { assert.Equal(t, got_content_filtered, string(expected_content)) - got_lock_file = filepath.Join(testPkgPath, "kcl.mod") - got_content, err = os.ReadFile(got_lock_file) + defer func() { + err = os.Truncate(got_lock_file, 0) + assert.Equal(t, err, nil) + } () + + got_mod_lock_file := filepath.Join(testPkgPath, "kcl.mod") + got_content, err = os.ReadFile(got_mod_lock_file) assert.Equal(t, err, nil) expected_path = filepath.Join(testPkgPath, "expect.mod") @@ -224,6 +229,11 @@ func TestModandLockFilesWithGitPackageDownload(t *testing.T) { assert.Equal(t, err, nil) assert.Equal(t, string(got_content), string(expected_content)) + + defer func() { + err = os.Truncate(got_mod_lock_file, 0) + assert.Equal(t, err, nil) + } () } func TestDependencyGraph(t *testing.T) { From 8cdac96662a313ff52337d227e923992c3d652e7 Mon Sep 17 00:00:00 2001 From: Asish Kumar Date: Tue, 20 Aug 2024 01:56:51 +0530 Subject: [PATCH 20/29] revert Signed-off-by: Asish Kumar --- pkg/client/client_test.go | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/pkg/client/client_test.go b/pkg/client/client_test.go index f2dbc503..e66fa83a 100644 --- a/pkg/client/client_test.go +++ b/pkg/client/client_test.go @@ -215,11 +215,6 @@ func TestModandLockFilesWithGitPackageDownload(t *testing.T) { assert.Equal(t, got_content_filtered, string(expected_content)) - defer func() { - err = os.Truncate(got_lock_file, 0) - assert.Equal(t, err, nil) - } () - got_mod_lock_file := filepath.Join(testPkgPath, "kcl.mod") got_content, err = os.ReadFile(got_mod_lock_file) assert.Equal(t, err, nil) @@ -229,11 +224,6 @@ func TestModandLockFilesWithGitPackageDownload(t *testing.T) { assert.Equal(t, err, nil) assert.Equal(t, string(got_content), string(expected_content)) - - defer func() { - err = os.Truncate(got_mod_lock_file, 0) - assert.Equal(t, err, nil) - } () } func TestDependencyGraph(t *testing.T) { From c8ffe70fac322f67442e5f27a1c15f6562b8762b Mon Sep 17 00:00:00 2001 From: Asish Kumar Date: Tue, 20 Aug 2024 09:06:55 +0530 Subject: [PATCH 21/29] add windows test Signed-off-by: Asish Kumar --- pkg/client/client_test.go | 53 ++++++++++++++++++++++++++------------- 1 file changed, 35 insertions(+), 18 deletions(-) diff --git a/pkg/client/client_test.go b/pkg/client/client_test.go index e66fa83a..1621aa50 100644 --- a/pkg/client/client_test.go +++ b/pkg/client/client_test.go @@ -176,6 +176,12 @@ func TestDownloadGitWithPackage(t *testing.T) { 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) @@ -196,34 +202,45 @@ func TestModandLockFilesWithGitPackageDownload(t *testing.T) { _, err = kpmcli.AddDepWithOpts(kclPkg, &opts) assert.Equal(t, err, nil) - got_lock_file := filepath.Join(testPkgPath, "kcl.mod.lock") - got_content, err := os.ReadFile(got_lock_file) + 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) + + modContentStr := strings.ReplaceAll(string(modContent), "\r\n", "") + modContentStr = strings.ReplaceAll(modContentStr, "\n", "") assert.Equal(t, err, nil) - got_content_lines := strings.Split(string(got_content), "\n") - got_content_filtered := "" - for _, line := range got_content_lines { - if !strings.Contains(line, "sum") { - got_content_filtered += line + "\n" - } - } - got_content_filtered = strings.TrimSuffix(got_content_filtered, "\n") + modExpectContent, err := os.ReadFile(testPkgPathModExpect) - expected_path := filepath.Join(testPkgPath, "expect.mod.lock") - expected_content, err := os.ReadFile(expected_path) + modExpectContentStr := strings.ReplaceAll(string(modExpectContent), "\r\n", "") + modExpectContentStr = strings.ReplaceAll(modExpectContentStr, "\n", "") assert.Equal(t, err, nil) - assert.Equal(t, got_content_filtered, string(expected_content)) + assert.Equal(t, modContentStr, modExpectContentStr) + + modLockContent, err := os.ReadFile(testPkgPathModLock) - got_mod_lock_file := filepath.Join(testPkgPath, "kcl.mod") - got_content, err = os.ReadFile(got_mod_lock_file) + modLockContentStr := strings.ReplaceAll(string(modLockContent), "\r\n", "") + modLockContentStr = strings.ReplaceAll(modLockContentStr, "\n", "") assert.Equal(t, err, nil) - expected_path = filepath.Join(testPkgPath, "expect.mod") - expected_content, err = os.ReadFile(expected_path) + 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, string(got_content), string(expected_content)) + assert.Equal(t, modLockContentStr, modLockExpectContentStr) + + 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) { From b36c95f361d562d6e9cc7d31502ad746d480ceab Mon Sep 17 00:00:00 2001 From: Asish Kumar Date: Tue, 20 Aug 2024 09:09:56 +0530 Subject: [PATCH 22/29] add test folder Signed-off-by: Asish Kumar --- .../test_mod_file_package/{ => test_pkg}/expect.mod | 0 .../{ => test_pkg}/expect.mod.lock | 0 .../test_mod_file_package/{ => test_pkg}/kcl.mod | 0 .../{ => test_pkg}/kcl.mod.lock | 0 .../test_mod_file_package/test_pkg_win/expect.mod | 4 ++++ .../test_pkg_win/expect.mod.lock | 12 ++++++++++++ .../test_mod_file_package/test_pkg_win/kcl.mod | 0 .../test_mod_file_package/test_pkg_win/kcl.mod.lock | 0 8 files changed, 16 insertions(+) rename pkg/client/test_data/test_mod_file_package/{ => test_pkg}/expect.mod (100%) rename pkg/client/test_data/test_mod_file_package/{ => test_pkg}/expect.mod.lock (100%) rename pkg/client/test_data/test_mod_file_package/{ => test_pkg}/kcl.mod (100%) rename pkg/client/test_data/test_mod_file_package/{ => test_pkg}/kcl.mod.lock (100%) create mode 100644 pkg/client/test_data/test_mod_file_package/test_pkg_win/expect.mod create mode 100644 pkg/client/test_data/test_mod_file_package/test_pkg_win/expect.mod.lock create mode 100644 pkg/client/test_data/test_mod_file_package/test_pkg_win/kcl.mod create mode 100644 pkg/client/test_data/test_mod_file_package/test_pkg_win/kcl.mod.lock diff --git a/pkg/client/test_data/test_mod_file_package/expect.mod b/pkg/client/test_data/test_mod_file_package/test_pkg/expect.mod similarity index 100% rename from pkg/client/test_data/test_mod_file_package/expect.mod rename to pkg/client/test_data/test_mod_file_package/test_pkg/expect.mod diff --git a/pkg/client/test_data/test_mod_file_package/expect.mod.lock b/pkg/client/test_data/test_mod_file_package/test_pkg/expect.mod.lock similarity index 100% rename from pkg/client/test_data/test_mod_file_package/expect.mod.lock rename to pkg/client/test_data/test_mod_file_package/test_pkg/expect.mod.lock diff --git a/pkg/client/test_data/test_mod_file_package/kcl.mod b/pkg/client/test_data/test_mod_file_package/test_pkg/kcl.mod similarity index 100% rename from pkg/client/test_data/test_mod_file_package/kcl.mod rename to pkg/client/test_data/test_mod_file_package/test_pkg/kcl.mod diff --git a/pkg/client/test_data/test_mod_file_package/kcl.mod.lock b/pkg/client/test_data/test_mod_file_package/test_pkg/kcl.mod.lock similarity index 100% rename from pkg/client/test_data/test_mod_file_package/kcl.mod.lock rename to pkg/client/test_data/test_mod_file_package/test_pkg/kcl.mod.lock diff --git a/pkg/client/test_data/test_mod_file_package/test_pkg_win/expect.mod b/pkg/client/test_data/test_mod_file_package/test_pkg_win/expect.mod new file mode 100644 index 00000000..18fcce1e --- /dev/null +++ b/pkg/client/test_data/test_mod_file_package/test_pkg_win/expect.mod @@ -0,0 +1,4 @@ +[package] + +[dependencies] +agent = { git = "https://github.com/kcl-lang/modules.git", commit = "ee03122b5f45b09eb48694422fc99a0772f6bba8", package = "agent" } \ No newline at end of file diff --git a/pkg/client/test_data/test_mod_file_package/test_pkg_win/expect.mod.lock b/pkg/client/test_data/test_mod_file_package/test_pkg_win/expect.mod.lock new file mode 100644 index 00000000..f7821a7d --- /dev/null +++ b/pkg/client/test_data/test_mod_file_package/test_pkg_win/expect.mod.lock @@ -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" diff --git a/pkg/client/test_data/test_mod_file_package/test_pkg_win/kcl.mod b/pkg/client/test_data/test_mod_file_package/test_pkg_win/kcl.mod new file mode 100644 index 00000000..e69de29b diff --git a/pkg/client/test_data/test_mod_file_package/test_pkg_win/kcl.mod.lock b/pkg/client/test_data/test_mod_file_package/test_pkg_win/kcl.mod.lock new file mode 100644 index 00000000..e69de29b From fee67ab15f17e825d4c267e3a1ee4d2adeb85134 Mon Sep 17 00:00:00 2001 From: Asish Kumar Date: Tue, 20 Aug 2024 09:23:03 +0530 Subject: [PATCH 23/29] fix failing unit test Signed-off-by: Asish Kumar --- pkg/client/client_test.go | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/pkg/client/client_test.go b/pkg/client/client_test.go index 1621aa50..19653a5f 100644 --- a/pkg/client/client_test.go +++ b/pkg/client/client_test.go @@ -206,7 +206,7 @@ func TestModandLockFilesWithGitPackageDownload(t *testing.T) { 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) modContentStr := strings.ReplaceAll(string(modContent), "\r\n", "") @@ -227,20 +227,30 @@ func TestModandLockFilesWithGitPackageDownload(t *testing.T) { modLockContentStr = strings.ReplaceAll(modLockContentStr, "\n", "") assert.Equal(t, err, nil) + got_content_lines := strings.Split(string(modLockContentStr), "\n") + got_content_filtered := "" + for _, line := range got_content_lines { + if !strings.Contains(line, "sum") { + got_content_filtered += line + "\n" + } + } + got_content_filtered = strings.TrimSuffix(got_content_filtered, "\n") + 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) + assert.Equal(t, got_content_filtered, modLockExpectContentStr) 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) { From b74e78534759f61999d6fd46ad9ab0f1112781eb Mon Sep 17 00:00:00 2001 From: Asish Kumar Date: Tue, 20 Aug 2024 09:39:13 +0530 Subject: [PATCH 24/29] remove the filter Signed-off-by: Asish Kumar --- pkg/client/client_test.go | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/pkg/client/client_test.go b/pkg/client/client_test.go index 19653a5f..9dc56cd3 100644 --- a/pkg/client/client_test.go +++ b/pkg/client/client_test.go @@ -227,14 +227,14 @@ func TestModandLockFilesWithGitPackageDownload(t *testing.T) { modLockContentStr = strings.ReplaceAll(modLockContentStr, "\n", "") assert.Equal(t, err, nil) - got_content_lines := strings.Split(string(modLockContentStr), "\n") - got_content_filtered := "" - for _, line := range got_content_lines { - if !strings.Contains(line, "sum") { - got_content_filtered += line + "\n" - } - } - got_content_filtered = strings.TrimSuffix(got_content_filtered, "\n") + // got_content_lines := strings.Split(string(modLockContentStr), "\n") + // got_content_filtered := "" + // for _, line := range got_content_lines { + // if !strings.Contains(line, "sum") { + // got_content_filtered += line + "\n" + // } + // } + // got_content_filtered = strings.TrimSuffix(got_content_filtered, "\n") modLockExpectContent, err := os.ReadFile(testPkgPathModLockExpect) @@ -242,7 +242,7 @@ func TestModandLockFilesWithGitPackageDownload(t *testing.T) { modLockExpectContentStr = strings.ReplaceAll(modLockExpectContentStr, "\n", "") assert.Equal(t, err, nil) - assert.Equal(t, got_content_filtered, modLockExpectContentStr) + assert.Equal(t, modLockContentStr, modLockExpectContentStr) defer func() { err = os.Truncate(testPkgPathMod, 0) From f973335bace11670339c92de4fe24d6c74dc9529 Mon Sep 17 00:00:00 2001 From: Asish Kumar Date: Tue, 20 Aug 2024 09:42:33 +0530 Subject: [PATCH 25/29] add nosumcheck Signed-off-by: Asish Kumar --- pkg/client/client_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/pkg/client/client_test.go b/pkg/client/client_test.go index 9dc56cd3..2d9b1f9b 100644 --- a/pkg/client/client_test.go +++ b/pkg/client/client_test.go @@ -197,6 +197,7 @@ func TestModandLockFilesWithGitPackageDownload(t *testing.T) { Package: "agent", }, }, + NoSumCheck: false, } _, err = kpmcli.AddDepWithOpts(kclPkg, &opts) From e0c8c5a3fd3af76c32a8d3512aee294dcae1df72 Mon Sep 17 00:00:00 2001 From: Asish Kumar Date: Tue, 20 Aug 2024 09:45:17 +0530 Subject: [PATCH 26/29] nosumcheck to true Signed-off-by: Asish Kumar --- pkg/client/client_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/client/client_test.go b/pkg/client/client_test.go index 2d9b1f9b..8f4f31be 100644 --- a/pkg/client/client_test.go +++ b/pkg/client/client_test.go @@ -197,7 +197,7 @@ func TestModandLockFilesWithGitPackageDownload(t *testing.T) { Package: "agent", }, }, - NoSumCheck: false, + NoSumCheck: true, } _, err = kpmcli.AddDepWithOpts(kclPkg, &opts) From 3bd9a776205926e692a884c8612da40eea0a4a16 Mon Sep 17 00:00:00 2001 From: Asish Kumar Date: Tue, 20 Aug 2024 09:54:40 +0530 Subject: [PATCH 27/29] remove sum from actual Signed-off-by: Asish Kumar --- pkg/client/client_test.go | 57 +++++++++++++++++++++++---------------- 1 file changed, 34 insertions(+), 23 deletions(-) diff --git a/pkg/client/client_test.go b/pkg/client/client_test.go index 8f4f31be..e67496c2 100644 --- a/pkg/client/client_test.go +++ b/pkg/client/client_test.go @@ -9,6 +9,7 @@ import ( "log" "os" "path/filepath" + "regexp" "runtime" "sort" "strings" @@ -197,7 +198,6 @@ func TestModandLockFilesWithGitPackageDownload(t *testing.T) { Package: "agent", }, }, - NoSumCheck: true, } _, err = kpmcli.AddDepWithOpts(kclPkg, &opts) @@ -209,41 +209,52 @@ func TestModandLockFilesWithGitPackageDownload(t *testing.T) { testPkgPathModLockExpect := filepath.Join(testPkgPath, "expect.mod.lock") 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) + modContentStr := string(modContent) + modExpectContentStr := string(modExpectContent) + + // Clean and normalize both strings + for _, str := range []*string{&modContentStr, &modExpectContentStr} { + *str = strings.ReplaceAll(*str, " ", "") + *str = strings.ReplaceAll(*str, "\r\n", "") + *str = strings.ReplaceAll(*str, "\n", "") + + // Remove the sum field and any trailing characters + sumRegex := regexp.MustCompile(`sum\s*=\s*"[^"]+"`) + *str = sumRegex.ReplaceAllString(*str, "") + + // Remove any trailing commas or whitespace + *str = strings.TrimRight(*str, ", \t\r\n") + } + + assert.Equal(t, modExpectContentStr, modContentStr) modLockContent, err := os.ReadFile(testPkgPathModLock) + assert.Equal(t, err, nil) - modLockContentStr := strings.ReplaceAll(string(modLockContent), "\r\n", "") - modLockContentStr = strings.ReplaceAll(modLockContentStr, "\n", "") + modLockExpectContent, err := os.ReadFile(testPkgPathModLockExpect) assert.Equal(t, err, nil) - // got_content_lines := strings.Split(string(modLockContentStr), "\n") - // got_content_filtered := "" - // for _, line := range got_content_lines { - // if !strings.Contains(line, "sum") { - // got_content_filtered += line + "\n" - // } - // } - // got_content_filtered = strings.TrimSuffix(got_content_filtered, "\n") + modLockContentStr := string(modLockContent) + modLockExpectContentStr := string(modLockExpectContent) - modLockExpectContent, err := os.ReadFile(testPkgPathModLockExpect) + // Clean and normalize both strings (same as above) + for _, str := range []*string{&modLockContentStr, &modLockExpectContentStr} { + *str = strings.ReplaceAll(*str, " ", "") + *str = strings.ReplaceAll(*str, "\r\n", "") + *str = strings.ReplaceAll(*str, "\n", "") - modLockExpectContentStr := strings.ReplaceAll(string(modLockExpectContent), "\r\n", "") - modLockExpectContentStr = strings.ReplaceAll(modLockExpectContentStr, "\n", "") - assert.Equal(t, err, nil) + sumRegex := regexp.MustCompile(`sum\s*=\s*"[^"]+"`) + *str = sumRegex.ReplaceAllString(*str, "") - assert.Equal(t, modLockContentStr, modLockExpectContentStr) + *str = strings.TrimRight(*str, ", \t\r\n") + } + + assert.Equal(t, modLockExpectContentStr, modLockContentStr) defer func() { err = os.Truncate(testPkgPathMod, 0) From 9b3de39e098557b4b3d4b3632bf32de84ba418d1 Mon Sep 17 00:00:00 2001 From: Asish Kumar Date: Tue, 20 Aug 2024 09:57:59 +0530 Subject: [PATCH 28/29] remove comments from client_test Signed-off-by: Asish Kumar --- pkg/client/client_test.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/pkg/client/client_test.go b/pkg/client/client_test.go index e67496c2..504300f2 100644 --- a/pkg/client/client_test.go +++ b/pkg/client/client_test.go @@ -223,11 +223,9 @@ func TestModandLockFilesWithGitPackageDownload(t *testing.T) { *str = strings.ReplaceAll(*str, "\r\n", "") *str = strings.ReplaceAll(*str, "\n", "") - // Remove the sum field and any trailing characters sumRegex := regexp.MustCompile(`sum\s*=\s*"[^"]+"`) *str = sumRegex.ReplaceAllString(*str, "") - // Remove any trailing commas or whitespace *str = strings.TrimRight(*str, ", \t\r\n") } @@ -242,7 +240,6 @@ func TestModandLockFilesWithGitPackageDownload(t *testing.T) { modLockContentStr := string(modLockContent) modLockExpectContentStr := string(modLockExpectContent) - // Clean and normalize both strings (same as above) for _, str := range []*string{&modLockContentStr, &modLockExpectContentStr} { *str = strings.ReplaceAll(*str, " ", "") *str = strings.ReplaceAll(*str, "\r\n", "") @@ -254,6 +251,8 @@ func TestModandLockFilesWithGitPackageDownload(t *testing.T) { *str = strings.TrimRight(*str, ", \t\r\n") } + fmt.Println(modLockContentStr) + assert.Equal(t, modLockExpectContentStr, modLockContentStr) defer func() { From d3a0c63418089ef1b014ff330f0f53431b2b7bbf Mon Sep 17 00:00:00 2001 From: Asish Kumar Date: Tue, 20 Aug 2024 10:00:45 +0530 Subject: [PATCH 29/29] remove comments from client_test Signed-off-by: Asish Kumar --- pkg/client/client_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/pkg/client/client_test.go b/pkg/client/client_test.go index 504300f2..9b4bac72 100644 --- a/pkg/client/client_test.go +++ b/pkg/client/client_test.go @@ -217,7 +217,6 @@ func TestModandLockFilesWithGitPackageDownload(t *testing.T) { modContentStr := string(modContent) modExpectContentStr := string(modExpectContent) - // Clean and normalize both strings for _, str := range []*string{&modContentStr, &modExpectContentStr} { *str = strings.ReplaceAll(*str, " ", "") *str = strings.ReplaceAll(*str, "\r\n", "")