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

feat: optimize api 'RunWithOpts' and disable some output with '--no_sum_check' #233

Merged
merged 1 commit into from
Dec 22, 2023
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
44 changes: 24 additions & 20 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}

Expand Down
43 changes: 24 additions & 19 deletions pkg/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package client

import (
"archive/tar"
"bytes"
"encoding/json"
"fmt"
"io"
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -845,17 +846,20 @@ 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)

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)
Expand All @@ -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"))
Expand Down
19 changes: 16 additions & 3 deletions pkg/opt/opt.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
36 changes: 36 additions & 0 deletions pkg/opt/opt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Loading