From 668e02917270a815101e0f921df291f52ad10580 Mon Sep 17 00:00:00 2001 From: spezifisch Date: Mon, 14 Oct 2024 19:23:26 +0200 Subject: [PATCH] [main] fix #70 --- stmps.go | 7 ++++--- stmps_test.go | 40 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 43 insertions(+), 4 deletions(-) diff --git a/stmps.go b/stmps.go index 7586604..70042f0 100644 --- a/stmps.go +++ b/stmps.go @@ -27,14 +27,15 @@ var testMode bool // This can be set to true during tests, too func readConfig(configFile *string) error { required_properties := []string{"auth.username", "auth.password", "server.host"} - if configFile != nil { + if configFile != nil && *configFile != "" { // use custom config file viper.SetConfigFile(*configFile) } else { // lookup default dirs viper.SetConfigName("stmp") // TODO this should be stmps viper.SetConfigType("toml") - viper.AddConfigPath("$HOME/.config/stmp") // TODO this should be stmps + viper.AddConfigPath("$HOME/.config/stmp") // TODO this should be stmps only + viper.AddConfigPath("$HOME/.config/stmps") viper.AddConfigPath(".") } @@ -101,7 +102,7 @@ func main() { list := flag.Bool("list", false, "list server data") cpuprofile := flag.String("cpuprofile", "", "write cpu profile to `file`") memprofile := flag.String("memprofile", "", "write memory profile to `file`") - configFile := flag.String("config", "c", "use config `file`") + configFile := flag.String("config", "", "use config `file`") flag.Parse() if *help { diff --git a/stmps_test.go b/stmps_test.go index a889e63..823e8f6 100644 --- a/stmps_test.go +++ b/stmps_test.go @@ -46,7 +46,45 @@ func TestMainWithoutTUI(t *testing.T) { }() // Set command-line arguments to trigger the help flag - os.Args = []string{"cmd", "--config=stmp-example.toml", "--help"} + os.Args = []string{"doesntmatter", "--config=stmp-example.toml"} + + main() + + if !exitCalled { + t.Fatalf("osExit was not called") + } +} + +// Regression test for https://github.com/spezifisch/stmps/issues/70 +func TestMainWithConfigFileEmptyString(t *testing.T) { + // Mock osExit to prevent actual exit during test + exitCalled := false + osExit = func(code int) { + exitCalled = true + + if code != 0x23420001 { + // Capture and print the stack trace + stackBuf := make([]byte, 1024) + stackSize := runtime.Stack(stackBuf, false) + stackTrace := string(stackBuf[:stackSize]) + + // Print the stack trace with new lines only + t.Fatalf("Unexpected exit with code: %d\nStack trace:\n%s\n", code, stackTrace) + } + // Since we don't abort execution here, we will run main() until the end or a panic. + } + headlessMode = true + testMode = true + + // Restore patches after the test + defer func() { + osExit = os.Exit + headlessMode = false + testMode = false + }() + + // Set command-line arguments to trigger the help flag + os.Args = []string{"stmps"} main()