From 9da7dfd5c3332298a345222a2011d58426a75ca2 Mon Sep 17 00:00:00 2001 From: Parth Sareen Date: Thu, 12 Nov 2026 14:35:02 -0800 Subject: [PATCH] cmd: show help for noninteractive bare invocations (#19557) --- cmd/cmd.go | 10 +++-- cmd/cmd_launcher_test.go | 82 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+), 3 deletions(-) diff --git a/cmd/cmd.go b/cmd/cmd.go index 7f74d3930..53d53c9e6 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -2554,19 +2554,23 @@ func NewCLI() *cobra.Command { CompletionOptions: cobra.CompletionOptions{ DisableDefaultCmd: true, }, - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { if version, _ := cmd.Flags().GetBool("version"); version { versionHandler(cmd, args) - return + return nil + } + if !isInteractiveTerminal() { + return cmd.Help() } if err := runWelcome(); err != nil { if !errors.Is(err, launch.ErrCancelled) { fmt.Fprintf(os.Stderr, "Error: %v\n", err) } - return + return nil } runInteractiveTUI(cmd) + return nil }, } diff --git a/cmd/cmd_launcher_test.go b/cmd/cmd_launcher_test.go index 6762e2fd3..89d6ad99b 100644 --- a/cmd/cmd_launcher_test.go +++ b/cmd/cmd_launcher_test.go @@ -1,7 +1,13 @@ package cmd import ( + "bytes" "context" + "net/http" + "net/http/httptest" + "os" + "strings" + "sync/atomic" "testing" "github.com/spf13/cobra" @@ -11,6 +17,82 @@ import ( "github.com/ollama/ollama/cmd/tui" ) +func TestRootCommandHelpAndLauncher(t *testing.T) { + stdin, err := os.Open(os.DevNull) + if err != nil { + t.Fatal(err) + } + oldStdin, oldInteractive := os.Stdin, isInteractiveTerminal + os.Stdin = stdin + t.Cleanup(func() { + os.Stdin, isInteractiveTerminal = oldStdin, oldInteractive + stdin.Close() + }) + + for _, tt := range []struct { + name string + interactive bool + args []string + wantHelp bool + }{ + {name: "noninteractive bare command", args: []string{}, wantHelp: true}, + {name: "interactive bare command", interactive: true, args: []string{}}, + {name: "interactive explicit help", interactive: true, args: []string{"--help"}, wantHelp: true}, + } { + t.Run(tt.name, func(t *testing.T) { + home := t.TempDir() + setCmdTestHome(t, home) + isInteractiveTerminal = func() bool { return tt.interactive } + + var requests atomic.Int32 + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + requests.Add(1) + http.Error(w, "launcher heartbeat", http.StatusServiceUnavailable) + })) + defer server.Close() + t.Setenv("OLLAMA_HOST", server.URL) + + var output bytes.Buffer + root := NewCLI() + root.SetArgs(tt.args) + root.SetOut(&output) + stderr := captureOutput(t, &os.Stderr, func() { + if err := root.Execute(); err != nil { + t.Errorf("Execute() = %v", err) + } + }) + + if tt.wantHelp { + for _, text := range []string{"Large language model runner", "Usage:", "Available Commands:"} { + if !strings.Contains(output.String(), text) { + t.Errorf("help missing %q: %s", text, output.String()) + } + } + if stderr != "" { + t.Errorf("unexpected stderr: %s", stderr) + } + if got := requests.Load(); got != 0 { + t.Errorf("help made %d server requests", got) + } + entries, err := os.ReadDir(home) + if err != nil { + t.Fatal(err) + } + if len(entries) != 0 { + t.Errorf("help wrote to home: %v", entries) + } + } else { + if output.Len() != 0 { + t.Errorf("interactive command printed help: %s", output.String()) + } + if got := requests.Load(); got != 1 { + t.Errorf("launcher made %d heartbeat requests, want 1", got) + } + } + }) + } +} + func setCmdTestHome(t *testing.T, dir string) { t.Helper() t.Setenv("HOME", dir)