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

fix: fix missing tag in command push #561

Merged
merged 10 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
207 changes: 106 additions & 101 deletions pkg/checker/checker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
pkg "kcl-lang.io/kpm/pkg/package"
"kcl-lang.io/kpm/pkg/reporter"
"kcl-lang.io/kpm/pkg/settings"
"kcl-lang.io/kpm/pkg/test"
)

func TestModCheckerCheck(t *testing.T) {
Expand Down Expand Up @@ -145,126 +146,130 @@ func getTestSettings() (*settings.Settings, error) {
}

func TestModCheckerCheck_WithTrustedSum(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("Skipping TestModCheckerCheck_WithTrustedSum test on Windows")
}
testFunc := func(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("Skipping TestModCheckerCheck_WithTrustedSum test on Windows")
}

// Start the local Docker registry required for testing
err := mock.StartDockerRegistry()
assert.Equal(t, err, nil)
// Start the local Docker registry required for testing
err := mock.StartDockerRegistry()
assert.Equal(t, err, nil)

// Push the test package to the local OCI registry
err = mock.PushTestPkgToRegistry()
assert.Equal(t, err, nil)
// Push the test package to the local OCI registry
err = mock.PushTestPkgToRegistry()
assert.Equal(t, err, nil)

// Initialize settings for use with the ModChecker
settings, err := getTestSettings()
assert.Equal(t, err, nil)
// Initialize settings for use with the ModChecker
settings, err := getTestSettings()
assert.Equal(t, err, nil)

// Initialize the ModChecker with required checkers
ModChecker := NewModChecker(WithCheckers(NewIdentChecker(), NewVersionChecker(), NewSumChecker(WithSettings(*settings))))
// Initialize the ModChecker with required checkers
ModChecker := NewModChecker(WithCheckers(NewIdentChecker(), NewVersionChecker(), NewSumChecker(WithSettings(*settings))))

deps1 := orderedmap.NewOrderedMap[string, pkg.Dependency]()
deps1.Set("kcl1", pkg.Dependency{
Name: "test_data",
FullName: "test_data",
Version: "0.0.1",
Sum: "RpZZIvrXwfn5dpt6LqBR8+FlPE9Y+BEou47L3qaCCqk=",
Source: downloader.Source{
Oci: &downloader.Oci{
Reg: "localhost:5001",
Repo: "test",
Tag: "0.0.1",
deps1 := orderedmap.NewOrderedMap[string, pkg.Dependency]()
deps1.Set("kcl1", pkg.Dependency{
Name: "test_data",
FullName: "test_data",
Version: "0.0.1",
Sum: "RpZZIvrXwfn5dpt6LqBR8+FlPE9Y+BEou47L3qaCCqk=",
Source: downloader.Source{
Oci: &downloader.Oci{
Reg: "localhost:5001",
Repo: "test",
Tag: "0.0.1",
},
},
},
})
})

deps2 := orderedmap.NewOrderedMap[string, pkg.Dependency]()
deps2.Set("kcl1", pkg.Dependency{
Name: "test_data",
FullName: "test_data",
Version: "0.0.1",
Sum: "Invalid-sum",
Source: downloader.Source{
Oci: &downloader.Oci{
Reg: "localhost:5001",
Repo: "test",
Tag: "0.0.1",
deps2 := orderedmap.NewOrderedMap[string, pkg.Dependency]()
deps2.Set("kcl1", pkg.Dependency{
Name: "test_data",
FullName: "test_data",
Version: "0.0.1",
Sum: "Invalid-sum",
Source: downloader.Source{
Oci: &downloader.Oci{
Reg: "localhost:5001",
Repo: "test",
Tag: "0.0.1",
},
},
},
})
})

tests := []struct {
name string
KclPkg pkg.KclPkg
wantErr bool
}{
{
name: "valid kcl package - with sum check",
KclPkg: pkg.KclPkg{
ModFile: pkg.ModFile{
Pkg: pkg.Package{
Name: "testmod",
Version: "0.0.1",
tests := []struct {
name string
KclPkg pkg.KclPkg
wantErr bool
}{
{
name: "valid kcl package - with sum check",
KclPkg: pkg.KclPkg{
ModFile: pkg.ModFile{
Pkg: pkg.Package{
Name: "testmod",
Version: "0.0.1",
},
HomePath: "path/to/modfile",
},
HomePath: "path/to/modfile",
},
HomePath: "path/to/kcl/pkg",
Dependencies: pkg.Dependencies{
Deps: deps1,
HomePath: "path/to/kcl/pkg",
Dependencies: pkg.Dependencies{
Deps: deps1,
},
NoSumCheck: false,
},
NoSumCheck: false,
wantErr: false,
},
wantErr: false,
},
{
name: "valid kcl package - with no sum check enabled",
KclPkg: pkg.KclPkg{
ModFile: pkg.ModFile{
Pkg: pkg.Package{
Name: "testmod",
Version: "0.0.1",
{
name: "valid kcl package - with no sum check enabled",
KclPkg: pkg.KclPkg{
ModFile: pkg.ModFile{
Pkg: pkg.Package{
Name: "testmod",
Version: "0.0.1",
},
HomePath: "path/to/modfile",
},
HomePath: "path/to/modfile",
},
HomePath: "path/to/kcl/pkg",
Dependencies: pkg.Dependencies{
Deps: deps2,
HomePath: "path/to/kcl/pkg",
Dependencies: pkg.Dependencies{
Deps: deps2,
},
NoSumCheck: true,
},
NoSumCheck: true,
wantErr: false,
},
wantErr: false,
},
{
name: "Invalid kcl package - with no sum check disabled - checksum mismatches",
KclPkg: pkg.KclPkg{
ModFile: pkg.ModFile{
Pkg: pkg.Package{
Name: "testmod",
Version: "0.0.1",
{
name: "Invalid kcl package - with no sum check disabled - checksum mismatches",
KclPkg: pkg.KclPkg{
ModFile: pkg.ModFile{
Pkg: pkg.Package{
Name: "testmod",
Version: "0.0.1",
},
HomePath: "path/to/modfile",
},
HomePath: "path/to/modfile",
},
HomePath: "path/to/kcl/pkg",
Dependencies: pkg.Dependencies{
Deps: deps2,
HomePath: "path/to/kcl/pkg",
Dependencies: pkg.Dependencies{
Deps: deps2,
},
NoSumCheck: false,
},
NoSumCheck: false,
wantErr: true,
},
wantErr: true,
},
}
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotErr := ModChecker.Check(tt.KclPkg)
if (gotErr != nil) != tt.wantErr {
t.Errorf("ModChecker.Check(%v) = %v, want error %v", tt.KclPkg, gotErr, tt.wantErr)
}
})
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotErr := ModChecker.Check(tt.KclPkg)
if (gotErr != nil) != tt.wantErr {
t.Errorf("ModChecker.Check(%v) = %v, want error %v", tt.KclPkg, gotErr, tt.wantErr)
}
})
}

// Clean the environment after all tests have been run
err = mock.CleanTestEnv()
assert.Equal(t, err, nil)
}

// Clean the environment after all tests have been run
err = mock.CleanTestEnv()
assert.Equal(t, err, nil)
test.RunTestWithGlobalLock(t, "TestModCheckerCheck_WithTrustedSum", testFunc)
}
4 changes: 2 additions & 2 deletions pkg/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2255,12 +2255,12 @@ func testPushWithInsecureSkipTLSverify(t *testing.T) {
kpmcli, err := NewKpmClient()
assert.Equal(t, err, nil)

_ = kpmcli.PushToOci("test", ociOpts)
_ = kpmcli.pushToOci("test", ociOpts)

assert.Equal(t, buf.String(), "")

kpmcli.SetInsecureSkipTLSverify(true)
_ = kpmcli.PushToOci("test", ociOpts)
_ = kpmcli.pushToOci("test", ociOpts)

assert.Equal(t, buf.String(), "Called Success\n")
}
Expand Down
122 changes: 122 additions & 0 deletions pkg/client/issues_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ import (
"github.com/stretchr/testify/assert"
"kcl-lang.io/kpm/pkg/downloader"
"kcl-lang.io/kpm/pkg/features"
"kcl-lang.io/kpm/pkg/mock"
"kcl-lang.io/kpm/pkg/opt"
pkg "kcl-lang.io/kpm/pkg/package"
"kcl-lang.io/kpm/pkg/reporter"
"kcl-lang.io/kpm/pkg/utils"
)

Expand Down Expand Up @@ -430,3 +433,122 @@ func TestKpmIssue226(t *testing.T) {
RunTestWithGlobalLockAndKpmCli(t, []TestSuite{{Name: "update_with_git_commit", TestFunc: test_update_with_git_commit}})
RunTestWithGlobalLockAndKpmCli(t, []TestSuite{{Name: "update_with_git_commit_invalid", TestFunc: test_update_with_git_commit_invalid}})
}

func TestKclIssue1768(t *testing.T) {
testPath := "github.com/kcl-lang/kcl/issues/1768"
test_push_with_tag := func(t *testing.T, kpmcli *KpmClient) {

err := mock.StartDockerRegistry()
if err != nil {
t.Errorf("Error starting docker registry: %v", err)
}

defer func() {
err = mock.CleanTestEnv()
if err != nil {
t.Errorf("Error stopping docker registry: %v", err)
}
}()

kpmcli.SetInsecureSkipTLSverify(true)
err = kpmcli.LoginOci("localhost:5001", "test", "1234")
if err != nil {
t.Errorf("Error logging in to docker registry: %v", err)
}

rootPath := getTestDir("issues")
pushedModPath := filepath.Join(rootPath, testPath, "pushed_mod")

modPath := filepath.Join(rootPath, testPath, "depends_on_pushed_mod")
modFileBk := filepath.Join(modPath, "kcl.mod.bk")
LockFileBk := filepath.Join(modPath, "kcl.mod.lock.bk")
modFile := filepath.Join(modPath, "kcl.mod")
LockFile := filepath.Join(modPath, "kcl.mod.lock")
modFileExpect := filepath.Join(modPath, "kcl.mod.expect")
LockFileExpect := filepath.Join(modPath, "kcl.mod.lock.expect")

defer func() {
_ = os.RemoveAll(modFile)
_ = os.RemoveAll(LockFile)
}()

err = copy.Copy(modFileBk, modFile)
if err != nil {
t.Fatal(err)
}
err = copy.Copy(LockFileBk, LockFile)
if err != nil {
t.Fatal(err)
}

var buf bytes.Buffer
kpmcli.SetLogWriter(&buf)

err = kpmcli.Push(
WithPushModPath(pushedModPath),
WithPushOciOptions(&opt.OciOptions{
Reg: "localhost:5001",
Repo: "test",
Ref: "oci_pushed_mod",
Tag: "v9.9.9",
}),
)

if err != (*reporter.KpmEvent)(nil) {
t.Errorf("Error pushing kcl package: %v", err)
}

assert.Contains(t, buf.String(), "package 'pushed_mod' will be pushed")
assert.Contains(t, buf.String(), "pushed [registry] localhost:5001/test/oci_pushed_mod")
assert.Contains(t, buf.String(), "digest: sha256:")

kmod, err := pkg.LoadKclPkgWithOpts(
pkg.WithPath(modPath),
)

err = kpmcli.Add(
WithAddKclPkg(kmod),
WithAddSource(
&downloader.Source{
Oci: &downloader.Oci{
Reg: "localhost:5001",
Repo: "test/oci_pushed_mod",
Tag: "v9.9.9",
}},
),
WithAddModSpec(
&downloader.ModSpec{
Name: "pushed_mod",
Version: "0.0.1",
},
),
)

if err != nil {
t.Errorf("Error adding dependency: %v", err)
}

modFileContent, err := os.ReadFile(modFile)
if err != nil {
t.Fatal(err)
}
lockFileContent, err := os.ReadFile(LockFile)
if err != nil {
t.Fatal(err)
}

modFileExpectContent, err := os.ReadFile(modFileExpect)
if err != nil {
t.Fatal(err)
}

lockFileExpectContent, err := os.ReadFile(LockFileExpect)
if err != nil {
t.Fatal(err)
}

assert.Equal(t, utils.RmNewline(string(modFileContent)), utils.RmNewline(string(modFileExpectContent)))
assert.Equal(t, utils.RmNewline(string(lockFileContent)), utils.RmNewline(string(lockFileExpectContent)))
}
RunTestWithGlobalLockAndKpmCli(t, []TestSuite{{Name: "test_push_with_tag", TestFunc: test_push_with_tag}})
}
Loading
Loading