2021-03-18 13:00:28 +00:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
2022-02-09 18:04:08 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2021-03-18 13:00:28 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/sergi/go-diff/diffmatchpatch"
|
2022-02-09 18:04:08 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2021-03-18 13:00:28 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestRootCmdAliasesToPackagesSubcommand(t *testing.T) {
|
|
|
|
request := "docker-archive:" + getFixtureImage(t, "image-pkg-coverage")
|
|
|
|
|
2021-03-22 19:25:04 +00:00
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
env map[string]string
|
|
|
|
assertions []traitAssertion
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "go-case",
|
|
|
|
assertions: []traitAssertion{
|
|
|
|
assertTableReport,
|
|
|
|
assertSuccessfulReturnCode,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "respond-to-output-binding",
|
|
|
|
env: map[string]string{
|
|
|
|
"SYFT_OUTPUT": "text",
|
|
|
|
},
|
|
|
|
assertions: []traitAssertion{
|
|
|
|
assertInOutput("[Image]"),
|
|
|
|
assertSuccessfulReturnCode,
|
|
|
|
},
|
|
|
|
},
|
2021-03-18 13:00:28 +00:00
|
|
|
}
|
|
|
|
|
2021-03-22 19:25:04 +00:00
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
2021-06-29 22:06:47 +00:00
|
|
|
aliasCmd, aliasStdout, aliasStderr := runSyft(t, test.env, request)
|
2021-03-22 19:25:04 +00:00
|
|
|
for _, traitFn := range test.assertions {
|
|
|
|
traitFn(t, aliasStdout, aliasStderr, aliasCmd.ProcessState.ExitCode())
|
|
|
|
}
|
2021-03-18 13:00:28 +00:00
|
|
|
|
2021-06-29 22:06:47 +00:00
|
|
|
pkgCmd, pkgsStdout, pkgsStderr := runSyft(t, test.env, "packages", request)
|
2021-03-22 19:25:04 +00:00
|
|
|
for _, traitFn := range test.assertions {
|
|
|
|
traitFn(t, pkgsStdout, pkgsStderr, pkgCmd.ProcessState.ExitCode())
|
|
|
|
}
|
2021-03-18 13:00:28 +00:00
|
|
|
|
2021-03-22 19:25:04 +00:00
|
|
|
if aliasStdout != pkgsStdout {
|
|
|
|
t.Errorf("packages and root command should have same report output but do not!")
|
|
|
|
dmp := diffmatchpatch.New()
|
|
|
|
diffs := dmp.DiffMain(aliasStdout, pkgsStdout, true)
|
|
|
|
t.Error(dmp.DiffPrettyText(diffs))
|
|
|
|
}
|
|
|
|
})
|
2021-03-18 13:00:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPersistentFlags(t *testing.T) {
|
|
|
|
request := "docker-archive:" + getFixtureImage(t, "image-pkg-coverage")
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args []string
|
|
|
|
env map[string]string
|
|
|
|
assertions []traitAssertion
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "quiet-flag",
|
|
|
|
// note: the root command will always show the deprecation warning, so the packages command is used instead
|
|
|
|
args: []string{"packages", "-q", request},
|
|
|
|
assertions: []traitAssertion{
|
|
|
|
func(tb testing.TB, stdout, stderr string, rc int) {
|
|
|
|
// ensure there is no status
|
|
|
|
if len(stderr) != 0 {
|
|
|
|
tb.Errorf("should have seen no stderr output, got %d bytes", len(stderr))
|
|
|
|
}
|
|
|
|
// ensure there is still a report
|
|
|
|
if len(stdout) == 0 {
|
|
|
|
tb.Errorf("should have seen a report on stdout, got nothing")
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "info-log-flag",
|
|
|
|
args: []string{"-v", request},
|
|
|
|
assertions: []traitAssertion{
|
|
|
|
assertLoggingLevel("info"),
|
|
|
|
assertSuccessfulReturnCode,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "debug-log-flag",
|
|
|
|
args: []string{"-vv", request},
|
|
|
|
assertions: []traitAssertion{
|
|
|
|
assertLoggingLevel("debug"),
|
|
|
|
assertSuccessfulReturnCode,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
2021-06-29 22:06:47 +00:00
|
|
|
cmd, stdout, stderr := runSyft(t, test.env, test.args...)
|
2021-03-18 13:00:28 +00:00
|
|
|
for _, traitFn := range test.assertions {
|
|
|
|
traitFn(t, stdout, stderr, cmd.ProcessState.ExitCode())
|
|
|
|
}
|
2023-04-14 18:33:36 +00:00
|
|
|
logOutputOnFailure(t, cmd, stdout, stderr)
|
2021-03-18 13:00:28 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2022-02-09 18:04:08 +00:00
|
|
|
|
|
|
|
func TestLogFile(t *testing.T) {
|
|
|
|
request := "docker-archive:" + getFixtureImage(t, "image-pkg-coverage")
|
|
|
|
|
|
|
|
envLogFile := filepath.Join(os.TempDir(), "a-pretty-log-file.log")
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args []string
|
|
|
|
env map[string]string
|
|
|
|
assertions []traitAssertion
|
|
|
|
cleanup func()
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "env-var-log-file-name",
|
|
|
|
args: []string{"-vv", request},
|
|
|
|
env: map[string]string{"SYFT_LOG_FILE": envLogFile},
|
|
|
|
assertions: []traitAssertion{
|
|
|
|
func(tb testing.TB, stdout, stderr string, rc int) {
|
|
|
|
tb.Helper()
|
|
|
|
_, err := os.Stat(envLogFile)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
cleanup: func() { assert.NoError(t, os.Remove(envLogFile)) },
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
|
|
|
t.Cleanup(test.cleanup)
|
|
|
|
|
|
|
|
cmd, stdout, stderr := runSyft(t, test.env, test.args...)
|
|
|
|
for _, traitFn := range test.assertions {
|
|
|
|
traitFn(t, stdout, stderr, cmd.ProcessState.ExitCode())
|
|
|
|
}
|
2023-04-14 18:33:36 +00:00
|
|
|
logOutputOnFailure(t, cmd, stdout, stderr)
|
2022-02-09 18:04:08 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|