-
Notifications
You must be signed in to change notification settings - Fork 49
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: Rohanraj123 <[email protected]>
- Loading branch information
1 parent
3950507
commit f306ac4
Showing
2 changed files
with
139 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package client | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
|
||
"kcl-lang.io/kpm/pkg/opt" | ||
pkg "kcl-lang.io/kpm/pkg/package" | ||
) | ||
|
||
// TestResolvePkgDepsMetadata verifies that dependency resolution works as expected. | ||
func TestResolvePkgDepsMetadata(t *testing.T) { | ||
// Create a real KpmClient | ||
client := &KpmClient{} | ||
|
||
// Initialize KclPkg using InitOptions | ||
initOptions := &opt.InitOptions{ | ||
InitPath: "testdata/sample_package", | ||
} | ||
kclPkg := pkg.NewKclPkg(initOptions) | ||
|
||
// Attempt to resolve package dependencies | ||
err := client.ResolvePkgDepsMetadata(&kclPkg, true) | ||
if err != nil { | ||
t.Errorf("ResolvePkgDepsMetadata failed: %v", err) | ||
} | ||
} | ||
|
||
// TestResolveDepsMetadataInJsonStr verifies that dependencies metadata is serialized correctly. | ||
func TestResolveDepsMetadataInJsonStr(t *testing.T) { | ||
client := &KpmClient{} | ||
|
||
// Initialize KclPkg using InitOptions | ||
initOptions := &opt.InitOptions{ | ||
InitPath: "testdata/sample_package", | ||
} | ||
kclPkg := pkg.NewKclPkg(initOptions) | ||
|
||
// Get dependencies metadata in JSON | ||
jsonStr, err := client.ResolveDepsMetadataInJsonStr(&kclPkg, true) | ||
if err != nil { | ||
t.Fatalf("ResolveDepsMetadataInJsonStr failed: %v", err) | ||
} | ||
|
||
// Print JSON output to inspect its structure | ||
t.Logf("JSON Output: %s", jsonStr) | ||
|
||
// Try to unmarshal into a generic structure if DepMetadata is unknown | ||
var deps interface{} | ||
err = json.Unmarshal([]byte(jsonStr), &deps) | ||
if err != nil { | ||
t.Errorf("Failed to parse JSON output: %v", err) | ||
} | ||
|
||
// Check if the output contains expected keys | ||
if deps == nil { | ||
t.Errorf("Expected some dependencies but got none") | ||
} | ||
} |
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,80 @@ | ||
package client | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"kcl-lang.io/kpm/pkg/opt" | ||
pkg "kcl-lang.io/kpm/pkg/package" | ||
) | ||
|
||
// TestPackagePkg verifies that the PackagePkg function correctly creates a .tar file. | ||
func TestPackagePkg(t *testing.T) { | ||
client := &KpmClient{} | ||
|
||
// Create a temporary directory for testing | ||
tempDir, err := os.MkdirTemp("", "kclpkg_test") | ||
if err != nil { | ||
t.Fatalf("Failed to create temp directory: %v", err) | ||
} | ||
defer os.RemoveAll(tempDir) // Clean up after test | ||
|
||
// Initialize KclPkg with a valid path | ||
initOptions := &opt.InitOptions{ | ||
InitPath: tempDir, | ||
} | ||
kclPkg := pkg.NewKclPkg(initOptions) | ||
|
||
// Run the packaging function | ||
tarPath, err := client.PackagePkg(&kclPkg, false) | ||
if err != nil { | ||
t.Fatalf("PackagePkg failed: %v", err) | ||
} | ||
|
||
// Check if the .tar file was created | ||
if _, err := os.Stat(tarPath); os.IsNotExist(err) { | ||
t.Errorf("Expected tar file at %s but it was not created", tarPath) | ||
} else { | ||
t.Logf("Tar file created successfully at %s", tarPath) | ||
} | ||
} | ||
|
||
// TestPackage verifies that the Package function correctly archives the package. | ||
func TestPackage(t *testing.T) { | ||
client := &KpmClient{} | ||
|
||
// Create a temporary directory for testing | ||
tempDir, err := os.MkdirTemp("", "kclpkg_test") | ||
if err != nil { | ||
t.Fatalf("Failed to create temp directory: %v", err) | ||
} | ||
defer os.RemoveAll(tempDir) // Clean up after test | ||
|
||
// Initialize KclPkg | ||
initOptions := &opt.InitOptions{ | ||
InitPath: tempDir, | ||
} | ||
kclPkg := pkg.NewKclPkg(initOptions) | ||
|
||
// Determine tar path | ||
tarPath := kclPkg.DefaultTarPath() | ||
|
||
// Ensure any existing tar file is removed before testing | ||
_ = os.Remove(tarPath) | ||
|
||
// Run the package function | ||
err = client.Package(&kclPkg, tarPath, false) | ||
if err != nil { | ||
t.Fatalf("Package failed: %v", err) | ||
} | ||
|
||
// Verify the tar file was created | ||
if _, err := os.Stat(tarPath); os.IsNotExist(err) { | ||
t.Errorf("Expected tar file at %s but it was not created", tarPath) | ||
} else { | ||
t.Logf("Tar file created successfully at %s", tarPath) | ||
} | ||
|
||
// Clean up after test | ||
_ = os.Remove(tarPath) | ||
} |