test_paths.py 73 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118
  1. # Copyright (c) Twisted Matrix Laboratories.
  2. # See LICENSE for details.
  3. """
  4. Test cases covering L{twisted.python.filepath}.
  5. """
  6. from __future__ import division, absolute_import
  7. import os, time, pickle, errno, stat
  8. from pprint import pformat
  9. from twisted.python.compat import _PY3, unicode
  10. from twisted.python.win32 import WindowsError, ERROR_DIRECTORY
  11. from twisted.python import filepath
  12. from twisted.python.runtime import platform
  13. from twisted.trial.unittest import SkipTest, SynchronousTestCase as TestCase
  14. from zope.interface.verify import verifyObject
  15. if not platform._supportsSymlinks():
  16. symlinkSkip = "Platform does not support symlinks"
  17. else:
  18. symlinkSkip = None
  19. class BytesTestCase(TestCase):
  20. """
  21. Override default method implementations to support byte paths.
  22. """
  23. def mktemp(self):
  24. """
  25. Return a temporary path, encoded as bytes.
  26. """
  27. return TestCase.mktemp(self).encode("utf-8")
  28. class AbstractFilePathTests(BytesTestCase):
  29. """
  30. Tests for L{IFilePath} implementations.
  31. """
  32. f1content = b"file 1"
  33. f2content = b"file 2"
  34. def _mkpath(self, *p):
  35. x = os.path.abspath(os.path.join(self.cmn, *p))
  36. self.all.append(x)
  37. return x
  38. def subdir(self, *dirname):
  39. os.mkdir(self._mkpath(*dirname))
  40. def subfile(self, *dirname):
  41. return open(self._mkpath(*dirname), "wb")
  42. def setUp(self):
  43. self.now = time.time()
  44. cmn = self.cmn = os.path.abspath(self.mktemp())
  45. self.all = [cmn]
  46. os.mkdir(cmn)
  47. self.subdir(b"sub1")
  48. with self.subfile(b"file1") as f:
  49. f.write(self.f1content)
  50. with self.subfile(b"sub1", b"file2") as f:
  51. f.write(self.f2content)
  52. self.subdir(b'sub3')
  53. self.subfile(b"sub3", b"file3.ext1").close()
  54. self.subfile(b"sub3", b"file3.ext2").close()
  55. self.subfile(b"sub3", b"file3.ext3").close()
  56. self.path = filepath.FilePath(cmn)
  57. self.root = filepath.FilePath(b"/")
  58. def test_verifyObject(self):
  59. """
  60. Instances of the path type being tested provide L{IFilePath}.
  61. """
  62. self.assertTrue(verifyObject(filepath.IFilePath, self.path))
  63. def test_segmentsFromPositive(self):
  64. """
  65. Verify that the segments between two paths are correctly identified.
  66. """
  67. self.assertEqual(
  68. self.path.child(b"a").child(b"b").child(b"c").segmentsFrom(self.path),
  69. [b"a", b"b", b"c"])
  70. def test_segmentsFromNegative(self):
  71. """
  72. Verify that segmentsFrom notices when the ancestor isn't an ancestor.
  73. """
  74. self.assertRaises(
  75. ValueError,
  76. self.path.child(b"a").child(b"b").child(b"c").segmentsFrom,
  77. self.path.child(b"d").child(b"c").child(b"e"))
  78. def test_walk(self):
  79. """
  80. Verify that walking the path gives the same result as the known file
  81. hierarchy.
  82. """
  83. x = [foo.path for foo in self.path.walk()]
  84. self.assertEqual(set(x), set(self.all))
  85. def test_parents(self):
  86. """
  87. L{FilePath.parents()} should return an iterator of every ancestor of
  88. the L{FilePath} in question.
  89. """
  90. L = []
  91. pathobj = self.path.child(b"a").child(b"b").child(b"c")
  92. fullpath = pathobj.path
  93. lastpath = fullpath
  94. thispath = os.path.dirname(fullpath)
  95. while lastpath != self.root.path:
  96. L.append(thispath)
  97. lastpath = thispath
  98. thispath = os.path.dirname(thispath)
  99. self.assertEqual([x.path for x in pathobj.parents()], L)
  100. def test_validSubdir(self):
  101. """
  102. Verify that a valid subdirectory will show up as a directory, but not as a
  103. file, not as a symlink, and be listable.
  104. """
  105. sub1 = self.path.child(b'sub1')
  106. self.assertTrue(sub1.exists(),
  107. "This directory does exist.")
  108. self.assertTrue(sub1.isdir(),
  109. "It's a directory.")
  110. self.assertFalse(sub1.isfile(),
  111. "It's a directory.")
  112. self.assertFalse(sub1.islink(),
  113. "It's a directory.")
  114. self.assertEqual(sub1.listdir(),
  115. [b'file2'])
  116. def test_invalidSubdir(self):
  117. """
  118. Verify that a subdirectory that doesn't exist is reported as such.
  119. """
  120. sub2 = self.path.child(b'sub2')
  121. self.assertFalse(sub2.exists(),
  122. "This directory does not exist.")
  123. def test_validFiles(self):
  124. """
  125. Make sure that we can read existent non-empty files.
  126. """
  127. f1 = self.path.child(b'file1')
  128. with f1.open() as f:
  129. self.assertEqual(f.read(), self.f1content)
  130. f2 = self.path.child(b'sub1').child(b'file2')
  131. with f2.open() as f:
  132. self.assertEqual(f.read(), self.f2content)
  133. def test_multipleChildSegments(self):
  134. """
  135. C{fp.descendant([a, b, c])} returns the same L{FilePath} as is returned
  136. by C{fp.child(a).child(b).child(c)}.
  137. """
  138. multiple = self.path.descendant([b'a', b'b', b'c'])
  139. single = self.path.child(b'a').child(b'b').child(b'c')
  140. self.assertEqual(multiple, single)
  141. def test_dictionaryKeys(self):
  142. """
  143. Verify that path instances are usable as dictionary keys.
  144. """
  145. f1 = self.path.child(b'file1')
  146. f1prime = self.path.child(b'file1')
  147. f2 = self.path.child(b'file2')
  148. dictoid = {}
  149. dictoid[f1] = 3
  150. dictoid[f1prime] = 4
  151. self.assertEqual(dictoid[f1], 4)
  152. self.assertEqual(list(dictoid.keys()), [f1])
  153. self.assertIs(list(dictoid.keys())[0], f1)
  154. self.assertIsNot(list(dictoid.keys())[0], f1prime) # sanity check
  155. dictoid[f2] = 5
  156. self.assertEqual(dictoid[f2], 5)
  157. self.assertEqual(len(dictoid), 2)
  158. def test_dictionaryKeyWithString(self):
  159. """
  160. Verify that path instances are usable as dictionary keys which do not clash
  161. with their string counterparts.
  162. """
  163. f1 = self.path.child(b'file1')
  164. dictoid = {f1: 'hello'}
  165. dictoid[f1.path] = 'goodbye'
  166. self.assertEqual(len(dictoid), 2)
  167. def test_childrenNonexistentError(self):
  168. """
  169. Verify that children raises the appropriate exception for non-existent
  170. directories.
  171. """
  172. self.assertRaises(filepath.UnlistableError,
  173. self.path.child(b'not real').children)
  174. def test_childrenNotDirectoryError(self):
  175. """
  176. Verify that listdir raises the appropriate exception for attempting to list
  177. a file rather than a directory.
  178. """
  179. self.assertRaises(filepath.UnlistableError,
  180. self.path.child(b'file1').children)
  181. def test_newTimesAreFloats(self):
  182. """
  183. Verify that all times returned from the various new time functions are ints
  184. (and hopefully therefore 'high precision').
  185. """
  186. for p in self.path, self.path.child(b'file1'):
  187. self.assertEqual(type(p.getAccessTime()), float)
  188. self.assertEqual(type(p.getModificationTime()), float)
  189. self.assertEqual(type(p.getStatusChangeTime()), float)
  190. def test_oldTimesAreInts(self):
  191. """
  192. Verify that all times returned from the various time functions are
  193. integers, for compatibility.
  194. """
  195. for p in self.path, self.path.child(b'file1'):
  196. self.assertEqual(type(p.getatime()), int)
  197. self.assertEqual(type(p.getmtime()), int)
  198. self.assertEqual(type(p.getctime()), int)
  199. class FakeWindowsPath(filepath.FilePath):
  200. """
  201. A test version of FilePath which overrides listdir to raise L{WindowsError}.
  202. """
  203. def listdir(self):
  204. """
  205. @raise WindowsError: always.
  206. """
  207. if _PY3:
  208. # For Python 3.3 and higher, WindowsError is an alias for OSError.
  209. # The first argument to the OSError constructor is errno, and the fourth
  210. # argument is winerror.
  211. # For further details, refer to:
  212. # https://docs.python.org/3/library/exceptions.html#OSError
  213. #
  214. # On Windows, if winerror is set in the constructor,
  215. # the errno value in the constructor is ignored, and OSError internally
  216. # maps the winerror value to an errno value.
  217. raise WindowsError(
  218. None,
  219. "A directory's validness was called into question",
  220. self.path,
  221. ERROR_DIRECTORY)
  222. else:
  223. raise WindowsError(
  224. ERROR_DIRECTORY,
  225. "A directory's validness was called into question")
  226. class ListingCompatibilityTests(BytesTestCase):
  227. """
  228. These tests verify compatibility with legacy behavior of directory listing.
  229. """
  230. def test_windowsErrorExcept(self):
  231. """
  232. Verify that when a WindowsError is raised from listdir, catching
  233. WindowsError works.
  234. """
  235. fwp = FakeWindowsPath(self.mktemp())
  236. self.assertRaises(filepath.UnlistableError, fwp.children)
  237. self.assertRaises(WindowsError, fwp.children)
  238. if not platform.isWindows():
  239. test_windowsErrorExcept.skip = "Only relevant on on Windows."
  240. def test_alwaysCatchOSError(self):
  241. """
  242. Verify that in the normal case where a directory does not exist, we will
  243. get an OSError.
  244. """
  245. fp = filepath.FilePath(self.mktemp())
  246. self.assertRaises(OSError, fp.children)
  247. def test_keepOriginalAttributes(self):
  248. """
  249. Verify that the Unlistable exception raised will preserve the attributes of
  250. the previously-raised exception.
  251. """
  252. fp = filepath.FilePath(self.mktemp())
  253. ose = self.assertRaises(OSError, fp.children)
  254. d1 = list(ose.__dict__.keys())
  255. d1.remove('originalException')
  256. d2 = list(ose.originalException.__dict__.keys())
  257. d1.sort()
  258. d2.sort()
  259. self.assertEqual(d1, d2)
  260. class ExplodingFile:
  261. """
  262. A C{file}-alike which raises exceptions from its I/O methods and keeps track
  263. of whether it has been closed.
  264. @ivar closed: A C{bool} which is C{False} until C{close} is called, then it
  265. is C{True}.
  266. """
  267. closed = False
  268. def read(self, n=0):
  269. """
  270. @raise IOError: Always raised.
  271. """
  272. raise IOError()
  273. def write(self, what):
  274. """
  275. @raise IOError: Always raised.
  276. """
  277. raise IOError()
  278. def close(self):
  279. """
  280. Mark the file as having been closed.
  281. """
  282. self.closed = True
  283. def __enter__(self):
  284. return self
  285. def __exit__(self, exc_type, exc_value, traceback):
  286. self.close()
  287. class TrackingFilePath(filepath.FilePath):
  288. """
  289. A subclass of L{filepath.FilePath} which maintains a list of all other paths
  290. created by clonePath.
  291. @ivar trackingList: A list of all paths created by this path via
  292. C{clonePath} (which also includes paths created by methods like
  293. C{parent}, C{sibling}, C{child}, etc (and all paths subsequently created
  294. by those paths, etc).
  295. @type trackingList: C{list} of L{TrackingFilePath}
  296. @ivar openedFiles: A list of all file objects opened by this
  297. L{TrackingFilePath} or any other L{TrackingFilePath} in C{trackingList}.
  298. @type openedFiles: C{list} of C{file}
  299. """
  300. def __init__(self, path, alwaysCreate=False, trackingList=None):
  301. filepath.FilePath.__init__(self, path, alwaysCreate)
  302. if trackingList is None:
  303. trackingList = []
  304. self.trackingList = trackingList
  305. self.openedFiles = []
  306. def open(self, *a, **k):
  307. """
  308. Override 'open' to track all files opened by this path.
  309. """
  310. f = filepath.FilePath.open(self, *a, **k)
  311. self.openedFiles.append(f)
  312. return f
  313. def openedPaths(self):
  314. """
  315. Return a list of all L{TrackingFilePath}s associated with this
  316. L{TrackingFilePath} that have had their C{open()} method called.
  317. """
  318. return [path for path in self.trackingList if path.openedFiles]
  319. def clonePath(self, name):
  320. """
  321. Override L{filepath.FilePath.clonePath} to give the new path a reference
  322. to the same tracking list.
  323. """
  324. clone = TrackingFilePath(name, trackingList=self.trackingList)
  325. self.trackingList.append(clone)
  326. return clone
  327. class ExplodingFilePath(filepath.FilePath):
  328. """
  329. A specialized L{FilePath} which always returns an instance of
  330. L{ExplodingFile} from its C{open} method.
  331. @ivar fp: The L{ExplodingFile} instance most recently returned from the
  332. C{open} method.
  333. """
  334. def __init__(self, pathName, originalExploder=None):
  335. """
  336. Initialize an L{ExplodingFilePath} with a name and a reference to the
  337. @param pathName: The path name as passed to L{filepath.FilePath}.
  338. @type pathName: C{str}
  339. @param originalExploder: The L{ExplodingFilePath} to associate opened
  340. files with.
  341. @type originalExploder: L{ExplodingFilePath}
  342. """
  343. filepath.FilePath.__init__(self, pathName)
  344. if originalExploder is None:
  345. originalExploder = self
  346. self._originalExploder = originalExploder
  347. def open(self, mode=None):
  348. """
  349. Create, save, and return a new C{ExplodingFile}.
  350. @param mode: Present for signature compatibility. Ignored.
  351. @return: A new C{ExplodingFile}.
  352. """
  353. f = self._originalExploder.fp = ExplodingFile()
  354. return f
  355. def clonePath(self, name):
  356. return ExplodingFilePath(name, self._originalExploder)
  357. class PermissionsTests(BytesTestCase):
  358. """
  359. Test Permissions and RWX classes
  360. """
  361. def assertNotUnequal(self, first, second, msg=None):
  362. """
  363. Tests that C{first} != C{second} is false. This method tests the
  364. __ne__ method, as opposed to L{assertEqual} (C{first} == C{second}),
  365. which tests the __eq__ method.
  366. Note: this should really be part of trial
  367. """
  368. if first != second:
  369. if msg is None:
  370. msg = '';
  371. if len(msg) > 0:
  372. msg += '\n'
  373. raise self.failureException(
  374. '%snot not unequal (__ne__ not implemented correctly):'
  375. '\na = %s\nb = %s\n'
  376. % (msg, pformat(first), pformat(second)))
  377. return first
  378. def test_rwxFromBools(self):
  379. """
  380. L{RWX}'s constructor takes a set of booleans
  381. """
  382. for r in (True, False):
  383. for w in (True, False):
  384. for x in (True, False):
  385. rwx = filepath.RWX(r, w, x)
  386. self.assertEqual(rwx.read, r)
  387. self.assertEqual(rwx.write, w)
  388. self.assertEqual(rwx.execute, x)
  389. rwx = filepath.RWX(True, True, True)
  390. self.assertTrue(rwx.read and rwx.write and rwx.execute)
  391. def test_rwxEqNe(self):
  392. """
  393. L{RWX}'s created with the same booleans are equivalent. If booleans
  394. are different, they are not equal.
  395. """
  396. for r in (True, False):
  397. for w in (True, False):
  398. for x in (True, False):
  399. self.assertEqual(filepath.RWX(r, w, x),
  400. filepath.RWX(r, w, x))
  401. self.assertNotUnequal(filepath.RWX(r, w, x),
  402. filepath.RWX(r, w, x))
  403. self.assertNotEqual(filepath.RWX(True, True, True),
  404. filepath.RWX(True, True, False))
  405. self.assertNotEqual(3, filepath.RWX(True, True, True))
  406. def test_rwxShorthand(self):
  407. """
  408. L{RWX}'s shorthand string should be 'rwx' if read, write, and execute
  409. permission bits are true. If any of those permissions bits are false,
  410. the character is replaced by a '-'.
  411. """
  412. def getChar(val, letter):
  413. if val:
  414. return letter
  415. return '-'
  416. for r in (True, False):
  417. for w in (True, False):
  418. for x in (True, False):
  419. rwx = filepath.RWX(r, w, x)
  420. self.assertEqual(rwx.shorthand(),
  421. getChar(r, 'r') +
  422. getChar(w, 'w') +
  423. getChar(x, 'x'))
  424. self.assertEqual(filepath.RWX(True, False, True).shorthand(), "r-x")
  425. def test_permissionsFromStat(self):
  426. """
  427. L{Permissions}'s constructor takes a valid permissions bitmask and
  428. parsaes it to produce the correct set of boolean permissions.
  429. """
  430. def _rwxFromStat(statModeInt, who):
  431. def getPermissionBit(what, who):
  432. return (statModeInt &
  433. getattr(stat, "S_I%s%s" % (what, who))) > 0
  434. return filepath.RWX(*[getPermissionBit(what, who) for what in
  435. ('R', 'W', 'X')])
  436. for u in range(0, 8):
  437. for g in range(0, 8):
  438. for o in range(0, 8):
  439. chmodString = "%d%d%d" % (u, g, o)
  440. chmodVal = int(chmodString, 8)
  441. perm = filepath.Permissions(chmodVal)
  442. self.assertEqual(perm.user,
  443. _rwxFromStat(chmodVal, "USR"),
  444. "%s: got user: %s" %
  445. (chmodString, perm.user))
  446. self.assertEqual(perm.group,
  447. _rwxFromStat(chmodVal, "GRP"),
  448. "%s: got group: %s" %
  449. (chmodString, perm.group))
  450. self.assertEqual(perm.other,
  451. _rwxFromStat(chmodVal, "OTH"),
  452. "%s: got other: %s" %
  453. (chmodString, perm.other))
  454. perm = filepath.Permissions(0o777)
  455. for who in ("user", "group", "other"):
  456. for what in ("read", "write", "execute"):
  457. self.assertTrue(getattr(getattr(perm, who), what))
  458. def test_permissionsEq(self):
  459. """
  460. Two L{Permissions}'s that are created with the same bitmask
  461. are equivalent
  462. """
  463. self.assertEqual(filepath.Permissions(0o777),
  464. filepath.Permissions(0o777))
  465. self.assertNotUnequal(filepath.Permissions(0o777),
  466. filepath.Permissions(0o777))
  467. self.assertNotEqual(filepath.Permissions(0o777),
  468. filepath.Permissions(0o700))
  469. self.assertNotEqual(3, filepath.Permissions(0o777))
  470. def test_permissionsShorthand(self):
  471. """
  472. L{Permissions}'s shorthand string is the RWX shorthand string for its
  473. user permission bits, group permission bits, and other permission bits
  474. concatenated together, without a space.
  475. """
  476. for u in range(0, 8):
  477. for g in range(0, 8):
  478. for o in range(0, 8):
  479. perm = filepath.Permissions(int("0o%d%d%d" % (u, g, o), 8))
  480. self.assertEqual(perm.shorthand(),
  481. ''.join(x.shorthand() for x in (
  482. perm.user, perm.group, perm.other)))
  483. self.assertEqual(filepath.Permissions(0o770).shorthand(), "rwxrwx---")
  484. class FilePathTests(AbstractFilePathTests):
  485. """
  486. Test various L{FilePath} path manipulations.
  487. In particular, note that tests defined on this class instead of on the base
  488. class are only run against L{twisted.python.filepath}.
  489. """
  490. def test_chmod(self):
  491. """
  492. L{FilePath.chmod} modifies the permissions of
  493. the passed file as expected (using C{os.stat} to check). We use some
  494. basic modes that should work everywhere (even on Windows).
  495. """
  496. for mode in (0o555, 0o777):
  497. self.path.child(b"sub1").chmod(mode)
  498. self.assertEqual(
  499. stat.S_IMODE(os.stat(self.path.child(b"sub1").path).st_mode),
  500. mode)
  501. def symlink(self, target, name):
  502. """
  503. Create a symbolic link named C{name} pointing at C{target}.
  504. @type target: C{str}
  505. @type name: C{str}
  506. @raise SkipTest: raised if symbolic links are not supported on the
  507. host platform.
  508. """
  509. if symlinkSkip:
  510. raise SkipTest(symlinkSkip)
  511. os.symlink(target, name)
  512. def createLinks(self):
  513. """
  514. Create several symbolic links to files and directories.
  515. """
  516. subdir = self.path.child(b"sub1")
  517. self.symlink(subdir.path, self._mkpath(b"sub1.link"))
  518. self.symlink(subdir.child(b"file2").path, self._mkpath(b"file2.link"))
  519. self.symlink(subdir.child(b"file2").path,
  520. self._mkpath(b"sub1", b"sub1.file2.link"))
  521. def test_realpathSymlink(self):
  522. """
  523. L{FilePath.realpath} returns the path of the ultimate target of a
  524. symlink.
  525. """
  526. self.createLinks()
  527. self.symlink(self.path.child(b"file2.link").path,
  528. self.path.child(b"link.link").path)
  529. self.assertEqual(self.path.child(b"link.link").realpath(),
  530. self.path.child(b"sub1").child(b"file2"))
  531. def test_realpathCyclicalSymlink(self):
  532. """
  533. L{FilePath.realpath} raises L{filepath.LinkError} if the path is a
  534. symbolic link which is part of a cycle.
  535. """
  536. self.symlink(self.path.child(b"link1").path, self.path.child(b"link2").path)
  537. self.symlink(self.path.child(b"link2").path, self.path.child(b"link1").path)
  538. self.assertRaises(filepath.LinkError,
  539. self.path.child(b"link2").realpath)
  540. def test_realpathNoSymlink(self):
  541. """
  542. L{FilePath.realpath} returns the path itself if the path is not a
  543. symbolic link.
  544. """
  545. self.assertEqual(self.path.child(b"sub1").realpath(),
  546. self.path.child(b"sub1"))
  547. def test_walkCyclicalSymlink(self):
  548. """
  549. Verify that walking a path with a cyclical symlink raises an error
  550. """
  551. self.createLinks()
  552. self.symlink(self.path.child(b"sub1").path,
  553. self.path.child(b"sub1").child(b"sub1.loopylink").path)
  554. def iterateOverPath():
  555. return [foo.path for foo in self.path.walk()]
  556. self.assertRaises(filepath.LinkError, iterateOverPath)
  557. def test_walkObeysDescendWithCyclicalSymlinks(self):
  558. """
  559. Verify that, after making a path with cyclical symlinks, when the
  560. supplied C{descend} predicate returns C{False}, the target is not
  561. traversed, as if it was a simple symlink.
  562. """
  563. self.createLinks()
  564. # we create cyclical symlinks
  565. self.symlink(self.path.child(b"sub1").path,
  566. self.path.child(b"sub1").child(b"sub1.loopylink").path)
  567. def noSymLinks(path):
  568. return not path.islink()
  569. def iterateOverPath():
  570. return [foo.path for foo in self.path.walk(descend=noSymLinks)]
  571. self.assertTrue(iterateOverPath())
  572. def test_walkObeysDescend(self):
  573. """
  574. Verify that when the supplied C{descend} predicate returns C{False},
  575. the target is not traversed.
  576. """
  577. self.createLinks()
  578. def noSymLinks(path):
  579. return not path.islink()
  580. x = [foo.path for foo in self.path.walk(descend=noSymLinks)]
  581. self.assertEqual(set(x), set(self.all))
  582. def test_getAndSet(self):
  583. content = b'newcontent'
  584. self.path.child(b'new').setContent(content)
  585. newcontent = self.path.child(b'new').getContent()
  586. self.assertEqual(content, newcontent)
  587. content = b'content'
  588. self.path.child(b'new').setContent(content, b'.tmp')
  589. newcontent = self.path.child(b'new').getContent()
  590. self.assertEqual(content, newcontent)
  591. def test_getContentFileClosing(self):
  592. """
  593. If reading from the underlying file raises an exception,
  594. L{FilePath.getContent} raises that exception after closing the file.
  595. """
  596. fp = ExplodingFilePath(b"")
  597. self.assertRaises(IOError, fp.getContent)
  598. self.assertTrue(fp.fp.closed)
  599. def test_symbolicLink(self):
  600. """
  601. Verify the behavior of the C{isLink} method against links and
  602. non-links. Also check that the symbolic link shares the directory
  603. property with its target.
  604. """
  605. s4 = self.path.child(b"sub4")
  606. s3 = self.path.child(b"sub3")
  607. self.symlink(s3.path, s4.path)
  608. self.assertTrue(s4.islink())
  609. self.assertFalse(s3.islink())
  610. self.assertTrue(s4.isdir())
  611. self.assertTrue(s3.isdir())
  612. def test_linkTo(self):
  613. """
  614. Verify that symlink creates a valid symlink that is both a link and a
  615. file if its target is a file, or a directory if its target is a
  616. directory.
  617. """
  618. targetLinks = [
  619. (self.path.child(b"sub2"), self.path.child(b"sub2.link")),
  620. (self.path.child(b"sub2").child(b"file3.ext1"),
  621. self.path.child(b"file3.ext1.link"))
  622. ]
  623. for target, link in targetLinks:
  624. target.linkTo(link)
  625. self.assertTrue(link.islink(), "This is a link")
  626. self.assertEqual(target.isdir(), link.isdir())
  627. self.assertEqual(target.isfile(), link.isfile())
  628. def test_linkToErrors(self):
  629. """
  630. Verify C{linkTo} fails in the following case:
  631. - the target is in a directory that doesn't exist
  632. - the target already exists
  633. """
  634. self.assertRaises(OSError, self.path.child(b"file1").linkTo,
  635. self.path.child(b'nosub').child(b'file1'))
  636. self.assertRaises(OSError, self.path.child(b"file1").linkTo,
  637. self.path.child(b'sub1').child(b'file2'))
  638. if symlinkSkip:
  639. test_symbolicLink.skip = symlinkSkip
  640. test_linkTo.skip = symlinkSkip
  641. test_linkToErrors.skip = symlinkSkip
  642. def testMultiExt(self):
  643. f3 = self.path.child(b'sub3').child(b'file3')
  644. exts = b'.foo', b'.bar', b'ext1', b'ext2', b'ext3'
  645. self.assertFalse(f3.siblingExtensionSearch(*exts))
  646. f3e = f3.siblingExtension(b".foo")
  647. f3e.touch()
  648. self.assertFalse(not f3.siblingExtensionSearch(*exts).exists())
  649. self.assertFalse(not f3.siblingExtensionSearch(b'*').exists())
  650. f3e.remove()
  651. self.assertFalse(f3.siblingExtensionSearch(*exts))
  652. def testPreauthChild(self):
  653. fp = filepath.FilePath(b'.')
  654. fp.preauthChild(b'foo/bar')
  655. self.assertRaises(filepath.InsecurePath, fp.child, u'/mon\u20acy')
  656. def testStatCache(self):
  657. p = self.path.child(b'stattest')
  658. p.touch()
  659. self.assertEqual(p.getsize(), 0)
  660. self.assertEqual(abs(p.getmtime() - time.time()) // 20, 0)
  661. self.assertEqual(abs(p.getctime() - time.time()) // 20, 0)
  662. self.assertEqual(abs(p.getatime() - time.time()) // 20, 0)
  663. self.assertTrue(p.exists())
  664. self.assertTrue(p.exists())
  665. # OOB removal: FilePath.remove() will automatically restat
  666. os.remove(p.path)
  667. # test caching
  668. self.assertTrue(p.exists())
  669. p.restat(reraise=False)
  670. self.assertFalse(p.exists())
  671. self.assertFalse(p.islink())
  672. self.assertFalse(p.isdir())
  673. self.assertFalse(p.isfile())
  674. def testPersist(self):
  675. newpath = pickle.loads(pickle.dumps(self.path))
  676. self.assertEqual(self.path.__class__, newpath.__class__)
  677. self.assertEqual(self.path.path, newpath.path)
  678. def testInsecureUNIX(self):
  679. self.assertRaises(filepath.InsecurePath, self.path.child, b"..")
  680. self.assertRaises(filepath.InsecurePath, self.path.child, b"/etc")
  681. self.assertRaises(filepath.InsecurePath, self.path.child, b"../..")
  682. def testInsecureWin32(self):
  683. self.assertRaises(filepath.InsecurePath, self.path.child, b"..\\..")
  684. self.assertRaises(filepath.InsecurePath, self.path.child, b"C:randomfile")
  685. if platform.getType() != 'win32':
  686. testInsecureWin32.skip = "Test will run only on Windows."
  687. def testInsecureWin32Whacky(self):
  688. """
  689. Windows has 'special' filenames like NUL and CON and COM1 and LPR
  690. and PRN and ... god knows what else. They can be located anywhere in
  691. the filesystem. For obvious reasons, we do not wish to normally permit
  692. access to these.
  693. """
  694. self.assertRaises(filepath.InsecurePath, self.path.child, b"CON")
  695. self.assertRaises(filepath.InsecurePath, self.path.child, b"C:CON")
  696. self.assertRaises(filepath.InsecurePath, self.path.child, r"C:\CON")
  697. if platform.getType() != 'win32':
  698. testInsecureWin32Whacky.skip = "Test will run only on Windows."
  699. def testComparison(self):
  700. self.assertEqual(filepath.FilePath(b'a'),
  701. filepath.FilePath(b'a'))
  702. self.assertTrue(filepath.FilePath(b'z') >
  703. filepath.FilePath(b'a'))
  704. self.assertTrue(filepath.FilePath(b'z') >=
  705. filepath.FilePath(b'a'))
  706. self.assertTrue(filepath.FilePath(b'a') >=
  707. filepath.FilePath(b'a'))
  708. self.assertTrue(filepath.FilePath(b'a') <=
  709. filepath.FilePath(b'a'))
  710. self.assertTrue(filepath.FilePath(b'a') <
  711. filepath.FilePath(b'z'))
  712. self.assertTrue(filepath.FilePath(b'a') <=
  713. filepath.FilePath(b'z'))
  714. self.assertTrue(filepath.FilePath(b'a') !=
  715. filepath.FilePath(b'z'))
  716. self.assertTrue(filepath.FilePath(b'z') !=
  717. filepath.FilePath(b'a'))
  718. self.assertFalse(filepath.FilePath(b'z') !=
  719. filepath.FilePath(b'z'))
  720. def test_descendantOnly(self):
  721. """
  722. If C{".."} is in the sequence passed to L{FilePath.descendant},
  723. L{InsecurePath} is raised.
  724. """
  725. self.assertRaises(
  726. filepath.InsecurePath,
  727. self.path.descendant, [u'mon\u20acy', u'..'])
  728. def testSibling(self):
  729. p = self.path.child(b'sibling_start')
  730. ts = p.sibling(b'sibling_test')
  731. self.assertEqual(ts.dirname(), p.dirname())
  732. self.assertEqual(ts.basename(), b'sibling_test')
  733. ts.createDirectory()
  734. self.assertIn(ts, self.path.children())
  735. def testTemporarySibling(self):
  736. ts = self.path.temporarySibling()
  737. self.assertEqual(ts.dirname(), self.path.dirname())
  738. self.assertNotIn(ts.basename(), self.path.listdir())
  739. ts.createDirectory()
  740. self.assertIn(ts, self.path.parent().children())
  741. def test_temporarySiblingExtension(self):
  742. """
  743. If L{FilePath.temporarySibling} is given an extension argument, it will
  744. produce path objects with that extension appended to their names.
  745. """
  746. testExtension = b".test-extension"
  747. ts = self.path.temporarySibling(testExtension)
  748. self.assertTrue(ts.basename().endswith(testExtension),
  749. "%s does not end with %s" % (
  750. ts.basename(), testExtension))
  751. def test_removeDirectory(self):
  752. """
  753. L{FilePath.remove} on a L{FilePath} that refers to a directory will
  754. recursively delete its contents.
  755. """
  756. self.path.remove()
  757. self.assertFalse(self.path.exists())
  758. def test_removeWithSymlink(self):
  759. """
  760. For a path which is a symbolic link, L{FilePath.remove} just deletes
  761. the link, not the target.
  762. """
  763. link = self.path.child(b"sub1.link")
  764. # setUp creates the sub1 child
  765. self.symlink(self.path.child(b"sub1").path, link.path)
  766. link.remove()
  767. self.assertFalse(link.exists())
  768. self.assertTrue(self.path.child(b"sub1").exists())
  769. def test_copyToDirectory(self):
  770. """
  771. L{FilePath.copyTo} makes a copy of all the contents of the directory
  772. named by that L{FilePath} if it is able to do so.
  773. """
  774. oldPaths = list(self.path.walk()) # Record initial state
  775. fp = filepath.FilePath(self.mktemp())
  776. self.path.copyTo(fp)
  777. self.path.remove()
  778. fp.copyTo(self.path)
  779. newPaths = list(self.path.walk()) # Record double-copy state
  780. newPaths.sort()
  781. oldPaths.sort()
  782. self.assertEqual(newPaths, oldPaths)
  783. def test_copyToMissingDestFileClosing(self):
  784. """
  785. If an exception is raised while L{FilePath.copyTo} is trying to open
  786. source file to read from, the destination file is closed and the
  787. exception is raised to the caller of L{FilePath.copyTo}.
  788. """
  789. nosuch = self.path.child(b"nothere")
  790. # Make it look like something to copy, even though it doesn't exist.
  791. # This could happen if the file is deleted between the isfile check and
  792. # the file actually being opened.
  793. nosuch.isfile = lambda: True
  794. # We won't get as far as writing to this file, but it's still useful for
  795. # tracking whether we closed it.
  796. destination = ExplodingFilePath(self.mktemp())
  797. self.assertRaises(IOError, nosuch.copyTo, destination)
  798. self.assertTrue(destination.fp.closed)
  799. def test_copyToFileClosing(self):
  800. """
  801. If an exception is raised while L{FilePath.copyTo} is copying bytes
  802. between two regular files, the source and destination files are closed
  803. and the exception propagates to the caller of L{FilePath.copyTo}.
  804. """
  805. destination = ExplodingFilePath(self.mktemp())
  806. source = ExplodingFilePath(__file__)
  807. self.assertRaises(IOError, source.copyTo, destination)
  808. self.assertTrue(source.fp.closed)
  809. self.assertTrue(destination.fp.closed)
  810. def test_copyToDirectoryItself(self):
  811. """
  812. L{FilePath.copyTo} fails with an OSError or IOError (depending on
  813. platform, as it propagates errors from open() and write()) when
  814. attempting to copy a directory to a child of itself.
  815. """
  816. self.assertRaises((OSError, IOError),
  817. self.path.copyTo, self.path.child(b'file1'))
  818. def test_copyToWithSymlink(self):
  819. """
  820. Verify that copying with followLinks=True copies symlink targets
  821. instead of symlinks
  822. """
  823. self.symlink(self.path.child(b"sub1").path,
  824. self.path.child(b"link1").path)
  825. fp = filepath.FilePath(self.mktemp())
  826. self.path.copyTo(fp)
  827. self.assertFalse(fp.child(b"link1").islink())
  828. self.assertEqual([x.basename() for x in fp.child(b"sub1").children()],
  829. [x.basename() for x in fp.child(b"link1").children()])
  830. def test_copyToWithoutSymlink(self):
  831. """
  832. Verify that copying with followLinks=False copies symlinks as symlinks
  833. """
  834. self.symlink(b"sub1", self.path.child(b"link1").path)
  835. fp = filepath.FilePath(self.mktemp())
  836. self.path.copyTo(fp, followLinks=False)
  837. self.assertTrue(fp.child(b"link1").islink())
  838. self.assertEqual(os.readlink(self.path.child(b"link1").path),
  839. os.readlink(fp.child(b"link1").path))
  840. def test_copyToMissingSource(self):
  841. """
  842. If the source path is missing, L{FilePath.copyTo} raises L{OSError}.
  843. """
  844. path = filepath.FilePath(self.mktemp())
  845. exc = self.assertRaises(OSError, path.copyTo, b'some other path')
  846. self.assertEqual(exc.errno, errno.ENOENT)
  847. def test_moveTo(self):
  848. """
  849. Verify that moving an entire directory results into another directory
  850. with the same content.
  851. """
  852. oldPaths = list(self.path.walk()) # Record initial state
  853. fp = filepath.FilePath(self.mktemp())
  854. self.path.moveTo(fp)
  855. fp.moveTo(self.path)
  856. newPaths = list(self.path.walk()) # Record double-move state
  857. newPaths.sort()
  858. oldPaths.sort()
  859. self.assertEqual(newPaths, oldPaths)
  860. def test_moveToExistsCache(self):
  861. """
  862. A L{FilePath} that has been moved aside with L{FilePath.moveTo} no
  863. longer registers as existing. Its previously non-existent target
  864. exists, though, as it was created by the call to C{moveTo}.
  865. """
  866. fp = filepath.FilePath(self.mktemp())
  867. fp2 = filepath.FilePath(self.mktemp())
  868. fp.touch()
  869. # Both a sanity check (make sure the file status looks right) and an
  870. # enticement for stat-caching logic to kick in and remember that these
  871. # exist / don't exist.
  872. self.assertTrue(fp.exists())
  873. self.assertFalse(fp2.exists())
  874. fp.moveTo(fp2)
  875. self.assertFalse(fp.exists())
  876. self.assertTrue(fp2.exists())
  877. def test_moveToExistsCacheCrossMount(self):
  878. """
  879. The assertion of test_moveToExistsCache should hold in the case of a
  880. cross-mount move.
  881. """
  882. self.setUpFaultyRename()
  883. self.test_moveToExistsCache()
  884. def test_moveToSizeCache(self, hook=lambda : None):
  885. """
  886. L{FilePath.moveTo} clears its destination's status cache, such that
  887. calls to L{FilePath.getsize} after the call to C{moveTo} will report the
  888. new size, not the old one.
  889. This is a separate test from C{test_moveToExistsCache} because it is
  890. intended to cover the fact that the destination's cache is dropped;
  891. test_moveToExistsCache doesn't cover this case because (currently) a
  892. file that doesn't exist yet does not cache the fact of its non-
  893. existence.
  894. """
  895. fp = filepath.FilePath(self.mktemp())
  896. fp2 = filepath.FilePath(self.mktemp())
  897. fp.setContent(b"1234")
  898. fp2.setContent(b"1234567890")
  899. hook()
  900. # Sanity check / kick off caching.
  901. self.assertEqual(fp.getsize(), 4)
  902. self.assertEqual(fp2.getsize(), 10)
  903. # Actually attempting to replace a file on Windows would fail with
  904. # ERROR_ALREADY_EXISTS, but we don't need to test that, just the cached
  905. # metadata, so, delete the file ...
  906. os.remove(fp2.path)
  907. # ... but don't clear the status cache, as fp2.remove() would.
  908. self.assertEqual(fp2.getsize(), 10)
  909. fp.moveTo(fp2)
  910. self.assertEqual(fp2.getsize(), 4)
  911. def test_moveToSizeCacheCrossMount(self):
  912. """
  913. The assertion of test_moveToSizeCache should hold in the case of a
  914. cross-mount move.
  915. """
  916. self.test_moveToSizeCache(hook=self.setUpFaultyRename)
  917. def test_moveToError(self):
  918. """
  919. Verify error behavior of moveTo: it should raises one of OSError or
  920. IOError if you want to move a path into one of its child. It's simply
  921. the error raised by the underlying rename system call.
  922. """
  923. self.assertRaises((OSError, IOError), self.path.moveTo, self.path.child(b'file1'))
  924. def setUpFaultyRename(self):
  925. """
  926. Set up a C{os.rename} that will fail with L{errno.EXDEV} on first call.
  927. This is used to simulate a cross-device rename failure.
  928. @return: a list of pair (src, dest) of calls to C{os.rename}
  929. @rtype: C{list} of C{tuple}
  930. """
  931. invokedWith = []
  932. def faultyRename(src, dest):
  933. invokedWith.append((src, dest))
  934. if len(invokedWith) == 1:
  935. raise OSError(errno.EXDEV, 'Test-induced failure simulating '
  936. 'cross-device rename failure')
  937. return originalRename(src, dest)
  938. originalRename = os.rename
  939. self.patch(os, "rename", faultyRename)
  940. return invokedWith
  941. def test_crossMountMoveTo(self):
  942. """
  943. C{moveTo} should be able to handle C{EXDEV} error raised by
  944. C{os.rename} when trying to move a file on a different mounted
  945. filesystem.
  946. """
  947. invokedWith = self.setUpFaultyRename()
  948. # Bit of a whitebox test - force os.rename, which moveTo tries
  949. # before falling back to a slower method, to fail, forcing moveTo to
  950. # use the slower behavior.
  951. self.test_moveTo()
  952. # A bit of a sanity check for this whitebox test - if our rename
  953. # was never invoked, the test has probably fallen into disrepair!
  954. self.assertTrue(invokedWith)
  955. def test_crossMountMoveToWithSymlink(self):
  956. """
  957. By default, when moving a symlink, it should follow the link and
  958. actually copy the content of the linked node.
  959. """
  960. invokedWith = self.setUpFaultyRename()
  961. f2 = self.path.child(b'file2')
  962. f3 = self.path.child(b'file3')
  963. self.symlink(self.path.child(b'file1').path, f2.path)
  964. f2.moveTo(f3)
  965. self.assertFalse(f3.islink())
  966. self.assertEqual(f3.getContent(), b'file 1')
  967. self.assertTrue(invokedWith)
  968. def test_crossMountMoveToWithoutSymlink(self):
  969. """
  970. Verify that moveTo called with followLinks=False actually create
  971. another symlink.
  972. """
  973. invokedWith = self.setUpFaultyRename()
  974. f2 = self.path.child(b'file2')
  975. f3 = self.path.child(b'file3')
  976. self.symlink(self.path.child(b'file1').path, f2.path)
  977. f2.moveTo(f3, followLinks=False)
  978. self.assertTrue(f3.islink())
  979. self.assertEqual(f3.getContent(), b'file 1')
  980. self.assertTrue(invokedWith)
  981. def test_createBinaryMode(self):
  982. """
  983. L{FilePath.create} should always open (and write to) files in binary
  984. mode; line-feed octets should be unmodified.
  985. (While this test should pass on all platforms, it is only really
  986. interesting on platforms which have the concept of binary mode, i.e.
  987. Windows platforms.)
  988. """
  989. path = filepath.FilePath(self.mktemp())
  990. with path.create() as f:
  991. self.assertIn("b", f.mode)
  992. f.write(b"\n")
  993. with open(path.path, "rb") as fp:
  994. read = fp.read()
  995. self.assertEqual(read, b"\n")
  996. def testOpen(self):
  997. # Opening a file for reading when it does not already exist is an error
  998. nonexistent = self.path.child(b'nonexistent')
  999. e = self.assertRaises(IOError, nonexistent.open)
  1000. self.assertEqual(e.errno, errno.ENOENT)
  1001. # Opening a file for writing when it does not exist is okay
  1002. writer = self.path.child(b'writer')
  1003. with writer.open('w') as f:
  1004. f.write(b'abc\ndef')
  1005. # Make sure those bytes ended up there - and test opening a file for
  1006. # reading when it does exist at the same time
  1007. with writer.open() as f:
  1008. self.assertEqual(f.read(), b'abc\ndef')
  1009. # Re-opening that file in write mode should erase whatever was there.
  1010. writer.open('w').close()
  1011. with writer.open() as f:
  1012. self.assertEqual(f.read(), b'')
  1013. # Put some bytes in a file so we can test that appending does not
  1014. # destroy them.
  1015. appender = self.path.child(b'appender')
  1016. with appender.open('w') as f:
  1017. f.write(b'abc')
  1018. with appender.open('a') as f:
  1019. f.write(b'def')
  1020. with appender.open('r') as f:
  1021. self.assertEqual(f.read(), b'abcdef')
  1022. # read/write should let us do both without erasing those bytes
  1023. with appender.open('r+') as f:
  1024. self.assertEqual(f.read(), b'abcdef')
  1025. # ANSI C *requires* an fseek or an fgetpos between an fread and an
  1026. # fwrite or an fwrite and an fread. We can't reliably get Python to
  1027. # invoke fgetpos, so we seek to a 0 byte offset from the current
  1028. # position instead. Also, Python sucks for making this seek
  1029. # relative to 1 instead of a symbolic constant representing the
  1030. # current file position.
  1031. f.seek(0, 1)
  1032. # Put in some new bytes for us to test for later.
  1033. f.write(b'ghi')
  1034. # Make sure those new bytes really showed up
  1035. with appender.open('r') as f:
  1036. self.assertEqual(f.read(), b'abcdefghi')
  1037. # write/read should let us do both, but erase anything that's there
  1038. # already.
  1039. with appender.open('w+') as f:
  1040. self.assertEqual(f.read(), b'')
  1041. f.seek(0, 1) # Don't forget this!
  1042. f.write(b'123')
  1043. # super append mode should let us read and write and also position the
  1044. # cursor at the end of the file, without erasing everything.
  1045. with appender.open('a+') as f:
  1046. # The order of these lines may seem surprising, but it is
  1047. # necessary. The cursor is not at the end of the file until after
  1048. # the first write.
  1049. f.write(b'456')
  1050. f.seek(0, 1) # Asinine.
  1051. self.assertEqual(f.read(), b'')
  1052. f.seek(0, 0)
  1053. self.assertEqual(f.read(), b'123456')
  1054. # Opening a file exclusively must fail if that file exists already.
  1055. nonexistent.requireCreate(True)
  1056. nonexistent.open('w').close()
  1057. existent = nonexistent
  1058. del nonexistent
  1059. self.assertRaises((OSError, IOError), existent.open)
  1060. def test_openWithExplicitBinaryMode(self):
  1061. """
  1062. Due to a bug in Python 2.7 on Windows including multiple 'b'
  1063. characters in the mode passed to the built-in open() will cause an
  1064. error. FilePath.open() ensures that only a single 'b' character is
  1065. included in the mode passed to the built-in open().
  1066. See http://bugs.python.org/issue7686 for details about the bug.
  1067. """
  1068. writer = self.path.child(b'explicit-binary')
  1069. with writer.open('wb') as file:
  1070. file.write(b'abc\ndef')
  1071. self.assertTrue(writer.exists)
  1072. def test_openWithRedundantExplicitBinaryModes(self):
  1073. """
  1074. Due to a bug in Python 2.7 on Windows including multiple 'b'
  1075. characters in the mode passed to the built-in open() will cause an
  1076. error. No matter how many 'b' modes are specified, FilePath.open()
  1077. ensures that only a single 'b' character is included in the mode
  1078. passed to the built-in open().
  1079. See http://bugs.python.org/issue7686 for details about the bug.
  1080. """
  1081. writer = self.path.child(b'multiple-binary')
  1082. with writer.open('wbb') as file:
  1083. file.write(b'abc\ndef')
  1084. self.assertTrue(writer.exists)
  1085. def test_existsCache(self):
  1086. """
  1087. Check that C{filepath.FilePath.exists} correctly restat the object if
  1088. an operation has occurred in the mean time.
  1089. """
  1090. fp = filepath.FilePath(self.mktemp())
  1091. self.assertFalse(fp.exists())
  1092. fp.makedirs()
  1093. self.assertTrue(fp.exists())
  1094. def test_makedirsMakesDirectoriesRecursively(self):
  1095. """
  1096. C{FilePath.makedirs} creates a directory at C{path}}, including
  1097. recursively creating all parent directories leading up to the path.
  1098. """
  1099. fp = filepath.FilePath(os.path.join(
  1100. self.mktemp(), b"foo", b"bar", b"baz"))
  1101. self.assertFalse(fp.exists())
  1102. fp.makedirs()
  1103. self.assertTrue(fp.exists())
  1104. self.assertTrue(fp.isdir())
  1105. def test_makedirsMakesDirectoriesWithIgnoreExistingDirectory(self):
  1106. """
  1107. Calling C{FilePath.makedirs} with C{ignoreExistingDirectory} set to
  1108. C{True} has no effect if directory does not exist.
  1109. """
  1110. fp = filepath.FilePath(self.mktemp())
  1111. self.assertFalse(fp.exists())
  1112. fp.makedirs(ignoreExistingDirectory=True)
  1113. self.assertTrue(fp.exists())
  1114. self.assertTrue(fp.isdir())
  1115. def test_makedirsThrowsWithExistentDirectory(self):
  1116. """
  1117. C{FilePath.makedirs} throws an C{OSError} exception
  1118. when called on a directory that already exists.
  1119. """
  1120. fp = filepath.FilePath(os.path.join(self.mktemp()))
  1121. fp.makedirs()
  1122. exception = self.assertRaises(OSError, fp.makedirs)
  1123. self.assertEqual(exception.errno, errno.EEXIST)
  1124. def test_makedirsAcceptsIgnoreExistingDirectory(self):
  1125. """
  1126. C{FilePath.makedirs} succeeds when called on a directory that already
  1127. exists and the c{ignoreExistingDirectory} argument is set to C{True}.
  1128. """
  1129. fp = filepath.FilePath(self.mktemp())
  1130. fp.makedirs()
  1131. self.assertTrue(fp.exists())
  1132. fp.makedirs(ignoreExistingDirectory=True)
  1133. self.assertTrue(fp.exists())
  1134. def test_makedirsIgnoreExistingDirectoryExistAlreadyAFile(self):
  1135. """
  1136. When C{FilePath.makedirs} is called with C{ignoreExistingDirectory} set
  1137. to C{True} it throws an C{OSError} exceptions if path is a file.
  1138. """
  1139. fp = filepath.FilePath(self.mktemp())
  1140. fp.create()
  1141. self.assertTrue(fp.isfile())
  1142. exception = self.assertRaises(
  1143. OSError, fp.makedirs, ignoreExistingDirectory=True)
  1144. self.assertEqual(exception.errno, errno.EEXIST)
  1145. def test_makedirsRaisesNonEexistErrorsIgnoreExistingDirectory(self):
  1146. """
  1147. When C{FilePath.makedirs} is called with C{ignoreExistingDirectory} set
  1148. to C{True} it raises an C{OSError} exception if exception errno is not
  1149. EEXIST.
  1150. """
  1151. def faultyMakedirs(path):
  1152. raise OSError(errno.EACCES, 'Permission Denied')
  1153. self.patch(os, 'makedirs', faultyMakedirs)
  1154. fp = filepath.FilePath(self.mktemp())
  1155. exception = self.assertRaises(
  1156. OSError, fp.makedirs, ignoreExistingDirectory=True)
  1157. self.assertEqual(exception.errno, errno.EACCES)
  1158. def test_changed(self):
  1159. """
  1160. L{FilePath.changed} indicates that the L{FilePath} has changed, but does
  1161. not re-read the status information from the filesystem until it is
  1162. queried again via another method, such as C{getsize}.
  1163. """
  1164. fp = filepath.FilePath(self.mktemp())
  1165. fp.setContent(b"12345")
  1166. self.assertEqual(fp.getsize(), 5)
  1167. # Someone else comes along and changes the file.
  1168. with open(fp.path, 'wb') as fObj:
  1169. fObj.write(b"12345678")
  1170. # Sanity check for caching: size should still be 5.
  1171. self.assertEqual(fp.getsize(), 5)
  1172. fp.changed()
  1173. # This path should look like we don't know what status it's in, not that
  1174. # we know that it didn't exist when last we checked.
  1175. self.assertIsNone(fp.statinfo)
  1176. self.assertEqual(fp.getsize(), 8)
  1177. def test_getPermissions_POSIX(self):
  1178. """
  1179. Getting permissions for a file returns a L{Permissions} object for
  1180. POSIX platforms (which supports separate user, group, and other
  1181. permissions bits.
  1182. """
  1183. for mode in (0o777, 0o700):
  1184. self.path.child(b"sub1").chmod(mode)
  1185. self.assertEqual(self.path.child(b"sub1").getPermissions(),
  1186. filepath.Permissions(mode))
  1187. self.path.child(b"sub1").chmod(0o764) #sanity check
  1188. self.assertEqual(
  1189. self.path.child(b"sub1").getPermissions().shorthand(),
  1190. "rwxrw-r--")
  1191. def test_deprecateStatinfoGetter(self):
  1192. """
  1193. Getting L{twisted.python.filepath.FilePath.statinfo} is deprecated.
  1194. """
  1195. fp = filepath.FilePath(self.mktemp())
  1196. fp.statinfo
  1197. warningInfo = self.flushWarnings([self.test_deprecateStatinfoGetter])
  1198. self.assertEqual(len(warningInfo), 1)
  1199. self.assertEqual(warningInfo[0]['category'], DeprecationWarning)
  1200. self.assertEqual(
  1201. warningInfo[0]['message'],
  1202. "twisted.python.filepath.FilePath.statinfo was deprecated in "
  1203. "Twisted 15.0.0; please use other FilePath methods such as "
  1204. "getsize(), isdir(), getModificationTime(), etc. instead")
  1205. def test_deprecateStatinfoSetter(self):
  1206. """
  1207. Setting L{twisted.python.filepath.FilePath.statinfo} is deprecated.
  1208. """
  1209. fp = filepath.FilePath(self.mktemp())
  1210. fp.statinfo = None
  1211. warningInfo = self.flushWarnings([self.test_deprecateStatinfoSetter])
  1212. self.assertEqual(len(warningInfo), 1)
  1213. self.assertEqual(warningInfo[0]['category'], DeprecationWarning)
  1214. self.assertEqual(
  1215. warningInfo[0]['message'],
  1216. "twisted.python.filepath.FilePath.statinfo was deprecated in "
  1217. "Twisted 15.0.0; please use other FilePath methods such as "
  1218. "getsize(), isdir(), getModificationTime(), etc. instead")
  1219. def test_deprecateStatinfoSetterSets(self):
  1220. """
  1221. Setting L{twisted.python.filepath.FilePath.statinfo} changes the value
  1222. of _statinfo such that getting statinfo again returns the new value.
  1223. """
  1224. fp = filepath.FilePath(self.mktemp())
  1225. fp.statinfo = None
  1226. self.assertIsNone(fp.statinfo)
  1227. def test_filePathNotDeprecated(self):
  1228. """
  1229. While accessing L{twisted.python.filepath.FilePath.statinfo} is
  1230. deprecated, the filepath itself is not.
  1231. """
  1232. filepath.FilePath(self.mktemp())
  1233. warningInfo = self.flushWarnings([self.test_filePathNotDeprecated])
  1234. self.assertEqual(warningInfo, [])
  1235. def test_getPermissions_Windows(self):
  1236. """
  1237. Getting permissions for a file returns a L{Permissions} object in
  1238. Windows. Windows requires a different test, because user permissions
  1239. = group permissions = other permissions. Also, chmod may not be able
  1240. to set the execute bit, so we are skipping tests that set the execute
  1241. bit.
  1242. """
  1243. # Change permission after test so file can be deleted
  1244. self.addCleanup(self.path.child(b"sub1").chmod, 0o777)
  1245. for mode in (0o777, 0o555):
  1246. self.path.child(b"sub1").chmod(mode)
  1247. self.assertEqual(self.path.child(b"sub1").getPermissions(),
  1248. filepath.Permissions(mode))
  1249. self.path.child(b"sub1").chmod(0o511) #sanity check to make sure that
  1250. # user=group=other permissions
  1251. self.assertEqual(self.path.child(b"sub1").getPermissions().shorthand(),
  1252. "r-xr-xr-x")
  1253. def test_whetherBlockOrSocket(self):
  1254. """
  1255. Ensure that a file is not a block or socket
  1256. """
  1257. self.assertFalse(self.path.isBlockDevice())
  1258. self.assertFalse(self.path.isSocket())
  1259. def test_statinfoBitsNotImplementedInWindows(self):
  1260. """
  1261. Verify that certain file stats are not available on Windows
  1262. """
  1263. self.assertRaises(NotImplementedError, self.path.getInodeNumber)
  1264. self.assertRaises(NotImplementedError, self.path.getDevice)
  1265. self.assertRaises(NotImplementedError, self.path.getNumberOfHardLinks)
  1266. self.assertRaises(NotImplementedError, self.path.getUserID)
  1267. self.assertRaises(NotImplementedError, self.path.getGroupID)
  1268. def test_statinfoBitsAreNumbers(self):
  1269. """
  1270. Verify that file inode/device/nlinks/uid/gid stats are numbers in
  1271. a POSIX environment
  1272. """
  1273. if _PY3:
  1274. numbers = int
  1275. else:
  1276. numbers = (int, long)
  1277. c = self.path.child(b'file1')
  1278. for p in self.path, c:
  1279. self.assertIsInstance(p.getInodeNumber(), numbers)
  1280. self.assertIsInstance(p.getDevice(), numbers)
  1281. self.assertIsInstance(p.getNumberOfHardLinks(), numbers)
  1282. self.assertIsInstance(p.getUserID(), numbers)
  1283. self.assertIsInstance(p.getGroupID(), numbers)
  1284. self.assertEqual(self.path.getUserID(), c.getUserID())
  1285. self.assertEqual(self.path.getGroupID(), c.getGroupID())
  1286. def test_statinfoNumbersAreValid(self):
  1287. """
  1288. Verify that the right numbers come back from the right accessor methods
  1289. for file inode/device/nlinks/uid/gid (in a POSIX environment)
  1290. """
  1291. # specify fake statinfo information
  1292. class FakeStat:
  1293. st_ino = 200
  1294. st_dev = 300
  1295. st_nlink = 400
  1296. st_uid = 500
  1297. st_gid = 600
  1298. # monkey patch in a fake restat method for self.path
  1299. fake = FakeStat()
  1300. def fakeRestat(*args, **kwargs):
  1301. self.path._statinfo = fake
  1302. self.path.restat = fakeRestat
  1303. # ensure that restat will need to be called to get values
  1304. self.path._statinfo = None
  1305. self.assertEqual(self.path.getInodeNumber(), fake.st_ino)
  1306. self.assertEqual(self.path.getDevice(), fake.st_dev)
  1307. self.assertEqual(self.path.getNumberOfHardLinks(), fake.st_nlink)
  1308. self.assertEqual(self.path.getUserID(), fake.st_uid)
  1309. self.assertEqual(self.path.getGroupID(), fake.st_gid)
  1310. if platform.isWindows():
  1311. test_statinfoBitsAreNumbers.skip = True
  1312. test_statinfoNumbersAreValid.skip = True
  1313. test_getPermissions_POSIX.skip = True
  1314. else:
  1315. test_statinfoBitsNotImplementedInWindows.skip = "Test will run only on Windows."
  1316. test_getPermissions_Windows.skip = "Test will run only on Windows."
  1317. class SetContentTests(BytesTestCase):
  1318. """
  1319. Tests for L{FilePath.setContent}.
  1320. """
  1321. def test_write(self):
  1322. """
  1323. Contents of the file referred to by a L{FilePath} can be written using
  1324. L{FilePath.setContent}.
  1325. """
  1326. pathString = self.mktemp()
  1327. path = filepath.FilePath(pathString)
  1328. path.setContent(b"hello, world")
  1329. with open(pathString, "rb") as fObj:
  1330. contents = fObj.read()
  1331. self.assertEqual(b"hello, world", contents)
  1332. def test_fileClosing(self):
  1333. """
  1334. If writing to the underlying file raises an exception,
  1335. L{FilePath.setContent} raises that exception after closing the file.
  1336. """
  1337. fp = ExplodingFilePath(b"")
  1338. self.assertRaises(IOError, fp.setContent, b"blah")
  1339. self.assertTrue(fp.fp.closed)
  1340. def test_nameCollision(self):
  1341. """
  1342. L{FilePath.setContent} will use a different temporary filename on each
  1343. invocation, so that multiple processes, threads, or reentrant
  1344. invocations will not collide with each other.
  1345. """
  1346. fp = TrackingFilePath(self.mktemp())
  1347. fp.setContent(b"alpha")
  1348. fp.setContent(b"beta")
  1349. # Sanity check: setContent should only open one derivative path each
  1350. # time to store the temporary file.
  1351. openedSiblings = fp.openedPaths()
  1352. self.assertEqual(len(openedSiblings), 2)
  1353. self.assertNotEqual(openedSiblings[0], openedSiblings[1])
  1354. def _assertOneOpened(self, fp, extension):
  1355. """
  1356. Assert that the L{TrackingFilePath} C{fp} was used to open one sibling
  1357. with the given extension.
  1358. @param fp: A L{TrackingFilePath} which should have been used to open
  1359. file at a sibling path.
  1360. @type fp: L{TrackingFilePath}
  1361. @param extension: The extension the sibling path is expected to have
  1362. had.
  1363. @type extension: L{bytes}
  1364. @raise: C{self.failureException} is raised if the extension of the
  1365. opened file is incorrect or if not exactly one file was opened
  1366. using C{fp}.
  1367. """
  1368. opened = fp.openedPaths()
  1369. self.assertEqual(len(opened), 1, "expected exactly one opened file")
  1370. self.assertTrue(
  1371. opened[0].basename().endswith(extension),
  1372. "%s does not end with %r extension" % (
  1373. opened[0].basename(), extension))
  1374. def test_defaultExtension(self):
  1375. """
  1376. L{FilePath.setContent} creates temporary files with the extension
  1377. I{.new} if no alternate extension value is given.
  1378. """
  1379. fp = TrackingFilePath(self.mktemp())
  1380. fp.setContent(b"hello")
  1381. self._assertOneOpened(fp, b".new")
  1382. def test_customExtension(self):
  1383. """
  1384. L{FilePath.setContent} creates temporary files with a user-supplied
  1385. extension so that if it is somehow interrupted while writing them the
  1386. file that it leaves behind will be identifiable.
  1387. """
  1388. fp = TrackingFilePath(self.mktemp())
  1389. fp.setContent(b"goodbye", b"-something-else")
  1390. self._assertOneOpened(fp, b"-something-else")
  1391. class UnicodeFilePathTests(TestCase):
  1392. """
  1393. L{FilePath} instances should have the same internal representation as they
  1394. were instantiated with.
  1395. """
  1396. def test_UnicodeInstantiation(self):
  1397. """
  1398. L{FilePath} instantiated with a text path will return a text-mode
  1399. FilePath.
  1400. """
  1401. fp = filepath.FilePath(u'./mon\u20acy')
  1402. self.assertEqual(type(fp.path), unicode)
  1403. def test_UnicodeInstantiationBytesChild(self):
  1404. """
  1405. Calling L{FilePath.child} on a text-mode L{FilePath} with a L{bytes}
  1406. subpath will return a bytes-mode FilePath.
  1407. """
  1408. fp = filepath.FilePath(u'./parent-mon\u20acy')
  1409. child = fp.child(u'child-mon\u20acy'.encode('utf-8'))
  1410. self.assertEqual(type(child.path), bytes)
  1411. def test_UnicodeInstantiationUnicodeChild(self):
  1412. """
  1413. Calling L{FilePath.child} on a text-mode L{FilePath} with a text
  1414. subpath will return a text-mode FilePath.
  1415. """
  1416. fp = filepath.FilePath(u'./parent-mon\u20acy')
  1417. child = fp.child(u'mon\u20acy')
  1418. self.assertEqual(type(child.path), unicode)
  1419. def test_UnicodeInstantiationUnicodePreauthChild(self):
  1420. """
  1421. Calling L{FilePath.preauthChild} on a text-mode L{FilePath} with a text
  1422. subpath will return a text-mode FilePath.
  1423. """
  1424. fp = filepath.FilePath(u'./parent-mon\u20acy')
  1425. child = fp.preauthChild(u'mon\u20acy')
  1426. self.assertEqual(type(child.path), unicode)
  1427. def test_UnicodeInstantiationBytesPreauthChild(self):
  1428. """
  1429. Calling L{FilePath.preauthChild} on a text-mode L{FilePath} with a bytes
  1430. subpath will return a bytes-mode FilePath.
  1431. """
  1432. fp = filepath.FilePath(u'./parent-mon\u20acy')
  1433. child = fp.preauthChild(u'child-mon\u20acy'.encode('utf-8'))
  1434. self.assertEqual(type(child.path), bytes)
  1435. def test_BytesInstantiation(self):
  1436. """
  1437. L{FilePath} instantiated with a L{bytes} path will return a bytes-mode
  1438. FilePath.
  1439. """
  1440. fp = filepath.FilePath(b"./")
  1441. self.assertEqual(type(fp.path), bytes)
  1442. def test_BytesInstantiationBytesChild(self):
  1443. """
  1444. Calling L{FilePath.child} on a bytes-mode L{FilePath} with a bytes
  1445. subpath will return a bytes-mode FilePath.
  1446. """
  1447. fp = filepath.FilePath(b"./")
  1448. child = fp.child(u'child-mon\u20acy'.encode('utf-8'))
  1449. self.assertEqual(type(child.path), bytes)
  1450. def test_BytesInstantiationUnicodeChild(self):
  1451. """
  1452. Calling L{FilePath.child} on a bytes-mode L{FilePath} with a text
  1453. subpath will return a text-mode FilePath.
  1454. """
  1455. fp = filepath.FilePath(u'parent-mon\u20acy'.encode('utf-8'))
  1456. child = fp.child(u"mon\u20acy")
  1457. self.assertEqual(type(child.path), unicode)
  1458. def test_BytesInstantiationBytesPreauthChild(self):
  1459. """
  1460. Calling L{FilePath.preauthChild} on a bytes-mode L{FilePath} with a
  1461. bytes subpath will return a bytes-mode FilePath.
  1462. """
  1463. fp = filepath.FilePath(u'./parent-mon\u20acy'.encode('utf-8'))
  1464. child = fp.preauthChild(u'child-mon\u20acy'.encode('utf-8'))
  1465. self.assertEqual(type(child.path), bytes)
  1466. def test_BytesInstantiationUnicodePreauthChild(self):
  1467. """
  1468. Calling L{FilePath.preauthChild} on a bytes-mode L{FilePath} with a text
  1469. subpath will return a text-mode FilePath.
  1470. """
  1471. fp = filepath.FilePath(u'./parent-mon\u20acy'.encode('utf-8'))
  1472. child = fp.preauthChild(u"mon\u20acy")
  1473. self.assertEqual(type(child.path), unicode)
  1474. def test_unicoderepr(self):
  1475. """
  1476. The repr of a L{unicode} L{FilePath} shouldn't burst into flames.
  1477. """
  1478. fp = filepath.FilePath(u"/mon\u20acy")
  1479. reprOutput = repr(fp)
  1480. if _PY3:
  1481. self.assertEqual("FilePath('/mon\u20acy')", reprOutput)
  1482. else:
  1483. self.assertEqual("FilePath(u'/mon\\u20acy')", reprOutput)
  1484. def test_bytesrepr(self):
  1485. """
  1486. The repr of a L{bytes} L{FilePath} shouldn't burst into flames.
  1487. """
  1488. fp = filepath.FilePath(u'/parent-mon\u20acy'.encode('utf-8'))
  1489. reprOutput = repr(fp)
  1490. if _PY3:
  1491. self.assertEqual(
  1492. "FilePath(b'/parent-mon\\xe2\\x82\\xacy')", reprOutput)
  1493. else:
  1494. self.assertEqual(
  1495. "FilePath('/parent-mon\\xe2\\x82\\xacy')", reprOutput)
  1496. def test_unicodereprWindows(self):
  1497. """
  1498. The repr of a L{unicode} L{FilePath} shouldn't burst into flames.
  1499. """
  1500. fp = filepath.FilePath(u"C:\\")
  1501. reprOutput = repr(fp)
  1502. if _PY3:
  1503. self.assertEqual("FilePath('C:\\\\')", reprOutput)
  1504. else:
  1505. self.assertEqual("FilePath(u'C:\\\\')", reprOutput)
  1506. def test_bytesreprWindows(self):
  1507. """
  1508. The repr of a L{bytes} L{FilePath} shouldn't burst into flames.
  1509. """
  1510. fp = filepath.FilePath(b"C:\\")
  1511. reprOutput = repr(fp)
  1512. if _PY3:
  1513. self.assertEqual("FilePath(b'C:\\\\')", reprOutput)
  1514. else:
  1515. self.assertEqual("FilePath('C:\\\\')", reprOutput)
  1516. if platform.isWindows():
  1517. test_unicoderepr.skip = "Test will not work on Windows"
  1518. test_bytesrepr.skip = "Test will not work on Windows"
  1519. else:
  1520. test_unicodereprWindows.skip = "Test only works on Windows"
  1521. test_bytesreprWindows.skip = "Test only works on Windows"
  1522. def test_mixedTypeGlobChildren(self):
  1523. """
  1524. C{globChildren} will return the same type as the pattern argument.
  1525. """
  1526. fp = filepath.FilePath(u"/")
  1527. children = fp.globChildren(b"*")
  1528. self.assertIsInstance(children[0].path, bytes)
  1529. def test_unicodeGlobChildren(self):
  1530. """
  1531. C{globChildren} works with L{unicode}.
  1532. """
  1533. fp = filepath.FilePath(u"/")
  1534. children = fp.globChildren(u"*")
  1535. self.assertIsInstance(children[0].path, unicode)
  1536. def test_unicodeBasename(self):
  1537. """
  1538. Calling C{basename} on an text- L{FilePath} returns L{unicode}.
  1539. """
  1540. fp = filepath.FilePath(u"./")
  1541. self.assertIsInstance(fp.basename(), unicode)
  1542. def test_unicodeDirname(self):
  1543. """
  1544. Calling C{dirname} on a text-mode L{FilePath} returns L{unicode}.
  1545. """
  1546. fp = filepath.FilePath(u"./")
  1547. self.assertIsInstance(fp.dirname(), unicode)
  1548. def test_unicodeParent(self):
  1549. """
  1550. Calling C{parent} on a text-mode L{FilePath} will return a text-mode
  1551. L{FilePath}.
  1552. """
  1553. fp = filepath.FilePath(u"./")
  1554. parent = fp.parent()
  1555. self.assertIsInstance(parent.path, unicode)
  1556. def test_mixedTypeTemporarySibling(self):
  1557. """
  1558. A L{bytes} extension to C{temporarySibling} will mean a L{bytes} mode
  1559. L{FilePath} is returned.
  1560. """
  1561. fp = filepath.FilePath(u"./mon\u20acy")
  1562. tempSibling = fp.temporarySibling(b".txt")
  1563. self.assertIsInstance(tempSibling.path, bytes)
  1564. def test_unicodeTemporarySibling(self):
  1565. """
  1566. A L{unicode} extension to C{temporarySibling} will mean a L{unicode}
  1567. mode L{FilePath} is returned.
  1568. """
  1569. fp = filepath.FilePath(u"/tmp/mon\u20acy")
  1570. tempSibling = fp.temporarySibling(u".txt")
  1571. self.assertIsInstance(tempSibling.path, unicode)
  1572. def test_mixedTypeSiblingExtensionSearch(self):
  1573. """
  1574. C{siblingExtensionSearch} called with L{bytes} on a L{unicode}-mode
  1575. L{FilePath} will return a L{list} of L{bytes}-mode L{FilePath}s.
  1576. """
  1577. fp = filepath.FilePath(u"./mon\u20acy")
  1578. sibling = filepath.FilePath(fp._asTextPath() + u".txt")
  1579. sibling.touch()
  1580. newPath = fp.siblingExtensionSearch(b".txt")
  1581. self.assertIsInstance(newPath, filepath.FilePath)
  1582. self.assertIsInstance(newPath.path, bytes)
  1583. def test_unicodeSiblingExtensionSearch(self):
  1584. """
  1585. C{siblingExtensionSearch} called with L{unicode} on a L{unicode}-mode
  1586. L{FilePath} will return a L{list} of L{unicode}-mode L{FilePath}s.
  1587. """
  1588. fp = filepath.FilePath(u"./mon\u20acy")
  1589. sibling = filepath.FilePath(fp._asTextPath() + u".txt")
  1590. sibling.touch()
  1591. newPath = fp.siblingExtensionSearch(u".txt")
  1592. self.assertIsInstance(newPath, filepath.FilePath)
  1593. self.assertIsInstance(newPath.path, unicode)
  1594. def test_mixedTypeSiblingExtension(self):
  1595. """
  1596. C{siblingExtension} called with L{bytes} on a L{unicode}-mode
  1597. L{FilePath} will return a L{bytes}-mode L{FilePath}.
  1598. """
  1599. fp = filepath.FilePath(u"./mon\u20acy")
  1600. sibling = filepath.FilePath(fp._asTextPath() + u".txt")
  1601. sibling.touch()
  1602. newPath = fp.siblingExtension(b".txt")
  1603. self.assertIsInstance(newPath, filepath.FilePath)
  1604. self.assertIsInstance(newPath.path, bytes)
  1605. def test_unicodeSiblingExtension(self):
  1606. """
  1607. C{siblingExtension} called with L{unicode} on a L{unicode}-mode
  1608. L{FilePath} will return a L{unicode}-mode L{FilePath}.
  1609. """
  1610. fp = filepath.FilePath(u"./mon\u20acy")
  1611. sibling = filepath.FilePath(fp._asTextPath() + u".txt")
  1612. sibling.touch()
  1613. newPath = fp.siblingExtension(u".txt")
  1614. self.assertIsInstance(newPath, filepath.FilePath)
  1615. self.assertIsInstance(newPath.path, unicode)
  1616. def test_mixedTypeChildSearchPreauth(self):
  1617. """
  1618. C{childSearchPreauth} called with L{bytes} on a L{unicode}-mode
  1619. L{FilePath} will return a L{bytes}-mode L{FilePath}.
  1620. """
  1621. fp = filepath.FilePath(u"./mon\u20acy")
  1622. fp.createDirectory()
  1623. self.addCleanup(lambda: fp.remove())
  1624. child = fp.child("text.txt")
  1625. child.touch()
  1626. newPath = fp.childSearchPreauth(b"text.txt")
  1627. self.assertIsInstance(newPath, filepath.FilePath)
  1628. self.assertIsInstance(newPath.path, bytes)
  1629. def test_unicodeChildSearchPreauth(self):
  1630. """
  1631. C{childSearchPreauth} called with L{unicode} on a L{unicode}-mode
  1632. L{FilePath} will return a L{unicode}-mode L{FilePath}.
  1633. """
  1634. fp = filepath.FilePath(u"./mon\u20acy")
  1635. fp.createDirectory()
  1636. self.addCleanup(lambda: fp.remove())
  1637. child = fp.child("text.txt")
  1638. child.touch()
  1639. newPath = fp.childSearchPreauth(u"text.txt")
  1640. self.assertIsInstance(newPath, filepath.FilePath)
  1641. self.assertIsInstance(newPath.path, unicode)
  1642. def test_asBytesModeFromUnicode(self):
  1643. """
  1644. C{asBytesMode} on a L{unicode}-mode L{FilePath} returns a new
  1645. L{bytes}-mode L{FilePath}.
  1646. """
  1647. fp = filepath.FilePath(u"./tmp")
  1648. newfp = fp.asBytesMode()
  1649. self.assertIsNot(fp, newfp)
  1650. self.assertIsInstance(newfp.path, bytes)
  1651. def test_asTextModeFromBytes(self):
  1652. """
  1653. C{asBytesMode} on a L{unicode}-mode L{FilePath} returns a new
  1654. L{bytes}-mode L{FilePath}.
  1655. """
  1656. fp = filepath.FilePath(b"./tmp")
  1657. newfp = fp.asTextMode()
  1658. self.assertIsNot(fp, newfp)
  1659. self.assertIsInstance(newfp.path, unicode)
  1660. def test_asBytesModeFromBytes(self):
  1661. """
  1662. C{asBytesMode} on a L{bytes}-mode L{FilePath} returns the same
  1663. L{bytes}-mode L{FilePath}.
  1664. """
  1665. fp = filepath.FilePath(b"./tmp")
  1666. newfp = fp.asBytesMode()
  1667. self.assertIs(fp, newfp)
  1668. self.assertIsInstance(newfp.path, bytes)
  1669. def test_asTextModeFromUnicode(self):
  1670. """
  1671. C{asTextMode} on a L{unicode}-mode L{FilePath} returns the same
  1672. L{unicode}-mode L{FilePath}.
  1673. """
  1674. fp = filepath.FilePath(u"./tmp")
  1675. newfp = fp.asTextMode()
  1676. self.assertIs(fp, newfp)
  1677. self.assertIsInstance(newfp.path, unicode)
  1678. def test_asBytesModeFromUnicodeWithEncoding(self):
  1679. """
  1680. C{asBytesMode} with an C{encoding} argument uses that encoding when
  1681. coercing the L{unicode}-mode L{FilePath} to a L{bytes}-mode L{FilePath}.
  1682. """
  1683. fp = filepath.FilePath(u"\u2603")
  1684. newfp = fp.asBytesMode(encoding="utf-8")
  1685. self.assertIn(b"\xe2\x98\x83", newfp.path)
  1686. def test_asTextModeFromBytesWithEncoding(self):
  1687. """
  1688. C{asTextMode} with an C{encoding} argument uses that encoding when
  1689. coercing the L{bytes}-mode L{FilePath} to a L{unicode}-mode L{FilePath}.
  1690. """
  1691. fp = filepath.FilePath(b'\xe2\x98\x83')
  1692. newfp = fp.asTextMode(encoding="utf-8")
  1693. self.assertIn(u"\u2603", newfp.path)
  1694. def test_asBytesModeFromUnicodeWithUnusableEncoding(self):
  1695. """
  1696. C{asBytesMode} with an C{encoding} argument that can't be used to encode
  1697. the unicode path raises a L{UnicodeError}.
  1698. """
  1699. fp = filepath.FilePath(u"\u2603")
  1700. with self.assertRaises(UnicodeError):
  1701. fp.asBytesMode(encoding="ascii")
  1702. def test_asTextModeFromBytesWithUnusableEncoding(self):
  1703. """
  1704. C{asTextMode} with an C{encoding} argument that can't be used to encode
  1705. the unicode path raises a L{UnicodeError}.
  1706. """
  1707. fp = filepath.FilePath(b"\u2603")
  1708. with self.assertRaises(UnicodeError):
  1709. fp.asTextMode(encoding="utf-32")