Skip to content
This repository was archived by the owner on Jul 18, 2025. It is now read-only.

Commit fb9defa

Browse files
author
Jean-Christophe Sirot
committed
Add unit test for -q flag of image ls command
Signed-off-by: Jean-Christophe Sirot <[email protected]>
1 parent 8b6826d commit fb9defa

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed

internal/commands/image/list_test.go

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package image
2+
3+
import (
4+
"bufio"
5+
"bytes"
6+
"fmt"
7+
"testing"
8+
9+
"gotest.tools/assert"
10+
11+
"github.com/deislabs/cnab-go/bundle"
12+
"github.com/docker/app/internal/store"
13+
"github.com/docker/cli/cli/command"
14+
"github.com/docker/distribution/reference"
15+
)
16+
17+
type mockRef string
18+
19+
func (ref mockRef) String() string {
20+
return string(ref)
21+
}
22+
23+
type bundleStoreStubForListCmd struct {
24+
refMap map[reference.Reference]*bundle.Bundle
25+
// in order to keep the reference in the same order between tests
26+
refList []reference.Reference
27+
}
28+
29+
func (b *bundleStoreStubForListCmd) Store(ref reference.Reference, bndle *bundle.Bundle) (reference.Reference, error) {
30+
b.refMap[ref] = bndle
31+
b.refList = append(b.refList, ref)
32+
return ref, nil
33+
}
34+
35+
func (b *bundleStoreStubForListCmd) Read(ref reference.Reference) (*bundle.Bundle, error) {
36+
bndl, ok := b.refMap[ref]
37+
if ok {
38+
return bndl, nil
39+
}
40+
return nil, fmt.Errorf("Bundle not found")
41+
}
42+
43+
func (b *bundleStoreStubForListCmd) List() ([]reference.Reference, error) {
44+
return b.refList, nil
45+
}
46+
47+
func (b *bundleStoreStubForListCmd) Remove(ref reference.Reference) error {
48+
return nil
49+
}
50+
51+
func TestListWithQuietFlag(t *testing.T) {
52+
var buf bytes.Buffer
53+
w := bufio.NewWriter(&buf)
54+
dockerCli, err := command.NewDockerCli(command.WithOutputStream(w))
55+
assert.NilError(t, err)
56+
bundleStore := &bundleStoreStubForListCmd{
57+
refMap: make(map[reference.Reference]*bundle.Bundle),
58+
refList: []reference.Reference{},
59+
}
60+
ref1, err := store.FromString("a855ac937f2ed375ba4396bbc49c4093e124da933acd2713fb9bc17d7562a087")
61+
assert.NilError(t, err)
62+
ref2, err := reference.Parse("foo/bar:1.0")
63+
assert.NilError(t, err)
64+
_, err = bundleStore.Store(ref1, &bundle.Bundle{})
65+
assert.NilError(t, err)
66+
_, err = bundleStore.Store(ref2, &bundle.Bundle{
67+
Version: "1.0.0",
68+
SchemaVersion: "1.0.0",
69+
Name: "Foo App",
70+
})
71+
assert.NilError(t, err)
72+
err = runList(dockerCli, imageListOption{quiet: true}, bundleStore)
73+
assert.NilError(t, err)
74+
expectedOutput := `a855ac937f2e
75+
9aae408ee04f
76+
`
77+
w.Flush()
78+
assert.Equal(t, buf.String(), expectedOutput)
79+
}

0 commit comments

Comments
 (0)