From 8c4937861f1e85e4e335341059a61a69bd2e1dfb Mon Sep 17 00:00:00 2001 From: Akash Kumar Date: Mon, 19 Feb 2024 11:31:08 +0530 Subject: [PATCH] add unit test Signed-off-by: Akash Kumar --- pkg/client/client_test.go | 27 ++++++++++++++++++- .../test_cyclic_dependency/aaa/kcl.mod | 7 +++++ .../test_cyclic_dependency/aaa/kcl.mod.lock | 6 +++++ .../test_cyclic_dependency/bbb/kcl.mod | 7 +++++ .../test_cyclic_dependency/bbb/kcl.mod.lock | 8 ++++++ 5 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 pkg/client/test_data/test_cyclic_dependency/aaa/kcl.mod create mode 100644 pkg/client/test_data/test_cyclic_dependency/aaa/kcl.mod.lock create mode 100644 pkg/client/test_data/test_cyclic_dependency/bbb/kcl.mod create mode 100644 pkg/client/test_data/test_cyclic_dependency/bbb/kcl.mod.lock diff --git a/pkg/client/client_test.go b/pkg/client/client_test.go index f0de55fc..2775390d 100644 --- a/pkg/client/client_test.go +++ b/pkg/client/client_test.go @@ -19,6 +19,7 @@ import ( "kcl-lang.io/kpm/pkg/git" "kcl-lang.io/kpm/pkg/opt" pkg "kcl-lang.io/kpm/pkg/package" + "kcl-lang.io/kpm/pkg/reporter" "kcl-lang.io/kpm/pkg/runner" "kcl-lang.io/kpm/pkg/utils" ) @@ -158,7 +159,31 @@ func TestDependencyGraph(t *testing.T) { } func TestCyclicDependency(t *testing.T) { - + testDir := getTestDir("test_cyclic_dependency") + assert.Equal(t, utils.DirExists(filepath.Join(testDir, "aaa")), true) + assert.Equal(t, utils.DirExists(filepath.Join(testDir, "aaa/kcl.mod")), true) + assert.Equal(t, utils.DirExists(filepath.Join(testDir, "bbb")), true) + assert.Equal(t, utils.DirExists(filepath.Join(testDir, "bbb/kcl.mod")), true) + + pkg_path := filepath.Join(testDir, "aaa") + + kpmcli, err := NewKpmClient() + assert.Equal(t, err, nil) + kclPkg, err := kpmcli.LoadPkgFromPath(pkg_path) + assert.Equal(t, err, nil) + + currentDir, err := os.Getwd() + assert.Equal(t, err, nil) + err = os.Chdir(pkg_path) + assert.Equal(t, err, nil) + + _, _, err = kpmcli.InitGraphAndDownloadDeps(kclPkg) + assert.Equal(t, err, reporter.NewErrorEvent( + reporter.CircularDependencyExist, nil, "adding dependencies results in a cycle", + )) + + err = os.Chdir(currentDir) + assert.Equal(t, err, nil) } func TestInitEmptyPkg(t *testing.T) { diff --git a/pkg/client/test_data/test_cyclic_dependency/aaa/kcl.mod b/pkg/client/test_data/test_cyclic_dependency/aaa/kcl.mod new file mode 100644 index 00000000..79bceba5 --- /dev/null +++ b/pkg/client/test_data/test_cyclic_dependency/aaa/kcl.mod @@ -0,0 +1,7 @@ +[package] +name = "aaa" +edition = "0.0.1" +version = "0.0.1" + +[dependencies] +bbb = { path = "../bbb" } diff --git a/pkg/client/test_data/test_cyclic_dependency/aaa/kcl.mod.lock b/pkg/client/test_data/test_cyclic_dependency/aaa/kcl.mod.lock new file mode 100644 index 00000000..4eb93c26 --- /dev/null +++ b/pkg/client/test_data/test_cyclic_dependency/aaa/kcl.mod.lock @@ -0,0 +1,6 @@ +[dependencies] + [dependencies.bbb] + name = "bbb" + full_name = "bbb_" + sum = "Cgx5yepaNrS3/p4fK04RQWf1dEpG3j49ntFqzrJJsxU=" + path = "../bbb" diff --git a/pkg/client/test_data/test_cyclic_dependency/bbb/kcl.mod b/pkg/client/test_data/test_cyclic_dependency/bbb/kcl.mod new file mode 100644 index 00000000..a70b8351 --- /dev/null +++ b/pkg/client/test_data/test_cyclic_dependency/bbb/kcl.mod @@ -0,0 +1,7 @@ +[package] +name = "bbb" +edition = "0.0.1" +version = "0.0.1" + +[dependencies] +aaa = { path = "../aaa" } diff --git a/pkg/client/test_data/test_cyclic_dependency/bbb/kcl.mod.lock b/pkg/client/test_data/test_cyclic_dependency/bbb/kcl.mod.lock new file mode 100644 index 00000000..e3672021 --- /dev/null +++ b/pkg/client/test_data/test_cyclic_dependency/bbb/kcl.mod.lock @@ -0,0 +1,8 @@ +[dependencies] + [dependencies.aaa] + name = "aaa" + full_name = "aaa_0.0.1" + version = "0.0.1" + sum = "hgAM5wYFzXo83S+OiT0LaBDnHU7Q9vqq3BPXlTDr8TY=" + path = "../aaa" +