/src/NzbDrone.Common.Test/OsPathFixture.cs

https://github.com/NzbDrone/NzbDrone · C# · 245 lines · 204 code · 41 blank · 0 comment · 5 complexity · 53d0786f79946da0d2719a0ea8a7a82c MD5 · raw file

  1. using NUnit.Framework;
  2. using NzbDrone.Common.Disk;
  3. using NzbDrone.Test.Common;
  4. using FluentAssertions;
  5. namespace NzbDrone.Common.Test
  6. {
  7. public class OsPathFixture : TestBase
  8. {
  9. [TestCase(@"C:\rooted\windows\path\", OsPathKind.Windows)]
  10. [TestCase(@"C:\rooted\windows\path", OsPathKind.Windows)]
  11. [TestCase(@"C:\", OsPathKind.Windows)]
  12. [TestCase(@"C:", OsPathKind.Windows)]
  13. [TestCase(@"\\rooted\unc\path\", OsPathKind.Windows)]
  14. [TestCase(@"\\rooted\unc\path", OsPathKind.Windows)]
  15. [TestCase(@"\relative\windows\path\", OsPathKind.Windows)]
  16. [TestCase(@"\relative\windows\path", OsPathKind.Windows)]
  17. [TestCase(@"relative\windows\path\", OsPathKind.Windows)]
  18. [TestCase(@"relative\windows\path", OsPathKind.Windows)]
  19. [TestCase(@"relative\", OsPathKind.Windows)]
  20. [TestCase(@"relative", OsPathKind.Unknown)]
  21. [TestCase("/rooted/linux/path/", OsPathKind.Unix)]
  22. [TestCase("/rooted/linux/path", OsPathKind.Unix)]
  23. [TestCase("/", OsPathKind.Unix)]
  24. [TestCase("linux/path", OsPathKind.Unix)]
  25. [TestCase(@"Castle:unrooted+linux+path", OsPathKind.Unknown)]
  26. public void should_auto_detect_kind(string path, OsPathKind kind)
  27. {
  28. var result = new OsPath(path);
  29. result.Kind.Should().Be(kind);
  30. if (kind == OsPathKind.Windows)
  31. {
  32. result.IsWindowsPath.Should().BeTrue();
  33. result.IsUnixPath.Should().BeFalse();
  34. }
  35. else if (kind == OsPathKind.Unix)
  36. {
  37. result.IsWindowsPath.Should().BeFalse();
  38. result.IsUnixPath.Should().BeTrue();
  39. }
  40. else
  41. {
  42. result.IsWindowsPath.Should().BeFalse();
  43. result.IsUnixPath.Should().BeFalse();
  44. }
  45. }
  46. [Test]
  47. public void should_add_directory_slash()
  48. {
  49. var osPath = new OsPath(@"C:\rooted\windows\path\");
  50. osPath.Directory.Should().NotBeNull();
  51. osPath.Directory.ToString().Should().Be(@"C:\rooted\windows\");
  52. }
  53. [TestCase(@"C:\rooted\windows\path", @"C:\rooted\windows\")]
  54. [TestCase(@"C:\rooted\windows\path\", @"C:\rooted\windows\")]
  55. [TestCase(@"C:\rooted", @"C:\")]
  56. [TestCase(@"C:", null)]
  57. [TestCase("/rooted/linux/path", "/rooted/linux/")]
  58. [TestCase("/rooted", "/")]
  59. [TestCase("/", null)]
  60. public void should_return_parent_directory(string path, string expectedParent)
  61. {
  62. var osPath = new OsPath(path);
  63. osPath.Directory.Should().NotBeNull();
  64. osPath.Directory.Should().Be(new OsPath(expectedParent));
  65. }
  66. [Test]
  67. public void should_return_empty_as_parent_of_root_unc()
  68. {
  69. var osPath = new OsPath(@"\\unc");
  70. osPath.Directory.IsEmpty.Should().BeTrue();
  71. }
  72. [TestCase(@"C:\rooted\windows\path")]
  73. [TestCase(@"C:")]
  74. [TestCase(@"\\blaat")]
  75. [TestCase("/rooted/linux/path")]
  76. [TestCase("/")]
  77. public void should_detect_rooted_ospaths(string path)
  78. {
  79. var osPath = new OsPath(path);
  80. osPath.IsRooted.Should().BeTrue();
  81. }
  82. [TestCase(@"\rooted\windows\path")]
  83. [TestCase(@"rooted\windows\path")]
  84. [TestCase(@"path")]
  85. [TestCase("linux/path")]
  86. [TestCase(@"Castle:unrooted+linux+path")]
  87. [TestCase(@"C:unrooted+linux+path")]
  88. public void should_detect_unrooted_ospaths(string path)
  89. {
  90. var osPath = new OsPath(path);
  91. osPath.IsRooted.Should().BeFalse();
  92. }
  93. [TestCase(@"C:\rooted\windows\path", "path")]
  94. [TestCase(@"C:", "C:")]
  95. [TestCase(@"\\blaat", "blaat")]
  96. [TestCase("/rooted/linux/path", "path")]
  97. [TestCase("/", null)]
  98. [TestCase(@"\rooted\windows\path\", "path")]
  99. [TestCase(@"rooted\windows\path", "path")]
  100. [TestCase(@"path", "path")]
  101. [TestCase("linux/path", "path")]
  102. public void should_return_filename(string path, string expectedFilePath)
  103. {
  104. var osPath = new OsPath(path);
  105. osPath.FileName.Should().Be(expectedFilePath);
  106. }
  107. [Test]
  108. public void should_compare_windows_ospathkind_case_insensitive()
  109. {
  110. var left = new OsPath(@"C:\rooted\Windows\path");
  111. var right = new OsPath(@"C:\rooted\windows\path");
  112. left.Should().Be(right);
  113. }
  114. [Test]
  115. public void should_compare_unix_ospathkind_case_sensitive()
  116. {
  117. var left = new OsPath(@"/rooted/Linux/path");
  118. var right = new OsPath(@"/rooted/linux/path");
  119. left.Should().NotBe(right);
  120. }
  121. [Test]
  122. public void should_not_ignore_trailing_slash_during_compare()
  123. {
  124. var left = new OsPath(@"/rooted/linux/path/");
  125. var right = new OsPath(@"/rooted/linux/path");
  126. left.Should().NotBe(right);
  127. }
  128. [TestCase(@"C:\Test", @"sub", @"C:\Test\sub")]
  129. [TestCase(@"C:\Test", @"sub\test", @"C:\Test\sub\test")]
  130. [TestCase(@"C:\Test\", @"\sub", @"C:\Test\sub")]
  131. [TestCase(@"C:\Test", @"sub\", @"C:\Test\sub\")]
  132. [TestCase(@"C:\Test", @"C:\Test2\sub", @"C:\Test2\sub")]
  133. [TestCase(@"/Test", @"sub", @"/Test/sub")]
  134. [TestCase(@"/Test", @"sub/", @"/Test/sub/")]
  135. [TestCase(@"/Test/", @"sub/test/", @"/Test/sub/test/")]
  136. [TestCase(@"/Test/", @"/Test2/", @"/Test2/")]
  137. [TestCase(@"C:\Test", "", @"C:\Test")]
  138. public void should_combine_path(string left, string right, string expectedResult)
  139. {
  140. var osPathLeft = new OsPath(left);
  141. var osPathRight = new OsPath(right);
  142. var result = osPathLeft + osPathRight;
  143. result.FullPath.Should().Be(expectedResult);
  144. }
  145. [Test]
  146. public void should_fix_slashes_windows()
  147. {
  148. var osPath = new OsPath(@"C:/on/windows/transmission\uses/forward/slashes");
  149. osPath.Kind.Should().Be(OsPathKind.Windows);
  150. osPath.FullPath.Should().Be(@"C:\on\windows\transmission\uses\forward\slashes");
  151. }
  152. [Test]
  153. public void should_fix_slashes_unix()
  154. {
  155. var osPath = new OsPath(@"/just/a/test\to\verify the/slashes\");
  156. osPath.Kind.Should().Be(OsPathKind.Unix);
  157. osPath.FullPath.Should().Be(@"/just/a/test/to/verify the/slashes/");
  158. }
  159. [Test]
  160. public void should_fix_double_slashes_unix()
  161. {
  162. var osPath = new OsPath(@"/just/a//test////to/verify the/slashes/");
  163. osPath.Kind.Should().Be(OsPathKind.Unix);
  164. osPath.FullPath.Should().Be(@"/just/a/test/to/verify the/slashes/");
  165. }
  166. [Test]
  167. public void should_combine_mixed_slashes()
  168. {
  169. var left = new OsPath(@"C:/on/windows/transmission");
  170. var right = new OsPath(@"uses/forward/slashes", OsPathKind.Unknown);
  171. var osPath = left + right;
  172. osPath.Kind.Should().Be(OsPathKind.Windows);
  173. osPath.FullPath.Should().Be(@"C:\on\windows\transmission\uses\forward\slashes");
  174. }
  175. [TestCase(@"C:\Test\Data\", @"C:\Test\Data\Sub\Folder", @"Sub\Folder")]
  176. [TestCase(@"C:\Test\Data\", @"C:\Test\Data2\Sub\Folder", @"..\Data2\Sub\Folder")]
  177. [TestCase(@"/parent/folder", @"/parent/folder/Sub/Folder", @"Sub/Folder")]
  178. public void should_create_relative_path(string parent, string child, string expected)
  179. {
  180. var left = new OsPath(child);
  181. var right = new OsPath(parent);
  182. var osPath = left - right;
  183. osPath.Kind.Should().Be(OsPathKind.Unknown);
  184. osPath.FullPath.Should().Be(expected);
  185. }
  186. [Test]
  187. public void should_parse_null_as_empty()
  188. {
  189. var result = new OsPath(null);
  190. result.FullPath.Should().BeEmpty();
  191. result.IsEmpty.Should().BeTrue();
  192. }
  193. [TestCase(@"C:\Test\", @"C:\Test", true)]
  194. [TestCase(@"C:\Test\", @"C:\Test\Contains\", true)]
  195. [TestCase(@"C:\Test\", @"C:\Other\", false)]
  196. public void should_evaluate_contains(string parent, string child, bool expectedResult)
  197. {
  198. var left = new OsPath(parent);
  199. var right = new OsPath(child);
  200. var result = left.Contains(right);
  201. result.Should().Be(expectedResult);
  202. }
  203. }
  204. }