Skip to content

Commit

Permalink
use expectErr
Browse files Browse the repository at this point in the history
  • Loading branch information
linyows committed Nov 17, 2024
1 parent 87d0c3d commit 458d9a2
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
20 changes: 13 additions & 7 deletions artifact/s3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ import (

func TestNewS3(t *testing.T) {
tests := []struct {
desc string
url string
expected *S3
err error
desc string
url string
expected *S3
expectErr bool
err error
}{
{
"valid small structure is returned",
Expand All @@ -30,6 +31,7 @@ func TestNewS3(t *testing.T) {
Endpoint: "",
url: "s3://ap-northeast-1/mybucket/v1.2.3/myapp-linux-x86_64.zip",
},
false,
nil,
},
{
Expand All @@ -42,21 +44,25 @@ func TestNewS3(t *testing.T) {
Endpoint: "http://localhost:9999/foobar",
url: "s3://ap-northeast-1/mybucket/myteam/myapp/v1.2.3/myapp-linux-x86_64.zip?endpoint=http://localhost:9999/foobar",
},
false,
nil,
},
{
"error is returned",
"s3://ap",
nil,
true,
fmt.Errorf("url parse error: s3://ap"),
},
}

for _, tt := range tests {
t.Run(tt.desc, func(t *testing.T) {
s3, err := NewS3(context.Background(), tt.url)
if err != tt.err && err.Error() != tt.err.Error() {
t.Errorf("expected error %s, got %s", tt.err, err)
if tt.expectErr {
if err == nil || err.Error() != tt.err.Error() {
t.Errorf("expected error %s, got %s", tt.err, err)
}
} else {
opts := []cmp.Option{
cmp.AllowUnexported(S3{}),
Expand All @@ -78,7 +84,7 @@ func (m *MockS3Client) GetObject(ctx context.Context, input *s3.GetObjectInput,
return m.GetObjectFunc(ctx, input, opts...)
}

// Helper to simulate an error during io.Copy
// Helper to simulate an error during io.Copy.
type errorReader struct{}

func (r *errorReader) Read(p []byte) (int, error) {
Expand Down
18 changes: 12 additions & 6 deletions registry/s3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ import (

func TestNewS3(t *testing.T) {
tests := []struct {
desc string
url string
expected *S3
err error
desc string
url string
expected *S3
expectErr bool
err error
}{
{
"valid small structure is returned",
Expand All @@ -29,6 +30,7 @@ func TestNewS3(t *testing.T) {
Endpoint: "",
Artifact: "",
},
false,
nil,
},
{
Expand All @@ -41,21 +43,25 @@ func TestNewS3(t *testing.T) {
Endpoint: "http://localhost:9999/foobar",
Artifact: "myapp-linux-x86_64.zip",
},
false,
nil,
},
{
"error is returned",
"s3://ap",
nil,
true,
fmt.Errorf("bucket is required: %s", s3Format),
},
}

for _, tt := range tests {
t.Run(tt.desc, func(t *testing.T) {
s3, err := NewS3(context.Background(), tt.url)
if err != tt.err && err.Error() != tt.err.Error() {
t.Errorf("expected error %s, got %s", tt.err, err)
if tt.expectErr {
if err == nil || err.Error() != tt.err.Error() {
t.Errorf("expected error %s, got %s", tt.err, err)
}
} else {
opts := []cmp.Option{
cmp.AllowUnexported(S3{}),
Expand Down

0 comments on commit 458d9a2

Please sign in to comment.