Skip to content

Commit

Permalink
add test & code clean
Browse files Browse the repository at this point in the history
Signed-off-by: Billy Zha <[email protected]>
  • Loading branch information
qweeah committed Oct 23, 2023
1 parent 3ea07c2 commit 581d03f
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 7 deletions.
4 changes: 2 additions & 2 deletions cmd/oras/internal/display/track/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (
type GraphTarget interface {
oras.GraphTarget
io.Closer
Prompt(desc ocispec.Descriptor, prompt string, verbose bool) error
Prompt(desc ocispec.Descriptor, prompt string) error
}

type graphTarget struct {
Expand Down Expand Up @@ -102,7 +102,7 @@ func (t *graphTarget) Close() error {
}

// Prompt prompts the user with the provided prompt and descriptor.
func (t *graphTarget) Prompt(desc ocispec.Descriptor, prompt string, verbose bool) error {
func (t *graphTarget) Prompt(desc ocispec.Descriptor, prompt string) error {
status, err := t.manager.Add()
if err != nil {
return err
Expand Down
28 changes: 28 additions & 0 deletions cmd/oras/internal/display/track/target_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,31 @@ func Test_referenceGraphTarget_PushReference(t *testing.T) {
t.Fatal(err)
}
}

func Test_referenceGraphTarget_Prompt(t *testing.T) {
// prepare
pty, device, err := testutils.NewPty()
if err != nil {
t.Fatal(err)
}
defer device.Close()
content := []byte("test")
desc := ocispec.Descriptor{
MediaType: "application/octet-stream",
Digest: digest.FromBytes(content),
Size: int64(len(content)),
}
// test
prompt := "prompt"
target, err := NewTarget(memory.New(), "action", "done", device)
if err != nil {
t.Fatal(err)
}
m := target.(*graphTarget).manager
target.Prompt(desc, prompt)
m.Close()
// validate
if err = testutils.MatchPty(pty, device, prompt, desc.MediaType, "100.00%", desc.Digest.String()); err != nil {
t.Fatal(err)
}
}
4 changes: 2 additions & 2 deletions cmd/oras/root/cp.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,12 +180,12 @@ func doCopy(ctx context.Context, src oras.ReadOnlyGraphTarget, dst oras.GraphTar
dst = tracked
extendedCopyOptions.OnCopySkipped = func(ctx context.Context, desc ocispec.Descriptor) error {
committed.Store(desc.Digest.String(), desc.Annotations[ocispec.AnnotationTitle])
return tracked.Prompt(desc, "Exists ", opts.Verbose)
return tracked.Prompt(desc, "Exists ")
}
extendedCopyOptions.PostCopy = func(ctx context.Context, desc ocispec.Descriptor) error {
committed.Store(desc.Digest.String(), desc.Annotations[ocispec.AnnotationTitle])
return display.PrintSuccessorStatus(ctx, desc, tracked, committed, func(desc ocispec.Descriptor) error {
return tracked.Prompt(desc, "Skipped", opts.Verbose)
return tracked.Prompt(desc, "Skipped")
})
}
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/oras/root/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,5 +293,5 @@ func printOnce(printed *sync.Map, s ocispec.Descriptor, msg string, verbose bool
return display.PrintStatus(s, msg, verbose)
}
// TTY
return tracked.Prompt(s, msg, verbose)
return tracked.Prompt(s, msg)
}
4 changes: 2 additions & 2 deletions cmd/oras/root/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,12 +263,12 @@ func updateDisplayOption(opts *oras.CopyGraphOptions, fetcher content.Fetcher, v
// TTY
opts.OnCopySkipped = func(ctx context.Context, desc ocispec.Descriptor) error {
committed.Store(desc.Digest.String(), desc.Annotations[ocispec.AnnotationTitle])
return tracked.Prompt(desc, "Exists ", verbose)
return tracked.Prompt(desc, "Exists ")
}
opts.PostCopy = func(ctx context.Context, desc ocispec.Descriptor) error {
committed.Store(desc.Digest.String(), desc.Annotations[ocispec.AnnotationTitle])
return display.PrintSuccessorStatus(ctx, desc, fetcher, committed, func(d ocispec.Descriptor) error {
return tracked.Prompt(d, "Skipped ", verbose)
return tracked.Prompt(d, "Skipped ")
})
}
}
Expand Down

0 comments on commit 581d03f

Please sign in to comment.