Borg/cmd/root_test.go
google-labs-jules[bot] d27d9f3a37 feat: Improve test coverage and refactor for testability
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.
2025-11-03 16:31:26 +00:00

64 lines
1.1 KiB
Go

package cmd
import (
"bytes"
"io"
"log/slog"
"testing"
"github.com/spf13/cobra"
)
func TestExecute(t *testing.T) {
err := Execute(slog.New(slog.NewTextHandler(io.Discard, nil)))
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
}
func executeCommand(cmd *cobra.Command, args ...string) (string, error) {
buf := new(bytes.Buffer)
cmd.SetOut(buf)
cmd.SetErr(buf)
cmd.SetArgs(args)
err := cmd.Execute()
return buf.String(), err
}
func Test_NewRootCmd(t *testing.T) {
if NewRootCmd() == nil {
t.Errorf("NewRootCmd is nil")
}
}
func Test_executeCommand(t *testing.T) {
type args struct {
cmd *cobra.Command
args []string
}
tests := []struct {
name string
args args
want string
wantErr bool
}{
{
name: "Test with no args",
args: args{
cmd: NewRootCmd(),
args: []string{},
},
want: "",
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := executeCommand(tt.args.cmd, tt.args.args...)
if (err != nil) != tt.wantErr {
t.Errorf("executeCommand() error = %v, wantErr %v", err, tt.wantErr)
return
}
})
}
}