diff --git a/pkg/minikube/out/out.go b/pkg/minikube/out/out.go index 4706f9a706..01dd5318de 100644 --- a/pkg/minikube/out/out.go +++ b/pkg/minikube/out/out.go @@ -33,9 +33,9 @@ import ( "github.com/Delta456/box-cli-maker/v2" "github.com/briandowns/spinner" "github.com/mattn/go-isatty" + "github.com/spf13/pflag" "k8s.io/klog/v2" - "k8s.io/minikube/pkg/minikube/localpath" "k8s.io/minikube/pkg/minikube/out/register" "k8s.io/minikube/pkg/minikube/style" "k8s.io/minikube/pkg/minikube/translate" @@ -381,14 +381,6 @@ func displayError(msg string, err error) { } func latestLogFilePath() (string, error) { - if len(os.Args) < 2 { - return "", fmt.Errorf("unable to detect command") - } - cmd := os.Args[1] - if cmd == "start" { - return localpath.LastStartLog(), nil - } - tmpdir := os.TempDir() files, err := ioutil.ReadDir(tmpdir) if err != nil { @@ -411,16 +403,32 @@ func latestLogFilePath() (string, error) { return fullPath, nil } +func command() (string, error) { + if len(pflag.Args()) < 1 { + return "", fmt.Errorf("unable to detect command") + } + + return pflag.Arg(0), nil +} + func displayGitHubIssueMessage() { - logPath, err := latestLogFilePath() + cmd, err := command() if err != nil { - klog.Warningf("failed to diplay GitHub issue message: %v", err) + klog.Warningf("failed to get command: %v", err) } msg := Sprintf(style.Sad, "If the above advice does not help, please let us know:") msg += Sprintf(style.URL, "https://github.com/kubernetes/minikube/issues/new/choose\n") - msg += Sprintf(style.Empty, "Please attach the following file to the GitHub issue:") - msg += Sprintf(style.Empty, "- {{.logPath}}", V{"logPath": logPath}) + msg += Sprintf(style.Empty, "Please run `minikube logs --file=logs.txt` and attach logs.txt to the GitHub issue.") + + if cmd != "start" { + logPath, err := latestLogFilePath() + if err != nil { + klog.Warningf("failed to get latest log file path: %v", err) + } + msg += Sprintf(style.Empty, "Please also attach the following file to the GitHub issue:") + msg += Sprintf(style.Empty, "- {{.logPath}}", V{"logPath": logPath}) + } BoxedErr(msg) } diff --git a/pkg/minikube/out/out_test.go b/pkg/minikube/out/out_test.go index ea0f729c8a..acaf13ac40 100644 --- a/pkg/minikube/out/out_test.go +++ b/pkg/minikube/out/out_test.go @@ -21,11 +21,12 @@ import ( "os" "path/filepath" "strconv" + "strings" "testing" "github.com/Delta456/box-cli-maker/v2" + "github.com/spf13/pflag" - "k8s.io/minikube/pkg/minikube/localpath" "k8s.io/minikube/pkg/minikube/style" "k8s.io/minikube/pkg/minikube/tests" "k8s.io/minikube/pkg/minikube/translate" @@ -130,37 +131,96 @@ func createLogFile() (string, error) { return f.Name(), nil } -func TestLatestLogPath(t *testing.T) { - filename, err := createLogFile() +func TestLatestLogFilePath(t *testing.T) { + want, err := createLogFile() if err != nil { t.Fatal(err) } - defer os.Remove(filename) + defer os.Remove(want) + got, err := latestLogFilePath() + if err != nil { + t.Errorf("latestLogFilePath() failed with error = %v", err) + } + if got != want { + t.Errorf("latestLogFilePath() = %q; wanted %q", got, want) + } +} + +func TestCommand(t *testing.T) { testCases := []struct { - args []string - want string + args []string + want string + shouldError bool }{ { []string{"minikube", "start"}, - localpath.LastStartLog(), + "start", + false, }, { - []string{"minikube", "status"}, - filename, + []string{"minikube", "--profile", "profile1", "start"}, + "start", + false, + }, + { + []string{"minikube"}, + "", + true, }, } + pflag.String("profile", "", "") + for _, tt := range testCases { oldArgs := os.Args defer func() { os.Args = oldArgs }() os.Args = tt.args - got, err := latestLogFilePath() - if err != nil { - t.Fatalf("os.Args = %s; latestLogFilePath() failed with error = %v", tt.args, err) + pflag.Parse() + got, err := command() + if err == nil && tt.shouldError { + t.Errorf("os.Args = %s; command() did not fail but was expected to", tt.args) + } + if err != nil && !tt.shouldError { + t.Errorf("os.Args = %s; command() failed with error = %v", tt.args, err) } if got != tt.want { - t.Errorf("os.Args = %s; latestLogFilePath() = %q; wanted %q", tt.args, got, tt.want) + t.Errorf("os.Args = %s; command() = %q; wanted %q", tt.args, got, tt.want) + } + } +} + +func TestDisplayGitHubIssueMessage(t *testing.T) { + testCases := []struct { + args []string + shouldContainMessage bool + }{ + { + []string{"minikube", "start"}, + false, + }, + { + []string{"minikube", "delete"}, + true, + }, + } + + msg := "Please also attach the following file to the GitHub issue:" + + for _, tt := range testCases { + oldArgs := os.Args + defer func() { os.Args = oldArgs }() + os.Args = tt.args + pflag.Parse() + f := tests.NewFakeFile() + SetErrFile(f) + displayGitHubIssueMessage() + output := f.String() + if strings.Contains(output, msg) && !tt.shouldContainMessage { + t.Errorf("os.Args = %s; displayGitHubIssueMessage() output = %q; did not expect it to contain = %q", tt.args, output, msg) + } + if !strings.Contains(output, msg) && tt.shouldContainMessage { + t.Errorf("os.Args = %s; displayGitHubIssueMessage() output = %q; expected to contain = %q", tt.args, output, msg) } } }