From ba02f264c1e0e8e99c17648044b62f573e02c095 Mon Sep 17 00:00:00 2001 From: zongz <68977949+zong-zhe@users.noreply.github.com> Date: Thu, 12 Dec 2024 14:57:49 +0800 Subject: [PATCH] fix: Remove the auto-updating for kcl.mod when using command metadata. (#565) * fix: remove kcl.mod autoupdating when kcl metadata Signed-off-by: zongz * fix: remote useless code changes Signed-off-by: zongz * fix: add test cases and fix offline mode Signed-off-by: zongz * fix: fix unit test cases Signed-off-by: zongz --------- Signed-off-by: zongz --- pkg/client/client_test.go | 22 +++++++++++++++---- .../test_metadata_offline/ugly.kcl.mod | 3 +++ .../kcl.mod | 7 ++++++ .../kcl.mod.lock | 7 ++++++ .../main.k | 1 + pkg/client/update.go | 9 +++++--- pkg/downloader/downloader.go | 11 ++++++++++ pkg/resolver/resolver.go | 2 +- pkg/visitor/visitor.go | 8 +++++++ 9 files changed, 62 insertions(+), 8 deletions(-) create mode 100644 pkg/client/test_data/test_no_download_with_metadata_offline/kcl.mod create mode 100644 pkg/client/test_data/test_no_download_with_metadata_offline/kcl.mod.lock create mode 100644 pkg/client/test_data/test_no_download_with_metadata_offline/main.k diff --git a/pkg/client/client_test.go b/pkg/client/client_test.go index 977fcb04..286d8987 100644 --- a/pkg/client/client_test.go +++ b/pkg/client/client_test.go @@ -819,10 +819,7 @@ func testResolveMetadataInJsonStr(t *testing.T) { if runtime.GOOS == "windows" { expectedPath = strings.ReplaceAll(expectedPath, "\\", "\\\\") } - expectedStr := fmt.Sprintf( - "{\"packages\":{\"flask_demo_kcl_manifests\":{\"name\":\"flask_demo_kcl_manifests\",\"manifest_path\":\"%s\"}}}", - expectedPath, - ) + expectedStr := "{\"packages\":{\"flask_demo_kcl_manifests\":{\"name\":\"flask_demo_kcl_manifests\",\"manifest_path\":\"\"}}}" assert.Equal(t, res, expectedStr) defer func() { if r := os.RemoveAll(expectedPath); r != nil { @@ -1126,6 +1123,22 @@ func testUpdateWithKclModlock(t *testing.T, kpmcli *KpmClient) { }() } +func TestNoDownloadWithMetadataOffline(t *testing.T) { + testFunc := func(t *testing.T, kpmcli *KpmClient) { + testDir := getTestDir("test_no_download_with_metadata_offline") + kclPkg, err := pkg.LoadKclPkg(testDir) + assert.Equal(t, err, nil) + var buf bytes.Buffer + kpmcli.SetLogWriter(&buf) + res, err := kpmcli.ResolveDepsMetadataInJsonStr(kclPkg, false) + assert.Equal(t, err, nil) + assert.Equal(t, buf.String(), "") + assert.Equal(t, res, "{\"packages\":{\"kcl4\":{\"name\":\"kcl4\",\"manifest_path\":\"\"}}}") + } + + RunTestWithGlobalLockAndKpmCli(t, []TestSuite{{Name: "test_no_download_with_metadata_offline", TestFunc: testFunc}}) +} + func testMetadataOffline(t *testing.T) { kpmcli, err := NewKpmClient() assert.Equal(t, err, nil) @@ -1156,6 +1169,7 @@ func testMetadataOffline(t *testing.T) { assert.Equal(t, err, nil) if runtime.GOOS == "windows" { uglyContent = []byte(strings.ReplaceAll(string(uglyContent), "\r\n", "\n")) + content_after_metadata = []byte(strings.ReplaceAll(string(content_after_metadata), "\r\n", "\n")) } assert.Equal(t, string(content_after_metadata), string(uglyContent)) diff --git a/pkg/client/test_data/test_metadata_offline/ugly.kcl.mod b/pkg/client/test_data/test_metadata_offline/ugly.kcl.mod index 718782c3..9a8fd8b6 100644 --- a/pkg/client/test_data/test_metadata_offline/ugly.kcl.mod +++ b/pkg/client/test_data/test_metadata_offline/ugly.kcl.mod @@ -2,3 +2,6 @@ name = "test_metadata_offline" edition = "0.0.1" version = "0.0.1" + + + diff --git a/pkg/client/test_data/test_no_download_with_metadata_offline/kcl.mod b/pkg/client/test_data/test_no_download_with_metadata_offline/kcl.mod new file mode 100644 index 00000000..582584ea --- /dev/null +++ b/pkg/client/test_data/test_no_download_with_metadata_offline/kcl.mod @@ -0,0 +1,7 @@ +[package] +name = "test_no_download_with_metadata_offline" +edition = "v0.10.0" +version = "0.0.1" + +[dependencies] +kcl4 = { git = "test_url", tag = "v0.0.1" } diff --git a/pkg/client/test_data/test_no_download_with_metadata_offline/kcl.mod.lock b/pkg/client/test_data/test_no_download_with_metadata_offline/kcl.mod.lock new file mode 100644 index 00000000..409a419c --- /dev/null +++ b/pkg/client/test_data/test_no_download_with_metadata_offline/kcl.mod.lock @@ -0,0 +1,7 @@ +[dependencies] + [dependencies.kcl4] + name = "kcl4" + full_name = "kcl4_0.0.1" + version = "0.0.1" + url = "test_url" + git_tag = "v0.0.1" diff --git a/pkg/client/test_data/test_no_download_with_metadata_offline/main.k b/pkg/client/test_data/test_no_download_with_metadata_offline/main.k new file mode 100644 index 00000000..fa7048e6 --- /dev/null +++ b/pkg/client/test_data/test_no_download_with_metadata_offline/main.k @@ -0,0 +1 @@ +The_first_kcl_program = 'Hello World!' \ No newline at end of file diff --git a/pkg/client/update.go b/pkg/client/update.go index 8e3c8033..c27eeacd 100644 --- a/pkg/client/update.go +++ b/pkg/client/update.go @@ -136,15 +136,18 @@ func (c *KpmClient) Update(options ...UpdateOption) (*pkg.KclPkg, error) { resolver.WithResolveKclMod(kMod), resolver.WithEnableCache(true), resolver.WithCachePath(c.homePath), + resolver.WithOffline(opts.offline), ) if err != nil { return nil, err } - err = kMod.UpdateModAndLockFile() - if err != nil { - return nil, err + if !opts.offline { + err = kMod.UpdateModAndLockFile() + if err != nil { + return nil, err + } } return kMod, nil diff --git a/pkg/downloader/downloader.go b/pkg/downloader/downloader.go index e627b6f8..b5ff073d 100644 --- a/pkg/downloader/downloader.go +++ b/pkg/downloader/downloader.go @@ -531,6 +531,10 @@ func (d *OciDownloader) Download(opts *DownloadOptions) error { } + if opts.Offline && !utils.DirExists(filepath.Join(opts.LocalPath, constants.KCL_MOD)) { + return ErrNotFoundAndOffline + } + return err } @@ -662,5 +666,12 @@ func (d *GitDownloader) Download(opts *DownloadOptions) error { return err } } + + if opts.Offline && !utils.DirExists(filepath.Join(opts.LocalPath, constants.KCL_MOD)) { + return ErrNotFoundAndOffline + } + return nil } + +var ErrNotFoundAndOffline = errors.New("not found and offline") diff --git a/pkg/resolver/resolver.go b/pkg/resolver/resolver.go index 8fcb2a43..72231221 100644 --- a/pkg/resolver/resolver.go +++ b/pkg/resolver/resolver.go @@ -151,7 +151,7 @@ func (dr *DepsResolver) Resolve(options ...ResolveOption) error { ModSpec: dep.Source.ModSpec, } } else { - depSource = &dep.Source + depSource = &dep.Source } depVisitor, err := visitorSelectorFunc(depSource) diff --git a/pkg/visitor/visitor.go b/pkg/visitor/visitor.go index b2e97fb9..c8adc2e7 100644 --- a/pkg/visitor/visitor.go +++ b/pkg/visitor/visitor.go @@ -1,6 +1,7 @@ package visitor import ( + "errors" "fmt" "io" "os" @@ -249,9 +250,13 @@ func (rv *RemoteVisitor) Visit(s *downloader.Source, v visitFunc) error { downloader.WithCachePath(cacheFullPath), downloader.WithEnableCache(rv.EnableCache), downloader.WithInsecureSkipTLSverify(rv.InsecureSkipTLSverify), + downloader.WithOffline(rv.Offline), )) if err != nil { + if errors.Is(err, downloader.ErrNotFoundAndOffline) && rv.Offline { + return nil + } return err } if !s.ModSpec.IsNil() { @@ -266,6 +271,9 @@ func (rv *RemoteVisitor) Visit(s *downloader.Source, v visitFunc) error { pkg.WithSettings(rv.Settings), ) if err != nil { + if rv.Offline { + return nil + } return err }