From 74240f3dc2e63773c6eb4a83e1f3b8969d58f15a Mon Sep 17 00:00:00 2001 From: Mrr Hak Date: Thu, 9 May 2024 16:16:43 +0700 Subject: [PATCH] perf(platform): add target platform specific - improve test converage -resolved platform specific --- .vscode/settings.json | 2 +- S3FileProvider/S3FileInfo.cs | 4 ++ S3FileProvider/S3OwinFileInfo.cs | 5 +++ S3FileProvider/S3OwinFileSystem.cs | 14 ++----- Test/S3FileInfoTest.cs | 4 +- Test/S3OwinFileInfoTest.cs | 64 ++++++++++-------------------- 6 files changed, 35 insertions(+), 58 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index b24f5fc..a69d896 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -4,6 +4,6 @@ "projectKey": "mrrhak_s3_file_provider" }, "cSpell.ignoreWords": ["commitlint", "fileprovider", "kimhak", "longkimhak"], - "cSpell.words": ["Owin", "wwwroot"], + "cSpell.words": ["NETFRAMEWORK", "NETSTANDARD", "Owin", "wwwroot"], "dotnet.preferCSharpExtension": true } diff --git a/S3FileProvider/S3FileInfo.cs b/S3FileProvider/S3FileInfo.cs index 09dc6d0..dff4039 100644 --- a/S3FileProvider/S3FileInfo.cs +++ b/S3FileProvider/S3FileInfo.cs @@ -67,7 +67,11 @@ public bool Exists /// /// Gets a value indicating whether the file is a directory. /// +#if NETFRAMEWORK || NETSTANDARD public bool IsDirectory => GetFileObject().Key.EndsWith("/"); +#else + public bool IsDirectory => GetFileObject().Key.EndsWith('/'); +#endif /// /// Creates a read stream for the file. diff --git a/S3FileProvider/S3OwinFileInfo.cs b/S3FileProvider/S3OwinFileInfo.cs index 685841a..4cc919f 100644 --- a/S3FileProvider/S3OwinFileInfo.cs +++ b/S3FileProvider/S3OwinFileInfo.cs @@ -50,7 +50,12 @@ public S3OwinFileInfo(IAmazonS3 amazonS3, string bucketName, string key) /// /// Gets a value indicating whether the file is a directory. /// + +#if NETFRAMEWORK || NETSTANDARD public bool IsDirectory => GetFileObject().Key.EndsWith("/"); +#else + public bool IsDirectory => GetFileObject().Key.EndsWith('/'); +#endif /// /// Creates a read stream for the file. diff --git a/S3FileProvider/S3OwinFileSystem.cs b/S3FileProvider/S3OwinFileSystem.cs index 41958da..31f9c10 100644 --- a/S3FileProvider/S3OwinFileSystem.cs +++ b/S3FileProvider/S3OwinFileSystem.cs @@ -56,17 +56,9 @@ protected virtual void Dispose(bool disposing) public bool TryGetDirectoryContents(string subpath, out IEnumerable contents) { subpath = subpath.TrimStart(S3Constant.PATH_SEPARATORS); - try - { - var s3DirectoryContents = new S3OwinDirectoryContents(amazonS3, bucketName, subpath); - contents = s3DirectoryContents.GetEnumerable(); - return true; - } - catch (Exception) - { - contents = Enumerable.Empty(); - return false; - } + var s3DirectoryContents = new S3OwinDirectoryContents(amazonS3, bucketName, subpath); + contents = s3DirectoryContents.GetEnumerable(); + return true; } /// diff --git a/Test/S3FileInfoTest.cs b/Test/S3FileInfoTest.cs index 1d9f271..3b13c5f 100644 --- a/Test/S3FileInfoTest.cs +++ b/Test/S3FileInfoTest.cs @@ -155,11 +155,11 @@ public void T006_IsDirectory() }); // Act - var s3FileInfo = new S3FileInfo(mockS3Client.Object, bucketName, key); + var s3FileInfo1 = new S3FileInfo(mockS3Client.Object, bucketName, key); var s3FileInfo2 = new S3FileInfo(mockS3Client2.Object, bucketName, folder); // Assert - Assert.False(s3FileInfo.IsDirectory); + Assert.False(s3FileInfo1.IsDirectory); Assert.True(s3FileInfo2.IsDirectory); } diff --git a/Test/S3OwinFileInfoTest.cs b/Test/S3OwinFileInfoTest.cs index c94b6fb..58d326d 100644 --- a/Test/S3OwinFileInfoTest.cs +++ b/Test/S3OwinFileInfoTest.cs @@ -13,31 +13,7 @@ public class S3OwinFileInfoTest private readonly string key = "hello-world.txt"; [Fact] - public void T001_Exists() - { - // Arrange - // Mock IAmazonS3 client - var mockS3Client = new Mock(); - mockS3Client - .Setup(client => client.GetObjectAsync(It.IsAny(), It.IsAny(), default)) - .ReturnsAsync(new GetObjectResponse - { - BucketName = bucketName, - HttpStatusCode = HttpStatusCode.OK, - Key = key, - }); - - // Act - var s3FileInfo = new S3FileInfo(mockS3Client.Object, bucketName, key); - var s3FileInfoNotExist = new S3FileInfo(mockS3Client.Object, bucketName, "not-exist.txt"); - - // Assert - Assert.True(s3FileInfo.Exists); - Assert.False(s3FileInfoNotExist.Exists); - } - - [Fact] - public void T002_Length() + public void T001_Length() { // Arrange const long expectedLength = 12; @@ -54,14 +30,14 @@ public void T002_Length() }); // Act - var s3FileInfo = new S3FileInfo(mockS3Client.Object, bucketName, key); + var s3OwinFileInfo = new S3OwinFileInfo(mockS3Client.Object, bucketName, key); // Assert - Assert.Equal(expectedLength, s3FileInfo.Length); + Assert.Equal(expectedLength, s3OwinFileInfo.Length); } [Fact] - public void T003_PhysicalPath() + public void T002_PhysicalPath() { // Arrange // Mock IAmazonS3 client @@ -76,14 +52,14 @@ public void T003_PhysicalPath() }); // Act - var s3FileInfo = new S3FileInfo(mockS3Client.Object, bucketName, key); + var s3OwinFileInfo = new S3OwinFileInfo(mockS3Client.Object, bucketName, key); // Assert - Assert.Null(s3FileInfo.PhysicalPath); + Assert.Null(s3OwinFileInfo.PhysicalPath); } [Fact] - public void T004_Name() + public void T003_Name() { // Arrange // Mock IAmazonS3 client @@ -98,14 +74,14 @@ public void T004_Name() }); // Act - var s3FileInfo = new S3FileInfo(mockS3Client.Object, bucketName, key); + var s3OwinFileInfo = new S3OwinFileInfo(mockS3Client.Object, bucketName, key); // Assert - Assert.Equal(key, s3FileInfo.Name); + Assert.Equal(key, s3OwinFileInfo.Name); } [Fact] - public void T005_LastModified() + public void T004_LastModified() { // Arrange var expectedLastModified = DateTime.UtcNow; @@ -122,14 +98,14 @@ public void T005_LastModified() }); // Act - var s3FileInfo = new S3FileInfo(mockS3Client.Object, bucketName, key); + var s3OwinFileInfo = new S3OwinFileInfo(mockS3Client.Object, bucketName, key); // Assert - Assert.Equal(expectedLastModified, s3FileInfo.LastModified); + Assert.Equal(expectedLastModified, s3OwinFileInfo.LastModified); } [Fact] - public void T006_IsDirectory() + public void T005_IsDirectory() { // Arrange const string folder = "/folder/"; @@ -155,16 +131,16 @@ public void T006_IsDirectory() }); // Act - var s3FileInfo = new S3FileInfo(mockS3Client.Object, bucketName, key); - var s3FileInfo2 = new S3FileInfo(mockS3Client2.Object, bucketName, folder); + var s3OwinFileInfo1 = new S3OwinFileInfo(mockS3Client.Object, bucketName, key); + var s3OwinFileInfo2 = new S3OwinFileInfo(mockS3Client2.Object, bucketName, folder); // Assert - Assert.False(s3FileInfo.IsDirectory); - Assert.True(s3FileInfo2.IsDirectory); + Assert.False(s3OwinFileInfo1.IsDirectory); + Assert.True(s3OwinFileInfo2.IsDirectory); } [Fact] - public void T007_CreateReadStream() + public void T006_CreateReadStream() { // Arrange const string streamContent = "Hello, World!"; @@ -181,8 +157,8 @@ public void T007_CreateReadStream() }); // Act - var s3FileInfo = new S3FileInfo(mockS3Client.Object, bucketName, key); - var stream = s3FileInfo.CreateReadStream(); + var s3OwinFileInfo = new S3OwinFileInfo(mockS3Client.Object, bucketName, key); + var stream = s3OwinFileInfo.CreateReadStream(); // Assert Assert.NotNull(stream);