Skip to content

[POC] feat: add KPM_NO_SUM env to work without checksum #616

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
26 changes: 25 additions & 1 deletion pkg/client/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
280 changes: 280 additions & 0 deletions pkg/client/update_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package client

import (
"kcl-lang.io/kpm/pkg/downloader"
pkg "kcl-lang.io/kpm/pkg/package"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -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)
}
}
}
6 changes: 6 additions & 0 deletions pkg/env/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
Loading