From f33650481be479e0526ee22f256ec89042829cf6 Mon Sep 17 00:00:00 2001 From: zongz Date: Tue, 12 Nov 2024 20:31:43 +0800 Subject: [PATCH 1/2] fix: add default settings for API LoadKclPkgWithOpts Signed-off-by: zongz --- pkg/package/package.go | 12 +++++++++--- pkg/package/package_test.go | 14 ++++++++++++++ .../test_data/load_without_settings/kcl.mod | 7 +++++++ .../test_data/load_without_settings/kcl.mod.lock | 8 ++++++++ pkg/package/test_data/load_without_settings/main.k | 1 + 5 files changed, 39 insertions(+), 3 deletions(-) create mode 100644 pkg/package/test_data/load_without_settings/kcl.mod create mode 100644 pkg/package/test_data/load_without_settings/kcl.mod.lock create mode 100644 pkg/package/test_data/load_without_settings/main.k diff --git a/pkg/package/package.go b/pkg/package/package.go index 6d0f8de6..c7f10913 100644 --- a/pkg/package/package.go +++ b/pkg/package/package.go @@ -74,6 +74,12 @@ func LoadKclPkgWithOpts(options ...LoadOption) (*KclPkg, error) { } pkgPath := opts.Path + var loadSettings *settings.Settings + if opts.Settings == nil { + loadSettings = settings.GetSettings() + } else { + loadSettings = opts.Settings + } modFile := new(ModFile) err := modFile.LoadModFile(filepath.Join(pkgPath, MOD_FILE)) @@ -114,7 +120,7 @@ func LoadKclPkgWithOpts(options ...LoadOption) (*KclPkg, error) { return nil, fmt.Errorf("could not load 'kcl.mod' in '%s'\n%w", pkgPath, err) } // 2. Fill the default oci registry, the default oci registry is in the settings. - err = fillDepsInfoWithSettings(&modFile.Dependencies, opts.Settings) + err = fillDepsInfoWithSettings(&modFile.Dependencies, loadSettings) if err != nil { return nil, fmt.Errorf("could not load 'kcl.mod' in '%s'\n%w", pkgPath, err) } @@ -136,8 +142,8 @@ func LoadKclPkgWithOpts(options ...LoadOption) (*KclPkg, error) { Version: lockDep.Version, }, Oci: &downloader.Oci{ - Reg: opts.Settings.DefaultOciRegistry(), - Repo: utils.JoinPath(opts.Settings.DefaultOciRepo(), lockDep.Name), + Reg: loadSettings.DefaultOciRegistry(), + Repo: utils.JoinPath(loadSettings.DefaultOciRepo(), lockDep.Name), Tag: lockDep.Version, }, } diff --git a/pkg/package/package_test.go b/pkg/package/package_test.go index b5a51846..3e47756b 100644 --- a/pkg/package/package_test.go +++ b/pkg/package/package_test.go @@ -155,3 +155,17 @@ func TestLoadPkgFromLock(t *testing.T) { assert.Equal(t, kpkg.Dependencies.Deps.GetOrDefault("helloworld", TestPkgDependency).Source.Oci.Repo, "kcl-lang/helloworld") assert.Equal(t, kpkg.Dependencies.Deps.GetOrDefault("helloworld", TestPkgDependency).Source.Oci.Tag, "0.1.2") } + +func TestLoadKclPkgWithoutSettings(t *testing.T){ + modPath := getTestDir("load_without_settings") + kMod, err := LoadKclPkgWithOpts( + WithPath(modPath), + ) + assert.Equal(t, err, nil) + assert.Equal(t, kMod.ModFile.Dependencies.Deps.Len(), 1) + assert.Equal(t, kMod.ModFile.Dependencies.Deps.GetOrDefault("helloworld", TestPkgDependency).Name, "helloworld") + assert.Equal(t, kMod.ModFile.Dependencies.Deps.GetOrDefault("helloworld", TestPkgDependency).FullName, "helloworld_0.1.4") + assert.Equal(t, kMod.ModFile.Dependencies.Deps.GetOrDefault("helloworld", TestPkgDependency).Source.Oci.Reg, "ghcr.io") + assert.Equal(t, kMod.ModFile.Dependencies.Deps.GetOrDefault("helloworld", TestPkgDependency).Source.Oci.Repo, "kcl-lang/helloworld") + assert.Equal(t, kMod.ModFile.Dependencies.Deps.GetOrDefault("helloworld", TestPkgDependency).Source.Oci.Tag, "0.1.4") +} \ No newline at end of file diff --git a/pkg/package/test_data/load_without_settings/kcl.mod b/pkg/package/test_data/load_without_settings/kcl.mod new file mode 100644 index 00000000..81e9a543 --- /dev/null +++ b/pkg/package/test_data/load_without_settings/kcl.mod @@ -0,0 +1,7 @@ +[package] +name = "load_without_settings" +edition = "v0.10.0" +version = "0.0.1" + +[dependencies] +helloworld = "0.1.4" diff --git a/pkg/package/test_data/load_without_settings/kcl.mod.lock b/pkg/package/test_data/load_without_settings/kcl.mod.lock new file mode 100644 index 00000000..1428530e --- /dev/null +++ b/pkg/package/test_data/load_without_settings/kcl.mod.lock @@ -0,0 +1,8 @@ +[dependencies] + [dependencies.helloworld] + name = "helloworld" + full_name = "helloworld_0.1.4" + version = "0.1.4" + reg = "ghcr.io" + repo = "kcl-lang/helloworld" + oci_tag = "0.1.4" diff --git a/pkg/package/test_data/load_without_settings/main.k b/pkg/package/test_data/load_without_settings/main.k new file mode 100644 index 00000000..fa7048e6 --- /dev/null +++ b/pkg/package/test_data/load_without_settings/main.k @@ -0,0 +1 @@ +The_first_kcl_program = 'Hello World!' \ No newline at end of file From 6a1949364af2ac01ac1723608d49dbd1b607d190 Mon Sep 17 00:00:00 2001 From: zongz Date: Tue, 12 Nov 2024 20:37:06 +0800 Subject: [PATCH 2/2] fix: fix if case Signed-off-by: zongz --- pkg/package/package.go | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/pkg/package/package.go b/pkg/package/package.go index c7f10913..87a8d949 100644 --- a/pkg/package/package.go +++ b/pkg/package/package.go @@ -74,11 +74,8 @@ func LoadKclPkgWithOpts(options ...LoadOption) (*KclPkg, error) { } pkgPath := opts.Path - var loadSettings *settings.Settings if opts.Settings == nil { - loadSettings = settings.GetSettings() - } else { - loadSettings = opts.Settings + opts.Settings = settings.GetSettings() } modFile := new(ModFile) @@ -120,7 +117,7 @@ func LoadKclPkgWithOpts(options ...LoadOption) (*KclPkg, error) { return nil, fmt.Errorf("could not load 'kcl.mod' in '%s'\n%w", pkgPath, err) } // 2. Fill the default oci registry, the default oci registry is in the settings. - err = fillDepsInfoWithSettings(&modFile.Dependencies, loadSettings) + err = fillDepsInfoWithSettings(&modFile.Dependencies, opts.Settings) if err != nil { return nil, fmt.Errorf("could not load 'kcl.mod' in '%s'\n%w", pkgPath, err) } @@ -142,8 +139,8 @@ func LoadKclPkgWithOpts(options ...LoadOption) (*KclPkg, error) { Version: lockDep.Version, }, Oci: &downloader.Oci{ - Reg: loadSettings.DefaultOciRegistry(), - Repo: utils.JoinPath(loadSettings.DefaultOciRepo(), lockDep.Name), + Reg: opts.Settings.DefaultOciRegistry(), + Repo: utils.JoinPath(opts.Settings.DefaultOciRepo(), lockDep.Name), Tag: lockDep.Version, }, }