From d6f49d35faa7254399b77d11c5f82095153122af Mon Sep 17 00:00:00 2001 From: Mauro van der Gun Date: Tue, 24 Oct 2023 22:06:27 -0400 Subject: [PATCH 1/2] improve sftp adapter unit tests --- .../FileSystem.Adapters.AmazonS3.csproj | 2 +- ...ileSystem.Adapters.AzureBlobStorage.csproj | 2 +- ...ileSystem.Adapters.AzureFileStorage.csproj | 2 +- ...leSystem.Adapters.MicrosoftOneDrive.csproj | 2 +- .../FileSystem.Adapters.Sftp.csproj | 2 +- FileSystem.Adapters.Sftp/src/ModelFactory.cs | 4 +- FileSystem.Adapters.Sftp/src/SftpAdapter.cs | 6 +- Tests/Tests.csproj | 12 +- .../AmazonS3AdapterTest.cs | 5 +- .../SftpAdapterTest.cs | 443 +++++++++++++++++- 10 files changed, 454 insertions(+), 26 deletions(-) diff --git a/FileSystem.Adapters.AmazonS3/FileSystem.Adapters.AmazonS3.csproj b/FileSystem.Adapters.AmazonS3/FileSystem.Adapters.AmazonS3.csproj index 94e0ce3..61b96a5 100644 --- a/FileSystem.Adapters.AmazonS3/FileSystem.Adapters.AmazonS3.csproj +++ b/FileSystem.Adapters.AmazonS3/FileSystem.Adapters.AmazonS3.csproj @@ -17,7 +17,7 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/FileSystem.Adapters.AzureBlobStorage/FileSystem.Adapters.AzureBlobStorage.csproj b/FileSystem.Adapters.AzureBlobStorage/FileSystem.Adapters.AzureBlobStorage.csproj index ef00059..d4439d6 100644 --- a/FileSystem.Adapters.AzureBlobStorage/FileSystem.Adapters.AzureBlobStorage.csproj +++ b/FileSystem.Adapters.AzureBlobStorage/FileSystem.Adapters.AzureBlobStorage.csproj @@ -17,7 +17,7 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/FileSystem.Adapters.AzureFileStorage/FileSystem.Adapters.AzureFileStorage.csproj b/FileSystem.Adapters.AzureFileStorage/FileSystem.Adapters.AzureFileStorage.csproj index 587b1ee..369afc5 100644 --- a/FileSystem.Adapters.AzureFileStorage/FileSystem.Adapters.AzureFileStorage.csproj +++ b/FileSystem.Adapters.AzureFileStorage/FileSystem.Adapters.AzureFileStorage.csproj @@ -17,7 +17,7 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/FileSystem.Adapters.MicrosoftOneDrive/FileSystem.Adapters.MicrosoftOneDrive.csproj b/FileSystem.Adapters.MicrosoftOneDrive/FileSystem.Adapters.MicrosoftOneDrive.csproj index 16516b4..1b60ef9 100644 --- a/FileSystem.Adapters.MicrosoftOneDrive/FileSystem.Adapters.MicrosoftOneDrive.csproj +++ b/FileSystem.Adapters.MicrosoftOneDrive/FileSystem.Adapters.MicrosoftOneDrive.csproj @@ -18,7 +18,7 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/FileSystem.Adapters.Sftp/FileSystem.Adapters.Sftp.csproj b/FileSystem.Adapters.Sftp/FileSystem.Adapters.Sftp.csproj index dc47f84..c075c3e 100644 --- a/FileSystem.Adapters.Sftp/FileSystem.Adapters.Sftp.csproj +++ b/FileSystem.Adapters.Sftp/FileSystem.Adapters.Sftp.csproj @@ -21,7 +21,7 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive - + diff --git a/FileSystem.Adapters.Sftp/src/ModelFactory.cs b/FileSystem.Adapters.Sftp/src/ModelFactory.cs index 669a028..92baa77 100644 --- a/FileSystem.Adapters.Sftp/src/ModelFactory.cs +++ b/FileSystem.Adapters.Sftp/src/ModelFactory.cs @@ -5,7 +5,7 @@ namespace SharpGrip.FileSystem.Adapters.Sftp { public static class ModelFactory { - public static IFile CreateFile(SftpFile file, string virtualPath) + public static IFile CreateFile(ISftpFile file, string virtualPath) { return new FileModel { @@ -17,7 +17,7 @@ public static IFile CreateFile(SftpFile file, string virtualPath) }; } - public static DirectoryModel CreateDirectory(SftpFile directory, string virtualPath) + public static DirectoryModel CreateDirectory(ISftpFile directory, string virtualPath) { return new DirectoryModel { diff --git a/FileSystem.Adapters.Sftp/src/SftpAdapter.cs b/FileSystem.Adapters.Sftp/src/SftpAdapter.cs index 5143e6e..e7f6abf 100644 --- a/FileSystem.Adapters.Sftp/src/SftpAdapter.cs +++ b/FileSystem.Adapters.Sftp/src/SftpAdapter.cs @@ -18,16 +18,16 @@ namespace SharpGrip.FileSystem.Adapters.Sftp { public class SftpAdapter : Adapter { - private readonly SftpClient client; + private readonly ISftpClient client; - public SftpAdapter(string prefix, string rootPath, SftpClient client, Action? configuration = null) : base(prefix, rootPath, configuration) + public SftpAdapter(string prefix, string rootPath, ISftpClient client, Action? configuration = null) : base(prefix, rootPath, configuration) { this.client = client; } public override void Dispose() { - client.Dispose(); + ((IBaseClient) client).Dispose(); } public override void Connect() diff --git a/Tests/Tests.csproj b/Tests/Tests.csproj index e386134..4a057e8 100644 --- a/Tests/Tests.csproj +++ b/Tests/Tests.csproj @@ -8,22 +8,22 @@ - - + + all runtime; build; native; contentfiles; analyzers; buildtransitive - - + + all runtime; build; native; contentfiles; analyzers; buildtransitive - + all runtime; build; native; contentfiles; analyzers; buildtransitive - + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/Tests/src/FileSystem.Adapters.AmazonS3/AmazonS3AdapterTest.cs b/Tests/src/FileSystem.Adapters.AmazonS3/AmazonS3AdapterTest.cs index 12fdf4d..42487df 100644 --- a/Tests/src/FileSystem.Adapters.AmazonS3/AmazonS3AdapterTest.cs +++ b/Tests/src/FileSystem.Adapters.AmazonS3/AmazonS3AdapterTest.cs @@ -237,9 +237,8 @@ public async Task Test_File_Exists_Async() Assert.True(await fileSystem.FileExistsAsync("prefix-1://test1.txt")); Assert.False(await fileSystem.FileExistsAsync("prefix-1://test2.txt")); - await Assert.ThrowsAsync(() => fileSystem.GetFileAsync("prefix-1://test2.txt")); - await Assert.ThrowsAsync(() => fileSystem.GetFileAsync("prefix-1://test3.txt")); - await Assert.ThrowsAsync(() => fileSystem.GetFileAsync("prefix-1://test4.txt")); + await Assert.ThrowsAsync(() => fileSystem.FileExistsAsync("prefix-1://test3.txt")); + await Assert.ThrowsAsync(() => fileSystem.FileExistsAsync("prefix-1://test4.txt")); } [Fact] diff --git a/Tests/src/FileSystem.Adapters.Sftp/SftpAdapterTest.cs b/Tests/src/FileSystem.Adapters.Sftp/SftpAdapterTest.cs index 493386e..eef150c 100644 --- a/Tests/src/FileSystem.Adapters.Sftp/SftpAdapterTest.cs +++ b/Tests/src/FileSystem.Adapters.Sftp/SftpAdapterTest.cs @@ -1,17 +1,29 @@ -using NSubstitute; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Net.Sockets; +using System.Threading.Tasks; +using NSubstitute; +using NSubstitute.ExceptionExtensions; using Renci.SshNet; +using Renci.SshNet.Common; +using Renci.SshNet.Sftp; +using SharpGrip.FileSystem.Adapters; using SharpGrip.FileSystem.Adapters.Sftp; using SharpGrip.FileSystem.Exceptions; using Xunit; +using DirectoryNotFoundException = SharpGrip.FileSystem.Exceptions.DirectoryNotFoundException; +using FileNotFoundException = SharpGrip.FileSystem.Exceptions.FileNotFoundException; namespace SharpGrip.FileSystem.Tests.FileSystem.Adapters.Sftp { - public class SftpAdapterTest + public class SftpAdapterTest : IAdapterTests { [Fact] public void Test_Instantiation() { - var sftpClient = Substitute.For("hostName", "userName", "password"); + var sftpClient = Substitute.For(); var sftpAdapter = new SftpAdapter("prefix", "/root-path", sftpClient); Assert.Equal("prefix", sftpAdapter.Prefix); @@ -19,12 +31,429 @@ public void Test_Instantiation() } [Fact] - public void Test_Connect() + public Task Test_Connect() { - var sftpClient = Substitute.For("hostName", "userName", "password"); - var sftpAdapter = new SftpAdapter("prefix-1", "/root-path-1", sftpClient); + var sftpClient = Substitute.For(); + var sftpAdapter = new SftpAdapter("prefix", "/root-path", sftpClient); + + sftpAdapter.Connect(); + + return Task.CompletedTask; + } + + [Fact] + public async Task Test_Get_File_Async() + { + var sftpClient = Substitute.For(); + var sftpAdapter = new SftpAdapter("prefix-1", "root-path-1", sftpClient); + var fileSystem = new SharpGrip.FileSystem.FileSystem(new List {sftpAdapter}); + + var sftpFile = Substitute.For(); + + sftpFile.Name.Returns("test1.txt"); + sftpFile.FullName.Returns("root-path-1/test1.txt"); + sftpFile.Length.Returns(1); + sftpFile.LastWriteTime.Returns(new DateTime(1970, 1, 1)); + sftpFile.IsDirectory.Returns(false); + + sftpClient.Get("root-path-1/test1.txt").Returns(sftpFile); + sftpClient.Get("root-path-1/test2.txt").Throws(new SftpPathNotFoundException()); + sftpClient.Get("root-path-1/test3.txt").Throws(new SshConnectionException()); + sftpClient.Get("root-path-1/test4.txt").Throws(new SocketException()); + sftpClient.Get("root-path-1/test5.txt").Throws(new SshAuthenticationException()); + sftpClient.Get("root-path-1/test6.txt").Throws(new ProxyException()); + + var file = await fileSystem.GetFileAsync("prefix-1://test1.txt"); + + Assert.Equal("test1.txt", file.Name); + Assert.Equal("root-path-1/test1.txt", file.Path); + Assert.Equal("prefix-1://test1.txt", file.VirtualPath); + Assert.Equal(1, file.Length); + Assert.Equal(new DateTime(1970, 1, 1), file.LastModifiedDateTime); + + await Assert.ThrowsAsync(() => fileSystem.GetFileAsync("prefix-1://test2.txt")); + await Assert.ThrowsAsync(() => fileSystem.GetFileAsync("prefix-1://test3.txt")); + await Assert.ThrowsAsync(() => fileSystem.GetFileAsync("prefix-1://test4.txt")); + await Assert.ThrowsAsync(() => fileSystem.GetFileAsync("prefix-1://test5.txt")); + await Assert.ThrowsAsync(() => fileSystem.GetFileAsync("prefix-1://test6.txt")); + } + + [Fact] + public async Task Test_Get_Directory_Async() + { + var sftpClient = Substitute.For(); + var sftpAdapter = new SftpAdapter("prefix-1", "root-path-1", sftpClient); + var fileSystem = new SharpGrip.FileSystem.FileSystem(new List {sftpAdapter}); + + var sftpFile = Substitute.For(); + + sftpFile.Name.Returns("test1"); + sftpFile.FullName.Returns("root-path-1/test1"); + sftpFile.Length.Returns(1); + sftpFile.LastWriteTime.Returns(new DateTime(1970, 1, 1)); + sftpFile.IsDirectory.Returns(true); + + sftpClient.Get("root-path-1/test1").Returns(sftpFile); + sftpClient.Get("root-path-1/test2").Throws(new SftpPathNotFoundException()); + sftpClient.Get("root-path-1/test3").Throws(new SshConnectionException()); + sftpClient.Get("root-path-1/test4").Throws(new SocketException()); + sftpClient.Get("root-path-1/test5").Throws(new SshAuthenticationException()); + sftpClient.Get("root-path-1/test6").Throws(new ProxyException()); + + var directory = await fileSystem.GetDirectoryAsync("prefix-1://test1"); + + Assert.Equal("test1", directory.Name); + Assert.Equal("root-path-1/test1", directory.Path); + Assert.Equal("prefix-1://test1", directory.VirtualPath); + Assert.Equal(new DateTime(1970, 1, 1), directory.LastModifiedDateTime); + + await Assert.ThrowsAsync(() => fileSystem.GetDirectoryAsync("prefix-1://test2")); + await Assert.ThrowsAsync(() => fileSystem.GetDirectoryAsync("prefix-1://test3")); + await Assert.ThrowsAsync(() => fileSystem.GetDirectoryAsync("prefix-1://test4")); + await Assert.ThrowsAsync(() => fileSystem.GetDirectoryAsync("prefix-1://test5")); + await Assert.ThrowsAsync(() => fileSystem.GetDirectoryAsync("prefix-1://test6")); + } + + [Fact] + public async Task Test_Get_Files_Async() + { + var sftpClient = Substitute.For(); + var sftpAdapter = new SftpAdapter("prefix-1", "root-path-1", sftpClient); + var fileSystem = new SharpGrip.FileSystem.FileSystem(new List {sftpAdapter}); + + var sftpFiles = new List(); + var sftpFile = Substitute.For(); + var sftpDirectory = Substitute.For(); + + sftpFiles.Add(sftpFile); + sftpFiles.Add(sftpDirectory); + + sftpFile.Name.Returns("test1.txt"); + sftpFile.FullName.Returns("root-path-1/test1.txt"); + sftpFile.Length.Returns(1); + sftpFile.LastWriteTime.Returns(new DateTime(1970, 1, 1)); + sftpFile.IsDirectory.Returns(false); + + sftpDirectory.Name.Returns("test1"); + sftpDirectory.FullName.Returns("root-path-1/test1"); + sftpFile.Length.Returns(1); + sftpDirectory.LastWriteTime.Returns(new DateTime(1970, 1, 1)); + sftpDirectory.IsDirectory.Returns(true); + + sftpClient.Get("root-path-1/test1").Returns(sftpDirectory); + sftpClient.ListDirectory("root-path-1/test1").Returns(sftpFiles); + sftpClient.Get("root-path-1/test2").Returns(sftpDirectory); + sftpClient.ListDirectory("root-path-1/test2").Throws(new SshConnectionException()); + sftpClient.Get("root-path-1/test3").Returns(sftpDirectory); + sftpClient.ListDirectory("root-path-1/test3").Throws(new SocketException()); + sftpClient.Get("root-path-1/test4").Returns(sftpDirectory); + sftpClient.ListDirectory("root-path-1/test4").Throws(new SshAuthenticationException()); + sftpClient.Get("root-path-1/test5").Returns(sftpDirectory); + sftpClient.ListDirectory("root-path-1/test5").Throws(new ProxyException()); + + var files = await fileSystem.GetFilesAsync("prefix-1://test1"); + var file = files.First(); + + Assert.Equal("test1.txt", file.Name); + Assert.Equal("root-path-1/test1.txt", file.Path); + Assert.Equal("prefix-1://test1.txt", file.VirtualPath); + Assert.Equal(1, file.Length); + Assert.Equal(new DateTime(1970, 1, 1), file.LastModifiedDateTime); + + await Assert.ThrowsAsync(() => fileSystem.GetFilesAsync("prefix-1://test2")); + await Assert.ThrowsAsync(() => fileSystem.GetFilesAsync("prefix-1://test3")); + await Assert.ThrowsAsync(() => fileSystem.GetFilesAsync("prefix-1://test4")); + await Assert.ThrowsAsync(() => fileSystem.GetFilesAsync("prefix-1://test5")); + } + + [Fact] + public async Task Test_Get_Directories_Async() + { + var sftpClient = Substitute.For(); + var sftpAdapter = new SftpAdapter("prefix-1", "root-path-1", sftpClient); + var fileSystem = new SharpGrip.FileSystem.FileSystem(new List {sftpAdapter}); + + var sftpDirectories = new List(); + var sftpDirectory1 = Substitute.For(); + var sftpDirectory2 = Substitute.For(); + + sftpDirectories.Add(sftpDirectory1); + sftpDirectories.Add(sftpDirectory2); + + sftpDirectory1.Name.Returns("test1"); + sftpDirectory1.FullName.Returns("root-path-1/test1"); + sftpDirectory1.Length.Returns(1); + sftpDirectory1.LastWriteTime.Returns(new DateTime(1970, 1, 1)); + sftpDirectory1.IsDirectory.Returns(true); + + sftpDirectory2.Name.Returns("test2"); + sftpDirectory2.FullName.Returns("root-path-1/test2"); + sftpDirectory2.Length.Returns(1); + sftpDirectory2.LastWriteTime.Returns(new DateTime(1970, 1, 1)); + sftpDirectory2.IsDirectory.Returns(true); + + sftpClient.Get("root-path-1/test1").Returns(sftpDirectory1); + sftpClient.ListDirectory("root-path-1/test1").Returns(sftpDirectories); + sftpClient.Get("root-path-1/test2").Returns(sftpDirectory1); + sftpClient.ListDirectory("root-path-1/test2").Throws(new SshConnectionException()); + sftpClient.Get("root-path-1/test3").Returns(sftpDirectory1); + sftpClient.ListDirectory("root-path-1/test3").Throws(new SocketException()); + sftpClient.Get("root-path-1/test4").Returns(sftpDirectory1); + sftpClient.ListDirectory("root-path-1/test4").Throws(new SshAuthenticationException()); + sftpClient.Get("root-path-1/test5").Returns(sftpDirectory1); + sftpClient.ListDirectory("root-path-1/test5").Throws(new ProxyException()); + + var directories = (await fileSystem.GetDirectoriesAsync("prefix-1://test1")).ToList(); + var directory1 = directories[0]; + var directory2 = directories[1]; + + Assert.Equal("test1", directory1.Name); + Assert.Equal("root-path-1/test1", directory1.Path); + Assert.Equal("prefix-1://test1", directory1.VirtualPath); + Assert.Equal(new DateTime(1970, 1, 1), directory1.LastModifiedDateTime); + + Assert.Equal("test2", directory2.Name); + Assert.Equal("root-path-1/test2", directory2.Path); + Assert.Equal("prefix-1://test2", directory2.VirtualPath); + Assert.Equal(new DateTime(1970, 1, 1), directory2.LastModifiedDateTime); + + await Assert.ThrowsAsync(() => fileSystem.GetDirectoriesAsync("prefix-1://test2")); + await Assert.ThrowsAsync(() => fileSystem.GetDirectoriesAsync("prefix-1://test3")); + await Assert.ThrowsAsync(() => fileSystem.GetDirectoriesAsync("prefix-1://test4")); + await Assert.ThrowsAsync(() => fileSystem.GetDirectoriesAsync("prefix-1://test5")); + } + + [Fact] + public async Task Test_File_Exists_Async() + { + var sftpClient = Substitute.For(); + var sftpAdapter = new SftpAdapter("prefix-1", "root-path-1", sftpClient); + var fileSystem = new SharpGrip.FileSystem.FileSystem(new List {sftpAdapter}); + + var sftpFile = Substitute.For(); + + sftpFile.Name.Returns("test1.txt"); + sftpFile.FullName.Returns("root-path-1/test1.txt"); + sftpFile.Length.Returns(1); + sftpFile.LastWriteTime.Returns(new DateTime(1970, 1, 1)); + sftpFile.IsDirectory.Returns(false); + + sftpClient.Get("root-path-1/test1.txt").Returns(sftpFile); + sftpClient.Get("root-path-1/test2.txt").Throws(new SftpPathNotFoundException()); + sftpClient.Get("root-path-1/test3.txt").Throws(new SshConnectionException()); + sftpClient.Get("root-path-1/test4.txt").Throws(new SocketException()); + sftpClient.Get("root-path-1/test5.txt").Throws(new SshAuthenticationException()); + sftpClient.Get("root-path-1/test6.txt").Throws(new ProxyException()); + + Assert.True(await fileSystem.FileExistsAsync("prefix-1://test1.txt")); + Assert.False(await fileSystem.FileExistsAsync("prefix-1://test2.txt")); + + await Assert.ThrowsAsync(() => fileSystem.FileExistsAsync("prefix-1://test3.txt")); + await Assert.ThrowsAsync(() => fileSystem.FileExistsAsync("prefix-1://test4.txt")); + await Assert.ThrowsAsync(() => fileSystem.FileExistsAsync("prefix-1://test5.txt")); + await Assert.ThrowsAsync(() => fileSystem.FileExistsAsync("prefix-1://test6.txt")); + } + + [Fact] + public async Task Test_Directory_Exists_Async() + { + var sftpClient = Substitute.For(); + var sftpAdapter = new SftpAdapter("prefix-1", "root-path-1", sftpClient); + var fileSystem = new SharpGrip.FileSystem.FileSystem(new List {sftpAdapter}); + + var sftpFile = Substitute.For(); + + sftpFile.Name.Returns("test1"); + sftpFile.FullName.Returns("root-path-1/test1"); + sftpFile.Length.Returns(1); + sftpFile.LastWriteTime.Returns(new DateTime(1970, 1, 1)); + sftpFile.IsDirectory.Returns(true); + + sftpClient.Get("root-path-1/test1").Returns(sftpFile); + sftpClient.Get("root-path-1/test2").Throws(new SftpPathNotFoundException()); + sftpClient.Get("root-path-1/test3").Throws(new SshConnectionException()); + sftpClient.Get("root-path-1/test4").Throws(new SocketException()); + sftpClient.Get("root-path-1/test5").Throws(new SshAuthenticationException()); + sftpClient.Get("root-path-1/test6").Throws(new ProxyException()); + + Assert.True(await fileSystem.DirectoryExistsAsync("prefix-1://test1")); + Assert.False(await fileSystem.DirectoryExistsAsync("prefix-1://test2")); + + await Assert.ThrowsAsync(() => fileSystem.DirectoryExistsAsync("prefix-1://test3")); + await Assert.ThrowsAsync(() => fileSystem.DirectoryExistsAsync("prefix-1://test4")); + await Assert.ThrowsAsync(() => fileSystem.DirectoryExistsAsync("prefix-1://test5")); + await Assert.ThrowsAsync(() => fileSystem.DirectoryExistsAsync("prefix-1://test6")); + } + + [Fact] + public async Task Test_Create_Directory_Async() + { + var sftpClient = Substitute.For(); + var sftpAdapter = new SftpAdapter("prefix-1", "root-path-1", sftpClient); + var fileSystem = new SharpGrip.FileSystem.FileSystem(new List {sftpAdapter}); + + var sftpFile = Substitute.For(); + + sftpFile.Name.Returns("test1"); + sftpFile.FullName.Returns("root-path-1/test1"); + sftpFile.Length.Returns(1); + sftpFile.LastWriteTime.Returns(new DateTime(1970, 1, 1)); + sftpFile.IsDirectory.Returns(true); + + sftpClient.Get("root-path-1/test1").Returns(sftpFile); + + await Assert.ThrowsAsync(() => fileSystem.CreateDirectoryAsync("prefix-1://test1")); + await fileSystem.CreateDirectoryAsync("prefix-1://test2"); + } + + [Fact] + public async Task Test_Delete_File_Async() + { + var sftpClient = Substitute.For(); + var sftpAdapter = new SftpAdapter("prefix-1", "root-path-1", sftpClient); + var fileSystem = new SharpGrip.FileSystem.FileSystem(new List {sftpAdapter}); + + var sftpFile = Substitute.For(); + + sftpFile.Name.Returns("test6.txt"); + sftpFile.FullName.Returns("root-path-1/test6.txt"); + sftpFile.Length.Returns(1); + sftpFile.LastWriteTime.Returns(new DateTime(1970, 1, 1)); + sftpFile.IsDirectory.Returns(false); + + sftpClient.Get("root-path-1/test1.txt").Throws(new SftpPathNotFoundException()); + sftpClient.Get("root-path-1/test2.txt").Throws(new SshConnectionException()); + sftpClient.Get("root-path-1/test3.txt").Throws(new SocketException()); + sftpClient.Get("root-path-1/test4.txt").Throws(new SshAuthenticationException()); + sftpClient.Get("root-path-1/test5.txt").Throws(new ProxyException()); + sftpClient.Get("root-path-1/test6.txt").Returns(sftpFile); + + await Assert.ThrowsAsync(() => fileSystem.DeleteFileAsync("prefix-1://test1.txt")); + await Assert.ThrowsAsync(() => fileSystem.DeleteFileAsync("prefix-1://test2.txt")); + await Assert.ThrowsAsync(() => fileSystem.DeleteFileAsync("prefix-1://test3.txt")); + await Assert.ThrowsAsync(() => fileSystem.DeleteFileAsync("prefix-1://test4.txt")); + await Assert.ThrowsAsync(() => fileSystem.DeleteFileAsync("prefix-1://test5.txt")); + await fileSystem.DeleteFileAsync("prefix-1://test6.txt"); + } + + [Fact] + public async Task Test_Delete_Directory_Async() + { + var sftpClient = Substitute.For(); + var sftpAdapter = new SftpAdapter("prefix-1", "root-path-1", sftpClient); + var fileSystem = new SharpGrip.FileSystem.FileSystem(new List {sftpAdapter}); + + var sftpFile = Substitute.For(); + + sftpFile.Name.Returns("test6"); + sftpFile.FullName.Returns("root-path-1/test6"); + sftpFile.Length.Returns(1); + sftpFile.LastWriteTime.Returns(new DateTime(1970, 1, 1)); + sftpFile.IsDirectory.Returns(true); + + sftpClient.Get("root-path-1/test1").Throws(new SftpPathNotFoundException()); + sftpClient.Get("root-path-1/test2").Throws(new SshConnectionException()); + sftpClient.Get("root-path-1/test3").Throws(new SocketException()); + sftpClient.Get("root-path-1/test4").Throws(new SshAuthenticationException()); + sftpClient.Get("root-path-1/test5").Throws(new ProxyException()); + sftpClient.Get("root-path-1/test6").Returns(sftpFile); + + await Assert.ThrowsAsync(() => fileSystem.DeleteDirectoryAsync("prefix-1://test1")); + await Assert.ThrowsAsync(() => fileSystem.DeleteDirectoryAsync("prefix-1://test2")); + await Assert.ThrowsAsync(() => fileSystem.DeleteDirectoryAsync("prefix-1://test3")); + await Assert.ThrowsAsync(() => fileSystem.DeleteDirectoryAsync("prefix-1://test4")); + await Assert.ThrowsAsync(() => fileSystem.DeleteDirectoryAsync("prefix-1://test5")); + await fileSystem.DeleteDirectoryAsync("prefix-1://test6"); + } + + [Fact] + public async Task Test_Read_File_Stream_Async() + { + var sftpClient = Substitute.For(); + var sftpAdapter = new SftpAdapter("prefix-1", "root-path-1", sftpClient); + var fileSystem = new SharpGrip.FileSystem.FileSystem(new List {sftpAdapter}); + + sftpClient.Get("root-path-1/test1.txt").Throws(new SftpPathNotFoundException()); + sftpClient.Get("root-path-1/test2.txt").Throws(new SshConnectionException()); + sftpClient.Get("root-path-1/test3.txt").Throws(new SocketException()); + sftpClient.Get("root-path-1/test4.txt").Throws(new SshAuthenticationException()); + sftpClient.Get("root-path-1/test5.txt").Throws(new ProxyException()); + + await Assert.ThrowsAsync(() => fileSystem.ReadFileStreamAsync("prefix-1://test1.txt")); + await Assert.ThrowsAsync(() => fileSystem.ReadFileStreamAsync("prefix-1://test2.txt")); + await Assert.ThrowsAsync(() => fileSystem.ReadFileStreamAsync("prefix-1://test3.txt")); + await Assert.ThrowsAsync(() => fileSystem.ReadFileStreamAsync("prefix-1://test4.txt")); + await Assert.ThrowsAsync(() => fileSystem.ReadFileStreamAsync("prefix-1://test5.txt")); + } + + [Fact] + public async Task Test_Read_File_Async() + { + var sftpClient = Substitute.For(); + var sftpAdapter = new SftpAdapter("prefix-1", "root-path-1", sftpClient); + var fileSystem = new SharpGrip.FileSystem.FileSystem(new List {sftpAdapter}); + + sftpClient.Get("root-path-1/test1.txt").Throws(new SftpPathNotFoundException()); + sftpClient.Get("root-path-1/test2.txt").Throws(new SshConnectionException()); + sftpClient.Get("root-path-1/test3.txt").Throws(new SocketException()); + sftpClient.Get("root-path-1/test4.txt").Throws(new SshAuthenticationException()); + sftpClient.Get("root-path-1/test5.txt").Throws(new ProxyException()); + + await Assert.ThrowsAsync(() => fileSystem.ReadFileAsync("prefix-1://test1.txt")); + await Assert.ThrowsAsync(() => fileSystem.ReadFileAsync("prefix-1://test2.txt")); + await Assert.ThrowsAsync(() => fileSystem.ReadFileAsync("prefix-1://test3.txt")); + await Assert.ThrowsAsync(() => fileSystem.ReadFileAsync("prefix-1://test4.txt")); + await Assert.ThrowsAsync(() => fileSystem.ReadFileAsync("prefix-1://test5.txt")); + } + + [Fact] + public async Task Test_Read_Text_File_Async() + { + var sftpClient = Substitute.For(); + var sftpAdapter = new SftpAdapter("prefix-1", "root-path-1", sftpClient); + var fileSystem = new SharpGrip.FileSystem.FileSystem(new List {sftpAdapter}); + + sftpClient.Get("root-path-1/test1.txt").Throws(new SftpPathNotFoundException()); + sftpClient.Get("root-path-1/test2.txt").Throws(new SshConnectionException()); + sftpClient.Get("root-path-1/test3.txt").Throws(new SocketException()); + sftpClient.Get("root-path-1/test4.txt").Throws(new SshAuthenticationException()); + sftpClient.Get("root-path-1/test5.txt").Throws(new ProxyException()); + + await Assert.ThrowsAsync(() => fileSystem.ReadFileAsync("prefix-1://test1.txt")); + await Assert.ThrowsAsync(() => fileSystem.ReadFileAsync("prefix-1://test2.txt")); + await Assert.ThrowsAsync(() => fileSystem.ReadFileAsync("prefix-1://test3.txt")); + await Assert.ThrowsAsync(() => fileSystem.ReadFileAsync("prefix-1://test4.txt")); + await Assert.ThrowsAsync(() => fileSystem.ReadFileAsync("prefix-1://test5.txt")); + } + + [Fact] + public async Task Test_Write_File_Async() + { + var sftpClient = Substitute.For(); + var sftpAdapter = new SftpAdapter("prefix-1", "root-path-1", sftpClient); + var fileSystem = new SharpGrip.FileSystem.FileSystem(new List {sftpAdapter}); + + sftpClient.OpenWrite("root-path-1/test1.txt").Throws(new SftpPathNotFoundException()); + sftpClient.OpenWrite("root-path-1/test2.txt").Throws(new SshConnectionException()); + sftpClient.OpenWrite("root-path-1/test3.txt").Throws(new SocketException()); + sftpClient.OpenWrite("root-path-1/test4.txt").Throws(new SshAuthenticationException()); + sftpClient.OpenWrite("root-path-1/test5.txt").Throws(new ProxyException()); + + await Assert.ThrowsAsync(() => fileSystem.WriteFileAsync("prefix-1://test1.txt", new MemoryStream())); + await Assert.ThrowsAsync(() => fileSystem.WriteFileAsync("prefix-1://test2.txt", new MemoryStream(), true)); + await Assert.ThrowsAsync(() => fileSystem.WriteFileAsync("prefix-1://test3.txt", new MemoryStream(), true)); + await Assert.ThrowsAsync(() => fileSystem.WriteFileAsync("prefix-1://test4.txt", new MemoryStream(), true)); + await Assert.ThrowsAsync(() => fileSystem.WriteFileAsync("prefix-1://test5.txt", new MemoryStream(), true)); + } + + [Fact] + public async Task Test_Append_File_Async() + { + var sftpClient = Substitute.For(); + var sftpAdapter = new SftpAdapter("prefix-1", "root-path-1", sftpClient); + var fileSystem = new SharpGrip.FileSystem.FileSystem(new List {sftpAdapter}); - Assert.Throws(() => sftpAdapter.Connect()); + await Assert.ThrowsAsync(() => fileSystem.AppendFileAsync("prefix-1://test1.txt", new MemoryStream())); } } } \ No newline at end of file From 179ffdb24a9341de044f9a73d21e859f1db0c017 Mon Sep 17 00:00:00 2001 From: Mauro van der Gun Date: Tue, 7 Nov 2023 16:00:37 -0400 Subject: [PATCH 2/2] update packages --- .../FileSystem.Adapters.AmazonS3.csproj | 2 +- .../FileSystem.Adapters.AzureBlobStorage.csproj | 2 +- .../FileSystem.Adapters.AzureFileStorage.csproj | 2 +- .../FileSystem.Adapters.GoogleDrive.csproj | 2 +- Tests/Tests.csproj | 6 +++--- 5 files changed, 7 insertions(+), 7 deletions(-) diff --git a/FileSystem.Adapters.AmazonS3/FileSystem.Adapters.AmazonS3.csproj b/FileSystem.Adapters.AmazonS3/FileSystem.Adapters.AmazonS3.csproj index 61b96a5..2ab9bd3 100644 --- a/FileSystem.Adapters.AmazonS3/FileSystem.Adapters.AmazonS3.csproj +++ b/FileSystem.Adapters.AmazonS3/FileSystem.Adapters.AmazonS3.csproj @@ -17,7 +17,7 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/FileSystem.Adapters.AzureBlobStorage/FileSystem.Adapters.AzureBlobStorage.csproj b/FileSystem.Adapters.AzureBlobStorage/FileSystem.Adapters.AzureBlobStorage.csproj index d4439d6..6ad8515 100644 --- a/FileSystem.Adapters.AzureBlobStorage/FileSystem.Adapters.AzureBlobStorage.csproj +++ b/FileSystem.Adapters.AzureBlobStorage/FileSystem.Adapters.AzureBlobStorage.csproj @@ -17,7 +17,7 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/FileSystem.Adapters.AzureFileStorage/FileSystem.Adapters.AzureFileStorage.csproj b/FileSystem.Adapters.AzureFileStorage/FileSystem.Adapters.AzureFileStorage.csproj index 369afc5..7cfdb45 100644 --- a/FileSystem.Adapters.AzureFileStorage/FileSystem.Adapters.AzureFileStorage.csproj +++ b/FileSystem.Adapters.AzureFileStorage/FileSystem.Adapters.AzureFileStorage.csproj @@ -17,7 +17,7 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/FileSystem.Adapters.GoogleDrive/FileSystem.Adapters.GoogleDrive.csproj b/FileSystem.Adapters.GoogleDrive/FileSystem.Adapters.GoogleDrive.csproj index 72aea8b..5906e06 100644 --- a/FileSystem.Adapters.GoogleDrive/FileSystem.Adapters.GoogleDrive.csproj +++ b/FileSystem.Adapters.GoogleDrive/FileSystem.Adapters.GoogleDrive.csproj @@ -17,7 +17,7 @@ - + diff --git a/Tests/Tests.csproj b/Tests/Tests.csproj index 4a057e8..8ec32dc 100644 --- a/Tests/Tests.csproj +++ b/Tests/Tests.csproj @@ -14,12 +14,12 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive - - + + all runtime; build; native; contentfiles; analyzers; buildtransitive - + all runtime; build; native; contentfiles; analyzers; buildtransitive