Skip to content

Commit

Permalink
fix: fix the localpath when 'kpm add' from oci registry without tag.
Browse files Browse the repository at this point in the history
  • Loading branch information
zong-zhe committed Jun 2, 2023
1 parent 07a2c9f commit ccb4866
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 6 deletions.
22 changes: 18 additions & 4 deletions pkg/mod/modfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,13 @@ func (dep *Dependency) FillDepInfo() error {
return nil
}

// 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
}

// Download will download the kcl package to localPath from registory.
func (dep *Dependency) Download(localPath string) (*Dependency, error) {
reporter.Report("kpm: adding dependency", dep.Name)
Expand All @@ -89,25 +96,31 @@ func (dep *Dependency) Download(localPath string) (*Dependency, error) {
if err != nil {
return nil, err
}
dep.Version = dep.Source.Git.Tag
dep.LocalFullPath = localPath
dep.FullName = dep.GenDepFullName()
}

if dep.Source.Oci != nil {
_, err := dep.Source.Oci.Download(localPath)
localPath, err := dep.Source.Oci.Download(localPath)
if err != nil {
return nil, err
}
dep.Version = dep.Source.Oci.Tag
dep.LocalFullPath = localPath
dep.FullName = dep.GenDepFullName()
}

var err error
dep.Sum, err = utils.HashDir(localPath)
dep.Sum, err = utils.HashDir(dep.LocalFullPath)
if err != nil {
return nil, err
}
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.
err = utils.CreateSymlink(dep.LocalFullPath, filepath.Join(filepath.Dir(localPath), dep.Name))
err = utils.CreateSymlink(dep.LocalFullPath, filepath.Join(filepath.Dir(dep.LocalFullPath), dep.Name))
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -141,6 +154,7 @@ func (dep *Oci) Download(localPath string) (string, error) {
}
reporter.Report("kpm: the lastest version", tagSelected, "will be pulled.")
dep.Tag = tagSelected
localPath = localPath + dep.Tag
} else {
tagSelected = dep.Tag
}
Expand Down
40 changes: 39 additions & 1 deletion pkg/mod/modfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ func TestGetFilePath(t *testing.T) {
assert.Equal(t, mfile.GetModLockFilePath(), filepath.Join(testPath, MOD_LOCK_FILE))
}

// TestDownloadGit test download from oci registry.
func TestDownloadOci(t *testing.T) {
testPath := filepath.Join(getTestDir("download"), "k8s_1.27.2")
err := os.MkdirAll(testPath, 0755)
Expand All @@ -193,7 +194,7 @@ func TestDownloadOci(t *testing.T) {

dep, err := depFromOci.Download(testPath)
assert.Equal(t, dep.Name, "k8s")
assert.Equal(t, dep.FullName, "")
assert.Equal(t, dep.FullName, "k8s_1.27.2")
assert.Equal(t, dep.Version, "1.27.2")
assert.Equal(t, dep.Sum, "ZI7L/uz53aDOIgVgxBbEPG7wGCWR+Gd3hhgYYRLoIY4=")
assert.NotEqual(t, dep.Source.Oci, nil)
Expand All @@ -209,3 +210,40 @@ func TestDownloadOci(t *testing.T) {
err = os.RemoveAll(getTestDir("download"))
assert.Equal(t, err, nil)
}

// TestDownloadLatestOci tests the case that the version is empty.
func TestDownloadLatestOci(t *testing.T) {
testPath := filepath.Join(getTestDir("download"), "a_random_name")
err := os.MkdirAll(testPath, 0755)
assert.Equal(t, err, nil)

depFromOci := Dependency{
Name: "k8s",
Version: "",
Source: Source{
Oci: &Oci{
Reg: settings.DEFAULT_REGISTRY,
Repo: filepath.Join(settings.DEFAULT_REPO, "k8s"),
Tag: "",
},
},
}

dep, err := depFromOci.Download(testPath)
assert.Equal(t, dep.Name, "k8s")
assert.Equal(t, dep.FullName, "k8s_1.27.2")
assert.Equal(t, dep.Version, "1.27.2")
assert.Equal(t, dep.Sum, "ZI7L/uz53aDOIgVgxBbEPG7wGCWR+Gd3hhgYYRLoIY4=")
assert.NotEqual(t, dep.Source.Oci, nil)
assert.Equal(t, dep.Source.Oci.Reg, settings.DEFAULT_REGISTRY)
assert.Equal(t, dep.Source.Oci.Repo, filepath.Join(settings.DEFAULT_REPO, "k8s"))
assert.Equal(t, dep.Source.Oci.Tag, "1.27.2")
assert.Equal(t, dep.LocalFullPath, testPath+"1.27.2")
assert.Equal(t, err, nil)

// Check whether the tar downloaded by `kpm add` has been deleted.
assert.Equal(t, utils.DirExists(filepath.Join(testPath, "k8s_1.27.2.tar")), false)

err = os.RemoveAll(getTestDir("download"))
assert.Equal(t, err, nil)
}
2 changes: 1 addition & 1 deletion pkg/package/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ func getDeps(deps modfile.Dependencies, lockDeps modfile.Dependencies, localPath
// Recursively download the dependencies of the new dependencies.
for _, d := range newDeps.Deps {
// Load kcl.mod file of the new downloaded dependencies.
modfile, err := modfile.LoadModFile(filepath.Join(localPath, d.FullName))
modfile, err := modfile.LoadModFile(d.LocalFullPath)
if err != nil {
if os.IsNotExist(err) {
continue
Expand Down

0 comments on commit ccb4866

Please sign in to comment.