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: Remove the auto-updating for kcl.mod when using command metadata. #565

Merged
merged 4 commits into from
Dec 12, 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
22 changes: 18 additions & 4 deletions pkg/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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))

Expand Down
3 changes: 3 additions & 0 deletions pkg/client/test_data/test_metadata_offline/ugly.kcl.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@
name = "test_metadata_offline"
edition = "0.0.1"
version = "0.0.1"



Original file line number Diff line number Diff line change
@@ -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" }
Original file line number Diff line number Diff line change
@@ -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"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The_first_kcl_program = 'Hello World!'
9 changes: 6 additions & 3 deletions pkg/client/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions pkg/downloader/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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")
2 changes: 1 addition & 1 deletion pkg/resolver/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 8 additions & 0 deletions pkg/visitor/visitor.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package visitor

import (
"errors"
"fmt"
"io"
"os"
Expand Down Expand Up @@ -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() {
Expand All @@ -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
}

Expand Down
Loading