package cli import ( "context" "io" "os" "path/filepath" "testing" "time" "gitea.maximumdirect.net/eric/notarius/internal/core/config" "go.maximumdirect.net/commandkit" ) func TestRunDebugSelectionRetainsNativeBooleanAndSuppliedEmptyDirectory(t *testing.T) { t.Chdir(t.TempDir()) configured := filepath.Join(t.TempDir(), "configured") for _, test := range []struct { name string args []string configured, want string enabled bool mode commandkit.ReportingMode }{ {"omitted", nil, configured, "", false, commandkit.ReportingModeOrdinary}, {"directory alone", []string{"--debug-dir", "override"}, configured, "", false, commandkit.ReportingModeOrdinary}, {"configured directory", []string{"--debug"}, configured, configured, true, commandkit.ReportingModeDebug}, {"native debug then quiet", []string{"--debug", "--quiet"}, configured, configured, true, commandkit.ReportingModeQuiet}, {"native quiet then debug", []string{"--quiet", "--debug"}, configured, configured, true, commandkit.ReportingModeDebug}, {"native debug false", []string{"--debug", "--debug=false"}, configured, "", false, commandkit.ReportingModeOrdinary}, {"empty configured", []string{"--debug"}, "", "", true, commandkit.ReportingModeDebug}, {"explicit empty override", []string{"--debug", "--debug-dir", ""}, configured, "", true, commandkit.ReportingModeDebug}, {"relative override", []string{"--debug", "--debug-dir", "override"}, configured, "override", true, commandkit.ReportingModeDebug}, {"override reenables empty configured", []string{"--debug", "--debug-dir", "override"}, "", "override", true, commandkit.ReportingModeDebug}, {"default", []string{"--debug"}, config.Default().Debug.Directory, "/tmp/notarius-debug", true, commandkit.ReportingModeDebug}, } { t.Run(test.name, func(t *testing.T) { called := false command := commandkit.NewCommand(commandkit.CommandSpec[runDebugInput]{ Name: "run", Flags: runDebugFlags(func(input *runDebugInput) *runDebugInput { return input }), Run: func(inv commandkit.Invocation, input runDebugInput) commandkit.Outcome { called = true directory, err := input.directory(test.configured) want := test.want if want != "" { want, _ = filepath.Abs(want) } if err != nil || directory != want || input.Enabled.Value != test.enabled || inv.ReportingMode() != test.mode { t.Errorf("directory=%q error=%v input=%#v mode=%v", directory, err, input, inv.ReportingMode()) } return commandkit.Success() }, }) app, err := commandkit.New(commandkit.Identity{Name: "test"}, command) if err != nil { t.Fatal(err) } result := app.Execute(context.Background(), append([]string{"run"}, test.args...), commandkit.Streams{Out: io.Discard, Err: io.Discard}) if result.Status != 0 || !called { t.Fatalf("native run input=%#v called=%v", result, called) } }) } } func TestRunDebugStartFailureRetainsAvailablePath(t *testing.T) { parent := t.TempDir() if err := os.Mkdir(filepath.Join(parent, "latest"), 0o700); err != nil { t.Fatal(err) } enabled := runDebugInput{Enabled: commandkit.Supplied[bool]{Value: true, Set: true}} capture, err := startRunDebug(enabled, parent, "run", time.Now()) if err == nil || capture == nil || capture.Path() != filepath.Join(parent, "run") { t.Fatalf("start=%#v %v", capture, err) } // The run handler must stop here; the partially allocated capture can still // finalize and report the start failure without engine/provider construction. if finalErr := capture.Finalize(nil, err); finalErr == nil { t.Fatal("start failure hidden") } if _, err := os.Stat(filepath.Join(capture.Path(), "outcome.json")); err != nil { t.Fatal(err) } } func TestDisabledRunDebugDoesNotAllocateOrChangeLatest(t *testing.T) { root := t.TempDir() unallocated := filepath.Join(root, "unallocated") if capture, err := startRunDebug(runDebugInput{}, unallocated, "run", time.Now()); err != nil || capture != nil { t.Fatalf("disabled capture=%#v %v", capture, err) } if _, err := os.Stat(unallocated); !os.IsNotExist(err) { t.Fatalf("disabled capture created its root: %v", err) } first, err := startRunDebug(runDebugInput{Enabled: commandkit.Supplied[bool]{Value: true}}, root, "first", time.Now()) if err != nil { t.Fatal(err) } for _, input := range []runDebugInput{ {}, {Directory: commandkit.Supplied[string]{Value: root, Set: true}}, {Enabled: commandkit.Supplied[bool]{Value: true}, Directory: commandkit.Supplied[string]{Set: true}}, } { capture, err := startRunDebug(input, root, "disabled", time.Now()) if err != nil || capture != nil { t.Fatalf("disabled start=%#v %v", capture, err) } } target, err := os.Readlink(filepath.Join(root, "latest")) if err != nil || target != "first" { t.Fatalf("disabled run changed latest=%q %v", target, err) } if _, err := os.Stat(filepath.Join(root, "disabled")); !os.IsNotExist(err) { t.Fatalf("disabled capture allocated: %v", err) } if err := first.Finalize(nil, nil); err != nil { t.Fatal(err) } }