test_path.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. # encoding: utf-8
  2. """Tests for genutils.path"""
  3. # Copyright (c) IPython Development Team.
  4. # Distributed under the terms of the Modified BSD License.
  5. import os
  6. import sys
  7. import tempfile
  8. import nose.tools as nt
  9. from ..testing.decorators import skip_if_not_win32, skip_win32
  10. from .. import path
  11. from .. import py3compat
  12. from ..tempdir import TemporaryDirectory
  13. def test_filefind():
  14. f = tempfile.NamedTemporaryFile()
  15. t = path.filefind(f.name, '.')
  16. def test_ensure_dir_exists():
  17. with TemporaryDirectory() as td:
  18. d = os.path.join(td, u'∂ir')
  19. path.ensure_dir_exists(d) # create it
  20. assert os.path.isdir(d)
  21. path.ensure_dir_exists(d) # no-op
  22. f = os.path.join(td, u'ƒile')
  23. open(f, 'w').close() # touch
  24. with nt.assert_raises(IOError):
  25. path.ensure_dir_exists(f)
  26. class TestLinkOrCopy(object):
  27. def setUp(self):
  28. self.tempdir = TemporaryDirectory()
  29. self.src = self.dst("src")
  30. with open(self.src, "w") as f:
  31. f.write("Hello, world!")
  32. def tearDown(self):
  33. self.tempdir.cleanup()
  34. def dst(self, *args):
  35. return os.path.join(self.tempdir.name, *args)
  36. def assert_inode_not_equal(self, a, b):
  37. nt.assert_not_equals(os.stat(a).st_ino, os.stat(b).st_ino,
  38. "%r and %r do reference the same indoes" %(a, b))
  39. def assert_inode_equal(self, a, b):
  40. nt.assert_equals(os.stat(a).st_ino, os.stat(b).st_ino,
  41. "%r and %r do not reference the same indoes" %(a, b))
  42. def assert_content_equal(self, a, b):
  43. with open(a) as a_f:
  44. with open(b) as b_f:
  45. nt.assert_equals(a_f.read(), b_f.read())
  46. @skip_win32
  47. def test_link_successful(self):
  48. dst = self.dst("target")
  49. path.link_or_copy(self.src, dst)
  50. self.assert_inode_equal(self.src, dst)
  51. @skip_win32
  52. def test_link_into_dir(self):
  53. dst = self.dst("some_dir")
  54. os.mkdir(dst)
  55. path.link_or_copy(self.src, dst)
  56. expected_dst = self.dst("some_dir", os.path.basename(self.src))
  57. self.assert_inode_equal(self.src, expected_dst)
  58. @skip_win32
  59. def test_target_exists(self):
  60. dst = self.dst("target")
  61. open(dst, "w").close()
  62. path.link_or_copy(self.src, dst)
  63. self.assert_inode_equal(self.src, dst)
  64. @skip_win32
  65. def test_no_link(self):
  66. real_link = os.link
  67. try:
  68. del os.link
  69. dst = self.dst("target")
  70. path.link_or_copy(self.src, dst)
  71. self.assert_content_equal(self.src, dst)
  72. self.assert_inode_not_equal(self.src, dst)
  73. finally:
  74. os.link = real_link
  75. @skip_if_not_win32
  76. def test_windows(self):
  77. dst = self.dst("target")
  78. path.link_or_copy(self.src, dst)
  79. self.assert_content_equal(self.src, dst)
  80. def test_link_twice(self):
  81. # Linking the same file twice shouldn't leave duplicates around.
  82. # See https://github.com/ipython/ipython/issues/6450
  83. dst = self.dst('target')
  84. path.link_or_copy(self.src, dst)
  85. path.link_or_copy(self.src, dst)
  86. self.assert_inode_equal(self.src, dst)
  87. nt.assert_equal(sorted(os.listdir(self.tempdir.name)), ['src', 'target'])