Skip to content

Commit 8986d6d

Browse files
author
Terry Howe
authored
refactor: DRY up text_test (#1473)
Signed-off-by: Terry Howe <[email protected]>
1 parent 141c5e1 commit 8986d6d

File tree

3 files changed

+75
-104
lines changed

3 files changed

+75
-104
lines changed

cmd/oras/internal/display/status/text_test.go

Lines changed: 35 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -43,187 +43,138 @@ func TestMain(m *testing.M) {
4343
m.Run()
4444
}
4545

46+
func validatePrinted(t *testing.T, expected string) {
47+
actual := strings.TrimSpace(builder.String())
48+
if expected != actual {
49+
t.Error("Output does not match expected <" + expected + "> actual <" + actual + ">")
50+
}
51+
}
52+
4653
func TestTextCopyHandler_OnMounted(t *testing.T) {
47-
defer builder.Reset()
48-
expected := "Mounted 0b442c23c1dd oci-image"
54+
builder.Reset()
4955
ch := NewTextCopyHandler(printer, mockFetcher.Fetcher)
5056
if ch.OnMounted(ctx, mockFetcher.OciImage) != nil {
5157
t.Error("OnMounted() should not return an error")
5258
}
53-
actual := strings.TrimSpace(builder.String())
54-
if expected != actual {
55-
t.Error("Output does not match expected <" + expected + "> actual <" + actual + ">")
56-
}
59+
validatePrinted(t, "Mounted 0b442c23c1dd oci-image")
5760
}
5861

5962
func TestTextCopyHandler_OnCopySkipped(t *testing.T) {
60-
defer builder.Reset()
61-
expected := "Exists 0b442c23c1dd oci-image"
63+
builder.Reset()
6264
ch := NewTextCopyHandler(printer, mockFetcher.Fetcher)
6365
if ch.OnCopySkipped(ctx, mockFetcher.OciImage) != nil {
6466
t.Error("OnCopySkipped() should not return an error")
6567
}
66-
actual := strings.TrimSpace(builder.String())
67-
if expected != actual {
68-
t.Error("Output does not match expected <" + expected + "> actual <" + actual + ">")
69-
}
68+
validatePrinted(t, "Exists 0b442c23c1dd oci-image")
7069
}
7170

7271
func TestTextCopyHandler_PostCopy(t *testing.T) {
73-
defer builder.Reset()
74-
expected := "Copied 0b442c23c1dd oci-image"
72+
builder.Reset()
7573
ch := NewTextCopyHandler(printer, mockFetcher.Fetcher)
7674
if ch.PostCopy(ctx, mockFetcher.OciImage) != nil {
7775
t.Error("PostCopy() should not return an error")
7876
}
7977
if ch.PostCopy(ctx, bogus) == nil {
8078
t.Error("PostCopy() should return an error")
8179
}
82-
actual := strings.TrimSpace(builder.String())
83-
if expected != actual {
84-
t.Error("Output does not match expected <" + expected + "> actual <" + actual + ">")
85-
}
80+
validatePrinted(t, "Copied 0b442c23c1dd oci-image")
8681
}
8782

8883
func TestTextCopyHandler_PreCopy(t *testing.T) {
89-
defer builder.Reset()
90-
expected := "Copying 0b442c23c1dd oci-image"
84+
builder.Reset()
9185
ch := NewTextCopyHandler(printer, mockFetcher.Fetcher)
9286
if ch.PreCopy(ctx, mockFetcher.OciImage) != nil {
9387
t.Error("PreCopy() should not return an error")
9488
}
95-
actual := strings.TrimSpace(builder.String())
96-
if expected != actual {
97-
t.Error("Output does not match expected <" + expected + "> actual <" + actual + ">")
98-
}
89+
validatePrinted(t, "Copying 0b442c23c1dd oci-image")
9990
}
10091

10192
func TestTextPullHandler_OnNodeDownloaded(t *testing.T) {
102-
defer builder.Reset()
103-
expected := "Downloaded 0b442c23c1dd oci-image"
93+
builder.Reset()
10494
ph := NewTextPullHandler(printer)
10595
if ph.OnNodeDownloaded(mockFetcher.OciImage) != nil {
10696
t.Error("OnNodeDownloaded() should not return an error")
10797
}
108-
actual := strings.TrimSpace(builder.String())
109-
if expected != actual {
110-
t.Error("Output does not match expected <" + expected + "> actual <" + actual + ">")
111-
}
98+
validatePrinted(t, "Downloaded 0b442c23c1dd oci-image")
11299
}
113100

114101
func TestTextPullHandler_OnNodeDownloading(t *testing.T) {
115-
defer builder.Reset()
116-
expected := "Downloading 0b442c23c1dd oci-image"
102+
builder.Reset()
117103
ph := NewTextPullHandler(printer)
118104
if ph.OnNodeDownloading(mockFetcher.OciImage) != nil {
119105
t.Error("OnNodeDownloading() should not return an error")
120106
}
121-
actual := strings.TrimSpace(builder.String())
122-
if expected != actual {
123-
t.Error("Output does not match expected <" + expected + "> actual <" + actual + ">")
124-
}
107+
validatePrinted(t, "Downloading 0b442c23c1dd oci-image")
125108
}
126109

127110
func TestTextPullHandler_OnNodeProcessing(t *testing.T) {
128-
defer builder.Reset()
129-
expected := "Processing 0b442c23c1dd oci-image"
111+
builder.Reset()
130112
ph := NewTextPullHandler(printer)
131113
if ph.OnNodeProcessing(mockFetcher.OciImage) != nil {
132114
t.Error("OnNodeProcessing() should not return an error")
133115
}
134-
actual := strings.TrimSpace(builder.String())
135-
if expected != actual {
136-
t.Error("Output does not match expected <" + expected + "> actual <" + actual + ">")
137-
}
116+
validatePrinted(t, "Processing 0b442c23c1dd oci-image")
138117
}
139118

140119
func TestTextPullHandler_OnNodeRestored(t *testing.T) {
141-
defer builder.Reset()
142-
expected := "Restored 0b442c23c1dd oci-image"
120+
builder.Reset()
143121
ph := NewTextPullHandler(printer)
144122
if ph.OnNodeRestored(mockFetcher.OciImage) != nil {
145123
t.Error("OnNodeRestored() should not return an error")
146124
}
147-
actual := strings.TrimSpace(builder.String())
148-
if expected != actual {
149-
t.Error("Output does not match expected <" + expected + "> actual <" + actual + ">")
150-
}
125+
validatePrinted(t, "Restored 0b442c23c1dd oci-image")
151126
}
152127

153128
func TestTextPullHandler_OnNodeSkipped(t *testing.T) {
154-
defer builder.Reset()
155-
expected := "Skipped 0b442c23c1dd oci-image"
129+
builder.Reset()
156130
ph := NewTextPullHandler(printer)
157131
if ph.OnNodeSkipped(mockFetcher.OciImage) != nil {
158132
t.Error("OnNodeSkipped() should not return an error")
159133
}
160-
actual := strings.TrimSpace(builder.String())
161-
if expected != actual {
162-
t.Error("Output does not match expected <" + expected + "> actual <" + actual + ">")
163-
}
134+
validatePrinted(t, "Skipped 0b442c23c1dd oci-image")
164135
}
165136

166137
func TestTextPushHandler_OnCopySkipped(t *testing.T) {
167-
defer builder.Reset()
168-
expected := "Exists 0b442c23c1dd oci-image"
138+
builder.Reset()
169139
ph := NewTextPushHandler(printer, mockFetcher.Fetcher)
170140
if ph.OnCopySkipped(ctx, mockFetcher.OciImage) != nil {
171141
t.Error("OnCopySkipped() should not return an error")
172142
}
173-
actual := strings.TrimSpace(builder.String())
174-
if expected != actual {
175-
t.Error("Output does not match expected <" + expected + "> actual <" + actual + ">")
176-
}
143+
validatePrinted(t, "Exists 0b442c23c1dd oci-image")
177144
}
178145

179146
func TestTextPushHandler_OnEmptyArtifact(t *testing.T) {
180-
defer builder.Reset()
181-
expected := "Uploading empty artifact"
147+
builder.Reset()
182148
ph := NewTextPushHandler(printer, mockFetcher.Fetcher)
183149
if ph.OnEmptyArtifact() != nil {
184150
t.Error("OnEmptyArtifact() should not return an error")
185151
}
186-
actual := strings.TrimSpace(builder.String())
187-
if expected != actual {
188-
t.Error("Output does not match expected <" + expected + "> actual <" + actual + ">")
189-
}
152+
validatePrinted(t, "Uploading empty artifact")
190153
}
191154

192155
func TestTextPushHandler_OnFileLoading(t *testing.T) {
193-
defer builder.Reset()
194-
expected := ""
156+
builder.Reset()
195157
ph := NewTextPushHandler(printer, mockFetcher.Fetcher)
196158
if ph.OnFileLoading("name") != nil {
197159
t.Error("OnFileLoading() should not return an error")
198160
}
199-
actual := strings.TrimSpace(builder.String())
200-
if expected != actual {
201-
t.Error("Output does not match expected <" + expected + "> actual <" + actual + ">")
202-
}
161+
validatePrinted(t, "")
203162
}
204163

205164
func TestTextPushHandler_PostCopy(t *testing.T) {
206-
defer builder.Reset()
207-
expected := "Uploaded 0b442c23c1dd oci-image"
165+
builder.Reset()
208166
ph := NewTextPushHandler(printer, mockFetcher.Fetcher)
209167
if ph.PostCopy(ctx, mockFetcher.OciImage) != nil {
210168
t.Error("PostCopy() should not return an error")
211169
}
212-
actual := strings.TrimSpace(builder.String())
213-
if expected != actual {
214-
t.Error("Output does not match expected <" + expected + "> actual <" + actual + ">")
215-
}
170+
validatePrinted(t, "Uploaded 0b442c23c1dd oci-image")
216171
}
217172

218173
func TestTextPushHandler_PreCopy(t *testing.T) {
219-
defer builder.Reset()
220-
expected := "Uploading 0b442c23c1dd oci-image"
174+
builder.Reset()
221175
ph := NewTextPushHandler(printer, mockFetcher.Fetcher)
222176
if ph.PreCopy(ctx, mockFetcher.OciImage) != nil {
223177
t.Error("PreCopy() should not return an error")
224178
}
225-
actual := strings.TrimSpace(builder.String())
226-
if expected != actual {
227-
t.Error("Output does not match expected <" + expected + "> actual <" + actual + ">")
228-
}
179+
validatePrinted(t, "Uploading 0b442c23c1dd oci-image")
229180
}

internal/testutils/error_fetcher.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
Copyright The ORAS Authors.
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.
14+
*/
15+
16+
package testutils
17+
18+
import (
19+
"context"
20+
"fmt"
21+
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
22+
"io"
23+
)
24+
25+
// ErrorFetcher implements content.Fetcher.
26+
type ErrorFetcher struct {
27+
ExpectedError error
28+
}
29+
30+
// NewErrorFetcher create and error fetcher
31+
func NewErrorFetcher() *ErrorFetcher {
32+
return &ErrorFetcher{
33+
ExpectedError: fmt.Errorf("expected error"),
34+
}
35+
}
36+
37+
// Fetch returns an error.
38+
func (f *ErrorFetcher) Fetch(context.Context, ocispec.Descriptor) (io.ReadCloser, error) {
39+
return nil, f.ExpectedError
40+
}

internal/testutils/fetcher.go

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,33 +19,13 @@ import (
1919
"bytes"
2020
"context"
2121
"encoding/json"
22-
"fmt"
23-
"io"
24-
2522
"github.com/opencontainers/go-digest"
2623
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
2724
"oras.land/oras-go/v2/content"
2825
"oras.land/oras-go/v2/content/memory"
2926
"oras.land/oras/internal/docker"
3027
)
3128

32-
// ErrorFetcher implements content.Fetcher.
33-
type ErrorFetcher struct {
34-
ExpectedError error
35-
}
36-
37-
// NewErrorFetcher create and error fetcher
38-
func NewErrorFetcher() *ErrorFetcher {
39-
return &ErrorFetcher{
40-
ExpectedError: fmt.Errorf("expected error"),
41-
}
42-
}
43-
44-
// Fetch returns an error.
45-
func (f *ErrorFetcher) Fetch(context.Context, ocispec.Descriptor) (io.ReadCloser, error) {
46-
return nil, f.ExpectedError
47-
}
48-
4929
// MockFetcher implements content.Fetcher and populates a memory store.
5030
type MockFetcher struct {
5131
store *memory.Store

0 commit comments

Comments
 (0)