Skip to content

Commit

Permalink
[main] fix #70
Browse files Browse the repository at this point in the history
  • Loading branch information
spezifisch committed Oct 14, 2024
1 parent ed22633 commit 668e029
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
7 changes: 4 additions & 3 deletions stmps.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(".")
}

Expand Down Expand Up @@ -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 {
Expand Down
40 changes: 39 additions & 1 deletion stmps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down

0 comments on commit 668e029

Please sign in to comment.