Skip to content

Commit

Permalink
Fix verbose option in CLI mode (#28)
Browse files Browse the repository at this point in the history
Before, `v` would always be false, so the `-verbose` option never
worked.
  • Loading branch information
tobyhs committed Jul 4, 2023
1 parent 278d7a9 commit 175f705
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
11 changes: 6 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,18 +103,19 @@ func cliOptions(stdout io.Writer, args []string) (*options, error) {
flags.StringVar(&opts.format, "format", "text", "The format of the output: text or markdown")
flags.StringVar(&opts.filename, "filename", "CODENOTIFY", "The filename in which file subscribers are defined")
flags.IntVar(&opts.subscriberThreshold, "subscriber-threshold", 0, "The threshold of notifying subscribers")
v := *flags.Bool("verbose", false, "Verbose messages printed to stderr")
var v bool
flags.BoolVar(&v, "verbose", false, "Verbose messages printed to stderr")

if err := flags.Parse(args); err != nil {
return nil, err
}

if v {
verbose = os.Stderr
} else {
verbose = ioutil.Discard
}

if err := flags.Parse(args); err != nil {
return nil, err
}

opts.print = func(notifs map[string][]string) error {
return opts.writeNotifications(stdout, notifs)
}
Expand Down
32 changes: 32 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
Expand Down Expand Up @@ -129,6 +130,37 @@ func TestMain(t *testing.T) {
}
}

func TestCliOptions(t *testing.T) {
var originalVerbose io.Writer = verbose
defer func() { verbose = originalVerbose }()
tests := []struct {
name string
args []string
verbose io.Writer
}{
{
name: "no arguments",
args: []string{},
verbose: ioutil.Discard,
},
{
name: "verbose option",
args: []string{"-verbose"},
verbose: os.Stderr,
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
stdout := &bytes.Buffer{}
cliOptions(stdout, test.args)
if verbose != test.verbose {
t.Errorf("expected verbose to be %v; got %v", test.verbose, verbose)
}
})
}
}

func TestWriteNotifications(t *testing.T) {
tests := []struct {
name string
Expand Down

0 comments on commit 175f705

Please sign in to comment.