Skip to content

Commit

Permalink
[PRFix modify test and use display.PrintStatus]
Browse files Browse the repository at this point in the history
Signed-off-by: Zoey Li <zoeyli@microsoft.com>
  • Loading branch information
lizMSFT committed Aug 24, 2022
1 parent 97c834b commit e41becd
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 27 deletions.
5 changes: 2 additions & 3 deletions cmd/oras/blob/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func pushCmd() *cobra.Command {
Example - Push blob "hi.txt":
oras blob push localhost:5000/hello hi.txt
Example - Push blob to the insecure registry:
Example - Push blob without TLS:
oras blob push localhost:5000/hello hi.txt --insecure
`,
Args: cobra.ExactArgs(2),
Expand Down Expand Up @@ -80,8 +80,7 @@ func pushBlob(opts pushBlobOptions) (err error) {
return err
}
if exists {
statusPrinter := display.StatusPrinter("Exists ", opts.Verbose)
if err := statusPrinter(ctx, desc); err != nil {
if err := display.PrintStatus(desc, "Exists ", opts.Verbose); err != nil {
return err
}
} else {
Expand Down
5 changes: 2 additions & 3 deletions internal/file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,9 @@ func PrepareContent(path string, mediaType string) (ocispec.Descriptor, io.ReadC
return ocispec.Descriptor{}, nil, err
}

desc := ocispec.Descriptor{
return ocispec.Descriptor{
MediaType: mediaType,
Digest: dgst,
Size: fi.Size(),
}
return desc, fp, nil
}, fp, nil
}
31 changes: 10 additions & 21 deletions internal/file/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ limitations under the License.
package file_test

import (
"io"
"os"
"path/filepath"
"reflect"
Expand All @@ -27,6 +28,8 @@ import (
"oras.land/oras/internal/file"
)

const blobMediaType = "application/mock-octet-stream"

func TestFile_PrepareContent(t *testing.T) {
// generate test content
tempDir := t.TempDir()
Expand All @@ -37,7 +40,6 @@ func TestFile_PrepareContent(t *testing.T) {
t.Fatal("error calling WriteFile(), error =", err)
}

blobMediaType := "application/octet-stream"
want := ocispec.Descriptor{
MediaType: blobMediaType,
Digest: digest.FromBytes(content),
Expand All @@ -53,19 +55,16 @@ func TestFile_PrepareContent(t *testing.T) {
if !reflect.DeepEqual(got, want) {
t.Errorf("PrepareContent() = %v, want %v", got, want)
}
actualContent, err := io.ReadAll(rc)
if err != nil {
t.Fatal("PrepareContent(): not able to read content from rc, error=", err)
}
if !reflect.DeepEqual(actualContent, content) {
t.Errorf("PrepareContent() = %v, want %v", actualContent, content)
}
}

func TestFile_PrepareContent_errMissingFileName(t *testing.T) {
// generate test content
tempDir := t.TempDir()
content := []byte("hello world!")
fileName := "test.txt"
path := filepath.Join(tempDir, fileName)
if err := os.WriteFile(path, content, 0444); err != nil {
t.Fatal("error calling WriteFile(), error =", err)
}
blobMediaType := "application/octet-stream"

// test PrepareContent with missing file name
_, _, err := file.PrepareContent("", blobMediaType)
expected := "missing file name"
Expand All @@ -75,16 +74,6 @@ func TestFile_PrepareContent_errMissingFileName(t *testing.T) {
}

func TestFile_PrepareContent_errOpenFile(t *testing.T) {
// generate test content
tempDir := t.TempDir()
content := []byte("hello world!")
fileName := "test.txt"
path := filepath.Join(tempDir, fileName)
if err := os.WriteFile(path, content, 0444); err != nil {
t.Fatal("error calling WriteFile(), error =", err)
}
blobMediaType := "application/octet-stream"

// test PrepareContent with nonexistent file
_, _, err := file.PrepareContent("nonexistent.txt", blobMediaType)
expected := "failed to open nonexistent.txt"
Expand Down

0 comments on commit e41becd

Please sign in to comment.