Skip to content

Commit

Permalink
resolved comments
Browse files Browse the repository at this point in the history
Signed-off-by: Xiaoxuan Wang <xiaoxuanwang@microsoft.com>
  • Loading branch information
Xiaoxuan Wang committed Nov 12, 2024
1 parent b3709ff commit fbedb63
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 54 deletions.
15 changes: 3 additions & 12 deletions cmd/oras/root/manifest/index/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,6 @@ func (tds *testCreateDisplayStatus) OnIndexPushed(path string) error {
return nil
}

func NewTestCreateDisplayStatus(onFetching, onFetched, onIndexPacked, onIndexPushed bool) status.ManifestIndexCreateHandler {
return &testCreateDisplayStatus{
onFetchingError: onFetching,
onFetchedError: onFetched,
onIndexPackedError: onIndexPacked,
onIndexPushedError: onIndexPushed,
}
}

func Test_fetchSourceManifests(t *testing.T) {
testContext := context.Background()
tests := []struct {
Expand All @@ -110,7 +101,7 @@ func Test_fetchSourceManifests(t *testing.T) {
{
name: "OnFetching error",
ctx: testContext,
displayStatus: NewTestCreateDisplayStatus(true, false, false, false),
displayStatus: &testCreateDisplayStatus{onFetchingError: true},
target: NewTestReadOnlyTarget("test content"),
sources: []string{"test"},
want: nil,
Expand All @@ -119,7 +110,7 @@ func Test_fetchSourceManifests(t *testing.T) {
{
name: "OnFetched error",
ctx: testContext,
displayStatus: NewTestCreateDisplayStatus(false, true, false, false),
displayStatus: &testCreateDisplayStatus{onFetchedError: true},
target: NewTestReadOnlyTarget("test content"),
sources: []string{"test"},
want: nil,
Expand All @@ -128,7 +119,7 @@ func Test_fetchSourceManifests(t *testing.T) {
{
name: "getPlatform error",
ctx: testContext,
displayStatus: NewTestCreateDisplayStatus(false, false, false, false),
displayStatus: &testCreateDisplayStatus{},
target: NewTestReadOnlyTarget("test content"),
sources: []string{"test"},
want: nil,
Expand Down
55 changes: 13 additions & 42 deletions cmd/oras/root/manifest/index/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,6 @@ import (
"oras.land/oras/cmd/oras/internal/display/status"
)

// // for quick drafting
// type badWriter struct {
// }

// func (bw *badWriter) Write(p []byte) (n int, err error) {
// return 0, fmt.Errorf("test error")
// }

type testUpdateDisplayStatus struct {
onFetchingError bool
onFetchedError bool
Expand Down Expand Up @@ -94,18 +86,6 @@ func (tds *testUpdateDisplayStatus) OnIndexMerged(indexRef string, desc ocispec.
return nil
}

func NewTestUpdateDisplayStatus(onFetching, onFetched, onIndexPacked, onIndexPushed, onManifestRemoved, onManifestAdded, onIndexMerged bool) status.ManifestIndexUpdateHandler {
return &testUpdateDisplayStatus{
onFetchingError: onFetching,
onFetchedError: onFetched,
onIndexPackedError: onIndexPacked,
onIndexPushedError: onIndexPushed,
onManifestRemovedError: onManifestRemoved,
onManifestAddedError: onManifestAdded,
onIndexMergedError: onIndexMerged,
}
}

var (
A = ocispec.Descriptor{
MediaType: ocispec.MediaTypeImageManifest,
Expand Down Expand Up @@ -138,7 +118,7 @@ func Test_doRemoveManifests(t *testing.T) {
name: "remove one matched item",
manifests: []ocispec.Descriptor{A, B, C},
digestSet: map[digest.Digest]bool{B.Digest: false},
displayStatus: NewTestUpdateDisplayStatus(false, false, false, false, false, false, false),
displayStatus: &testUpdateDisplayStatus{},
indexRef: "test01",
want: []ocispec.Descriptor{A, C},
wantErr: false,
Expand All @@ -147,7 +127,7 @@ func Test_doRemoveManifests(t *testing.T) {
name: "remove all matched items",
manifests: []ocispec.Descriptor{A, B, A, C, A, A, A},
digestSet: map[digest.Digest]bool{A.Digest: false},
displayStatus: NewTestUpdateDisplayStatus(false, false, false, false, false, false, false),
displayStatus: &testUpdateDisplayStatus{},
indexRef: "test02",
want: []ocispec.Descriptor{B, C},
wantErr: false,
Expand All @@ -156,7 +136,7 @@ func Test_doRemoveManifests(t *testing.T) {
name: "remove correctly when there is only one item",
manifests: []ocispec.Descriptor{A},
digestSet: map[digest.Digest]bool{A.Digest: false},
displayStatus: NewTestUpdateDisplayStatus(false, false, false, false, false, false, false),
displayStatus: &testUpdateDisplayStatus{},
indexRef: "test03",
want: []ocispec.Descriptor{},
wantErr: false,
Expand All @@ -165,7 +145,7 @@ func Test_doRemoveManifests(t *testing.T) {
name: "remove multiple distinct manifests",
manifests: []ocispec.Descriptor{A, B, C},
digestSet: map[digest.Digest]bool{A.Digest: false, C.Digest: false},
displayStatus: NewTestUpdateDisplayStatus(false, false, false, false, false, false, false),
displayStatus: &testUpdateDisplayStatus{},
indexRef: "test04",
want: []ocispec.Descriptor{B},
wantErr: false,
Expand All @@ -174,7 +154,7 @@ func Test_doRemoveManifests(t *testing.T) {
name: "remove multiple duplicate manifests",
manifests: []ocispec.Descriptor{A, B, C, C, B, A, B},
digestSet: map[digest.Digest]bool{A.Digest: false, C.Digest: false},
displayStatus: NewTestUpdateDisplayStatus(false, false, false, false, false, false, false),
displayStatus: &testUpdateDisplayStatus{},
indexRef: "test05",
want: []ocispec.Descriptor{B, B, B},
wantErr: false,
Expand All @@ -183,7 +163,7 @@ func Test_doRemoveManifests(t *testing.T) {
name: "return error when deleting a nonexistent item",
manifests: []ocispec.Descriptor{A, C},
digestSet: map[digest.Digest]bool{B.Digest: false},
displayStatus: NewTestUpdateDisplayStatus(false, false, false, false, false, false, false),
displayStatus: &testUpdateDisplayStatus{},
indexRef: "test06",
want: nil,
wantErr: true,
Expand All @@ -192,20 +172,11 @@ func Test_doRemoveManifests(t *testing.T) {
name: "handler error",
manifests: []ocispec.Descriptor{A, B, C},
digestSet: map[digest.Digest]bool{B.Digest: false},
displayStatus: NewTestUpdateDisplayStatus(false, false, false, false, true, false, false),
displayStatus: &testUpdateDisplayStatus{onManifestRemovedError: true},
indexRef: "test07",
want: nil,
wantErr: true,
},
// {
// name: "bad writer test",
// manifests: []ocispec.Descriptor{A, B, C},
// digestSet: map[digest.Digest]bool{B.Digest: false},
// displayStatus: status.NewTextManifestIndexUpdateHandler(output.NewPrinter(&badWriter{}, os.Stderr, false)),
// indexRef: "test draft",
// want: nil,
// wantErr: false,
// },
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down Expand Up @@ -235,7 +206,7 @@ func Test_fetchIndex(t *testing.T) {
{
name: "OnFetching error",
ctx: testContext,
handler: NewTestUpdateDisplayStatus(true, false, false, false, false, false, false),
handler: &testUpdateDisplayStatus{onFetchingError: true},
target: NewTestReadOnlyTarget("index"),
reference: "test",
want: ocispec.Index{},
Expand All @@ -244,7 +215,7 @@ func Test_fetchIndex(t *testing.T) {
{
name: "OnFetched error",
ctx: testContext,
handler: NewTestUpdateDisplayStatus(false, true, false, false, false, false, false),
handler: &testUpdateDisplayStatus{onFetchedError: true},
target: NewTestReadOnlyTarget("index"),
reference: "test",
want: ocispec.Index{},
Expand All @@ -253,7 +224,7 @@ func Test_fetchIndex(t *testing.T) {
{
name: "Unmarshall error",
ctx: testContext,
handler: NewTestUpdateDisplayStatus(false, false, false, false, false, false, false),
handler: &testUpdateDisplayStatus{},
target: NewTestReadOnlyTarget("index"),
reference: "test",
want: ocispec.Index{},
Expand Down Expand Up @@ -289,7 +260,7 @@ func Test_mergeIndexes(t *testing.T) {
{
name: "OnFetching error",
ctx: testContext,
displayStatus: NewTestUpdateDisplayStatus(true, false, false, false, false, false, false),
displayStatus: &testUpdateDisplayStatus{onFetchingError: true},
manifests: []ocispec.Descriptor{},
target: NewTestReadOnlyTarget("index"),
mergeArguments: []string{"test"},
Expand All @@ -299,7 +270,7 @@ func Test_mergeIndexes(t *testing.T) {
{
name: "OnFetched error",
ctx: testContext,
displayStatus: NewTestUpdateDisplayStatus(false, true, false, false, false, false, false),
displayStatus: &testUpdateDisplayStatus{onFetchedError: true},
manifests: []ocispec.Descriptor{},
target: NewTestReadOnlyTarget("index"),
mergeArguments: []string{"test"},
Expand All @@ -309,7 +280,7 @@ func Test_mergeIndexes(t *testing.T) {
{
name: "unmarshall error",
ctx: testContext,
displayStatus: NewTestUpdateDisplayStatus(false, false, false, false, false, false, false),
displayStatus: &testUpdateDisplayStatus{},
manifests: []ocispec.Descriptor{},
target: NewTestReadOnlyTarget("index"),
mergeArguments: []string{"test"},
Expand Down

0 comments on commit fbedb63

Please sign in to comment.