| Jack Jansen | 10882f6 | 2003-01-28 21:39:28 +0000 | [diff] [blame] | 1 | # Copyright (C) 2003 Python Software Foundation | 
 | 2 |  | 
 | 3 | import unittest | 
| Jack Jansen | 10882f6 | 2003-01-28 21:39:28 +0000 | [diff] [blame] | 4 | import os | 
| Jack Jansen | addc585 | 2003-01-28 23:54:05 +0000 | [diff] [blame] | 5 | import sys | 
| Jack Jansen | 10882f6 | 2003-01-28 21:39:28 +0000 | [diff] [blame] | 6 | from test import test_support | 
 | 7 |  | 
| R. David Murray | 59beec3 | 2009-03-30 19:04:00 +0000 | [diff] [blame] | 8 | MacOS = test_support.import_module('MacOS') | 
 | 9 | #The following modules should exist if MacOS exists. | 
 | 10 | import Carbon.File | 
 | 11 | import macostools | 
 | 12 |  | 
| Jack Jansen | 10882f6 | 2003-01-28 21:39:28 +0000 | [diff] [blame] | 13 | TESTFN2 = test_support.TESTFN + '2' | 
 | 14 |  | 
 | 15 | class TestMacostools(unittest.TestCase): | 
 | 16 |  | 
 | 17 |     def setUp(self): | 
 | 18 |         fp = open(test_support.TESTFN, 'w') | 
 | 19 |         fp.write('hello world\n') | 
 | 20 |         fp.close() | 
 | 21 |         rfp = MacOS.openrf(test_support.TESTFN, '*wb') | 
 | 22 |         rfp.write('goodbye world\n') | 
 | 23 |         rfp.close() | 
 | 24 |  | 
 | 25 |     def tearDown(self): | 
| Florent Xicluna | 6257a7b | 2010-03-31 22:01:03 +0000 | [diff] [blame] | 26 |         test_support.unlink(test_support.TESTFN) | 
 | 27 |         test_support.unlink(TESTFN2) | 
| Tim Peters | f2715e0 | 2003-02-19 02:35:07 +0000 | [diff] [blame] | 28 |  | 
| Jack Jansen | 10882f6 | 2003-01-28 21:39:28 +0000 | [diff] [blame] | 29 |     def compareData(self): | 
 | 30 |         fp = open(test_support.TESTFN, 'r') | 
 | 31 |         data1 = fp.read() | 
 | 32 |         fp.close() | 
 | 33 |         fp = open(TESTFN2, 'r') | 
 | 34 |         data2 = fp.read() | 
 | 35 |         fp.close() | 
 | 36 |         if data1 != data2: | 
 | 37 |             return 'Data forks differ' | 
 | 38 |         rfp = MacOS.openrf(test_support.TESTFN, '*rb') | 
 | 39 |         data1 = rfp.read(1000) | 
 | 40 |         rfp.close() | 
 | 41 |         rfp = MacOS.openrf(TESTFN2, '*rb') | 
 | 42 |         data2 = rfp.read(1000) | 
 | 43 |         rfp.close() | 
 | 44 |         if data1 != data2: | 
 | 45 |             return 'Resource forks differ' | 
 | 46 |         return '' | 
| Tim Peters | f2715e0 | 2003-02-19 02:35:07 +0000 | [diff] [blame] | 47 |  | 
| Jack Jansen | 10882f6 | 2003-01-28 21:39:28 +0000 | [diff] [blame] | 48 |     def test_touched(self): | 
 | 49 |         # This really only tests that nothing unforeseen happens. | 
| Florent Xicluna | 6257a7b | 2010-03-31 22:01:03 +0000 | [diff] [blame] | 50 |         with test_support.check_warnings(('macostools.touched*', | 
 | 51 |                                           DeprecationWarning), quiet=True): | 
| Brett Cannon | 5e26351 | 2007-05-20 23:17:38 +0000 | [diff] [blame] | 52 |             macostools.touched(test_support.TESTFN) | 
| Tim Peters | f2715e0 | 2003-02-19 02:35:07 +0000 | [diff] [blame] | 53 |  | 
| Ronald Oussoren | ab3f5cb | 2010-02-07 11:38:28 +0000 | [diff] [blame] | 54 |     if sys.maxint < 2**32: | 
 | 55 |         def test_copy(self): | 
| Florent Xicluna | 6257a7b | 2010-03-31 22:01:03 +0000 | [diff] [blame] | 56 |             test_support.unlink(TESTFN2) | 
| Ronald Oussoren | ab3f5cb | 2010-02-07 11:38:28 +0000 | [diff] [blame] | 57 |             macostools.copy(test_support.TESTFN, TESTFN2) | 
 | 58 |             self.assertEqual(self.compareData(), '') | 
| Tim Peters | f2715e0 | 2003-02-19 02:35:07 +0000 | [diff] [blame] | 59 |  | 
| Ronald Oussoren | ab3f5cb | 2010-02-07 11:38:28 +0000 | [diff] [blame] | 60 |     if sys.maxint < 2**32: | 
 | 61 |         def test_mkalias(self): | 
| Florent Xicluna | 6257a7b | 2010-03-31 22:01:03 +0000 | [diff] [blame] | 62 |             test_support.unlink(TESTFN2) | 
| Ronald Oussoren | ab3f5cb | 2010-02-07 11:38:28 +0000 | [diff] [blame] | 63 |             macostools.mkalias(test_support.TESTFN, TESTFN2) | 
 | 64 |             fss, _, _ = Carbon.File.ResolveAliasFile(TESTFN2, 0) | 
 | 65 |             self.assertEqual(fss.as_pathname(), os.path.realpath(test_support.TESTFN)) | 
| Tim Peters | f2715e0 | 2003-02-19 02:35:07 +0000 | [diff] [blame] | 66 |  | 
| Ronald Oussoren | ab3f5cb | 2010-02-07 11:38:28 +0000 | [diff] [blame] | 67 |         def test_mkalias_relative(self): | 
| Florent Xicluna | 6257a7b | 2010-03-31 22:01:03 +0000 | [diff] [blame] | 68 |             test_support.unlink(TESTFN2) | 
| Ronald Oussoren | ab3f5cb | 2010-02-07 11:38:28 +0000 | [diff] [blame] | 69 |             # If the directory doesn't exist, then chances are this is a new | 
 | 70 |             # install of Python so don't create it since the user might end up | 
 | 71 |             # running ``sudo make install`` and creating the directory here won't | 
 | 72 |             # leave it with the proper permissions. | 
 | 73 |             if not os.path.exists(sys.prefix): | 
 | 74 |                 return | 
 | 75 |             macostools.mkalias(test_support.TESTFN, TESTFN2, sys.prefix) | 
 | 76 |             fss, _, _ = Carbon.File.ResolveAliasFile(TESTFN2, 0) | 
 | 77 |             self.assertEqual(fss.as_pathname(), os.path.realpath(test_support.TESTFN)) | 
| Tim Peters | f2715e0 | 2003-02-19 02:35:07 +0000 | [diff] [blame] | 78 |  | 
 | 79 |  | 
| Jack Jansen | 10882f6 | 2003-01-28 21:39:28 +0000 | [diff] [blame] | 80 | def test_main(): | 
| Benjamin Peterson | 6f5a2b5 | 2008-06-19 21:39:06 +0000 | [diff] [blame] | 81 |     # Skip on wide unicode | 
 | 82 |     if len(u'\0'.encode('unicode-internal')) == 4: | 
| Benjamin Peterson | 888a39b | 2009-03-26 20:48:25 +0000 | [diff] [blame] | 83 |         raise unittest.SkipTest("test_macostools is broken in USC4") | 
| Jack Jansen | 10882f6 | 2003-01-28 21:39:28 +0000 | [diff] [blame] | 84 |     test_support.run_unittest(TestMacostools) | 
 | 85 |  | 
 | 86 |  | 
 | 87 | if __name__ == '__main__': | 
 | 88 |     test_main() |