This commit introduces a significant refactoring of the `cmd` package to improve testability and increases test coverage across the application. Key changes include: - Refactored Cobra commands to use `RunE` for better error handling and testing. - Extracted business logic from command handlers into separate, testable functions. - Added comprehensive unit tests for the `cmd`, `compress`, `github`, `logger`, and `pwa` packages. - Added tests for missing command-line arguments, as requested. - Implemented the `borg all` command to clone all public repositories for a GitHub user or organization. - Restored and improved the `collect pwa` functionality. - Removed duplicate code and fixed various bugs. - Addressed a resource leak in the `all` command. - Improved error handling in the `pwa` package. - Refactored `main.go` to remove duplicated logic. - Fixed several other minor bugs and inconsistencies.
56 lines
1.3 KiB
Go
56 lines
1.3 KiB
Go
package cmd
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/Snider/Borg/pkg/datanode"
|
|
"github.com/Snider/Borg/pkg/pwa"
|
|
)
|
|
|
|
func TestCollectPWACmd_NoURI(t *testing.T) {
|
|
rootCmd := NewRootCmd()
|
|
collectCmd := NewCollectCmd()
|
|
collectPWACmd := NewCollectPWACmd()
|
|
collectCmd.AddCommand(&collectPWACmd.Command)
|
|
rootCmd.AddCommand(collectCmd)
|
|
_, err := executeCommand(rootCmd, "collect", "pwa")
|
|
if err == nil {
|
|
t.Fatalf("expected an error, but got none")
|
|
}
|
|
if !strings.Contains(err.Error(), "uri is required") {
|
|
t.Fatalf("unexpected error message: %v", err)
|
|
}
|
|
}
|
|
func Test_NewCollectPWACmd(t *testing.T) {
|
|
if NewCollectPWACmd() == nil {
|
|
t.Errorf("NewCollectPWACmd is nil")
|
|
}
|
|
}
|
|
|
|
func TestCollectPWA_Good(t *testing.T) {
|
|
mockClient := &pwa.MockPWAClient{
|
|
ManifestURL: "https://example.com/manifest.json",
|
|
DN: datanode.New(),
|
|
Err: nil,
|
|
}
|
|
|
|
_, err := CollectPWA(mockClient, "https://example.com", "/dev/null", "datanode", "none")
|
|
if err != nil {
|
|
t.Fatalf("CollectPWA failed: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestCollectPWA_Bad(t *testing.T) {
|
|
mockClient := &pwa.MockPWAClient{
|
|
ManifestURL: "",
|
|
DN: nil,
|
|
Err: fmt.Errorf("pwa error"),
|
|
}
|
|
|
|
_, err := CollectPWA(mockClient, "https://example.com", "/dev/null", "datanode", "none")
|
|
if err == nil {
|
|
t.Fatalf("expected an error, but got none")
|
|
}
|
|
}
|