|
1 | 1 | using Flow.Launcher.Plugin.SharedCommands; |
2 | 2 | using NUnit.Framework; |
3 | 3 | using NUnit.Framework.Legacy; |
| 4 | +using System.IO; |
4 | 5 |
|
5 | 6 | namespace Flow.Launcher.Test |
6 | 7 | { |
@@ -50,5 +51,89 @@ public void GivenTwoPathsAreTheSame_WhenCheckPathContains_ThenShouldBeExpectedRe |
50 | 51 | { |
51 | 52 | ClassicAssert.AreEqual(expectedResult, FilesFolders.PathContains(parentPath, path, allowEqual: expectedResult)); |
52 | 53 | } |
| 54 | + |
| 55 | + [Test] |
| 56 | + public void TryDeleteDirectoryRobust_WhenDirectoryDoesNotExist_ReturnsTrue() |
| 57 | + { |
| 58 | + // Arrange |
| 59 | + string nonExistentPath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); |
| 60 | + |
| 61 | + // Act |
| 62 | + bool result = FilesFolders.TryDeleteDirectoryRobust(nonExistentPath); |
| 63 | + |
| 64 | + // Assert |
| 65 | + ClassicAssert.IsTrue(result); |
| 66 | + } |
| 67 | + |
| 68 | + [Test] |
| 69 | + public void TryDeleteDirectoryRobust_WhenDirectoryIsEmpty_DeletesSuccessfully() |
| 70 | + { |
| 71 | + // Arrange |
| 72 | + string tempDir = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); |
| 73 | + Directory.CreateDirectory(tempDir); |
| 74 | + |
| 75 | + // Act |
| 76 | + bool result = FilesFolders.TryDeleteDirectoryRobust(tempDir); |
| 77 | + |
| 78 | + // Assert |
| 79 | + ClassicAssert.IsTrue(result); |
| 80 | + ClassicAssert.IsFalse(Directory.Exists(tempDir)); |
| 81 | + } |
| 82 | + |
| 83 | + [Test] |
| 84 | + public void TryDeleteDirectoryRobust_WhenDirectoryHasFiles_DeletesSuccessfully() |
| 85 | + { |
| 86 | + // Arrange |
| 87 | + string tempDir = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); |
| 88 | + Directory.CreateDirectory(tempDir); |
| 89 | + File.WriteAllText(Path.Combine(tempDir, "test.txt"), "test content"); |
| 90 | + |
| 91 | + // Act |
| 92 | + bool result = FilesFolders.TryDeleteDirectoryRobust(tempDir); |
| 93 | + |
| 94 | + // Assert |
| 95 | + ClassicAssert.IsTrue(result); |
| 96 | + ClassicAssert.IsFalse(Directory.Exists(tempDir)); |
| 97 | + } |
| 98 | + |
| 99 | + [Test] |
| 100 | + public void TryDeleteDirectoryRobust_WhenDirectoryHasNestedStructure_DeletesSuccessfully() |
| 101 | + { |
| 102 | + // Arrange |
| 103 | + string tempDir = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); |
| 104 | + Directory.CreateDirectory(tempDir); |
| 105 | + string subDir1 = Path.Combine(tempDir, "SubDir1"); |
| 106 | + string subDir2 = Path.Combine(tempDir, "SubDir2"); |
| 107 | + Directory.CreateDirectory(subDir1); |
| 108 | + Directory.CreateDirectory(subDir2); |
| 109 | + File.WriteAllText(Path.Combine(subDir1, "file1.txt"), "content1"); |
| 110 | + File.WriteAllText(Path.Combine(subDir2, "file2.txt"), "content2"); |
| 111 | + File.WriteAllText(Path.Combine(tempDir, "root.txt"), "root content"); |
| 112 | + |
| 113 | + // Act |
| 114 | + bool result = FilesFolders.TryDeleteDirectoryRobust(tempDir); |
| 115 | + |
| 116 | + // Assert |
| 117 | + ClassicAssert.IsTrue(result); |
| 118 | + ClassicAssert.IsFalse(Directory.Exists(tempDir)); |
| 119 | + } |
| 120 | + |
| 121 | + [Test] |
| 122 | + public void TryDeleteDirectoryRobust_WhenFileIsReadOnly_RemovesAttributeAndDeletes() |
| 123 | + { |
| 124 | + // Arrange |
| 125 | + string tempDir = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); |
| 126 | + Directory.CreateDirectory(tempDir); |
| 127 | + string filePath = Path.Combine(tempDir, "readonly.txt"); |
| 128 | + File.WriteAllText(filePath, "readonly content"); |
| 129 | + File.SetAttributes(filePath, FileAttributes.ReadOnly); |
| 130 | + |
| 131 | + // Act |
| 132 | + bool result = FilesFolders.TryDeleteDirectoryRobust(tempDir); |
| 133 | + |
| 134 | + // Assert |
| 135 | + ClassicAssert.IsTrue(result); |
| 136 | + ClassicAssert.IsFalse(Directory.Exists(tempDir)); |
| 137 | + } |
53 | 138 | } |
54 | 139 | } |
0 commit comments