Skip to content

Commit

Permalink
fix: Add tests under different features
Browse files Browse the repository at this point in the history
Signed-off-by: zongz <zongzhe1024@163.com>
  • Loading branch information
zong-zhe committed Nov 15, 2024
1 parent 5eb17c1 commit 23c4211
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 14 deletions.
14 changes: 14 additions & 0 deletions pkg/client/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/otiai10/copy"
"gotest.tools/v3/assert"
"kcl-lang.io/kpm/pkg/downloader"
"kcl-lang.io/kpm/pkg/features"
"kcl-lang.io/kpm/pkg/utils"
)

Expand Down Expand Up @@ -80,6 +81,19 @@ func testRunWithModSpecVersion(t *testing.T, kpmcli *KpmClient) {
}

func TestRun(t *testing.T) {
features.Enable(features.SupportNewStorage)
RunTestWithGlobalLockAndKpmCli(t, "TestRunWithOciDownloader", testRunWithOciDownloader)
RunTestWithGlobalLockAndKpmCli(t, "TestRunDefaultRegistryDep", testRunDefaultRegistryDep)
RunTestWithGlobalLockAndKpmCli(t, "TestRunInVendor", testRunInVendor)
RunTestWithGlobalLockAndKpmCli(t, "TestRunRemoteWithArgsInvalid", testRunRemoteWithArgsInvalid)
RunTestWithGlobalLockAndKpmCli(t, "TestRunRemoteWithArgs", testRunRemoteWithArgs)
RunTestWithGlobalLockAndKpmCli(t, "TestRunWithNoSumCheck", testRunWithNoSumCheck)
RunTestWithGlobalLockAndKpmCli(t, "TestRunWithGitPackage", testRunWithGitPackage)
RunTestWithGlobalLockAndKpmCli(t, "TestRunGit", testRunGit)
RunTestWithGlobalLockAndKpmCli(t, "TestRunOciWithSettingsFile", testRunOciWithSettingsFile)
RunTestWithGlobalLockAndKpmCli(t, "TestRunWithModSpecVersion", testRunWithModSpecVersion)

features.Disable(features.SupportNewStorage)
RunTestWithGlobalLockAndKpmCli(t, "TestRunWithOciDownloader", testRunWithOciDownloader)
RunTestWithGlobalLockAndKpmCli(t, "TestRunDefaultRegistryDep", testRunDefaultRegistryDep)
RunTestWithGlobalLockAndKpmCli(t, "TestRunInVendor", testRunInVendor)
Expand Down
79 changes: 66 additions & 13 deletions pkg/downloader/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,39 @@ func (d *OciDownloader) Download(opts *DownloadOptions) error {
return err
}
}
} else {
reporter.ReportMsgTo(
fmt.Sprintf(
"downloading '%s:%s' from '%s/%s:%s'",
ociSource.Repo, ociSource.Tag, ociSource.Reg, ociSource.Repo, ociSource.Tag,
),
opts.LogWriter,
)

err = ociCli.Pull(localPath, ociSource.Tag)
if err != nil {
return err
}
tarPath, err := utils.FindPkgArchive(localPath)
if err != nil {
return err
}
if utils.IsTar(tarPath) {
err = utils.UnTarDir(tarPath, localPath)
} else {
err = utils.ExtractTarball(tarPath, localPath)
}
if err != nil {
return fmt.Errorf("failed to untar the kcl package tar from '%s' into '%s'", tarPath, localPath)
}

// After untar the downloaded kcl package tar file, remove the tar file.
if utils.DirExists(tarPath) {
rmErr := os.Remove(tarPath)
if rmErr != nil {
return fmt.Errorf("failed to remove the downloaded kcl package tar file '%s'", tarPath)
}
}
}
} else {
reporter.ReportMsgTo(
Expand Down Expand Up @@ -498,6 +531,19 @@ func (d *GitDownloader) Download(opts *DownloadOptions) error {
git.WithTag(gitSource.Tag),
}

var msg string
if len(opts.Source.Git.Tag) != 0 {
msg = fmt.Sprintf("with tag '%s'", opts.Source.Git.Tag)
}

if len(opts.Source.Git.Commit) != 0 {
msg = fmt.Sprintf("with commit '%s'", opts.Source.Git.Commit)
}

if len(opts.Source.Git.Branch) != 0 {
msg = fmt.Sprintf("with branch '%s'", opts.Source.Git.Branch)
}

if ok, err := features.Enabled(features.SupportNewStorage); err == nil && ok {
if opts.EnableCache {
cacheFullPath := opts.CachePath
Expand Down Expand Up @@ -525,6 +571,10 @@ func (d *GitDownloader) Download(opts *DownloadOptions) error {
return err
}
} else {
reporter.ReportMsgTo(
fmt.Sprintf("cloning '%s' %s", opts.Source.Git.Url, msg),
opts.LogWriter,
)
// If not, clone the bare repository from the remote git repository, update the cache.
if utils.DirExists(cacheFullPath) {
err = os.Remove(cacheFullPath)
Expand Down Expand Up @@ -558,21 +608,24 @@ func (d *GitDownloader) Download(opts *DownloadOptions) error {
}
}
}
} else {
reporter.ReportMsgTo(
fmt.Sprintf("cloning '%s' %s", opts.Source.Git.Url, msg),
opts.LogWriter,
)
// If the cache is disabled, clone the repository from the remote git repository.
_, err := git.CloneWithOpts(
append(
cloneOpts,
git.WithRepoURL(gitSource.Url),
git.WithLocalPath(opts.LocalPath),
)...,
)
if err != nil {
return err
}
}
} else {
var msg string
if len(opts.Source.Git.Tag) != 0 {
msg = fmt.Sprintf("with tag '%s'", opts.Source.Git.Tag)
}

if len(opts.Source.Git.Commit) != 0 {
msg = fmt.Sprintf("with commit '%s'", opts.Source.Git.Commit)
}

if len(opts.Source.Git.Branch) != 0 {
msg = fmt.Sprintf("with branch '%s'", opts.Source.Git.Branch)
}

reporter.ReportMsgTo(
fmt.Sprintf("cloning '%s' %s", opts.Source.Git.Url, msg),
opts.LogWriter,
Expand Down
2 changes: 1 addition & 1 deletion pkg/features/features.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const (

var features = map[string]bool{
SupportMVS: false,
SupportNewStorage: true,
SupportNewStorage: false,
}

func init() {
Expand Down

0 comments on commit 23c4211

Please sign in to comment.