Skip to content

Commit

Permalink
fix: fix rename failure when adding dependeny with only source
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 11, 2024
1 parent 5de98e2 commit 806307c
Show file tree
Hide file tree
Showing 8 changed files with 211 additions and 101 deletions.
1 change: 1 addition & 0 deletions pkg/client/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ func (c *KpmClient) Add(options ...AddOption) error {
modSpec = &downloader.ModSpec{
Name: depPkg.ModFile.Pkg.Name,
Version: depPkg.ModFile.Pkg.Version,
Alias: depSource.ModSpec.Alias,
}
depSource.ModSpec = modSpec
}
Expand Down
289 changes: 188 additions & 101 deletions pkg/client/add_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,104 +221,191 @@ 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)))
}
}

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

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),
WithAddSource(&downloader.Source{
ModSpec: &downloader.ModSpec{
Alias: "newpkg",
},
Oci: &downloader.Oci{
Reg: "ghcr.io",
Repo: "kcl-lang/helloworld",
},
}),
)

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)))
}
2 changes: 2 additions & 0 deletions pkg/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,13 @@ func TestWithGlobalLock(t *testing.T) {
test.RunTestWithGlobalLock(t, "TestPullWithModSpec", testPullWithModSpec)
test.RunTestWithGlobalLock(t, "testAddWithOnlyModSpec", testAddWithOnlyModSpec)
test.RunTestWithGlobalLock(t, "testAddRenameWithModSpec", testAddRenameWithModSpec)
test.RunTestWithGlobalLock(t, "testAddRenameWithNoSpec", testAddRenameWithNoSpec)

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

// TestDownloadOci test download from oci registry.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[package]
name = "rename"
edition = "v0.10.0"
version = "0.0.1"
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "rename"
edition = "v0.10.0"
version = "0.0.1"

[dependencies]
newpkg = { oci = "oci://ghcr.io/kcl-lang/helloworld", tag = "0.1.4", package = "helloworld", version = "0.1.4" }
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[dependencies]
[dependencies.newpkg]
name = "newpkg"
full_name = "helloworld_0.1.4"
version = "0.1.4"
reg = "ghcr.io"
repo = "kcl-lang/helloworld"
oci_tag = "0.1.4"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The_first_kcl_program = 'Hello World!'

0 comments on commit 806307c

Please sign in to comment.