-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* [wip] add CLI test foundation Signed-off-by: Alex Goodman <[email protected]> * test: wire CLI tests up in Taskfile Signed-off-by: Will Murphy <[email protected]> --------- Signed-off-by: Alex Goodman <[email protected]> Signed-off-by: Will Murphy <[email protected]> Co-authored-by: Alex Goodman <[email protected]>
- Loading branch information
1 parent
479001c
commit 01de30c
Showing
6 changed files
with
430 additions
and
1 deletion.
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
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,68 @@ | ||
package cli | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestInstallCmd(t *testing.T) { | ||
|
||
type step struct { | ||
name string | ||
args []string | ||
env map[string]string | ||
assertions []traitAssertion | ||
} | ||
|
||
tests := []struct { | ||
name string | ||
steps []step | ||
}{ | ||
{ | ||
name: "use go-install method", | ||
steps: []step{ | ||
{ | ||
name: "install", | ||
args: []string{"install", "-c", "testdata/go-install-method.yaml"}, | ||
assertions: []traitAssertion{ | ||
assertSuccessfulReturnCode, | ||
assertFileInStoreExists(".binny.state.json"), | ||
assertFileInStoreExists("binny"), | ||
assertManagedToolOutput("binny", []string{"--version"}, "binny v0.7.0\n"), | ||
}, | ||
}, | ||
{ | ||
name: "list", | ||
args: []string{"list", "-c", "testdata/go-install-method.yaml", "-o", "json"}, | ||
assertions: []traitAssertion{ | ||
assertSuccessfulReturnCode, | ||
assertJson, | ||
assertInOutput(`"installedVersion": "v0.7.0"`), | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
// we always have a clean slate for every test, but a shared state for each step | ||
d := t.TempDir() | ||
|
||
for _, s := range test.steps { | ||
t.Run(s.name, func(t *testing.T) { | ||
if s.env == nil { | ||
s.env = make(map[string]string) | ||
} | ||
s.env["BINNY_ROOT"] = d | ||
|
||
cmd, stdout, stderr := runBinny(t, s.env, s.args...) | ||
for _, traitFn := range s.assertions { | ||
traitFn(t, d, stdout, stderr, cmd.ProcessState.ExitCode()) | ||
} | ||
|
||
logOutputOnFailure(t, cmd, stdout, stderr) | ||
}) | ||
} | ||
}) | ||
} | ||
} |
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,14 @@ | ||
tools: | ||
- name: binny | ||
version: | ||
want: v0.7.0 | ||
method: go-proxy | ||
with: | ||
module: github.com/anchore/binny | ||
allow-unresolved-version: true | ||
method: go-install | ||
with: | ||
entrypoint: cmd/binny | ||
module: github.com/anchore/binny | ||
ldflags: | ||
- -X main.version={{ .Version }} |
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,139 @@ | ||
package cli | ||
|
||
import ( | ||
"encoding/json" | ||
"github.com/google/go-cmp/cmp" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
"regexp" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/acarl005/stripansi" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
type traitAssertion func(tb testing.TB, storeRoot, stdout, stderr string, rc int) | ||
|
||
func assertFileOutput(tb testing.TB, path string, assertions ...traitAssertion) traitAssertion { | ||
tb.Helper() | ||
|
||
return func(tb testing.TB, storeRoot, _, stderr string, rc int) { | ||
content, err := os.ReadFile(path) | ||
require.NoError(tb, err) | ||
contentStr := string(content) | ||
|
||
for _, assertion := range assertions { | ||
// treat the file content as stdout | ||
assertion(tb, storeRoot, contentStr, stderr, rc) | ||
} | ||
} | ||
} | ||
|
||
func assertJson(tb testing.TB, _, stdout, _ string, _ int) { | ||
tb.Helper() | ||
var data interface{} | ||
|
||
if err := json.Unmarshal([]byte(stdout), &data); err != nil { | ||
tb.Errorf("expected to find a JSON report, but was unmarshalable: %+v", err) | ||
} | ||
} | ||
|
||
func assertLoggingLevel(level string) traitAssertion { | ||
// match examples: | ||
// "[0000] INFO" | ||
// "[0012] DEBUG" | ||
logPattern := regexp.MustCompile(`(?m)^\[\d\d\d\d\]\s+` + strings.ToUpper(level)) | ||
return func(tb testing.TB, _, _, stderr string, _ int) { | ||
tb.Helper() | ||
if !logPattern.MatchString(stripansi.Strip(stderr)) { | ||
tb.Errorf("output did not indicate the %q logging level", level) | ||
} | ||
} | ||
} | ||
|
||
func assertNotInOutput(data string) traitAssertion { | ||
return func(tb testing.TB, _, stdout, stderr string, _ int) { | ||
tb.Helper() | ||
if strings.Contains(stripansi.Strip(stderr), data) { | ||
tb.Errorf("data=%q was found in stderr, but should not have been there", data) | ||
} | ||
if strings.Contains(stripansi.Strip(stdout), data) { | ||
tb.Errorf("data=%q was found in stdout, but should not have been there", data) | ||
} | ||
} | ||
} | ||
|
||
func assertNoStderr(tb testing.TB, _, _, stderr string, _ int) { | ||
tb.Helper() | ||
if len(stderr) > 0 { | ||
tb.Errorf("expected stderr to be empty, but wasn't") | ||
if showOutput != nil && *showOutput { | ||
tb.Errorf("STDERR:%s", stderr) | ||
} | ||
} | ||
} | ||
|
||
func assertInOutput(data string) traitAssertion { | ||
return func(tb testing.TB, _, stdout, stderr string, _ int) { | ||
tb.Helper() | ||
stdout = stripansi.Strip(stdout) | ||
stderr = stripansi.Strip(stderr) | ||
if !strings.Contains(stdout, data) && !strings.Contains(stderr, data) { | ||
tb.Errorf("data=%q was NOT found in any output, but should have been there", data) | ||
if showOutput != nil && *showOutput { | ||
tb.Errorf("STDOUT:%s\nSTDERR:%s", stdout, stderr) | ||
} | ||
} | ||
} | ||
} | ||
|
||
func assertStdoutLengthGreaterThan(length uint) traitAssertion { | ||
return func(tb testing.TB, _, stdout, _ string, _ int) { | ||
tb.Helper() | ||
if uint(len(stdout)) < length { | ||
tb.Errorf("not enough output (expected at least %d, got %d)", length, len(stdout)) | ||
} | ||
} | ||
} | ||
|
||
func assertFailingReturnCode(tb testing.TB, _, _, _ string, rc int) { | ||
tb.Helper() | ||
if rc == 0 { | ||
tb.Errorf("expected a failure but got rc=%d", rc) | ||
} | ||
} | ||
|
||
func assertSuccessfulReturnCode(tb testing.TB, _, _, _ string, rc int) { | ||
tb.Helper() | ||
if rc != 0 { | ||
tb.Errorf("expected no failure but got rc=%d", rc) | ||
} | ||
} | ||
|
||
func assertFileInStoreExists(file string) traitAssertion { | ||
return func(tb testing.TB, storeRoot, _, _ string, _ int) { | ||
tb.Helper() | ||
path := filepath.Join(storeRoot, file) | ||
if _, err := os.Stat(path); err != nil { | ||
tb.Errorf("expected file to exist %s", path) | ||
} | ||
} | ||
} | ||
|
||
func assertManagedToolOutput(tool string, args []string, expectedStdout string) traitAssertion { | ||
return func(tb testing.TB, storeRoot, _, _ string, _ int) { | ||
tb.Helper() | ||
|
||
path := filepath.Join(storeRoot, tool) | ||
cmd := exec.Command(path, args...) | ||
|
||
gotStdout, _, err := runCommand(cmd, nil) | ||
require.NoError(tb, err) | ||
|
||
if d := cmp.Diff(expectedStdout, gotStdout); d != "" { | ||
tb.Errorf("unexpected output (-want +got):\n%s", d) | ||
} | ||
} | ||
} |
Oops, something went wrong.