Skip to content

Commit

Permalink
downloader pattern implemented with test added
Browse files Browse the repository at this point in the history
Signed-off-by: d4v1d03 <amit08072005@gmail.com>
  • Loading branch information
d4v1d03 committed May 30, 2024
1 parent 3627caa commit 78e34fc
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 7 deletions.
7 changes: 6 additions & 1 deletion pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -918,7 +918,12 @@ func (c *KpmClient) AcquireTheLatestOciVersion(ociSource pkg.Oci) (string, error
// Download will download the dependency to the local path.
func (c *KpmClient) Download(dep *pkg.Dependency, homePath, localPath string) (*pkg.Dependency, error) {
if dep.Source.Git != nil {
_, err := c.DownloadFromGit(dep.Source.Git, localPath)
err := c.DepDownloader.Download(*downloader.NewDownloadOptions(
downloader.WithLocalPath(localPath),
downloader.WithSource(dep.Source),
downloader.WithLogWriter(c.logWriter),
downloader.WithSettings(c.settings),
))
if err != nil {
return nil, err
}
Expand Down
42 changes: 36 additions & 6 deletions pkg/downloader/downloader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,57 @@ func getTestDir(subDir string) string {
}

func TestOciDownloader(t *testing.T) {
path := getTestDir("test_oci")
ociTestDir := getTestDir("oci_test_dir")
if err := os.MkdirAll(ociTestDir, os.ModePerm); err != nil {
t.Fatal(err)
}

defer func() {
_ = os.RemoveAll(path)
_ = os.RemoveAll(ociTestDir)
}()

ociDownloader := OciDownloader{
downloader := OciDownloader{
Platform: "linux/amd64",
}

err := ociDownloader.Download(*NewDownloadOptions(
options := NewDownloadOptions(
WithSource(pkg.Source{
Oci: &pkg.Oci{
Reg: "ghcr.io",
Repo: "zong-zhe/helloworld",
Tag: "0.0.3",
},
}),
WithLocalPath(path),
WithLocalPath(ociTestDir),
)

err := downloader.Download(*options)

assert.Equal(t, err, nil)
assert.Equal(t, true, utils.DirExists(filepath.Join(ociTestDir, "artifact.tgz")))

gitTestDir := getTestDir("git_test_dir")
if err := os.MkdirAll(gitTestDir, os.ModePerm); err != nil {
t.Fatal(err)
}

defer func() {
_ = os.RemoveAll(gitTestDir)
}()

gitDownloader := GitDownloader{}

err = gitDownloader.Download(*NewDownloadOptions(
WithSource(pkg.Source{
Git: &pkg.Git{
Url: "https://github.com/kcl-lang/flask-demo-kcl-manifests.git",
Commit: "ade147b",
},
}),
WithLocalPath(gitTestDir),
))

assert.Equal(t, err, nil)
assert.Equal(t, true, utils.DirExists(filepath.Join(path, "artifact.tgz")))
assert.Equal(t, false, utils.DirExists(filepath.Join(gitTestDir, "some_expected_file")))
}

0 comments on commit 78e34fc

Please sign in to comment.