diff --git a/pkg/client/update.go b/pkg/client/update.go index 82b67248..663c3788 100644 --- a/pkg/client/update.go +++ b/pkg/client/update.go @@ -3,9 +3,12 @@ package client import ( "encoding/json" "fmt" + "kcl-lang.io/kpm/pkg/env" + "net/url" "path/filepath" ocispec "github.com/opencontainers/image-spec/specs-go/v1" + "golang.org/x/mod/module" "kcl-lang.io/kpm/pkg/checker" "kcl-lang.io/kpm/pkg/constants" "kcl-lang.io/kpm/pkg/features" @@ -139,7 +142,7 @@ func (c *KpmClient) Update(options ...UpdateOption) (*pkg.KclPkg, error) { } selectedDep.LocalFullPath = dep.LocalFullPath - if selectedDep.Sum == "" { + if selectedDep.Sum == "" && NeedCheckSum(*selectedDep) { sum, err := c.AcquireDepSum(*selectedDep) if err != nil { return err @@ -223,3 +226,24 @@ func (c *KpmClient) AcquireDepSum(dep pkg.Dependency) (string, error) { return "", nil } + +// NeedCheckSum reports whether to check the checksum for the given module. +func NeedCheckSum(dep pkg.Dependency) bool { + path := "" + if dep.Source.Local != nil { + path = dep.Source.Local.Path + } + // extract the path from the url, like: ghcr.io/kcl-lang/konfig + if dep.Source.Oci != nil { + path = fmt.Sprintf("%s/%s", dep.Source.Oci.Reg, dep.Source.Oci.Repo) + } + // extract the path from the url, like: github.com/kcl-lang/kpm + if dep.Source.Git != nil { + parse, err := url.Parse(dep.Source.Git.Url) + if err != nil { + return false + } + path = parse.Host + parse.Path + } + return !module.MatchPrefixPatterns(env.GetKpmNoSum(), path) +} diff --git a/pkg/client/update_test.go b/pkg/client/update_test.go index dba38d1e..e6f43063 100644 --- a/pkg/client/update_test.go +++ b/pkg/client/update_test.go @@ -1,6 +1,8 @@ package client import ( + "kcl-lang.io/kpm/pkg/downloader" + pkg "kcl-lang.io/kpm/pkg/package" "os" "path/filepath" "testing" @@ -85,3 +87,281 @@ func testUpdate(t *testing.T, kpmcli *KpmClient) { assert.Equal(t, utils.RmNewline(string(expectedModLock)), utils.RmNewline(string(gotModLock))) } } + +func TestNeedCheckSum(t *testing.T) { + type args struct { + dep pkg.Dependency + } + tests := []struct { + name string + env string + args args + want bool + }{ + { + name: "local_path_with_full_match", + env: "/path", + args: args{ + dep: pkg.Dependency{ + Source: downloader.Source{ + Git: nil, + Oci: nil, + Local: &downloader.Local{Path: "/path"}, + }, + }, + }, + want: false, + }, + { + name: "local_path_with_prefix_match", + env: "/path", + args: args{ + dep: pkg.Dependency{ + Source: downloader.Source{ + Git: nil, + Oci: nil, + Local: &downloader.Local{Path: "/path/pkg"}, + }, + }, + }, + want: false, + }, + { + name: "local_path_with_suffix_match", + env: "/path/*/pkg", + args: args{ + dep: pkg.Dependency{ + Source: downloader.Source{ + Git: nil, + Oci: nil, + Local: &downloader.Local{Path: "/path/foo/pkg"}, + }, + }, + }, + want: false, + }, + { + name: "local_path_without_match", + env: "/foo/bar", + args: args{ + dep: pkg.Dependency{ + Source: downloader.Source{ + Git: nil, + Oci: nil, + Local: &downloader.Local{Path: "/path/foo/bar"}, + }, + }, + }, + want: true, + }, + { + name: "oci_path_with_full_match", + env: "ghcr.io/kcl-lang/konfig", + args: args{ + dep: pkg.Dependency{ + Source: downloader.Source{ + Git: nil, + Oci: &downloader.Oci{Reg: "ghcr.io", Repo: "kcl-lang/konfig"}, + Local: nil, + }, + }, + }, + want: false, + }, + { + name: "oci_path_with_prefix_match", + env: "ghcr.io/kcl-lang", + args: args{ + dep: pkg.Dependency{ + Source: downloader.Source{ + Git: nil, + Oci: &downloader.Oci{Reg: "ghcr.io", Repo: "kcl-lang/konfig"}, + Local: nil, + }, + }, + }, + want: false, + }, + { + name: "oci_path_with_suffix_match", + env: "ghcr.io/kcl-lang/*", + args: args{ + dep: pkg.Dependency{ + Source: downloader.Source{ + Git: nil, + Oci: &downloader.Oci{Reg: "ghcr.io", Repo: "kcl-lang/konfig"}, + Local: nil, + }, + }, + }, + want: false, + }, + { + name: "oci_path_with_suffix_match2", + env: "ghcr.io/*/konfig", + args: args{ + dep: pkg.Dependency{ + Source: downloader.Source{ + Git: nil, + Oci: &downloader.Oci{Reg: "ghcr.io", Repo: "kcl-lang/konfig"}, + Local: nil, + }, + }, + }, + want: false, + }, + { + name: "oci_path_without_match", + env: "ghcr.io/kcl-lang/foo", + args: args{ + dep: pkg.Dependency{ + Source: downloader.Source{ + Git: nil, + Oci: &downloader.Oci{Reg: "ghcr.io", Repo: "kcl-lang/konfig"}, + Local: nil, + }, + }, + }, + want: true, + }, + { + name: "oci_path_without_match2", + env: "docker.io/kcl-lang/foo", + args: args{ + dep: pkg.Dependency{ + Source: downloader.Source{ + Git: nil, + Oci: &downloader.Oci{Reg: "ghcr.io", Repo: "kcl-lang/konfig"}, + Local: nil, + }, + }, + }, + want: true, + }, + { + name: "git_path_with_full_match", + env: "github.com/kcl-lang/konfig", + args: args{ + dep: pkg.Dependency{ + Source: downloader.Source{ + Git: &downloader.Git{Url: "github.com/kcl-lang/konfig"}, + Oci: nil, + Local: nil, + }, + }, + }, + want: false, + }, + { + name: "git_path_with_prefix_match", + env: "github.com/kcl-lang", + args: args{ + dep: pkg.Dependency{ + Source: downloader.Source{ + Git: &downloader.Git{Url: "github.com/kcl-lang/konfig"}, + Oci: nil, + Local: nil, + }, + }, + }, + want: false, + }, + { + name: "git_path_with_suffix_match", + env: "github.com/*/konfig", + args: args{ + dep: pkg.Dependency{ + Source: downloader.Source{ + Git: &downloader.Git{Url: "github.com/kcl-lang/konfig"}, + Oci: nil, + Local: nil, + }, + }, + }, + want: false, + }, + { + name: "git_path_without_match", + env: "github.com/kcl-lang/foo", + args: args{ + dep: pkg.Dependency{ + Source: downloader.Source{ + Git: &downloader.Git{Url: "github.com/kcl-lang/konfig"}, + Oci: nil, + Local: nil, + }, + }, + }, + want: true, + }, + { + name: "git_path_without_match2", + env: "gitea.com/kcl-lang/foo", + args: args{ + dep: pkg.Dependency{ + Source: downloader.Source{ + Git: &downloader.Git{Url: "github.com/kcl-lang/konfig"}, + Oci: nil, + Local: nil, + }, + }, + }, + want: true, + }, + { + name: "multiple_path_with_match", + env: "github.com/kcl-lang/*,ghcr.io/kcl-lang/*,/path", + args: args{ + dep: pkg.Dependency{ + Source: downloader.Source{ + Git: &downloader.Git{Url: "github.com/kcl-lang/konfig"}, + Oci: nil, + Local: nil, + }, + }, + }, + want: false, + }, + { + name: "multiple_path_with_match2", + env: "github.com/kcl-lang/*,ghcr.io/kcl-lang/*,/path", + args: args{ + dep: pkg.Dependency{ + Source: downloader.Source{ + Git: nil, + Oci: &downloader.Oci{Reg: "ghcr.io", Repo: "kcl-lang/konfig"}, + Local: nil, + }, + }, + }, + want: false, + }, + { + name: "multiple_path_with_match3", + env: "github.com/kcl-lang/*,ghcr.io/kcl-lang/*,/path", + args: args{ + dep: pkg.Dependency{ + Source: downloader.Source{ + Git: nil, + Oci: nil, + Local: &downloader.Local{Path: "/path/foo/bar"}, + }, + }, + }, + want: false, + }, + } + for _, tt := range tests { + err := os.Setenv("KPM_NO_SUM", tt.env) + if err != nil { + t.Fatal(err) + } + t.Run(tt.name, func(t *testing.T) { + assert.Equal(t, tt.want, NeedCheckSum(tt.args.dep), "NeedCheckSum(%v)", tt.args.dep) + }) + err = os.Unsetenv("KPM_NO_SUM") + if err != nil { + t.Fatal(err) + } + } +} diff --git a/pkg/env/env.go b/pkg/env/env.go index cb4ddae9..8f8d6c18 100644 --- a/pkg/env/env.go +++ b/pkg/env/env.go @@ -12,6 +12,7 @@ import ( const PKG_PATH = "KCL_PKG_PATH" const DEFAULT_PKG_PATH_IN_UER_HOME = ".kcl" const KPM_SUB_DIR = "kpm" +const KPM_NO_SUM = "KPM_NO_SUM" // GetEnvPkgPath will return the env $KCL_PKG_PATH. func GetEnvPkgPath() string { @@ -42,3 +43,8 @@ func GetAbsPkgPath() (string, error) { return kpmHome, nil } + +// GetKpmNoSum will return the env $KPM_NO_SUM. +func GetKpmNoSum() string { + return os.Getenv(KPM_NO_SUM) +}