Skip to content

Commit cc8e914

Browse files
authored
Refactoring and fixed tests (#17)
* Fixed most of the warnings * Clean up test project * Fixed and refactored StorageBaseTests * Upgraded package version
1 parent deabf59 commit cc8e914

File tree

14 files changed

+104
-169
lines changed

14 files changed

+104
-169
lines changed

Directory.Build.props

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
<PackageLicenseExpression>MIT</PackageLicenseExpression>
1414
<PackageReadmeFile>README.md</PackageReadmeFile>
1515
<Product>Managed Code - Storage</Product>
16-
<Version>0.0.1</Version>
17-
<PackageVersion>0.0.1</PackageVersion>
16+
<Version>0.1.0</Version>
17+
<PackageVersion>0.1.0</PackageVersion>
1818
</PropertyGroup>
1919
<PropertyGroup Condition="'$(GITHUB_ACTIONS)' == 'true'">
2020
<ContinuousIntegrationBuild>true</ContinuousIntegrationBuild>

ManagedCode.Storage.Azure/AzureStorage.cs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using System.Collections.Generic;
33
using System.IO;
44
using System.Runtime.CompilerServices;
5-
using System.Text;
65
using System.Threading;
76
using System.Threading.Tasks;
87
using Azure;
@@ -16,7 +15,7 @@ namespace ManagedCode.Storage.Azure;
1615

1716
public class AzureStorage : IAzureStorage
1817
{
19-
private BlobContainerClient _blobContainerClient;
18+
private readonly BlobContainerClient _blobContainerClient;
2019
private readonly AzureStorageOptions _options;
2120

2221
public AzureStorage(AzureStorageOptions options)
@@ -150,17 +149,18 @@ public async Task<BlobMetadata> GetBlobAsync(string blobName, CancellationToken
150149
};
151150
}
152151

153-
public async IAsyncEnumerable<BlobMetadata> GetBlobsAsync(IEnumerable<string> blobNames, CancellationToken cancellationToken = default)
152+
public async IAsyncEnumerable<BlobMetadata> GetBlobsAsync(IEnumerable<string> blobNames,
153+
[EnumeratorCancellation] CancellationToken cancellationToken = default)
154154
{
155155
foreach (var blob in blobNames)
156156
{
157157
yield return await GetBlobAsync(blob, cancellationToken);
158158
}
159159
}
160160

161-
public async IAsyncEnumerable<BlobMetadata> GetBlobListAsync(CancellationToken cancellationToken = default)
161+
public async IAsyncEnumerable<BlobMetadata> GetBlobListAsync([EnumeratorCancellation] CancellationToken cancellationToken = default)
162162
{
163-
var blobs = _blobContainerClient.GetBlobsAsync();
163+
var blobs = _blobContainerClient.GetBlobsAsync(cancellationToken: cancellationToken);
164164

165165
await foreach (var item in blobs.AsPages())
166166
{
@@ -181,14 +181,14 @@ public async IAsyncEnumerable<BlobMetadata> GetBlobListAsync(CancellationToken c
181181
public async Task UploadStreamAsync(string blobName, Stream dataStream, CancellationToken cancellationToken = default)
182182
{
183183
var blobClient = _blobContainerClient.GetBlobClient(blobName);
184-
184+
185185
try
186186
{
187187
await blobClient.UploadAsync(dataStream, cancellationToken);
188188
}
189189
catch (RequestFailedException)
190190
{
191-
await CreateContainerAsync();
191+
await CreateContainerAsync(cancellationToken);
192192
await blobClient.UploadAsync(dataStream, cancellationToken);
193193
}
194194
}
@@ -203,7 +203,7 @@ public async Task UploadAsync(string blobName, string content, CancellationToken
203203
}
204204
catch (RequestFailedException)
205205
{
206-
await CreateContainerAsync();
206+
await CreateContainerAsync(cancellationToken);
207207
await blobClient.UploadAsync(BinaryData.FromString(content), cancellationToken);
208208
}
209209
}
@@ -220,7 +220,7 @@ public async Task UploadFileAsync(string blobName, string pathToFile, Cancellati
220220
}
221221
catch (RequestFailedException)
222222
{
223-
await CreateContainerAsync();
223+
await CreateContainerAsync(cancellationToken);
224224
await blobClient.UploadAsync(fs, cancellationToken);
225225
}
226226
}
@@ -251,7 +251,7 @@ public async Task UploadAsync(BlobMetadata blobMetadata, byte[] data, Cancellati
251251
}
252252
catch (RequestFailedException)
253253
{
254-
await CreateContainerAsync();
254+
await CreateContainerAsync(cancellationToken);
255255
await blobClient.UploadAsync(BinaryData.FromBytes(data), cancellationToken);
256256
}
257257
}
@@ -268,10 +268,10 @@ public async Task<string> UploadAsync(string content, CancellationToken cancella
268268
}
269269
catch (RequestFailedException)
270270
{
271-
await CreateContainerAsync();
271+
await CreateContainerAsync(cancellationToken);
272272
await blobClient.UploadAsync(BinaryData.FromString(content), cancellationToken);
273273
}
274-
274+
275275
return fileName;
276276
}
277277

@@ -286,7 +286,7 @@ public async Task<string> UploadAsync(Stream dataStream, CancellationToken cance
286286
}
287287
catch (RequestFailedException)
288288
{
289-
await CreateContainerAsync();
289+
await CreateContainerAsync(cancellationToken);
290290
await blobClient.UploadAsync(dataStream, cancellationToken);
291291
}
292292

@@ -304,8 +304,8 @@ public async Task CreateContainerAsync(CancellationToken cancellationToken = def
304304
await _blobContainerClient.CreateIfNotExistsAsync(PublicAccessType.BlobContainer, cancellationToken: cancellationToken);
305305
}
306306

307-
await _blobContainerClient.SetAccessPolicyAsync(_options.PublicAccessType);
307+
await _blobContainerClient.SetAccessPolicyAsync(_options.PublicAccessType, cancellationToken: cancellationToken);
308308
}
309-
309+
310310
#endregion
311311
}

ManagedCode.Storage.Core/LocalFile.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public LocalFile(bool keepAlive = false) : this(Path.GetTempFileName(), keepAliv
1616

1717
public LocalFile(string path, bool keepAlive = false)
1818
{
19-
string directory;
19+
string? directory;
2020
KeepAlive = keepAlive;
2121

2222
if (string.IsNullOrEmpty(Path.GetExtension(path)))

ManagedCode.Storage.FileSystem/FileSystemStorage.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ public async Task UploadStreamAsync(string blobName, Stream dataStream, Cancella
198198
}
199199
}
200200

201-
public async Task UploadFileAsync(string blobName, string pathToFile = null, CancellationToken cancellationToken = default)
201+
public async Task UploadFileAsync(string blobName, string pathToFile, CancellationToken cancellationToken = default)
202202
{
203203
using (var fs = new FileStream(pathToFile, FileMode.Open, FileAccess.Read))
204204
{

ManagedCode.Storage.FileSystem/Options/FileSystemStorageOptions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
public class FileSystemStorageOptions
44
{
5-
public string CommonPath { get; set; }
6-
public string Path { get; set; }
5+
public string CommonPath { get; set; } = null!;
6+
public string Path { get; set; } = null!;
77
}

ManagedCode.Storage.Gcp/Extensions/ServiceCollectionExtensions.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
using System;
2-
using System.IO;
3-
using System.Reflection;
4-
using Google.Apis.Auth.OAuth2;
52
using ManagedCode.Storage.Core;
63
using ManagedCode.Storage.Gcp.Options;
74
using Microsoft.Extensions.DependencyInjection;

ManagedCode.Storage.Gcp/GCPStorage.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ private async Task UploadStreamInternalAsync(string blobName, Stream dataStream,
254254
}
255255
catch
256256
{
257-
await CreateContainerAsync();
257+
await CreateContainerAsync(cancellationToken);
258258
await _storageClient.UploadObjectAsync(_bucket, blobName, contentType, dataStream, null, cancellationToken);
259259
}
260260
}
@@ -267,7 +267,8 @@ public async Task CreateContainerAsync(CancellationToken cancellationToken = def
267267
{
268268
if (_gcpStorageOptions.OriginalOptions != null)
269269
{
270-
await _storageClient.CreateBucketAsync(_gcpStorageOptions.BucketOptions.ProjectId, _bucket, _gcpStorageOptions.OriginalOptions, cancellationToken);
270+
await _storageClient.CreateBucketAsync(_gcpStorageOptions.BucketOptions.ProjectId, _bucket, _gcpStorageOptions.OriginalOptions,
271+
cancellationToken);
271272
}
272273
else
273274
{

ManagedCode.Storage.Gcp/Options/GCPStorageOptions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public class GCPStorageOptions
77
{
88
public string AuthFileName { get; set; } = null!;
99
public BucketOptions BucketOptions { get; set; } = null!;
10-
public GoogleCredential? GoogleCredential { get; set; } = null!;
10+
public GoogleCredential? GoogleCredential { get; set; }
1111
public CreateBucketOptions? OriginalOptions { get; set; }
1212
public StorageClientBuilder? StorageClientBuilder { get; set; }
1313
}

ManagedCode.Storage.Tests/AWS/AWSStorageTests.cs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
using System.IO;
2-
using System.Text;
3-
using System.Threading.Tasks;
4-
using Amazon;
5-
using Amazon.Runtime;
1+
using Amazon;
62
using Amazon.S3;
73
using FluentAssertions;
84
using ManagedCode.Storage.Aws;
@@ -21,12 +17,14 @@ protected override ServiceProvider ConfigureServices()
2117
var services = new ServiceCollection();
2218

2319
//aws libarary overwrites property values. you should only create configurations this way.
24-
var awsConfig = new AmazonS3Config();
25-
awsConfig.RegionEndpoint = RegionEndpoint.EUWest1;
26-
awsConfig.ForcePathStyle = true;
27-
awsConfig.UseHttp = true;
28-
awsConfig.ServiceURL = "http://localhost:4566"; //this is the default port for the aws s3 emulator, must be last in the list
29-
20+
var awsConfig = new AmazonS3Config
21+
{
22+
RegionEndpoint = RegionEndpoint.EUWest1,
23+
ForcePathStyle = true,
24+
UseHttp = true,
25+
ServiceURL = "http://localhost:4566" //this is the default port for the aws s3 emulator, must be last in the list
26+
};
27+
3028
services.AddAWSStorageAsDefault(opt =>
3129
{
3230
opt.PublicKey = "localkey";
@@ -44,12 +42,12 @@ protected override ServiceProvider ConfigureServices()
4442
});
4543
return services.BuildServiceProvider();
4644
}
47-
45+
4846
[Fact]
4947
public void StorageAsDefaultTest()
5048
{
5149
var storage = ServiceProvider.GetService<IAWSStorage>();
5250
var defaultStorage = ServiceProvider.GetService<IStorage>();
53-
storage.GetType().FullName.Should().Be(defaultStorage.GetType().FullName);
51+
storage?.GetType().FullName.Should().Be(defaultStorage?.GetType().FullName);
5452
}
5553
}

ManagedCode.Storage.Tests/Azure/AzureStorageTests.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
using System.IO;
2-
using System.Text;
3-
using System.Threading.Tasks;
4-
using Azure.Storage.Blobs;
5-
using Azure.Storage.Blobs.Models;
6-
using FluentAssertions;
1+
using FluentAssertions;
72
using ManagedCode.Storage.Azure;
83
using ManagedCode.Storage.Azure.Extensions;
94
using ManagedCode.Storage.Azure.Options;
@@ -42,7 +37,7 @@ public void StorageAsDefaultTest()
4237
{
4338
var storage = ServiceProvider.GetService<IAzureStorage>();
4439
var defaultStorage = ServiceProvider.GetService<IStorage>();
45-
storage.GetType().FullName.Should().Be(defaultStorage.GetType().FullName);
40+
storage?.GetType().FullName.Should().Be(defaultStorage?.GetType().FullName);
4641
}
4742

4843
}

ManagedCode.Storage.Tests/FileSystem/FileSystemTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,6 @@ public void StorageAsDefaultTest()
3535
{
3636
var storage = ServiceProvider.GetService<IFileSystemStorage>();
3737
var defaultStorage = ServiceProvider.GetService<IStorage>();
38-
storage.GetType().FullName.Should().Be(defaultStorage.GetType().FullName);
38+
storage?.GetType().FullName.Should().Be(defaultStorage?.GetType().FullName);
3939
}
4040
}

ManagedCode.Storage.Tests/GCP/GoogleStorageTests.cs

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
1-
using System;
2-
using System.IO;
3-
using System.Text;
4-
using System.Threading.Tasks;
5-
using Amazon.Runtime.Documents;
6-
using FluentAssertions;
7-
using Google.Apis.Auth.OAuth2;
1+
using FluentAssertions;
82
using Google.Cloud.Storage.V1;
93
using ManagedCode.Storage.Core;
104
using ManagedCode.Storage.Gcp;
@@ -23,7 +17,6 @@ protected override ServiceProvider ConfigureServices()
2317

2418
services.AddGCPStorageAsDefault(opt =>
2519
{
26-
//opt.GoogleCredential = GoogleCredential.FromFile("google_auth.json");
2720
opt.BucketOptions = new BucketOptions()
2821
{
2922
ProjectId = "api-project-0000000000000",
@@ -35,7 +28,7 @@ protected override ServiceProvider ConfigureServices()
3528
BaseUri = "http://localhost:4443/storage/v1/",
3629
};
3730
});
38-
31+
3932
services.AddGCPStorage(new GCPStorageOptions
4033
{
4134
BucketOptions = new BucketOptions()
@@ -46,12 +39,12 @@ protected override ServiceProvider ConfigureServices()
4639
});
4740
return services.BuildServiceProvider();
4841
}
49-
42+
5043
[Fact]
5144
public void StorageAsDefaultTest()
5245
{
5346
var storage = ServiceProvider.GetService<IGCPStorage>();
5447
var defaultStorage = ServiceProvider.GetService<IStorage>();
55-
storage.GetType().FullName.Should().Be(defaultStorage.GetType().FullName);
48+
storage?.GetType().FullName.Should().Be(defaultStorage?.GetType().FullName);
5649
}
57-
}
50+
}

0 commit comments

Comments
 (0)