From eb11c71e291139d1f607dfe66b97cdcfe0bff556 Mon Sep 17 00:00:00 2001 From: Xiaoxuan Wang Date: Mon, 8 Apr 2024 06:23:55 +0000 Subject: [PATCH] increase coverage Signed-off-by: Xiaoxuan Wang --- .../display/metadata/text/push_test.go | 74 +++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 cmd/oras/internal/display/metadata/text/push_test.go diff --git a/cmd/oras/internal/display/metadata/text/push_test.go b/cmd/oras/internal/display/metadata/text/push_test.go new file mode 100644 index 000000000..b715fcd0e --- /dev/null +++ b/cmd/oras/internal/display/metadata/text/push_test.go @@ -0,0 +1,74 @@ +/* +Copyright The ORAS Authors. +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package text + +import ( + "bytes" + "fmt" + "io" + "testing" + + "github.com/opencontainers/go-digest" + ocispec "github.com/opencontainers/image-spec/specs-go/v1" +) + +// errorWriter implements the io.Writer interface returns an error in Write. +type errorWriter struct{} + +func (w *errorWriter) Write(p []byte) (n int, err error) { + return 0, fmt.Errorf("got an error") +} + +func TestPushHandler_OnCompleted(t *testing.T) { + content := []byte("content") + tests := []struct { + name string + out io.Writer + root ocispec.Descriptor + wantErr bool + }{ + { + "good path", + &bytes.Buffer{}, + ocispec.Descriptor{ + MediaType: "example", + Digest: digest.FromBytes(content), + Size: int64(len(content)), + }, + false, + }, + { + "error path", + &errorWriter{}, + ocispec.Descriptor{ + MediaType: "example", + Digest: digest.FromBytes(content), + Size: int64(len(content)), + }, + true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + p := &PushHandler{ + out: tt.out, + } + if err := p.OnCompleted(tt.root); (err != nil) != tt.wantErr { + t.Errorf("PushHandler.OnCompleted() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +}