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