blob: ed80c1d4d9719e6c6c7129808769ec4c19fc65e6 [file] [log] [blame]
Jack Jansen10882f62003-01-28 21:39:28 +00001# Copyright (C) 2003 Python Software Foundation
2
3import unittest
Jack Jansen10882f62003-01-28 21:39:28 +00004import os
Jack Jansenaddc5852003-01-28 23:54:05 +00005import sys
Jack Jansen10882f62003-01-28 21:39:28 +00006from test import test_support
7
R. David Murray59beec32009-03-30 19:04:00 +00008MacOS = test_support.import_module('MacOS')
9#The following modules should exist if MacOS exists.
10import Carbon.File
11import macostools
12
Jack Jansen10882f62003-01-28 21:39:28 +000013TESTFN2 = test_support.TESTFN + '2'
14
Zachary Ware1f702212013-12-10 14:09:20 -060015requires_32bit = unittest.skipUnless(sys.maxint < 2**32, '32-bit only test')
16
Jack Jansen10882f62003-01-28 21:39:28 +000017class 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 Xicluna6257a7b2010-03-31 22:01:03 +000028 test_support.unlink(test_support.TESTFN)
29 test_support.unlink(TESTFN2)
Tim Petersf2715e02003-02-19 02:35:07 +000030
Jack Jansen10882f62003-01-28 21:39:28 +000031 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 Petersf2715e02003-02-19 02:35:07 +000049
Jack Jansen10882f62003-01-28 21:39:28 +000050 def test_touched(self):
51 # This really only tests that nothing unforeseen happens.
Florent Xicluna6257a7b2010-03-31 22:01:03 +000052 with test_support.check_warnings(('macostools.touched*',
53 DeprecationWarning), quiet=True):
Brett Cannon5e263512007-05-20 23:17:38 +000054 macostools.touched(test_support.TESTFN)
Tim Petersf2715e02003-02-19 02:35:07 +000055
Zachary Ware1f702212013-12-10 14:09:20 -060056 @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 Petersf2715e02003-02-19 02:35:07 +000061
Zachary Ware1f702212013-12-10 14:09:20 -060062 @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 Petersf2715e02003-02-19 02:35:07 +000068
Zachary Ware1f702212013-12-10 14:09:20 -060069 @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 Petersf2715e02003-02-19 02:35:07 +000082
83
Jack Jansen10882f62003-01-28 21:39:28 +000084def test_main():
Benjamin Peterson6f5a2b52008-06-19 21:39:06 +000085 # Skip on wide unicode
86 if len(u'\0'.encode('unicode-internal')) == 4:
Benjamin Peterson888a39b2009-03-26 20:48:25 +000087 raise unittest.SkipTest("test_macostools is broken in USC4")
Jack Jansen10882f62003-01-28 21:39:28 +000088 test_support.run_unittest(TestMacostools)
89
90
91if __name__ == '__main__':
92 test_main()