Skip to content

Commit

Permalink
fix: fix bugs and tests
Browse files Browse the repository at this point in the history
Signed-off-by: Shiwei Zhang <[email protected]>
  • Loading branch information
shizhMSFT committed Dec 30, 2024
1 parent d327a75 commit 5246e26
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 19 deletions.
2 changes: 2 additions & 0 deletions cmd/oras/internal/display/status/progress/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ func NewManager(tty *os.File, prompt map[progress.State]string) (progress.Manage
console: c,
renderDone: make(chan struct{}),
renderClosed: make(chan struct{}),
prompt: prompt,
}
m.start()
return m, nil
Expand Down Expand Up @@ -105,6 +106,7 @@ func (m *manager) Track(desc ocispec.Descriptor) (progress.Tracker, error) {
}

s := newStatus()
s.descriptor = desc
m.statusLock.Lock()
m.status = append(m.status, s)
m.statusLock.Unlock()
Expand Down
3 changes: 2 additions & 1 deletion cmd/oras/internal/display/status/progress/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"fmt"
"testing"

ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"oras.land/oras/cmd/oras/internal/display/status/console"
"oras.land/oras/internal/testutils"
)
Expand All @@ -41,7 +42,7 @@ func Test_manager_render(t *testing.T) {
}
height, _ := m.console.GetHeightWidth()
for i := 0; i < height; i++ {
if _, err := m.Add(); err != nil {
if _, err := m.Track(ocispec.Descriptor{}); err != nil {
t.Fatal(err)
}
}
Expand Down
28 changes: 16 additions & 12 deletions cmd/oras/internal/display/status/progress/messenger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,24 @@ limitations under the License.
package progress

import (
v1 "github.com/opencontainers/image-spec/specs-go/v1"
"testing"

ocispec "github.com/opencontainers/image-spec/specs-go/v1"
)

func Test_Messenger(t *testing.T) {
var msg *status
ch := make(chan *status, BufferSize)
messenger := &Messenger{ch: ch}
desc := ocispec.Descriptor{
Digest: "mouse",
Size: 100,
}
messenger := &Messenger{
ch: ch,
desc: desc,
}

messenger.Start()
messenger.start()
select {
case msg = <-ch:
if msg.offset != -1 {
Expand All @@ -35,12 +43,8 @@ func Test_Messenger(t *testing.T) {
t.Error("Expected start message")
}

desc := v1.Descriptor{
Digest: "mouse",
Size: 100,
}
expected := int64(50)
messenger.Send("Reading", desc, expected)
messenger.send("Reading", expected)
select {
case msg = <-ch:
if msg.offset != expected {
Expand All @@ -53,8 +57,8 @@ func Test_Messenger(t *testing.T) {
t.Error("Expected status message")
}

messenger.Send("Reading", desc, expected)
messenger.Send("Read", desc, desc.Size)
messenger.send("Reading", expected)
messenger.send("Read", desc.Size)
select {
case msg = <-ch:
if msg.offset != desc.Size {
Expand All @@ -73,7 +77,7 @@ func Test_Messenger(t *testing.T) {
}

expected = int64(-1)
messenger.Stop()
messenger.stop()
select {
case msg = <-ch:
if msg.offset != expected {
Expand All @@ -83,7 +87,7 @@ func Test_Messenger(t *testing.T) {
t.Error("Expected END status message")
}

messenger.Stop()
messenger.stop()
select {
case msg = <-ch:
if msg != nil {
Expand Down
13 changes: 9 additions & 4 deletions cmd/oras/internal/display/status/track/target_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"oras.land/oras-go/v2/content/memory"
"oras.land/oras-go/v2/errdef"
"oras.land/oras-go/v2/registry/remote"
"oras.land/oras/internal/progress"
"oras.land/oras/internal/testutils"
)

Expand Down Expand Up @@ -62,9 +63,11 @@ func Test_referenceGraphTarget_PushReference(t *testing.T) {
}
// test
tag := "tagged"
actionPrompt := "action"
donePrompt := "done"
target, err := NewTarget(&testReferenceGraphTarget{src}, actionPrompt, donePrompt, device)
prompt := map[progress.State]string{
progress.StateTransmitted: donePrompt,
}
target, err := NewTarget(&testReferenceGraphTarget{src}, prompt, device)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -108,9 +111,11 @@ func Test_graphTarget_Push_alreadyExists(t *testing.T) {
t.Fatal("Failed to prepare test environment:", err)
}
// test
actionPrompt := "action"
donePrompt := "done"
target, err := NewTarget(src, actionPrompt, donePrompt, device)
prompt := map[progress.State]string{
progress.StateTransmitted: donePrompt,
}
target, err := NewTarget(src, prompt, device)
if err != nil {
t.Fatal(err)
}
Expand Down
5 changes: 3 additions & 2 deletions internal/testutils/prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (

ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"oras.land/oras-go/v2"
"oras.land/oras/internal/progress"
)

// PromptDiscarder mocks trackable GraphTarget with discarded prompt.
Expand All @@ -29,7 +30,7 @@ type PromptDiscarder struct {
}

// Prompt discards the prompt.
func (p *PromptDiscarder) Prompt(ocispec.Descriptor, string) error {
func (p *PromptDiscarder) Report(ocispec.Descriptor, progress.State) error {
return nil
}

Expand All @@ -48,6 +49,6 @@ func NewErrorPrompt(err error) *ErrorPrompt {
}

// Prompt mocks an errored prompt.
func (e *ErrorPrompt) Prompt(ocispec.Descriptor, string) error {
func (e *ErrorPrompt) Report(ocispec.Descriptor, progress.State) error {
return e.wanted
}

0 comments on commit 5246e26

Please sign in to comment.