Skip to content

Commit

Permalink
added feature flag
Browse files Browse the repository at this point in the history
Signed-off-by: Nishant Bansal <[email protected]>
  • Loading branch information
NishantBansal2003 committed Nov 25, 2024
1 parent b9f3bda commit 5a944ea
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 12 deletions.
26 changes: 14 additions & 12 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -947,18 +947,20 @@ func (c *KpmClient) Download(dep *pkg.Dependency, homePath, localPath string) (*
}

func (c *KpmClient) ValidateDependency(dep *pkg.Dependency) error {
tmpKclPkg := pkg.KclPkg{
HomePath: dep.LocalFullPath,
Dependencies: pkg.Dependencies{Deps: func() *orderedmap.OrderedMap[string, pkg.Dependency] {
m := orderedmap.NewOrderedMap[string, pkg.Dependency]()
m.Set(dep.Name, *dep)
return m
}()},
NoSumCheck: c.GetNoSumCheck(),
}

if err := c.DepChecker.Check(tmpKclPkg); err != nil {
return reporter.NewErrorEvent(reporter.InvalidKclPkg, err, fmt.Sprintf("%s package does not match the original kcl package", dep.FullName))
if ok, err := features.Enabled(features.SupportCheckSum); err == nil && ok {
tmpKclPkg := pkg.KclPkg{
HomePath: dep.LocalFullPath,
Dependencies: pkg.Dependencies{Deps: func() *orderedmap.OrderedMap[string, pkg.Dependency] {
m := orderedmap.NewOrderedMap[string, pkg.Dependency]()
m.Set(dep.Name, *dep)
return m
}()},
NoSumCheck: c.GetNoSumCheck(),
}

if err := c.DepChecker.Check(tmpKclPkg); err != nil {
return reporter.NewErrorEvent(reporter.InvalidKclPkg, err, fmt.Sprintf("%s package does not match the original kcl package", dep.FullName))
}
}

return nil
Expand Down
44 changes: 44 additions & 0 deletions pkg/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"kcl-lang.io/kcl-go/pkg/kcl"
"kcl-lang.io/kpm/pkg/downloader"
"kcl-lang.io/kpm/pkg/env"
"kcl-lang.io/kpm/pkg/features"
"kcl-lang.io/kpm/pkg/opt"
pkg "kcl-lang.io/kpm/pkg/package"
"kcl-lang.io/kpm/pkg/reporter"
Expand Down Expand Up @@ -2263,3 +2264,46 @@ func testPushWithInsecureSkipTLSverify(t *testing.T) {

assert.Equal(t, buf.String(), "Called Success\n")
}

func TestValidateDependency(t *testing.T) {
features.Enable(features.SupportCheckSum)
defer features.Disable(features.SupportCheckSum)

kpmcli, err := NewKpmClient()
assert.Equal(t, err, nil)

dep1 := pkg.Dependency{
Name: "helloworld",
FullName: "helloworld_0.1.2",
Version: "0.1.2",
Sum: "PN0OMEV9M8VGFn1CtA/T3bcgZmMJmOo+RkBrLKIWYeQ=",
LocalFullPath: "path/to/kcl/package",
Source: downloader.Source{
Oci: &downloader.Oci{
Reg: "ghcr.io",
Repo: "kcl-lang/helloworld",
Tag: "0.1.2",
},
},
}
err = kpmcli.ValidateDependency(&dep1)
assert.Equal(t, err, nil)

dep2 := pkg.Dependency{
Name: "helloworld",
FullName: "helloworld_0.1.2",
Version: "0.1.2",
Sum: "fail-to-validate-dependency",
LocalFullPath: "path/to/kcl/package",
Source: downloader.Source{
Oci: &downloader.Oci{
Reg: "ghcr.io",
Repo: "kcl-lang/helloworld",
Tag: "0.1.2",
},
},
}

err = kpmcli.ValidateDependency(&dep2)
assert.Error(t, err)
}
3 changes: 3 additions & 0 deletions pkg/features/features.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,15 @@ const (
SupportMVS = "SupportMVS"
// SupportNewStorage is the feature gate for enabling the support for the new storage structure.
SupportNewStorage = "SupportNewStorage"
// SupportCheckSum is the feature gate for enabling the support for the checksum verification.
SupportCheckSum = "SupportCheckSum"
)

var (
features = map[string]bool{
SupportMVS: false,
SupportNewStorage: false,
SupportCheckSum: false,
}
mu sync.Mutex
)
Expand Down

0 comments on commit 5a944ea

Please sign in to comment.