Skip to content

Commit

Permalink
perf(platform): add target platform specific
Browse files Browse the repository at this point in the history
- improve test converage
-resolved platform specific
  • Loading branch information
Mrr Hak authored and Mrr Hak committed May 9, 2024
1 parent 4a6a287 commit 74240f3
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 58 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
4 changes: 4 additions & 0 deletions S3FileProvider/S3FileInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,11 @@ public bool Exists
/// <summary>
/// Gets a value indicating whether the file is a directory.
/// </summary>
#if NETFRAMEWORK || NETSTANDARD
public bool IsDirectory => GetFileObject().Key.EndsWith("/");
#else
public bool IsDirectory => GetFileObject().Key.EndsWith('/');
#endif

/// <summary>
/// Creates a read stream for the file.
Expand Down
5 changes: 5 additions & 0 deletions S3FileProvider/S3OwinFileInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,12 @@ public S3OwinFileInfo(IAmazonS3 amazonS3, string bucketName, string key)
/// <summary>
/// Gets a value indicating whether the file is a directory.
/// </summary>

#if NETFRAMEWORK || NETSTANDARD
public bool IsDirectory => GetFileObject().Key.EndsWith("/");
#else
public bool IsDirectory => GetFileObject().Key.EndsWith('/');
#endif

/// <summary>
/// Creates a read stream for the file.
Expand Down
14 changes: 3 additions & 11 deletions S3FileProvider/S3OwinFileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,9 @@ protected virtual void Dispose(bool disposing)
public bool TryGetDirectoryContents(string subpath, out IEnumerable<IFileInfo> 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<S3OwinFileInfo>();
return false;
}
var s3DirectoryContents = new S3OwinDirectoryContents(amazonS3, bucketName, subpath);
contents = s3DirectoryContents.GetEnumerable();
return true;
}

/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions Test/S3FileInfoTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
64 changes: 20 additions & 44 deletions Test/S3OwinFileInfoTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<IAmazonS3>();
mockS3Client
.Setup(client => client.GetObjectAsync(It.IsAny<string>(), It.IsAny<string>(), 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;
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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;
Expand All @@ -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/";
Expand All @@ -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!";
Expand All @@ -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);
Expand Down

0 comments on commit 74240f3

Please sign in to comment.