-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: zongzhe <[email protected]>
- Loading branch information
Showing
11 changed files
with
187 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
[package] | ||
name = "test_visit_dir" | ||
edition = "v0.10.0" | ||
version = "0.0.1" |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
The_first_kcl_program = 'Hello World!' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
[package] | ||
name = "test_visit_tar" | ||
edition = "v0.10.0" | ||
version = "0.0.1" |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
The_first_kcl_program = 'Hello World!' |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package visitor | ||
|
||
import ( | ||
"bytes" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"gotest.tools/v3/assert" | ||
"kcl-lang.io/kpm/pkg/downloader" | ||
pkg "kcl-lang.io/kpm/pkg/package" | ||
"kcl-lang.io/kpm/pkg/settings" | ||
) | ||
|
||
const testDataDir = "test_data" | ||
|
||
func getTestDir(subDir string) string { | ||
pwd, _ := os.Getwd() | ||
testDir := filepath.Join(pwd, testDataDir) | ||
testDir = filepath.Join(testDir, subDir) | ||
|
||
return testDir | ||
} | ||
|
||
func TestVisitPkgDir(t *testing.T) { | ||
pkgDir := getTestDir("test_visit_dir") | ||
pVisitor := PkgVisitor{} | ||
source, err := downloader.NewSourceFromStr(pkgDir) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
err = pVisitor.Visit(source, func(pkg *pkg.KclPkg) error { | ||
assert.Equal(t, pkg.GetPkgName(), "test_visit_dir") | ||
assert.Equal(t, pkg.GetPkgVersion(), "0.0.1") | ||
return nil | ||
}) | ||
assert.NilError(t, err) | ||
} | ||
|
||
func TestVisitPkgTar(t *testing.T) { | ||
pkgTar := filepath.Join(getTestDir("test_visit_tar"), "test_visit_tar-0.0.1.tar") | ||
pVisitor := PkgVisitor{} | ||
source, err := downloader.NewSourceFromStr(pkgTar) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
err = pVisitor.Visit(source, func(pkg *pkg.KclPkg) error { | ||
assert.Equal(t, pkg.GetPkgName(), "test_visit_tar") | ||
assert.Equal(t, pkg.GetPkgVersion(), "0.0.1") | ||
return nil | ||
}) | ||
assert.NilError(t, err) | ||
} | ||
|
||
func TestVisitPkgRemote(t *testing.T) { | ||
var buf bytes.Buffer | ||
remotePkgVisitor := RemoteVisitor{ | ||
PkgVisitor: &PkgVisitor{ | ||
LogWriter: &buf, | ||
Settings: settings.GetSettings(), | ||
}, | ||
Downloader: &downloader.DepDownloader{}, | ||
} | ||
|
||
tests := []struct { | ||
sourceStr string | ||
expectedPkgName string | ||
expectedPkgVer string | ||
expectedLog string | ||
}{ | ||
{ | ||
sourceStr: "oci://ghcr.io/kcl-lang/helloworld?tag=0.1.2", | ||
expectedPkgName: "helloworld", | ||
expectedPkgVer: "0.1.2", | ||
expectedLog: "downloading 'kcl-lang/helloworld:0.1.2' from 'ghcr.io/kcl-lang/helloworld:0.1.2'\n", | ||
}, | ||
{ | ||
sourceStr: "git://github.com/kcl-lang/flask-demo-kcl-manifests.git?branch=main", | ||
expectedPkgName: "flask_manifests", | ||
expectedPkgVer: "0.0.1", | ||
expectedLog: "cloning 'https://github.com/kcl-lang/flask-demo-kcl-manifests.git' with branch 'main'\n", | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
buf.Reset() | ||
source, err := downloader.NewSourceFromStr(tt.sourceStr) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
err = remotePkgVisitor.Visit(source, func(pkg *pkg.KclPkg) error { | ||
assert.Equal(t, pkg.GetPkgName(), tt.expectedPkgName) | ||
assert.Equal(t, pkg.GetPkgVersion(), tt.expectedPkgVer) | ||
return nil | ||
}) | ||
assert.Equal(t, buf.String(), tt.expectedLog) | ||
assert.NilError(t, err) | ||
} | ||
} |