Skip to content

Commit bc7386f

Browse files
authored
test: hide the cli help flag to avoid panic when running parallel (#2279)
Unfortunately the upstream package does not feel there [is a reason to remove their global help flag variable hack](urfave/cli#2176 (comment)) that can cause a panic when running in parallel, so this has us hide the help flag for all commands when running the tests so that the code path is skipped entirely
1 parent 31aab3e commit bc7386f

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

cmd/osv-scanner/internal/cmd/run.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"errors"
66
"io"
77
"log/slog"
8+
"os"
89
"testing"
910

1011
scalibr "github.com/google/osv-scalibr/version"
@@ -23,6 +24,14 @@ var (
2324
type CommandBuilder = func(stdout, stderr io.Writer) *cli.Command
2425

2526
func Run(args []string, stdout, stderr io.Writer, commands []CommandBuilder) int {
27+
// urfave/cli uses a global for its help flag which makes it possible for a nil
28+
// pointer dereference if running in a parallel setting, which our test suite
29+
// does, so this is used to hide the help flag so the global won't be used
30+
// unless a particular env variable is set
31+
//
32+
// see https://github.com/urfave/cli/issues/2176
33+
shouldHideHelp := testing.Testing() && os.Getenv("TEST_SHOW_HELP") != "true"
34+
2635
// --- Setup Logger ---
2736
logHandler := cmdlogger.New(stdout, stderr)
2837

@@ -50,14 +59,18 @@ func Run(args []string, stdout, stderr io.Writer, commands []CommandBuilder) int
5059

5160
cmds := make([]*cli.Command, 0, len(commands))
5261
for _, cmd := range commands {
53-
cmds = append(cmds, cmd(stdout, stderr))
62+
c := cmd(stdout, stderr)
63+
c.HideHelp = shouldHideHelp
64+
65+
cmds = append(cmds, c)
5466
}
5567

5668
app := &cli.Command{
5769
Name: "osv-scanner",
5870
Version: version.OSVVersion,
5971
Usage: "scans various mediums for dependencies and checks them against the OSV database",
6072
Suggest: true,
73+
HideHelp: shouldHideHelp,
6174
Writer: stdout,
6275
ErrWriter: stderr,
6376
DefaultCommand: "scan",

cmd/osv-scanner/main_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import (
99

1010
//nolint:paralleltest
1111
func Test_run(t *testing.T) {
12+
t.Setenv("TEST_SHOW_HELP", "true")
13+
1214
tests := []testcmd.Case{
1315
{
1416
Name: "",

0 commit comments

Comments
 (0)