path_ut.pyx 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. # cython: c_string_type=str, c_string_encoding=utf8
  2. from util.folder.path cimport TFsPath
  3. from util.generic.string cimport TString, TStringBuf
  4. from util.generic.vector cimport TVector
  5. import unittest
  6. import yatest.common
  7. import os.path
  8. class TestPath(unittest.TestCase):
  9. def test_ctor1(self):
  10. cdef TFsPath path = TFsPath()
  11. self.assertEqual(path.IsDefined(), False)
  12. self.assertEquals(path.c_str(), "")
  13. def test_ctor2(self):
  14. cdef TString str_path = "/a/b/c"
  15. cdef TFsPath path = TFsPath(str_path)
  16. self.assertEqual(path.IsDefined(), True)
  17. self.assertEquals(path.c_str(), "/a/b/c")
  18. def test_ctor3(self):
  19. cdef TStringBuf buf_path = "/a/b/c"
  20. cdef TFsPath path = TFsPath(buf_path)
  21. self.assertEqual(path.IsDefined(), True)
  22. self.assertEquals(path.c_str(), "/a/b/c")
  23. def test_ctor4(self):
  24. cdef char* char_path = "/a/b/c"
  25. cdef TFsPath path = TFsPath(char_path)
  26. self.assertEqual(path.IsDefined(), True)
  27. self.assertEquals(path.c_str(), "/a/b/c")
  28. def test_assignment(self):
  29. cdef TFsPath path1 = TFsPath("/a/b")
  30. cdef TFsPath path2 = TFsPath("/a/c")
  31. self.assertEquals(path1.GetPath(), "/a/b")
  32. self.assertEquals(path2.GetPath(), "/a/c")
  33. path2 = path1
  34. self.assertEquals(path1.GetPath(), "/a/b")
  35. self.assertEquals(path2.GetPath(), "/a/b")
  36. def test_check_defined(self):
  37. cdef TFsPath path1 = TFsPath()
  38. with self.assertRaises(RuntimeError):
  39. path1.CheckDefined()
  40. self.assertEqual(path1.IsDefined(), False)
  41. if path1:
  42. assert False
  43. else:
  44. pass
  45. cdef TFsPath path2 = TFsPath("")
  46. with self.assertRaises(RuntimeError):
  47. path2.CheckDefined()
  48. self.assertEqual(path2.IsDefined(), False)
  49. if path2:
  50. assert False
  51. else:
  52. pass
  53. cdef TFsPath path3 = TFsPath("/")
  54. path3.CheckDefined()
  55. self.assertEqual(path3.IsDefined(), True)
  56. if path3:
  57. pass
  58. else:
  59. assert False
  60. def test_comparison(self):
  61. cdef TFsPath path1 = TFsPath("/a/b")
  62. cdef TFsPath path2 = TFsPath("/a/c")
  63. cdef TFsPath path3 = TFsPath("/a/b")
  64. self.assertEqual(path1 == path3, True)
  65. self.assertEqual(path1 != path2, True)
  66. self.assertEqual(path3 != path2, True)
  67. def test_concatenation(self):
  68. cdef TFsPath path1 = TFsPath("/a")
  69. cdef TFsPath path2 = TFsPath("b")
  70. cdef TFsPath path3 = path1 / path2
  71. cdef TFsPath path4 = TFsPath("/a/b")
  72. self.assertEqual(path3 == path4, True)
  73. def test_fix(self):
  74. cdef TFsPath path = TFsPath("test_fix/b/c/../d")
  75. cdef TFsPath fixed = path.Fix()
  76. self.assertEquals(fixed.GetPath(), "test_fix/b/d")
  77. def test_parts(self):
  78. cdef TFsPath path = TFsPath("/a/b/c")
  79. self.assertEquals(path.GetPath(), "/a/b/c")
  80. self.assertEquals(path.GetName(), "c")
  81. self.assertEquals(path.GetExtension(), "")
  82. self.assertEquals(path.Basename(), "c")
  83. self.assertEquals(path.Dirname(), "/a/b")
  84. cdef TFsPath path_ext = TFsPath("/a/b/c.ext")
  85. self.assertEquals(path_ext.GetPath(), "/a/b/c.ext")
  86. self.assertEquals(path_ext.GetName(), "c.ext")
  87. self.assertEquals(path_ext.GetExtension(), "ext")
  88. self.assertEquals(path_ext.Basename(), "c.ext")
  89. self.assertEquals(path_ext.Dirname(), "/a/b")
  90. cdef TFsPath path_only_ext = TFsPath("/a/b/.ext")
  91. self.assertEquals(path_only_ext.GetPath(), "/a/b/.ext")
  92. self.assertEquals(path_only_ext.GetName(), ".ext")
  93. self.assertEquals(path_only_ext.GetExtension(), "")
  94. self.assertEquals(path_only_ext.Basename(), ".ext")
  95. self.assertEquals(path_only_ext.Dirname(), "/a/b")
  96. cdef TFsPath path_dir = TFsPath("/a/b/")
  97. self.assertEquals(path_dir.GetPath(), "/a/b/")
  98. self.assertEquals(path_dir.GetName(), "b")
  99. self.assertEquals(path_dir.GetExtension(), "")
  100. self.assertEquals(path_dir.Basename(), "b")
  101. self.assertEquals(path_dir.Dirname(), "/a")
  102. def test_absolute(self):
  103. cdef TFsPath path_absolute = TFsPath("/a/b/c")
  104. self.assertEquals(path_absolute.IsAbsolute(), True)
  105. self.assertEquals(path_absolute.IsRelative(), False)
  106. self.assertEquals(path_absolute.IsSubpathOf(TFsPath("/a/b")), True)
  107. self.assertEquals(path_absolute.IsNonStrictSubpathOf(TFsPath("/a/b")), True)
  108. self.assertEquals(TFsPath("/a/b").IsContainerOf(path_absolute), True)
  109. self.assertEquals(path_absolute.IsSubpathOf(TFsPath("/a/b/c")), False)
  110. self.assertEquals(path_absolute.IsNonStrictSubpathOf(TFsPath("/a/b/c")), True)
  111. self.assertEquals(TFsPath("/a/b/c").IsContainerOf(path_absolute), False)
  112. self.assertEquals(path_absolute.IsSubpathOf(TFsPath("/a/c")), False)
  113. self.assertEquals(path_absolute.IsNonStrictSubpathOf(TFsPath("/a/c")), False)
  114. self.assertEquals(TFsPath("/a/c").IsContainerOf(path_absolute), False)
  115. with self.assertRaises(RuntimeError):
  116. path_absolute.RelativeTo(TFsPath("/a/c"))
  117. self.assertEquals(path_absolute.RelativePath(TFsPath("/a/с")).GetPath(), "../b/c")
  118. self.assertEquals(path_absolute.RelativeTo(TFsPath("/a")).GetPath(), "b/c")
  119. self.assertEquals(path_absolute.RelativePath(TFsPath("/a")).GetPath(), "b/c")
  120. self.assertEquals(path_absolute.RelativeTo(TFsPath("/")).GetPath(), "a/b/c")
  121. self.assertEquals(path_absolute.RelativePath(TFsPath("/")).GetPath(), "a/b/c")
  122. with self.assertRaises(RuntimeError):
  123. path_absolute.RelativeTo(TFsPath("./a"))
  124. with self.assertRaises(RuntimeError):
  125. path_absolute.RelativePath(TFsPath("d"))
  126. self.assertEquals(path_absolute.RelativePath(TFsPath("./a")).GetPath(), "b/c")
  127. self.assertEquals(path_absolute.Parent().GetPath(), "/a/b")
  128. self.assertEquals(path_absolute.Child("d").GetPath(), "/a/b/c/d")
  129. def test_relative(self):
  130. cdef TFsPath path_relative_1 = TFsPath("a/b/c")
  131. self.assertEquals(path_relative_1.IsAbsolute(), False)
  132. self.assertEquals(path_relative_1.IsRelative(), True)
  133. self.assertEquals(path_relative_1.IsSubpathOf(TFsPath("a/b")), True)
  134. self.assertEquals(path_relative_1.IsNonStrictSubpathOf(TFsPath("a/b")), True)
  135. self.assertEquals(TFsPath("a/b").IsContainerOf(path_relative_1), True)
  136. self.assertEquals(path_relative_1.IsSubpathOf(TFsPath("a/b/c")), False)
  137. self.assertEquals(path_relative_1.IsNonStrictSubpathOf(TFsPath("a/b/c")), True)
  138. self.assertEquals(TFsPath("a/b/c").IsContainerOf(path_relative_1), False)
  139. self.assertEquals(path_relative_1.IsSubpathOf(TFsPath("a/c")), False)
  140. self.assertEquals(path_relative_1.IsNonStrictSubpathOf(TFsPath("a/c")), False)
  141. self.assertEquals(TFsPath("a/c").IsContainerOf(path_relative_1), False)
  142. self.assertEquals(path_relative_1.Parent().GetPath(), "a/b")
  143. self.assertEquals(path_relative_1.Child("d").GetPath(), "a/b/c/d")
  144. cdef TFsPath path_relative_2 = TFsPath("./a/b/c")
  145. self.assertEquals(path_relative_2.IsAbsolute(), False)
  146. self.assertEquals(path_relative_2.IsRelative(), True)
  147. self.assertEquals(path_relative_2.IsSubpathOf(TFsPath("a/b")), True)
  148. self.assertEquals(path_relative_2.IsNonStrictSubpathOf(TFsPath("a/b")), True)
  149. self.assertEquals(TFsPath("a/b").IsContainerOf(path_relative_2), True)
  150. self.assertEquals(path_relative_2.IsSubpathOf(TFsPath("a/b/c")), False)
  151. self.assertEquals(path_relative_2.IsNonStrictSubpathOf(TFsPath("a/b/c")), True)
  152. self.assertEquals(TFsPath("a/b/c").IsContainerOf(path_relative_2), False)
  153. self.assertEquals(path_relative_2.IsSubpathOf(TFsPath("a/c")), False)
  154. self.assertEquals(path_relative_2.IsNonStrictSubpathOf(TFsPath("a/c")), False)
  155. self.assertEquals(TFsPath("a/c").IsContainerOf(path_relative_2), False)
  156. with self.assertRaises(RuntimeError):
  157. path_relative_2.RelativeTo(TFsPath("a/c"))
  158. self.assertEquals(path_relative_2.RelativePath(TFsPath("a/с")).GetPath(), "../b/c")
  159. self.assertEquals(path_relative_2.RelativeTo(TFsPath("a")).GetPath(), "b/c")
  160. self.assertEquals(path_relative_2.RelativePath(TFsPath("a")).GetPath(), "b/c")
  161. self.assertEquals(path_relative_2.RelativeTo(TFsPath("./")).GetPath(), "a/b/c")
  162. self.assertEquals(path_relative_2.RelativePath(TFsPath("/a")).GetPath(), "b/c")
  163. with self.assertRaises(RuntimeError):
  164. self.assertEquals(path_relative_2.RelativePath(TFsPath("./")).GetPath(), "a/b/c")
  165. with self.assertRaises(RuntimeError):
  166. path_relative_2.RelativeTo(TFsPath("/d"))
  167. with self.assertRaises(RuntimeError):
  168. path_relative_2.RelativePath(TFsPath("/d"))
  169. with self.assertRaises(RuntimeError):
  170. path_relative_2.RelativePath(TFsPath("/"))
  171. self.assertEquals(path_relative_2.Parent().GetPath(), "a/b")
  172. self.assertEquals(path_relative_2.Child("d").GetPath(), "a/b/c/d")
  173. def test_mkdir(self):
  174. cdef TFsPath directory = TFsPath("test_mkdir")
  175. cdef TFsPath full = directory / directory
  176. cdef TFsPath internal = full / directory
  177. with self.assertRaises(RuntimeError):
  178. full.MkDir()
  179. full.MkDirs()
  180. internal.MkDir()
  181. def test_list(self):
  182. cdef TFsPath dir = TFsPath("test_list")
  183. dir.MkDir()
  184. TFsPath("test_list/b").Touch()
  185. TFsPath("test_list/c").Touch()
  186. cdef TVector[TFsPath] files
  187. cdef TVector[TString] names
  188. dir.List(files)
  189. dir.ListNames(names)
  190. self.assertEquals(files.size(), 2)
  191. self.assertEquals(sorted([files[0].GetPath(), files[1].GetPath()]), ["test_list/b", "test_list/c"])
  192. self.assertEquals(names.size(), 2)
  193. self.assertEquals(sorted(list(names)), ["b", "c"])
  194. def test_contains(self):
  195. cdef TFsPath path = TFsPath("a/b/c")
  196. self.assertEquals(path.Contains("c"), True)
  197. self.assertEquals(path.Contains("b"), True)
  198. self.assertEquals(path.Contains("d"), False)
  199. def test_delete(self):
  200. cdef TFsPath root = TFsPath("/")
  201. with self.assertRaises(RuntimeError):
  202. root.DeleteIfExists()
  203. with self.assertRaises(RuntimeError):
  204. root.ForceDelete()
  205. cdef TFsPath directory = TFsPath("test_delete")
  206. cdef TFsPath full = directory / directory
  207. full.MkDirs()
  208. self.assertEquals(full.Exists(), True)
  209. with self.assertRaises(RuntimeError):
  210. directory.DeleteIfExists()
  211. self.assertEquals(directory.Exists(), True)
  212. directory.ForceDelete()
  213. self.assertEquals(directory.Exists(), False)
  214. cdef TFsPath local_file = TFsPath("test_delete_1")
  215. self.assertEquals(local_file.Exists(), False)
  216. local_file.DeleteIfExists()
  217. self.assertEquals(local_file.Exists(), False)
  218. local_file.ForceDelete()
  219. self.assertEquals(local_file.Exists(), False)
  220. local_file.Touch()
  221. self.assertEquals(local_file.Exists(), True)
  222. local_file.DeleteIfExists()
  223. self.assertEquals(local_file.Exists(), False)
  224. local_file.Touch()
  225. self.assertEquals(local_file.Exists(), True)
  226. local_file.ForceDelete()
  227. self.assertEquals(local_file.Exists(), False)
  228. full.MkDirs()
  229. self.assertEquals(full.Exists(), True)
  230. full.DeleteIfExists()
  231. self.assertEquals(full.Exists(), False)
  232. self.assertEquals(directory.Exists(), True)
  233. directory.DeleteIfExists()
  234. self.assertEquals(directory.Exists(), False)
  235. def test_checks(self):
  236. cdef TFsPath local_file = TFsPath("test_checks")
  237. with self.assertRaises(RuntimeError):
  238. local_file.CheckExists()
  239. local_file.Touch()
  240. self.assertEquals(local_file.Exists(), True)
  241. self.assertEquals(local_file.IsDirectory(), False)
  242. self.assertEquals(local_file.IsFile(), True)
  243. self.assertEquals(local_file.IsSymlink(), False)
  244. local_file.CheckExists()
  245. local_file.DeleteIfExists()
  246. local_file.MkDir()
  247. self.assertEquals(local_file.Exists(), True)
  248. self.assertEquals(local_file.IsDirectory(), True)
  249. self.assertEquals(local_file.IsFile(), False)
  250. self.assertEquals(local_file.IsSymlink(), False)
  251. local_file.CheckExists()
  252. def test_rename(self):
  253. cdef TFsPath path = TFsPath("test_rename_a")
  254. path.Touch()
  255. cdef TString path_str = "test_rename_b"
  256. cdef TFsPath path_from_str = TFsPath(path_str)
  257. self.assertEquals(path.Exists(), True)
  258. self.assertEquals(path_from_str.Exists(), False)
  259. path.RenameTo(path_str)
  260. self.assertEquals(path.Exists(), False)
  261. self.assertEquals(path_from_str.Exists(), True)
  262. cdef const char* path_char = "test_rename_c"
  263. cdef TFsPath path_from_char = TFsPath(path_char)
  264. self.assertEquals(path_from_str.Exists(), True)
  265. self.assertEquals(path_from_char.Exists(), False)
  266. path_from_str.RenameTo(path_char)
  267. self.assertEquals(path_from_str.Exists(), False)
  268. self.assertEquals(path_from_char.Exists(), True)
  269. path_from_char.RenameTo(path)
  270. self.assertEquals(path_from_char.Exists(), False)
  271. self.assertEquals(path.Exists(), True)
  272. path.ForceRenameTo(path_str)
  273. self.assertEquals(path_from_str.Exists(), True)
  274. self.assertEquals(path.Exists(), False)
  275. with self.assertRaises(RuntimeError):
  276. path_from_str.RenameTo("")
  277. def test_copy(self):
  278. cdef TString dst = "test_copy_dst"
  279. cdef TFsPath src_path = TFsPath("test_copy_src")
  280. cdef TFsPath dst_path = TFsPath(dst)
  281. self.assertEquals(src_path.Exists(), False)
  282. src_path.Touch()
  283. self.assertEquals(src_path.Exists(), True)
  284. src_path.CopyTo(dst, False)
  285. self.assertEquals(src_path.Exists(), True)
  286. self.assertEquals(dst_path.Exists(), True)
  287. def test_real_path(self):
  288. cdef TFsPath path = TFsPath("test_real_path_a")
  289. path.Touch()
  290. real_work_path = os.path.join(os.path.realpath(yatest.common.work_path()), "test_real_path_a")
  291. self.assertEquals(path.RealPath().GetPath(), real_work_path)
  292. self.assertEquals(path.RealLocation().GetPath(), real_work_path)
  293. with self.assertRaises(RuntimeError):
  294. path.ReadLink()
  295. def test_cwd(self):
  296. cdef TFsPath path = TFsPath.Cwd()
  297. self.assertEquals(path.GetPath(), os.path.realpath(yatest.common.work_path()))
  298. def test_swap(self):
  299. cdef TFsPath first = TFsPath("first")
  300. cdef TFsPath second = TFsPath("second")
  301. self.assertEquals(first.GetPath(), "first")
  302. self.assertEquals(second.GetPath(), "second")
  303. first.Swap(second)
  304. self.assertEquals(first.GetPath(), "second")
  305. self.assertEquals(second.GetPath(), "first")
  306. second.Swap(first)
  307. self.assertEquals(first.GetPath(), "first")
  308. self.assertEquals(second.GetPath(), "second")