From ebd432874d46e63cca525e56b0a812df42775121 Mon Sep 17 00:00:00 2001 From: zongz Date: Fri, 22 Dec 2023 10:20:05 +0800 Subject: [PATCH] feat: optimize api 'RunWithOpts' and disable some output with '--no_sum_check' Signed-off-by: zongz --- pkg/client/client.go | 44 +++++++++++++++++++++------------------ pkg/client/client_test.go | 43 +++++++++++++++++++++----------------- pkg/opt/opt.go | 19 ++++++++++++++--- pkg/opt/opt_test.go | 36 ++++++++++++++++++++++++++++++++ 4 files changed, 100 insertions(+), 42 deletions(-) diff --git a/pkg/client/client.go b/pkg/client/client.go index 6a90000f..7038f129 100644 --- a/pkg/client/client.go +++ b/pkg/client/client.go @@ -178,27 +178,31 @@ func (c *KpmClient) ResolvePkgDepsMetadata(kclPkg *pkg.KclPkg, update bool) erro searchPath = c.homePath } - // alian the dependencies between kcl.mod and kcl.mod.lock - // clean the dependencies in kcl.mod.lock which not in kcl.mod - // clean the dependencies in kcl.mod.lock and kcl.mod which have different version - for name, dep := range kclPkg.Dependencies.Deps { - modDep, ok := kclPkg.ModFile.Dependencies.Deps[name] - if !ok || !dep.WithTheSameVersion(modDep) { - reporter.ReportMsgTo( - fmt.Sprintf("removing '%s' with version '%s'", name, dep.Version), - c.logWriter, - ) - delete(kclPkg.Dependencies.Deps, name) + // If under the mode of '--no_sum_check', the checksum of the package will not be checked. + // There is no kcl.mod.lock, and the dependencies in kcl.mod and kcl.mod.lock do not need to be aligned. + if !c.noSumCheck { + // alian the dependencies between kcl.mod and kcl.mod.lock + // clean the dependencies in kcl.mod.lock which not in kcl.mod + // clean the dependencies in kcl.mod.lock and kcl.mod which have different version + for name, dep := range kclPkg.Dependencies.Deps { + modDep, ok := kclPkg.ModFile.Dependencies.Deps[name] + if !ok || !dep.WithTheSameVersion(modDep) { + reporter.ReportMsgTo( + fmt.Sprintf("removing '%s' with version '%s'", name, dep.Version), + c.logWriter, + ) + delete(kclPkg.Dependencies.Deps, name) + } } - } - // add the dependencies in kcl.mod which not in kcl.mod.lock - for name, d := range kclPkg.ModFile.Dependencies.Deps { - if _, ok := kclPkg.Dependencies.Deps[name]; !ok { - reporter.ReportMsgTo( - fmt.Sprintf("adding '%s' with version '%s'", name, d.Version), - c.logWriter, - ) - kclPkg.Dependencies.Deps[name] = d + // add the dependencies in kcl.mod which not in kcl.mod.lock + for name, d := range kclPkg.ModFile.Dependencies.Deps { + if _, ok := kclPkg.Dependencies.Deps[name]; !ok { + reporter.ReportMsgTo( + fmt.Sprintf("adding '%s' with version '%s'", name, d.Version), + c.logWriter, + ) + kclPkg.Dependencies.Deps[name] = d + } } } diff --git a/pkg/client/client_test.go b/pkg/client/client_test.go index 9247a5ad..dbf95168 100644 --- a/pkg/client/client_test.go +++ b/pkg/client/client_test.go @@ -2,6 +2,7 @@ package client import ( "archive/tar" + "bytes" "encoding/json" "fmt" "io" @@ -234,15 +235,15 @@ func TestVendorDeps(t *testing.T) { kcl2Sum, _ := utils.HashDir(filepath.Join(kpm_home, "kcl2")) depKcl1 := pkg.Dependency{ - Name: "kcl1", - FullName: "kcl1", - Sum: kcl1Sum, + Name: "kcl1", + FullName: "kcl1", + Sum: kcl1Sum, } depKcl2 := pkg.Dependency{ - Name: "kcl2", - FullName: "kcl2", - Sum: kcl2Sum, + Name: "kcl2", + FullName: "kcl2", + Sum: kcl2Sum, } kclPkg := pkg.KclPkg{ @@ -316,15 +317,15 @@ func TestResolveDepsVendorMode(t *testing.T) { kcl2Sum, _ := utils.HashDir(filepath.Join(kpm_home, "kcl2")) depKcl1 := pkg.Dependency{ - Name: "kcl1", - FullName: "kcl1", - Sum: kcl1Sum, + Name: "kcl1", + FullName: "kcl1", + Sum: kcl1Sum, } depKcl2 := pkg.Dependency{ - Name: "kcl2", - FullName: "kcl2", - Sum: kcl2Sum, + Name: "kcl2", + FullName: "kcl2", + Sum: kcl2Sum, } kclPkg := pkg.KclPkg{ @@ -382,15 +383,15 @@ func TestCompileWithEntryFile(t *testing.T) { kcl1Sum, _ := utils.HashDir(filepath.Join(kpm_home, "kcl1")) depKcl1 := pkg.Dependency{ - Name: "kcl1", - FullName: "kcl1", - Sum: kcl1Sum, + Name: "kcl1", + FullName: "kcl1", + Sum: kcl1Sum, } kcl2Sum, _ := utils.HashDir(filepath.Join(kpm_home, "kcl2")) depKcl2 := pkg.Dependency{ - Name: "kcl2", - FullName: "kcl2", - Sum: kcl2Sum, + Name: "kcl2", + FullName: "kcl2", + Sum: kcl2Sum, } kclPkg := pkg.KclPkg{ @@ -845,10 +846,12 @@ func TestRunWithNoSumCheck(t *testing.T) { func TestUpdateWithNoSumCheck(t *testing.T) { pkgPath := getTestDir("test_update_no_sum_check") - kpmcli, err := NewKpmClient() assert.Equal(t, err, nil) + var buf bytes.Buffer + kpmcli.SetLogWriter(&buf) + kpmcli.SetNoSumCheck(true) kclPkg, err := kpmcli.LoadPkgFromPath(pkgPath) assert.Equal(t, err, nil) @@ -856,6 +859,7 @@ func TestUpdateWithNoSumCheck(t *testing.T) { err = kpmcli.UpdateDeps(kclPkg) assert.Equal(t, err, nil) assert.Equal(t, utils.DirExists(filepath.Join(pkgPath, "kcl.mod.lock")), false) + assert.Equal(t, buf.String(), "") kpmcli.SetNoSumCheck(false) kclPkg, err = kpmcli.LoadPkgFromPath(pkgPath) @@ -864,6 +868,7 @@ func TestUpdateWithNoSumCheck(t *testing.T) { err = kpmcli.UpdateDeps(kclPkg) assert.Equal(t, err, nil) assert.Equal(t, utils.DirExists(filepath.Join(pkgPath, "kcl.mod.lock")), true) + assert.Equal(t, buf.String(), "adding 'helloworld' with version '0.1.1'\ndownloading 'kcl-lang/helloworld:0.1.1' from 'ghcr.io/kcl-lang/helloworld:0.1.1'\n") defer func() { _ = os.Remove(filepath.Join(pkgPath, "kcl.mod.lock")) diff --git a/pkg/opt/opt.go b/pkg/opt/opt.go index b039d37f..1780e976 100644 --- a/pkg/opt/opt.go +++ b/pkg/opt/opt.go @@ -30,10 +30,23 @@ func MergeOptions(opts ...CompileOptions) CompileOptions { var opt = DefaultCompileOptions() for _, o := range opts { opt.Merge(*o.Option) - opt.isVendor = o.isVendor - opt.hasSettingsYaml = o.hasSettingsYaml + if o.writer != nil { + opt.writer = o.writer + } + + if o.isVendor { + opt.isVendor = o.isVendor + } + + if o.hasSettingsYaml { + opt.hasSettingsYaml = o.hasSettingsYaml + } + + if o.noSumCheck { + opt.noSumCheck = o.noSumCheck + } + opt.entries = append(opt.entries, o.entries...) - opt.noSumCheck = o.noSumCheck } return *opt } diff --git a/pkg/opt/opt_test.go b/pkg/opt/opt_test.go index a04ea6a0..54dfeb88 100644 --- a/pkg/opt/opt_test.go +++ b/pkg/opt/opt_test.go @@ -20,3 +20,39 @@ func TestWorkDirAsPkgPath(t *testing.T) { opts.SetEntries([]string{"override.k"}) assert.Equal(t, opts.Entries(), []string{"override.k"}) } + +func TestCompilationMerge(t *testing.T) { + opts := MergeOptions( + WithEntries([]string{"test.k"}), + ) + assert.Equal(t, opts.Entries(), []string{"test.k"}) + assert.Equal(t, opts.NoSumCheck(), false) + assert.Equal(t, opts.IsVendor(), false) + + opts = MergeOptions( + opts, + WithNoSumCheck(true), + ) + + assert.Equal(t, opts.Entries(), []string{"test.k"}) + assert.Equal(t, opts.NoSumCheck(), true) + assert.Equal(t, opts.IsVendor(), false) + + opts = MergeOptions( + opts, + WithVendor(true), + ) + + assert.Equal(t, opts.Entries(), []string{"test.k"}) + assert.Equal(t, opts.NoSumCheck(), true) + assert.Equal(t, opts.IsVendor(), true) + + opts = MergeOptions( + opts, + WithEntries([]string{"test1.k"}), + ) + + assert.Equal(t, opts.Entries(), []string{"test.k", "test1.k"}) + assert.Equal(t, opts.NoSumCheck(), true) + assert.Equal(t, opts.IsVendor(), true) +}