Files
homarr/tools/homarr-dev/internal/cli/cli_test.go
T
+12 fcd7c979df feat!: release v2 (#6545)
Co-authored-by: Artur <145776561+arturict@users.noreply.github.com>
Co-authored-by: arturict <arturict@users.noreply.github.com>
Co-authored-by: Thomas <thomas@ajnart.fr>
Co-authored-by: Afonso Jorge Ramos <afonsojorgeramos@gmail.com>
Co-authored-by: Danura30082 <danura30082+github@gmail.com>
Co-authored-by: Kushie <122986913+kushiemoon-dev@users.noreply.github.com>
Co-authored-by: ajnart <thomas@ajnart.fr>
Co-authored-by: ajnart <thomas@ajnart.dev>
Co-authored-by: chenjun <chenjun@foxmail.com>
Co-authored-by: Wikibear <wikibear@users.noreply.github.com>
Co-authored-by: Bergasha <bergasha@gmail.com>
Co-authored-by: leafyelin <43306196+leafyelin@users.noreply.github.com>
Co-authored-by: Lucas Araujo <lucas.lra@gmail.com>
Co-authored-by: GreenMonito <86085570+GreenMonito@users.noreply.github.com>
Co-authored-by: Nerdy-Technician <roffo@nerdy-technician.social>
Co-authored-by: Jun <34545896+jcstrong@users.noreply.github.com>
Co-authored-by: Edmond Abraham <54119952+gitedmond@users.noreply.github.com>
Co-authored-by: thrawnx <tight.bulb9384@fastmail.com>
Co-authored-by: Chris <58918656+nordada@users.noreply.github.com>
Co-authored-by: yi111 <153097222+Yi-111-a@users.noreply.github.com>
Co-authored-by: Yanis <naamaneyanis@gmail.com>
Co-authored-by: Wikibear <me@sej.de>
Co-authored-by: alessiottavo <93441886+alessiottavo@users.noreply.github.com>
2026-10-02 21:00:14 +02:00

108 lines
2.8 KiB
Go

package cli
import (
"errors"
"strings"
"testing"
"github.com/spf13/cobra"
)
func TestRootCommandOpensBrowser(t *testing.T) {
want := errors.New("browser started")
app := NewApp()
app.RunBrowser = func() error { return want }
if err := app.runRoot(&cobra.Command{}, nil); !errors.Is(err, want) {
t.Fatalf("root command error = %v, want %v", err, want)
}
}
func TestRootCommandLaunchesWhenRunFlagIsProvided(t *testing.T) {
want := errors.New("PR launched")
app := NewApp()
app.Launch = func(*cobra.Command, []string) error { return want }
cmd := &cobra.Command{}
cmd.Flags().Bool("pr", false, "")
if err := cmd.Flags().Set("pr", "true"); err != nil {
t.Fatal(err)
}
if err := app.runRoot(cmd, nil); !errors.Is(err, want) {
t.Fatalf("root command error = %v, want %v", err, want)
}
}
func TestLaunchRejectsTagWithPR(t *testing.T) {
app := NewApp()
// Registering the flags binds them to app.pr and resets it, so the command
// has to exist before the test sets the value it wants to assert on.
cmd := app.runCommand()
app.pr = 42
err := app.launch(cmd, []string{"feature"})
if err == nil || !strings.Contains(err.Error(), "cannot be used together") {
t.Fatalf("error = %v", err)
}
}
func TestLaunchRejectsNegativePR(t *testing.T) {
app := NewApp()
cmd := app.runCommand()
app.pr = -1
err := app.launch(cmd, nil)
if err == nil || !strings.Contains(err.Error(), "must be positive") {
t.Fatalf("error = %v", err)
}
}
func TestLaunchRejectsMissingImage(t *testing.T) {
app := NewApp()
err := app.launch(app.runCommand(), nil)
if err == nil || !strings.Contains(err.Error(), "provide a tag or --pr") {
t.Fatalf("error = %v", err)
}
}
// The root command doubles as `run`, so both must accept the same launch flags.
func TestRootAndRunShareLaunchFlags(t *testing.T) {
app := NewApp()
root := app.Root()
run, _, err := root.Find([]string{"run"})
if err != nil {
t.Fatal(err)
}
for _, name := range []string{"pr", "demo", "env", "detach"} {
if root.Flags().Lookup(name) == nil {
t.Fatalf("root command is missing the %q flag", name)
}
if run.Flags().Lookup(name) == nil {
t.Fatalf("run command is missing the %q flag", name)
}
}
}
func TestCICommandRegisteredAndValidated(t *testing.T) {
app := NewApp()
root := app.Root()
ciCmd, _, err := root.Find([]string{"ci"})
if err != nil {
t.Fatalf("failed to find 'ci' command: %v", err)
}
if ciCmd.Name() != "ci" {
t.Fatalf("expected command name 'ci', got %q", ciCmd.Name())
}
if ciCmd.Flags().Lookup("watch") == nil {
t.Fatalf("expected --watch flag on 'ci' command")
}
// Test invalid PR argument
err = ciCmd.RunE(ciCmd, []string{"invalid-num"})
if err == nil || !strings.Contains(err.Error(), "invalid PR number") {
t.Fatalf("expected invalid PR number error, got: %v", err)
}
}