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

fix: fix redownload when the remote repo has no mod file #542

Merged
merged 7 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
308 changes: 190 additions & 118 deletions pkg/client/add_test.go

Large diffs are not rendered by default.

30 changes: 0 additions & 30 deletions pkg/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (
"kcl-lang.io/kcl-go/pkg/kcl"
"kcl-lang.io/kpm/pkg/downloader"
"kcl-lang.io/kpm/pkg/env"
"kcl-lang.io/kpm/pkg/features"
"kcl-lang.io/kpm/pkg/opt"
pkg "kcl-lang.io/kpm/pkg/package"
"kcl-lang.io/kpm/pkg/reporter"
Expand All @@ -36,25 +35,6 @@ import (
"kcl-lang.io/kpm/pkg/utils"
)

const testDataDir = "test_data"

func getTestDir(subDir string) string {
pwd, _ := os.Getwd()
testDir := filepath.Join(pwd, testDataDir)
testDir = filepath.Join(testDir, subDir)

return testDir
}

func initTestDir(subDir string) string {
testDir := getTestDir(subDir)
// clean the test data
_ = os.RemoveAll(testDir)
_ = os.Mkdir(testDir, 0755)

return testDir
}

func TestWithGlobalLock(t *testing.T) {
test.RunTestWithGlobalLock(t, "TestUpdateWithKclMod", testUpdateWithKclMod)
test.RunTestWithGlobalLock(t, "TestUpdateWithKclModlock", testUpdateWithKclModlock)
Expand Down Expand Up @@ -83,7 +63,6 @@ func TestWithGlobalLock(t *testing.T) {
test.RunTestWithGlobalLock(t, "TestDownloadGitWithPackage", testDownloadGitWithPackage)
test.RunTestWithGlobalLock(t, "TestModandLockFilesWithGitPackageDownload", testModandLockFilesWithGitPackageDownload)
test.RunTestWithGlobalLock(t, "TestDependencyGraph", testDependencyGraph)
test.RunTestWithGlobalLock(t, "TestAddWithModSpec", testAddWithModSpec)
test.RunTestWithGlobalLock(t, "TestRunRemoteWithArgsInvalid", testRunRemoteWithArgsInvalid)
test.RunTestWithGlobalLock(t, "TestRunRemoteWithArgs", testRunRemoteWithArgs)
test.RunTestWithGlobalLock(t, "TestRunWithNoSumCheck", testRunWithGitPackage)
Expand All @@ -93,18 +72,9 @@ func TestWithGlobalLock(t *testing.T) {
test.RunTestWithGlobalLock(t, "TestPull", testPull)
test.RunTestWithGlobalLock(t, "TestPullWithInsecureSkipTLSverify", testPullWithInsecureSkipTLSverify)
test.RunTestWithGlobalLock(t, "TestPullWithModSpec", testPullWithModSpec)
test.RunTestWithGlobalLock(t, "testAddWithOnlyModSpec", testAddWithOnlyModSpec)
test.RunTestWithGlobalLock(t, "testAddRenameWithModSpec", testAddRenameWithModSpec)
test.RunTestWithGlobalLock(t, "testAddRenameWithNoSpec", testAddRenameWithNoSpec)
test.RunTestWithGlobalLock(t, "testPullWithOnlySpec", testPullWithOnlySpec)
test.RunTestWithGlobalLock(t, "TestRunWithModSpecVersion", testRunWithModSpecVersion)
test.RunTestWithGlobalLock(t, "TestGraph", testGraph)

features.Enable(features.SupportNewStorage)
test.RunTestWithGlobalLock(t, "testAddWithModSpec", testAddWithModSpec)
test.RunTestWithGlobalLock(t, "testAddWithOnlyModSpec", testAddWithOnlyModSpec)
test.RunTestWithGlobalLock(t, "testAddRenameWithModSpec", testAddRenameWithModSpec)
test.RunTestWithGlobalLock(t, "testAddRenameWithNoSpec", testAddRenameWithNoSpec)
}

// TestDownloadOci test download from oci registry.
Expand Down
6 changes: 4 additions & 2 deletions pkg/client/graph_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"
"golang.org/x/mod/module"
"gotest.tools/v3/assert"
pkg "kcl-lang.io/kpm/pkg/package"
"kcl-lang.io/kpm/pkg/utils"
)
Expand Down Expand Up @@ -43,5 +43,7 @@ func testGraph(t *testing.T) {
t.Fatalf("failed to display graph: %v", err)
}

assert.Equal(t, utils.RmNewline(graStr), "pkg@0.0.1 dep@0.0.1pkg@0.0.1 helloworld@0.1.4dep@0.0.1 helloworld@0.1.4")
assert.Contains(t, utils.RmNewline(graStr), "pkg@0.0.1 dep@0.0.1")
assert.Contains(t, utils.RmNewline(graStr), "pkg@0.0.1 helloworld@0.1.4")
assert.Contains(t, utils.RmNewline(graStr), "dep@0.0.1 helloworld@0.1.4")
}
60 changes: 60 additions & 0 deletions pkg/client/test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package client

import (
"fmt"
"os"
"path/filepath"
"testing"
)

const testDataDir = "test_data"

func getTestDir(subDir string) string {
pwd, _ := os.Getwd()
testDir := filepath.Join(pwd, testDataDir)
testDir = filepath.Join(testDir, subDir)

return testDir
}

func initTestDir(subDir string) string {
testDir := getTestDir(subDir)
// clean the test data
_ = os.RemoveAll(testDir)
_ = os.Mkdir(testDir, 0755)

return testDir
}

// Use a global variable to store the kpmcli instance.
func RunTestWithGlobalLockAndKpmCli(t *testing.T, name string, testFunc func(t *testing.T, kpmcli *KpmClient)) {
t.Run(name, func(t *testing.T) {
kpmcli, err := NewKpmClient()
if err != nil {
t.Errorf("Error acquiring lock: %v", err)
}
err = kpmcli.AcquirePackageCacheLock()
if err != nil {
t.Errorf("Error acquiring lock: %v", err)
}

defer func() {
err = kpmcli.ReleasePackageCacheLock()
if err != nil {
t.Errorf("Error acquiring lock: %v", err)
}
}()

// create a tmp dir as kpm home for test
tmpDir, err := os.MkdirTemp("", "")
if err != nil {
t.Errorf("Error acquiring lock: %v", err)
}
// clean the temp dir.
defer os.RemoveAll(tmpDir)
kpmcli.SetHomePath(tmpDir)

testFunc(t, kpmcli)
fmt.Printf("%s completed\n", name)
})
}
4 changes: 4 additions & 0 deletions pkg/client/test_data/add_with_mod_spec/git_mod_0/kcl.mod.bk
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[package]
name = "git"
edition = "v0.10.0"
version = "0.0.1"
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "git"
edition = "v0.10.0"
version = "0.0.1"

[dependencies]
cc = { git = "https://github.com/kcl-lang/flask-demo-kcl-manifests.git", commit = "8308200", version = "0.0.1" }
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[dependencies]
[dependencies.cc]
name = "cc"
full_name = "cc_0.0.1"
version = "0.0.1"
url = "https://github.com/kcl-lang/flask-demo-kcl-manifests.git"
commit = "8308200"
1 change: 1 addition & 0 deletions pkg/client/test_data/add_with_mod_spec/git_mod_0/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The_first_kcl_program = 'Hello World!'
4 changes: 4 additions & 0 deletions pkg/client/test_data/add_with_mod_spec/git_mod_1/kcl.mod.bk
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[package]
name = "git"
edition = "v0.10.0"
version = "0.0.1"
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "git"
edition = "v0.10.0"
version = "0.0.1"

[dependencies]
cc = { git = "https://github.com/kcl-lang/flask-demo-kcl-manifests.git", commit = "5ab0fff", version = "0.0.1" }
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[dependencies]
[dependencies.cc]
name = "cc"
full_name = "cc_0.0.1"
version = "0.0.1"
url = "https://github.com/kcl-lang/flask-demo-kcl-manifests.git"
commit = "5ab0fff"
1 change: 1 addition & 0 deletions pkg/client/test_data/add_with_mod_spec/git_mod_1/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The_first_kcl_program = 'Hello World!'
2 changes: 1 addition & 1 deletion pkg/downloader/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ func (d *DepDownloader) Download(opts *DownloadOptions) error {
localPath := opts.LocalPath
cacheFullPath := opts.CachePath
if ok, err := features.Enabled(features.SupportNewStorage); err == nil && !ok && opts.EnableCache {
if utils.DirExists(cacheFullPath) && utils.DirExists(filepath.Join(cacheFullPath, constants.KCL_MOD)) &&
if utils.DirExists(cacheFullPath) &&
// If the version in modspec is empty, meanings the latest version is needed.
// The latest version should be requested first and the cache should be updated.
((opts.Source.ModSpec != nil && opts.Source.ModSpec.Version != "") || opts.Source.ModSpec == nil) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/package/modfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,7 @@ func ParseOpt(opt *opt.RegistryOptions) (*Dependency, error) {
Version: opt.Registry.Tag,
Name: opt.Registry.Ref,
},
Oci: &ociSource,
Oci: &ociSource,
},
Version: opt.Registry.Tag,
}, nil
Expand Down
8 changes: 8 additions & 0 deletions pkg/package/test_data/test_rename_pkg/kcl.mod.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[dependencies]
[dependencies.newpkg]
name = "newpkg"
full_name = "newpkg_0.0.1"
version = "0.0.1"
reg = "ghcr.io"
repo = "kcl-lang/helloworld"
oci_tag = "0.1.4"
7 changes: 0 additions & 7 deletions pkg/visitor/visitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,13 +222,6 @@ func (rv *RemoteVisitor) Visit(s *downloader.Source, v visitFunc) error {
defer os.RemoveAll(tmpDir)
}

if !utils.DirExists(modFullPath) {
err := os.MkdirAll(modFullPath, 0755)
if err != nil {
return err
}
}

credCli, err = downloader.LoadCredentialFile(rv.Settings.CredentialsFile)
if err != nil {
return err
Expand Down