/Lib/test/test_macostools.py

http://unladen-swallow.googlecode.com/ · Python · 101 lines · 80 code · 14 blank · 7 comment · 11 complexity · ccba798832b9f348dd76e5c74ea0df6d MD5 · raw file

  1. # Copyright (C) 2003 Python Software Foundation
  2. import unittest
  3. import macostools
  4. import Carbon.File
  5. import MacOS
  6. import os
  7. import sys
  8. from test import test_support
  9. TESTFN2 = test_support.TESTFN + '2'
  10. class TestMacostools(unittest.TestCase):
  11. def setUp(self):
  12. fp = open(test_support.TESTFN, 'w')
  13. fp.write('hello world\n')
  14. fp.close()
  15. rfp = MacOS.openrf(test_support.TESTFN, '*wb')
  16. rfp.write('goodbye world\n')
  17. rfp.close()
  18. def tearDown(self):
  19. try:
  20. os.unlink(test_support.TESTFN)
  21. except:
  22. pass
  23. try:
  24. os.unlink(TESTFN2)
  25. except:
  26. pass
  27. def compareData(self):
  28. fp = open(test_support.TESTFN, 'r')
  29. data1 = fp.read()
  30. fp.close()
  31. fp = open(TESTFN2, 'r')
  32. data2 = fp.read()
  33. fp.close()
  34. if data1 != data2:
  35. return 'Data forks differ'
  36. rfp = MacOS.openrf(test_support.TESTFN, '*rb')
  37. data1 = rfp.read(1000)
  38. rfp.close()
  39. rfp = MacOS.openrf(TESTFN2, '*rb')
  40. data2 = rfp.read(1000)
  41. rfp.close()
  42. if data1 != data2:
  43. return 'Resource forks differ'
  44. return ''
  45. def test_touched(self):
  46. # This really only tests that nothing unforeseen happens.
  47. import warnings
  48. with warnings.catch_warnings():
  49. warnings.filterwarnings('ignore', 'macostools.touched*',
  50. DeprecationWarning)
  51. macostools.touched(test_support.TESTFN)
  52. def test_copy(self):
  53. try:
  54. os.unlink(TESTFN2)
  55. except:
  56. pass
  57. macostools.copy(test_support.TESTFN, TESTFN2)
  58. self.assertEqual(self.compareData(), '')
  59. def test_mkalias(self):
  60. try:
  61. os.unlink(TESTFN2)
  62. except:
  63. pass
  64. macostools.mkalias(test_support.TESTFN, TESTFN2)
  65. fss, _, _ = Carbon.File.ResolveAliasFile(TESTFN2, 0)
  66. self.assertEqual(fss.as_pathname(), os.path.realpath(test_support.TESTFN))
  67. def test_mkalias_relative(self):
  68. try:
  69. os.unlink(TESTFN2)
  70. except:
  71. pass
  72. # If the directory doesn't exist, then chances are this is a new
  73. # install of Python so don't create it since the user might end up
  74. # running ``sudo make install`` and creating the directory here won't
  75. # leave it with the proper permissions.
  76. if not os.path.exists(sys.prefix):
  77. return
  78. macostools.mkalias(test_support.TESTFN, TESTFN2, sys.prefix)
  79. fss, _, _ = Carbon.File.ResolveAliasFile(TESTFN2, 0)
  80. self.assertEqual(fss.as_pathname(), os.path.realpath(test_support.TESTFN))
  81. def test_main():
  82. # Skip on wide unicode
  83. if len(u'\0'.encode('unicode-internal')) == 4:
  84. raise test_support.TestSkipped("test_macostools is broken in USC4")
  85. test_support.run_unittest(TestMacostools)
  86. if __name__ == '__main__':
  87. test_main()