Skip to content

Commit af499ea

Browse files
committed
feat(checksum): add KPM_NO_SUM env support
Signed-off-by: suyiiyii <[email protected]>
1 parent adea5e5 commit af499ea

File tree

3 files changed

+311
-1
lines changed

3 files changed

+311
-1
lines changed

pkg/client/update.go

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@ package client
33
import (
44
"encoding/json"
55
"fmt"
6+
"kcl-lang.io/kpm/pkg/env"
7+
"net/url"
68
"path/filepath"
79

810
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
11+
"golang.org/x/mod/module"
912
"kcl-lang.io/kpm/pkg/checker"
1013
"kcl-lang.io/kpm/pkg/constants"
1114
"kcl-lang.io/kpm/pkg/features"
@@ -139,7 +142,7 @@ func (c *KpmClient) Update(options ...UpdateOption) (*pkg.KclPkg, error) {
139142
}
140143

141144
selectedDep.LocalFullPath = dep.LocalFullPath
142-
if selectedDep.Sum == "" {
145+
if selectedDep.Sum == "" && NeedCheckSum(*selectedDep) {
143146
sum, err := c.AcquireDepSum(*selectedDep)
144147
if err != nil {
145148
return err
@@ -223,3 +226,24 @@ func (c *KpmClient) AcquireDepSum(dep pkg.Dependency) (string, error) {
223226

224227
return "", nil
225228
}
229+
230+
// NeedCheckSum reports whether to check the checksum for the given module.
231+
func NeedCheckSum(dep pkg.Dependency) bool {
232+
path := ""
233+
if dep.Source.Local != nil {
234+
path = dep.Source.Local.Path
235+
}
236+
// extract the path from the url, like: ghcr.io/kcl-lang/konfig
237+
if dep.Source.Oci != nil {
238+
path = fmt.Sprintf("%s/%s", dep.Source.Oci.Reg, dep.Source.Oci.Repo)
239+
}
240+
// extract the path from the url, like: github.com/kcl-lang/kpm
241+
if dep.Source.Git != nil {
242+
parse, err := url.Parse(dep.Source.Git.Url)
243+
if err != nil {
244+
return false
245+
}
246+
path = parse.Host + parse.Path
247+
}
248+
return !module.MatchPrefixPatterns(env.GetKpmNoSum(), path)
249+
}

pkg/client/update_test.go

Lines changed: 280 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package client
22

33
import (
4+
"kcl-lang.io/kpm/pkg/downloader"
5+
pkg "kcl-lang.io/kpm/pkg/package"
46
"os"
57
"path/filepath"
68
"testing"
@@ -85,3 +87,281 @@ func testUpdate(t *testing.T, kpmcli *KpmClient) {
8587
assert.Equal(t, utils.RmNewline(string(expectedModLock)), utils.RmNewline(string(gotModLock)))
8688
}
8789
}
90+
91+
func TestNeedCheckSum(t *testing.T) {
92+
type args struct {
93+
dep pkg.Dependency
94+
}
95+
tests := []struct {
96+
name string
97+
env string
98+
args args
99+
want bool
100+
}{
101+
{
102+
name: "local_path_with_full_match",
103+
env: "/path",
104+
args: args{
105+
dep: pkg.Dependency{
106+
Source: downloader.Source{
107+
Git: nil,
108+
Oci: nil,
109+
Local: &downloader.Local{Path: "/path"},
110+
},
111+
},
112+
},
113+
want: false,
114+
},
115+
{
116+
name: "local_path_with_prefix_match",
117+
env: "/path",
118+
args: args{
119+
dep: pkg.Dependency{
120+
Source: downloader.Source{
121+
Git: nil,
122+
Oci: nil,
123+
Local: &downloader.Local{Path: "/path/pkg"},
124+
},
125+
},
126+
},
127+
want: false,
128+
},
129+
{
130+
name: "local_path_with_suffix_match",
131+
env: "/path/*/pkg",
132+
args: args{
133+
dep: pkg.Dependency{
134+
Source: downloader.Source{
135+
Git: nil,
136+
Oci: nil,
137+
Local: &downloader.Local{Path: "/path/foo/pkg"},
138+
},
139+
},
140+
},
141+
want: false,
142+
},
143+
{
144+
name: "local_path_without_match",
145+
env: "/foo/bar",
146+
args: args{
147+
dep: pkg.Dependency{
148+
Source: downloader.Source{
149+
Git: nil,
150+
Oci: nil,
151+
Local: &downloader.Local{Path: "/path/foo/bar"},
152+
},
153+
},
154+
},
155+
want: true,
156+
},
157+
{
158+
name: "oci_path_with_full_match",
159+
env: "ghcr.io/kcl-lang/konfig",
160+
args: args{
161+
dep: pkg.Dependency{
162+
Source: downloader.Source{
163+
Git: nil,
164+
Oci: &downloader.Oci{Reg: "ghcr.io", Repo: "kcl-lang/konfig"},
165+
Local: nil,
166+
},
167+
},
168+
},
169+
want: false,
170+
},
171+
{
172+
name: "oci_path_with_prefix_match",
173+
env: "ghcr.io/kcl-lang",
174+
args: args{
175+
dep: pkg.Dependency{
176+
Source: downloader.Source{
177+
Git: nil,
178+
Oci: &downloader.Oci{Reg: "ghcr.io", Repo: "kcl-lang/konfig"},
179+
Local: nil,
180+
},
181+
},
182+
},
183+
want: false,
184+
},
185+
{
186+
name: "oci_path_with_suffix_match",
187+
env: "ghcr.io/kcl-lang/*",
188+
args: args{
189+
dep: pkg.Dependency{
190+
Source: downloader.Source{
191+
Git: nil,
192+
Oci: &downloader.Oci{Reg: "ghcr.io", Repo: "kcl-lang/konfig"},
193+
Local: nil,
194+
},
195+
},
196+
},
197+
want: false,
198+
},
199+
{
200+
name: "oci_path_with_suffix_match2",
201+
env: "ghcr.io/*/konfig",
202+
args: args{
203+
dep: pkg.Dependency{
204+
Source: downloader.Source{
205+
Git: nil,
206+
Oci: &downloader.Oci{Reg: "ghcr.io", Repo: "kcl-lang/konfig"},
207+
Local: nil,
208+
},
209+
},
210+
},
211+
want: false,
212+
},
213+
{
214+
name: "oci_path_without_match",
215+
env: "ghcr.io/kcl-lang/foo",
216+
args: args{
217+
dep: pkg.Dependency{
218+
Source: downloader.Source{
219+
Git: nil,
220+
Oci: &downloader.Oci{Reg: "ghcr.io", Repo: "kcl-lang/konfig"},
221+
Local: nil,
222+
},
223+
},
224+
},
225+
want: true,
226+
},
227+
{
228+
name: "oci_path_without_match2",
229+
env: "docker.io/kcl-lang/foo",
230+
args: args{
231+
dep: pkg.Dependency{
232+
Source: downloader.Source{
233+
Git: nil,
234+
Oci: &downloader.Oci{Reg: "ghcr.io", Repo: "kcl-lang/konfig"},
235+
Local: nil,
236+
},
237+
},
238+
},
239+
want: true,
240+
},
241+
{
242+
name: "git_path_with_full_match",
243+
env: "github.com/kcl-lang/konfig",
244+
args: args{
245+
dep: pkg.Dependency{
246+
Source: downloader.Source{
247+
Git: &downloader.Git{Url: "github.com/kcl-lang/konfig"},
248+
Oci: nil,
249+
Local: nil,
250+
},
251+
},
252+
},
253+
want: false,
254+
},
255+
{
256+
name: "git_path_with_prefix_match",
257+
env: "github.com/kcl-lang",
258+
args: args{
259+
dep: pkg.Dependency{
260+
Source: downloader.Source{
261+
Git: &downloader.Git{Url: "github.com/kcl-lang/konfig"},
262+
Oci: nil,
263+
Local: nil,
264+
},
265+
},
266+
},
267+
want: false,
268+
},
269+
{
270+
name: "git_path_with_suffix_match",
271+
env: "github.com/*/konfig",
272+
args: args{
273+
dep: pkg.Dependency{
274+
Source: downloader.Source{
275+
Git: &downloader.Git{Url: "github.com/kcl-lang/konfig"},
276+
Oci: nil,
277+
Local: nil,
278+
},
279+
},
280+
},
281+
want: false,
282+
},
283+
{
284+
name: "git_path_without_match",
285+
env: "github.com/kcl-lang/foo",
286+
args: args{
287+
dep: pkg.Dependency{
288+
Source: downloader.Source{
289+
Git: &downloader.Git{Url: "github.com/kcl-lang/konfig"},
290+
Oci: nil,
291+
Local: nil,
292+
},
293+
},
294+
},
295+
want: true,
296+
},
297+
{
298+
name: "git_path_without_match2",
299+
env: "gitea.com/kcl-lang/foo",
300+
args: args{
301+
dep: pkg.Dependency{
302+
Source: downloader.Source{
303+
Git: &downloader.Git{Url: "github.com/kcl-lang/konfig"},
304+
Oci: nil,
305+
Local: nil,
306+
},
307+
},
308+
},
309+
want: true,
310+
},
311+
{
312+
name: "multiple_path_with_match",
313+
env: "github.com/kcl-lang/*,ghcr.io/kcl-lang/*,/path",
314+
args: args{
315+
dep: pkg.Dependency{
316+
Source: downloader.Source{
317+
Git: &downloader.Git{Url: "github.com/kcl-lang/konfig"},
318+
Oci: nil,
319+
Local: nil,
320+
},
321+
},
322+
},
323+
want: false,
324+
},
325+
{
326+
name: "multiple_path_with_match2",
327+
env: "github.com/kcl-lang/*,ghcr.io/kcl-lang/*,/path",
328+
args: args{
329+
dep: pkg.Dependency{
330+
Source: downloader.Source{
331+
Git: nil,
332+
Oci: &downloader.Oci{Reg: "ghcr.io", Repo: "kcl-lang/konfig"},
333+
Local: nil,
334+
},
335+
},
336+
},
337+
want: false,
338+
},
339+
{
340+
name: "multiple_path_with_match3",
341+
env: "github.com/kcl-lang/*,ghcr.io/kcl-lang/*,/path",
342+
args: args{
343+
dep: pkg.Dependency{
344+
Source: downloader.Source{
345+
Git: nil,
346+
Oci: nil,
347+
Local: &downloader.Local{Path: "/path/foo/bar"},
348+
},
349+
},
350+
},
351+
want: false,
352+
},
353+
}
354+
for _, tt := range tests {
355+
err := os.Setenv("KPM_NO_SUM", tt.env)
356+
if err != nil {
357+
t.Fatal(err)
358+
}
359+
t.Run(tt.name, func(t *testing.T) {
360+
assert.Equal(t, tt.want, NeedCheckSum(tt.args.dep), "NeedCheckSum(%v)", tt.args.dep)
361+
})
362+
err = os.Unsetenv("KPM_NO_SUM")
363+
if err != nil {
364+
t.Fatal(err)
365+
}
366+
}
367+
}

pkg/env/env.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
const PKG_PATH = "KCL_PKG_PATH"
1313
const DEFAULT_PKG_PATH_IN_UER_HOME = ".kcl"
1414
const KPM_SUB_DIR = "kpm"
15+
const KPM_NO_SUM = "KPM_NO_SUM"
1516

1617
// GetEnvPkgPath will return the env $KCL_PKG_PATH.
1718
func GetEnvPkgPath() string {
@@ -42,3 +43,8 @@ func GetAbsPkgPath() (string, error) {
4243

4344
return kpmHome, nil
4445
}
46+
47+
// GetKpmNoSum will return the env $KPM_NO_SUM.
48+
func GetKpmNoSum() string {
49+
return os.Getenv(KPM_NO_SUM)
50+
}

0 commit comments

Comments
 (0)