app: Fix a few edge cases with version flag (#443)

* app: Show just version if DB is missing

Signed-off-by: Simarpreet Singh <simar@linux.com>

* app: Dont panic if cache-dir is bogus

Signed-off-by: Simarpreet Singh <simar@linux.com>

* app: DRY up logic for showVersion

Signed-off-by: Simarpreet Singh <simar@linux.com>
This commit is contained in:
Simarpreet Singh
2020-03-24 02:09:05 -07:00
committed by GitHub
parent 94eb7cc592
commit 6fbdec6e83
2 changed files with 56 additions and 33 deletions

View File

@@ -60,21 +60,38 @@ Vulnerability DB:
{
name: "sad path, no DB is available",
args: args{
outputFormat: "table",
outputFormat: "json",
version: "1.2.3",
},
expectedOutput: `unable to display current version: unexpected end of JSON input`,
expectedOutput: `{"Version":"1.2.3"}
`,
},
{
name: "sad path, bogus cache dir",
args: args{
outputFormat: "json",
version: "1.2.3",
cacheDir: "/foo/bar/bogus",
},
expectedOutput: `{"Version":"1.2.3"}
`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
d, _ := ioutil.TempDir("", "Test_showVersion-*")
defer func() {
os.RemoveAll(d)
}()
var cacheDir string
switch {
case tt.args.cacheDir != "":
cacheDir = tt.args.cacheDir
default:
cacheDir, _ = ioutil.TempDir("", "Test_showVersion-*")
defer func() {
os.RemoveAll(cacheDir)
}()
}
if tt.createDB {
db.Init(d)
db.Init(cacheDir)
db.Config{}.SetMetadata(db.Metadata{
Version: 42,
Type: 1,
@@ -87,7 +104,7 @@ Vulnerability DB:
var wb []byte
fw := fakeIOWriter{written: wb}
showVersion(d, tt.args.outputFormat, tt.args.version, &fw)
showVersion(cacheDir, tt.args.outputFormat, tt.args.version, &fw)
assert.Equal(t, tt.expectedOutput, string(fw.written), tt.name)
})
}