Skip to content

Commit

Permalink
main.go: Non-zero -list exit (fix oz#70)
Browse files Browse the repository at this point in the history
  • Loading branch information
jnd-au committed Oct 28, 2024
1 parent 75be867 commit 56806ee
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
5 changes: 5 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,11 @@ func parseMainArgs() *model {
q = arg
}
results := SearchZones(strings.ToLower(q))
if len(results) < 1 {
fmt.Fprintf(os.Stderr(), "Unknown time zone %s\n", q)
os.Exit(3)
return nil
}
results.Print(os.Stdout())
os.Exit(0)
return nil
Expand Down
15 changes: 9 additions & 6 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,18 +258,21 @@ func TestMainArgList(t *testing.T) {
}

osWrapper.Setargs([]string{"-list", "!"})
expectedOutput := "Unknown time zone !"
expectedErrOutput := "Unknown time zone !"
parseMainArgsWithPanicRecovery(&err)
if err != nil {
t.Errorf("Unexpected failure for -list flag: %v", err)
t.Errorf("Unexpected failure for `-list !` flag: %v", err)
}
if osWrapper.ExitCode == nil {
t.Error("Main -list should exit, but did not")
t.Error("Main `-list !` should exit, but did not")
} else if *osWrapper.ExitCode != 3 {
t.Errorf("Main -list should exit with code 3, but got %v", *osWrapper.ExitCode)
t.Errorf("Main `-list !` should exit with code 3, but got %v", *osWrapper.ExitCode)
}
if output := strings.TrimSpace(osWrapper.ConsumeStderr()); output != expectedOutput {
t.Errorf("Main -list should have printed '%v', but got '%v'", expectedOutput, output)
if errOutput := strings.TrimSpace(osWrapper.ConsumeStderr()); errOutput != expectedErrOutput {
t.Errorf("Main `-list !` should have stderr '%v', but got '%v'", expectedErrOutput, errOutput)
}
if stdOutput := osWrapper.ConsumeStdout(); len(stdOutput) > 0 {
t.Errorf("Main `-list !` should have empty stdout, but got '%v'", stdOutput)
}
}

Expand Down

0 comments on commit 56806ee

Please sign in to comment.