Skip to content

Commit

Permalink
fix: move some test into separate kpm home
Browse files Browse the repository at this point in the history
Signed-off-by: zongz <[email protected]>
  • Loading branch information
zong-zhe committed Nov 13, 2024
1 parent 446bc96 commit fd19baa
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 85 deletions.
203 changes: 127 additions & 76 deletions pkg/client/add_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,55 +24,74 @@ func TestAddWithModSpec(t *testing.T) {
name: "TestAddOciWithModSpec",
pkgSubPath: "oci",
sourceUrl: "oci://ghcr.io/kcl-lang/helloworld?tag=0.1.4&mod=subhelloworld:0.0.1",
msg: "downloading 'kcl-lang/helloworld:0.1.4' from 'ghcr.io/kcl-lang/helloworld:0.1.4'adding dependency 'subhelloworld'add dependency 'subhelloworld:0.0.1' successfully",
msg: "downloading 'kcl-lang/helloworld:0.1.4' from 'ghcr.io/kcl-lang/helloworld:0.1.4'" +
"adding dependency 'subhelloworld'" +
"add dependency 'subhelloworld:0.0.1' successfully",
},
{
name: "TestAddGitWithModSpec",
pkgSubPath: "git",
sourceUrl: "git://github.com/kcl-lang/flask-demo-kcl-manifests.git?commit=8308200&mod=cc:0.0.1",
msg: "cloning 'https://github.com/kcl-lang/flask-demo-kcl-manifests.git' with commit '8308200'adding dependency 'cc'add dependency 'cc:0.0.1' successfully",
msg: "cloning 'https://github.com/kcl-lang/flask-demo-kcl-manifests.git' with commit '8308200'" +
"adding dependency 'cc'" +
"add dependency 'cc:0.0.1' successfully",
},
{
name: "TestAddGitWithModSpec",
pkgSubPath: "git_mod_0",
sourceUrl: "git://github.com/kcl-lang/flask-demo-kcl-manifests.git?commit=8308200&mod=cc",
msg: "cloning 'https://github.com/kcl-lang/flask-demo-kcl-manifests.git' with commit '8308200'adding dependency 'cc'add dependency 'cc:0.0.1' successfully",
msg: "cloning 'https://github.com/kcl-lang/flask-demo-kcl-manifests.git' with commit '8308200'" +
"adding dependency 'cc'" +
"add dependency 'cc:0.0.1' successfully",
},
{
name: "TestAddGitWithoutModFileWithModSpec",
pkgSubPath: "git_mod_1",
sourceUrl: "git://github.com/kcl-lang/flask-demo-kcl-manifests.git?commit=5ab0fff&mod=cc",
msg: "cloning 'https://github.com/kcl-lang/flask-demo-kcl-manifests.git' with commit '5ab0fff'adding dependency 'cc'add dependency 'cc:0.0.1' successfully",
msg: "cloning 'https://github.com/kcl-lang/flask-demo-kcl-manifests.git' with commit '5ab0fff'" +
"adding dependency 'cc'" +
"add dependency 'cc:0.0.1' successfully",
},
{
name: "TestAddLocalWithModSpec",
pkgSubPath: filepath.Join("local", "pkg"),
sourceUrl: "../dep?mod=sub:0.0.1",
msg: "adding dependency 'sub'add dependency 'sub:0.0.1' successfully",
msg: "adding dependency 'sub'" +
"add dependency 'sub:0.0.1' successfully",
},
{
name: "TestAddOciWithEmptyVersion",
pkgSubPath: "empty_version",
sourceUrl: "oci://ghcr.io/kcl-lang/helloworld?tag=0.1.4&mod=subhelloworld",
msg: "downloading 'kcl-lang/helloworld:0.1.4' from 'ghcr.io/kcl-lang/helloworld:0.1.4'adding dependency 'subhelloworld'add dependency 'subhelloworld:0.0.1' successfully",
msg: "downloading 'kcl-lang/helloworld:0.1.4' from 'ghcr.io/kcl-lang/helloworld:0.1.4'" +
"adding dependency 'subhelloworld'" +
"add dependency 'subhelloworld:0.0.1' successfully",
},
{
name: "TestAddOciWithNoSpec",
pkgSubPath: "no_spec",
sourceUrl: "oci://ghcr.io/kcl-lang/helloworld?tag=0.1.4",
msg: "downloading 'kcl-lang/helloworld:0.1.4' from 'ghcr.io/kcl-lang/helloworld:0.1.4'adding dependency 'helloworld'add dependency 'helloworld:0.1.4' successfully",
msg: "downloading 'kcl-lang/helloworld:0.1.4' from 'ghcr.io/kcl-lang/helloworld:0.1.4'" +
"adding dependency 'helloworld'" +
"add dependency 'helloworld:0.1.4' successfully",
},
{
name: "TestAddOciWithNoTag",
pkgSubPath: "no_oci_ref",
sourceUrl: "oci://ghcr.io/kcl-lang/helloworld",
msg: "the lastest version '0.1.4' will be downloadeddownloading 'kcl-lang/helloworld:0.1.4' from 'ghcr.io/kcl-lang/helloworld:0.1.4'adding dependency 'helloworld'add dependency 'helloworld:0.1.4' successfully",
msg: "the lastest version '0.1.4' will be downloaded" +
"downloading 'kcl-lang/helloworld:0.1.4' from 'ghcr.io/kcl-lang/helloworld:0.1.4'" +
"adding dependency 'helloworld'" +
"add dependency 'helloworld:0.1.4' successfully",
},
{
name: "TestAddGitWithNoTag",
pkgSubPath: "no_git_ref",
sourceUrl: "git://github.com/kcl-lang/flask-demo-kcl-manifests.git",
msg: "the lastest version 'ade147b' will be downloadedcloning 'https://github.com/kcl-lang/flask-demo-kcl-manifests.git' with commit 'ade147b'adding dependency 'flask_manifests'add dependency 'flask_manifests:0.0.1' successfully",
msg: "the lastest version 'ade147b' will be downloaded" +
"cloning 'https://github.com/kcl-lang/flask-demo-kcl-manifests.git' with commit 'ade147b'" +
"adding dependency 'flask_manifests'" +
"add dependency 'flask_manifests:0.0.1' successfully",
},
}

Expand Down Expand Up @@ -158,7 +177,7 @@ func TestAddWithModSpec(t *testing.T) {
}
}

func testAddRenameWithModSpec(t *testing.T) {
func TestAddRenameWithModSpec(t *testing.T) {
testDir := getTestDir("add_with_mod_spec")
pkgPath := filepath.Join(testDir, "rename_spec_only")

Expand Down Expand Up @@ -191,33 +210,39 @@ func testAddRenameWithModSpec(t *testing.T) {
}
}()

kpmcli, err := NewKpmClient()
if err != nil {
t.Fatal(err)
}

kpkg, err := pkg.LoadKclPkgWithOpts(
pkg.WithPath(pkgPath),
pkg.WithSettings(kpmcli.GetSettings()),
)
testFunc := func(t *testing.T, kpmcli *KpmClient) {
var buf bytes.Buffer
kpmcli.SetLogWriter(&buf)
kpkg, err := pkg.LoadKclPkgWithOpts(
pkg.WithPath(pkgPath),
pkg.WithSettings(kpmcli.GetSettings()),
)
if err != nil {
t.Fatal(err)
}

if err != nil {
t.Fatal(err)
}
err = kpmcli.Add(
WithAddKclPkg(kpkg),
WithAddModSpec(&downloader.ModSpec{
Name: "helloworld",
Version: "0.1.2",
}),
WithAlias("newpkg"),
)

err = kpmcli.Add(
WithAddKclPkg(kpkg),
WithAddModSpec(&downloader.ModSpec{
Name: "helloworld",
Version: "0.1.2",
}),
WithAlias("newpkg"),
)
if err != nil {
t.Fatal(err)
}

if err != nil {
t.Fatal(err)
assert.Equal(t, utils.RmNewline(
"downloading 'kcl-lang/helloworld:0.1.2' from 'ghcr.io/kcl-lang/helloworld:0.1.2'"+
"adding dependency 'helloworld'"+
"add dependency 'helloworld:0.1.2' successfully",
), utils.RmNewline(buf.String()))
}

RunTestWithGlobalLockAndKpmCli(t, "TestAddRenameWithModSpec", testFunc)

expectedMod, err := os.ReadFile(modExpect)
if err != nil {
t.Fatal(err)
Expand All @@ -241,23 +266,34 @@ func testAddRenameWithModSpec(t *testing.T) {
assert.Equal(t, utils.RmNewline(string(expectedLock)), utils.RmNewline(string(gotLock)))
}

func testAddWithOnlyModSpec(t *testing.T) {
func TestAddWithOnlyModSpec(t *testing.T) {
testCases := []struct {
name string
testDir string
pkgSubDir string
msg string
modSpec *downloader.ModSpec
}{
{
name: "TestAddWithOnlyModSpec",
testDir: "add_with_mod_spec",
pkgSubDir: "spec_only",
msg: "downloading 'kcl-lang/helloworld:0.1.4' from 'ghcr.io/kcl-lang/helloworld:0.1.4'" +
"adding dependency 'helloworld'" +
"add dependency 'helloworld:0.1.4' successfully",
modSpec: &downloader.ModSpec{
Name: "helloworld",
Version: "0.1.4",
},
},
{
name: "TestAddWithOnlyModSpecButNoVersion",
testDir: "add_with_mod_spec",
pkgSubDir: "spec_only_no_ver",
msg: "the lastest version '0.1.4' will be downloaded" +
"downloading 'kcl-lang/helloworld:0.1.4' from 'ghcr.io/kcl-lang/helloworld:0.1.4'" +
"adding dependency 'helloworld'" +
"add dependency 'helloworld:0.1.4' successfully",
modSpec: &downloader.ModSpec{
Name: "helloworld",
},
Expand Down Expand Up @@ -297,29 +333,33 @@ func testAddWithOnlyModSpec(t *testing.T) {
}
}()

kpmcli, err := NewKpmClient()
if err != nil {
t.Fatal(err)
}
testFunc := func(t *testing.T, kpmcli *KpmClient) {
var buf bytes.Buffer
kpmcli.SetLogWriter(&buf)

kpkg, err := pkg.LoadKclPkgWithOpts(
pkg.WithPath(pkgPath),
pkg.WithSettings(kpmcli.GetSettings()),
)
kpkg, err := pkg.LoadKclPkgWithOpts(
pkg.WithPath(pkgPath),
pkg.WithSettings(kpmcli.GetSettings()),
)

if err != nil {
t.Fatal(err)
}
if err != nil {
t.Fatal(err)
}

err = kpmcli.Add(
WithAddKclPkg(kpkg),
WithAddModSpec(tc.modSpec),
)
err = kpmcli.Add(
WithAddKclPkg(kpkg),
WithAddModSpec(tc.modSpec),
)

if err != nil {
t.Fatal(err)
if err != nil {
t.Fatal(err)
}

assert.Equal(t, utils.RmNewline(tc.msg), utils.RmNewline(buf.String()))
}

RunTestWithGlobalLockAndKpmCli(t, tc.name, testFunc)

expectedMod, err := os.ReadFile(modExpect)
if err != nil {
t.Fatal(err)
Expand All @@ -344,7 +384,7 @@ func testAddWithOnlyModSpec(t *testing.T) {
}
}

func testAddRenameWithNoSpec(t *testing.T) {
func TestAddRenameWithNoSpec(t *testing.T) {
testDir := getTestDir("add_with_mod_spec")
pkgPath := filepath.Join(testDir, "rename_no_spec")

Expand Down Expand Up @@ -377,37 +417,48 @@ func testAddRenameWithNoSpec(t *testing.T) {
}
}()

kpmcli, err := NewKpmClient()
if err != nil {
t.Fatal(err)
}
testFunc := func(t *testing.T, kpmcli *KpmClient) {

kpkg, err := pkg.LoadKclPkgWithOpts(
pkg.WithPath(pkgPath),
pkg.WithSettings(kpmcli.GetSettings()),
)
var buf bytes.Buffer
kpmcli.SetLogWriter(&buf)

if err != nil {
t.Fatal(err)
}
kpkg, err := pkg.LoadKclPkgWithOpts(
pkg.WithPath(pkgPath),
pkg.WithSettings(kpmcli.GetSettings()),
)

err = kpmcli.Add(
WithAddKclPkg(kpkg),
WithAddSource(&downloader.Source{
ModSpec: &downloader.ModSpec{
Alias: "newpkg",
},
Oci: &downloader.Oci{
Reg: "ghcr.io",
Repo: "kcl-lang/helloworld",
},
}),
)
if err != nil {
t.Fatal(err)
}

if err != nil {
t.Fatal(err)
err = kpmcli.Add(
WithAddKclPkg(kpkg),
WithAddSource(&downloader.Source{
ModSpec: &downloader.ModSpec{
Alias: "newpkg",
},
Oci: &downloader.Oci{
Reg: "ghcr.io",
Repo: "kcl-lang/helloworld",
},
}),
)

assert.Equal(t,
"the lastest version '0.1.4' will be downloaded"+
"downloading 'kcl-lang/helloworld:0.1.4' from 'ghcr.io/kcl-lang/helloworld:0.1.4'"+
"adding dependency 'helloworld'"+
"add dependency 'helloworld:0.1.4' successfully",
utils.RmNewline(buf.String()),
)

if err != nil {
t.Fatal(err)
}
}

RunTestWithGlobalLockAndKpmCli(t, "TestAddRenameWithNoSpec", testFunc)

expectedMod, err := os.ReadFile(modExpect)
if err != nil {
t.Fatal(err)
Expand Down
9 changes: 0 additions & 9 deletions pkg/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ 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 @@ -73,17 +72,9 @@ func TestWithGlobalLock(t *testing.T) {
test.RunTestWithGlobalLock(t, "TestPull", testPull)
test.RunTestWithGlobalLock(t, "TestPullWithInsecureSkipTLSverify", testPullWithInsecureSkipTLSverify)
test.RunTestWithGlobalLock(t, "TestPullWithModSpec", testPullWithModSpec)
test.RunTestWithGlobalLock(t, "testAddWithOnlyModSpec", testAddWithOnlyModSpec)
test.RunTestWithGlobalLock(t, "testAddRenameWithModSpec", testAddRenameWithModSpec)
test.RunTestWithGlobalLock(t, "testAddRenameWithNoSpec", testAddRenameWithNoSpec)
test.RunTestWithGlobalLock(t, "testPullWithOnlySpec", testPullWithOnlySpec)
test.RunTestWithGlobalLock(t, "TestRunWithModSpecVersion", testRunWithModSpecVersion)
test.RunTestWithGlobalLock(t, "TestGraph", testGraph)

features.Enable(features.SupportNewStorage)
test.RunTestWithGlobalLock(t, "testAddWithOnlyModSpec", testAddWithOnlyModSpec)
test.RunTestWithGlobalLock(t, "testAddRenameWithModSpec", testAddRenameWithModSpec)
test.RunTestWithGlobalLock(t, "testAddRenameWithNoSpec", testAddRenameWithNoSpec)
}

// TestDownloadOci test download from oci registry.
Expand Down

0 comments on commit fd19baa

Please sign in to comment.