Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: cover missing lines [skip ci] #184

Merged
merged 1 commit into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions internal/aws/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,16 @@ import (
"github.com/bilalcaliskan/s3-manager/internal/utils"
)

func CreateClient(opts *options.RootOptions) (*s3.Client, error) {
func createConfig(opts *options.RootOptions) (cfg aws.Config, err error) {
appCreds := aws.NewCredentialsCache(credentials.NewStaticCredentialsProvider(opts.AccessKey, opts.SecretKey, ""))
cfg, err := config.LoadDefaultConfig(context.Background(),
return config.LoadDefaultConfig(context.Background(),
config.WithRegion(opts.Region),
config.WithCredentialsProvider(appCreds),
)
}

func CreateClient(opts *options.RootOptions) (*s3.Client, error) {
cfg, err := createConfig(opts)
if err != nil {
return nil, err
}
Expand Down
38 changes: 38 additions & 0 deletions internal/aws/aws_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,37 @@ func TestDeleteFiles(t *testing.T) {
},
},
},
{
"Success with non-empty file list and dry-run enabled",
nil,
func(ctx context.Context, params *s3.DeleteObjectInput, optFns ...func(*s3.Options)) (*s3.DeleteObjectOutput, error) {
return &s3.DeleteObjectOutput{}, nil
},
true,
[]types.Object{
{
ETag: aws.String("03c0fe42b7efa3470fc99037a8e5449d"),
Key: aws.String("../../testdata/file4.txt"),
StorageClass: types.ObjectStorageClass("STANDART"),
Size: aws.Int64(500),
LastModified: aws.Time(time.Now().Add(-5 * time.Hour)),
},
{
ETag: aws.String("03c0fe42b7efa3470fc99037a8e54122"),
Key: aws.String("../../testdata/file5.txt"),
StorageClass: types.ObjectStorageClass("STANDART"),
Size: aws.Int64(1000),
LastModified: aws.Time(time.Now().Add(-2 * time.Hour)),
},
{
ETag: aws.String("03c0fe42b7efa3470fc99037a8e5443d"),
Key: aws.String("../../testdata/file6.txt"),
StorageClass: types.ObjectStorageClass("STANDART"),
Size: aws.Int64(1500),
LastModified: aws.Time(time.Now().Add(-10 * time.Hour)),
},
},
},
{
"Failure caused by delete object err",
constants.ErrInjected,
Expand Down Expand Up @@ -171,6 +202,13 @@ func getMockBody(path string) io.ReadCloser {
return io.NopCloser(body)
}

func TestCreateClient(t *testing.T) {
rootOpts := options.GetMockedRootOptions()
cl, err := CreateClient(rootOpts)
assert.Nil(t, err)
assert.NotNil(t, cl)
}

// TestSearchString is a test function that tests the behavior of the SearchString function.
//
// It creates test cases with different scenarios and verifies the expected results.
Expand Down
Loading