test_macostools.py 2.9 KB

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