Skip to content

Commit

Permalink
fix: fix test case
Browse files Browse the repository at this point in the history
Signed-off-by: zongz <zongzhe1024@163.com>
  • Loading branch information
zong-zhe committed Nov 13, 2024
1 parent fd2477b commit 912889b
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 105 deletions.
206 changes: 103 additions & 103 deletions pkg/client/add_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ 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: "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",
Expand All @@ -42,7 +42,7 @@ func testAddWithModSpec(t *testing.T) {
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' successfull",
msg: "adding dependency 'cc'add dependency 'cc:0.0.1' successfully",
},
{
name: "TestAddLocalWithModSpec",
Expand Down Expand Up @@ -247,104 +247,104 @@ func testAddRenameWithModSpec(t *testing.T) {
}

func testAddWithOnlyModSpec(t *testing.T) {
testCases := []struct {
testDir string
pkgSubDir string
modSpec *downloader.ModSpec
}{
{
testDir: "add_with_mod_spec",
pkgSubDir: "spec_only",
modSpec: &downloader.ModSpec{
Name: "helloworld",
Version: "0.1.4",
},
},
{
testDir: "add_with_mod_spec",
pkgSubDir: "spec_only_no_ver",
modSpec: &downloader.ModSpec{
Name: "helloworld",
},
},
}

for _, tc := range testCases {
testDir := getTestDir(tc.testDir)
pkgPath := filepath.Join(testDir, tc.pkgSubDir)

modbkPath := filepath.Join(pkgPath, "kcl.mod.bk")
modPath := filepath.Join(pkgPath, "kcl.mod")
modExpect := filepath.Join(pkgPath, "kcl.mod.expect")
lockbkPath := filepath.Join(pkgPath, "kcl.mod.lock.bk")
lockPath := filepath.Join(pkgPath, "kcl.mod.lock")
lockExpect := filepath.Join(pkgPath, "kcl.mod.lock.expect")

err := copy.Copy(modbkPath, modPath)
if err != nil {
t.Fatal(err)
}

err = copy.Copy(lockbkPath, lockPath)
if err != nil {
t.Fatal(err)
}

defer func() {
// remove the copied files
err := os.RemoveAll(modPath)
if err != nil {
t.Fatal(err)
}
err = os.RemoveAll(lockPath)
if err != nil {
t.Fatal(err)
}
}()

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

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

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

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

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

expectedMod, err := os.ReadFile(modExpect)
if err != nil {
t.Fatal(err)
}
gotMod, err := os.ReadFile(modPath)
if err != nil {
t.Fatal(err)
}

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

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

assert.Equal(t, utils.RmNewline(string(expectedMod)), utils.RmNewline(string(gotMod)))
assert.Equal(t, utils.RmNewline(string(expectedLock)), utils.RmNewline(string(gotLock)))
}
}
testCases := []struct {
testDir string
pkgSubDir string
modSpec *downloader.ModSpec
}{
{
testDir: "add_with_mod_spec",
pkgSubDir: "spec_only",
modSpec: &downloader.ModSpec{
Name: "helloworld",
Version: "0.1.4",
},
},
{
testDir: "add_with_mod_spec",
pkgSubDir: "spec_only_no_ver",
modSpec: &downloader.ModSpec{
Name: "helloworld",
},
},
}

for _, tc := range testCases {
testDir := getTestDir(tc.testDir)
pkgPath := filepath.Join(testDir, tc.pkgSubDir)

modbkPath := filepath.Join(pkgPath, "kcl.mod.bk")
modPath := filepath.Join(pkgPath, "kcl.mod")
modExpect := filepath.Join(pkgPath, "kcl.mod.expect")
lockbkPath := filepath.Join(pkgPath, "kcl.mod.lock.bk")
lockPath := filepath.Join(pkgPath, "kcl.mod.lock")
lockExpect := filepath.Join(pkgPath, "kcl.mod.lock.expect")

err := copy.Copy(modbkPath, modPath)
if err != nil {
t.Fatal(err)
}

err = copy.Copy(lockbkPath, lockPath)
if err != nil {
t.Fatal(err)
}

defer func() {
// remove the copied files
err := os.RemoveAll(modPath)
if err != nil {
t.Fatal(err)
}
err = os.RemoveAll(lockPath)
if err != nil {
t.Fatal(err)
}
}()

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

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

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

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

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

expectedMod, err := os.ReadFile(modExpect)
if err != nil {
t.Fatal(err)
}
gotMod, err := os.ReadFile(modPath)
if err != nil {
t.Fatal(err)
}

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

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

assert.Equal(t, utils.RmNewline(string(expectedMod)), utils.RmNewline(string(gotMod)))
assert.Equal(t, utils.RmNewline(string(expectedLock)), utils.RmNewline(string(gotLock)))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ edition = "v0.10.0"
version = "0.0.1"

[dependencies]
cc = { git = "https://github.com/kcl-lang/flask-demo-kcl-manifests.git", commit = "8308200", version = "0.0.1" }
cc = { git = "https://github.com/kcl-lang/flask-demo-kcl-manifests.git", commit = "5ab0fff", version = "0.0.1" }
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
full_name = "cc_0.0.1"
version = "0.0.1"
url = "https://github.com/kcl-lang/flask-demo-kcl-manifests.git"
commit = "8308200"
commit = "5ab0fff"

0 comments on commit 912889b

Please sign in to comment.