diff --git a/src/FileSystem.Adapters.Sftp/src/SftpAdapter.cs b/src/FileSystem.Adapters.Sftp/src/SftpAdapter.cs index 13acc4e..cb9ec63 100644 --- a/src/FileSystem.Adapters.Sftp/src/SftpAdapter.cs +++ b/src/FileSystem.Adapters.Sftp/src/SftpAdapter.cs @@ -172,7 +172,7 @@ public override async Task> GetDirectoriesAsync(string p { try { - directories = client.ListDirectory(path).Where(item => item.IsDirectory).Select(ModelFactory.CreateDirectory).ToList(); + directories = client.ListDirectory(path).Where(item => item.IsDirectory && item.Name != "." && item.Name != "..").Select(ModelFactory.CreateDirectory).ToList(); } catch (Exception ex) { diff --git a/tests/FileSystem.Tests/Base/IntegrationTestAdapter.cs b/tests/FileSystem.Tests/Base/IntegrationTestAdapter.cs index 7eb891d..2b2a960 100644 --- a/tests/FileSystem.Tests/Base/IntegrationTestAdapter.cs +++ b/tests/FileSystem.Tests/Base/IntegrationTestAdapter.cs @@ -67,6 +67,16 @@ public void Init() [TestCategory("IntegrationTest")] public override async Task GetFilesAsync_IfDirectoryNotExist_Should_Throw_DirectoryNotFoundException() => await base.GetFilesAsync_IfDirectoryNotExist_Should_Throw_DirectoryNotFoundException(); + [Fact(DisplayName = "GetDirectoriesAsync_IfSuccess_Should_ReturnDirectories")] + [TestMethod] + [TestCategory("IntegrationTest")] + public override async Task GetDirectoriesAsync_IfSuccess_Should_ReturnDirectories() => await base.GetDirectoriesAsync_IfSuccess_Should_ReturnDirectories(); + + [Fact(DisplayName = "GetDirectoriesAsync_IfDirectoryNotExist_Should_Throw_DirectoryNotFoundException")] + [TestMethod] + [TestCategory("IntegrationTest")] + public override async Task GetDirectoriesAsync_IfDirectoryNotExist_Should_Throw_DirectoryNotFoundException() => await base.GetDirectoriesAsync_IfDirectoryNotExist_Should_Throw_DirectoryNotFoundException(); + [Fact(DisplayName = "CreateDirectoryAsync_IfSuccess_Should_ReturnDirectoryExists")] [TestMethod] [TestCategory("IntegrationTest")] @@ -95,17 +105,17 @@ public void Init() [TestCategory("IntegrationTest")] public override async Task DeleteDirectoryAsync_IfNotExists_Should_ThrowFileNotFoundException() => await base.DeleteDirectoryAsync_IfNotExists_Should_ThrowFileNotFoundException(); - [Fact(DisplayName = "ReadFile")] + [Fact(DisplayName = "ReadFile_IfSuccess_Should_ReturnLength")] [TestCategory("IntegrationTest")] - public override void ReadFile() => base.ReadFile(); + public override void ReadFile_IfSuccess_Should_ReturnLength() => base.ReadFile_IfSuccess_Should_ReturnLength(); [Fact(DisplayName = "ReadFile_IfFileNotExist_Should_ThrowFileNotFoundException")] [TestCategory("IntegrationTest")] public override void ReadFile_IfFileNotExist_Should_ThrowFileNotFoundException() => base.ReadFile_IfFileNotExist_Should_ThrowFileNotFoundException(); - [Fact(DisplayName = "ReadFileAsync")] + [Fact(DisplayName = "ReadFileAsync_IfSuccess_Should_ReturnLength")] [TestCategory("IntegrationTest")] - public override async Task ReadFileAsync() => await base.ReadFileAsync(); + public override async Task ReadFileAsync_IfSuccess_Should_ReturnLength() => await base.ReadFileAsync_IfSuccess_Should_ReturnLength(); [Fact(DisplayName = "ReadFileAsync_IfFileNotExist_Should_ThrowFileNotFoundException")] [TestCategory("IntegrationTest")] diff --git a/tests/FileSystem.Tests/Base/TestAdapter.cs b/tests/FileSystem.Tests/Base/TestAdapter.cs index bea71ae..1d814e3 100644 --- a/tests/FileSystem.Tests/Base/TestAdapter.cs +++ b/tests/FileSystem.Tests/Base/TestAdapter.cs @@ -151,6 +151,39 @@ public virtual async Task GetFilesAsync_IfDirectoryNotExist_Should_Throw_Directo await Assert.ThrowsExceptionAsync(async () => await _adapter!.GetFilesAsync(directory)); } + public virtual async Task GetDirectoriesAsync_IfSuccess_Should_ReturnDirectories() + { + //Arrange + var directory = faker.Database.Random.AlphaNumeric(30); + var directory1 = faker.Database.Random.AlphaNumeric(30); + var directory2 = faker.Database.Random.AlphaNumeric(30); + var directory3 = faker.Database.Random.AlphaNumeric(30); + var directory4 = faker.Database.Random.AlphaNumeric(30); + var directory5 = faker.Database.Random.AlphaNumeric(30); + + _adapter!.CreateDirectory(directory); + _adapter!.CreateDirectory(directory + "/" + directory1); + _adapter!.CreateDirectory(directory + "/" + directory2); + _adapter!.CreateDirectory(directory + "/" + directory3); + _adapter!.CreateDirectory(directory + "/" + directory4); + _adapter!.CreateDirectory(directory + "/" + directory5); + + //Act + var directories = await _adapter!.GetDirectoriesAsync(directory, new System.Threading.CancellationToken()); + + //Assert + Assert.AreEqual(5, directories.Count()); + } + + public virtual async Task GetDirectoriesAsync_IfDirectoryNotExist_Should_Throw_DirectoryNotFoundException() + { + //Arrange + var directory = faker.Database.Random.AlphaNumeric(30); + + //Assert + await Assert.ThrowsExceptionAsync(async () => await _adapter!.GetDirectoriesAsync(directory)); + } + public virtual async Task CreateDirectoryAsync_IfSuccess_Should_ReturnDirectoryExists() { //Arrange @@ -219,7 +252,7 @@ public virtual async Task DeleteDirectoryAsync_IfNotExists_Should_ThrowFileNotFo await Assert.ThrowsExceptionAsync(async () => await _adapter!.DeleteDirectoryAsync(directory)); } - public virtual void ReadFile() + public virtual void ReadFile_IfSuccess_Should_ReturnLength() { //Arrange var directory = faker.Database.Random.AlphaNumeric(30); @@ -245,7 +278,7 @@ public virtual void ReadFile_IfFileNotExist_Should_ThrowFileNotFoundException() Assert.ThrowsException(() => _adapter!.ReadFile(directory + "/" + fileName)); } - public virtual async Task ReadFileAsync() + public virtual async Task ReadFileAsync_IfSuccess_Should_ReturnLength() { //Arrange var directory = faker.Database.Random.AlphaNumeric(30); @@ -270,4 +303,30 @@ public virtual async Task ReadFileAsync_IfFileNotExist_Should_ThrowFileNotFoundE //Act and Assert await Assert.ThrowsExceptionAsync(async () => await _adapter!.ReadFileAsync(directory + "/" + fileName)); } + + public virtual void ReadTextFile_IfSuccess_Should_ReturnLength() + { + //Arrange + var directory = faker.Database.Random.AlphaNumeric(30); + _adapter!.CreateDirectory(directory); + var fileName = faker.Database.Random.AlphaNumeric(50); + _adapter!.WriteFile(directory + "/" + fileName, "ReadFile"); + + //Act + var readFile = _adapter!.ReadTextFile(directory + "/" + fileName); + + //Act and Assert + Assert.AreEqual("ReadFile", readFile); + } + + public virtual void ReadTextFile_IfFileNotExist_Should_ThrowFileNotFoundException() + { + //Arrange + var directory = faker.Database.Random.AlphaNumeric(30); + _adapter!.CreateDirectory(directory); + var fileName = faker.Database.Random.AlphaNumeric(50); + + //Act and Assert + Assert.ThrowsException(() => _adapter!.ReadTextFile(directory + "/" + fileName)); + } } diff --git a/tests/FileSystem.Tests/Base/UnitTestAdapter.cs b/tests/FileSystem.Tests/Base/UnitTestAdapter.cs index be97055..cba4ad5 100644 --- a/tests/FileSystem.Tests/Base/UnitTestAdapter.cs +++ b/tests/FileSystem.Tests/Base/UnitTestAdapter.cs @@ -92,7 +92,7 @@ public void Init() [TestMethod] [TestCategory("UnitTest")] - public override void ReadFile() => base.ReadFile(); + public override void ReadFile_IfSuccess_Should_ReturnLength() => base.ReadFile_IfSuccess_Should_ReturnLength(); [TestMethod] [TestCategory("UnitTest")] @@ -100,7 +100,7 @@ public void Init() [TestMethod] [TestCategory("UnitTest")] - public override async Task ReadFileAsync() => await base.ReadFileAsync(); + public override async Task ReadFileAsync_IfSuccess_Should_ReturnLength() => await base.ReadFileAsync_IfSuccess_Should_ReturnLength(); [TestMethod] [TestCategory("UnitTest")]