-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #32 from adevinta/lava-scan
cmd/lava: rename "lava run" command to "lava scan" and add tests
- Loading branch information
Showing
6 changed files
with
127 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
// Copyright 2023 Adevinta | ||
|
||
package scan | ||
|
||
import ( | ||
"flag" | ||
"log/slog" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/jroimartin/clilog" | ||
|
||
"github.com/adevinta/lava/internal/gitserver/gittest" | ||
) | ||
|
||
func TestMain(m *testing.M) { | ||
flag.Parse() | ||
|
||
level := slog.LevelError | ||
if testing.Verbose() { | ||
level = slog.LevelDebug | ||
} | ||
|
||
h := clilog.NewCLIHandler(os.Stderr, &clilog.HandlerOptions{Level: level}) | ||
slog.SetDefault(slog.New(h)) | ||
|
||
os.Exit(m.Run()) | ||
} | ||
|
||
func TestRun(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
wantExitCode int | ||
repo string | ||
}{ | ||
{ | ||
name: "good repo", | ||
wantExitCode: 0, | ||
repo: "testdata/goodrepo.tar", | ||
}, | ||
{ | ||
name: "vulnerable repo", | ||
wantExitCode: 103, | ||
repo: "testdata/vulnrepo.tar", | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
oldPwd := mustGetwd() | ||
oldCfgfile := *cfgfile | ||
oldOsExit := osExit | ||
defer func() { | ||
mustChdir(oldPwd) | ||
*cfgfile = oldCfgfile | ||
osExit = oldOsExit | ||
}() | ||
|
||
tmpPath, err := gittest.ExtractTemp(tt.repo) | ||
if err != nil { | ||
t.Fatalf("unexpected error extracting test repository: %v", err) | ||
} | ||
defer os.RemoveAll(tmpPath) | ||
|
||
*cfgfile = filepath.Join(tmpPath, "lava.yaml") | ||
|
||
var exitCode int | ||
osExit = func(status int) { | ||
exitCode = status | ||
} | ||
|
||
if err := run(nil); err != nil { | ||
t.Fatalf("run error: %v", err) | ||
} | ||
|
||
if exitCode != tt.wantExitCode { | ||
t.Errorf("unexpected exit code: got: %v, want: %v", exitCode, tt.wantExitCode) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
// mustGetwd returns a rooted path name corresponding to the current | ||
// directory. It panics on error. | ||
func mustGetwd() string { | ||
wd, err := os.Getwd() | ||
if err != nil { | ||
panic(err) | ||
} | ||
return wd | ||
} | ||
|
||
// mustChdir changes the current working directory to the named | ||
// directory. It panics on error. | ||
func mustChdir(path string) { | ||
if err := os.Chdir(path); err != nil { | ||
panic(err) | ||
} | ||
} |
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters