blob: 642a584c1dc49436e2bde490607d492a1dd66224 [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
15class 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):
26 try:
27 os.unlink(test_support.TESTFN)
28 except:
29 pass
30 try:
31 os.unlink(TESTFN2)
32 except:
33 pass
Tim Petersf2715e02003-02-19 02:35:07 +000034
Jack Jansen10882f62003-01-28 21:39:28 +000035 def compareData(self):
36 fp = open(test_support.TESTFN, 'r')
37 data1 = fp.read()
38 fp.close()
39 fp = open(TESTFN2, 'r')
40 data2 = fp.read()
41 fp.close()
42 if data1 != data2:
43 return 'Data forks differ'
44 rfp = MacOS.openrf(test_support.TESTFN, '*rb')
45 data1 = rfp.read(1000)
46 rfp.close()
47 rfp = MacOS.openrf(TESTFN2, '*rb')
48 data2 = rfp.read(1000)
49 rfp.close()
50 if data1 != data2:
51 return 'Resource forks differ'
52 return ''
Tim Petersf2715e02003-02-19 02:35:07 +000053
Jack Jansen10882f62003-01-28 21:39:28 +000054 def test_touched(self):
55 # This really only tests that nothing unforeseen happens.
Brett Cannon5e263512007-05-20 23:17:38 +000056 import warnings
Brett Cannon672237d2008-09-09 00:49:16 +000057 with warnings.catch_warnings():
Brett Cannon5e263512007-05-20 23:17:38 +000058 warnings.filterwarnings('ignore', 'macostools.touched*',
59 DeprecationWarning)
60 macostools.touched(test_support.TESTFN)
Tim Petersf2715e02003-02-19 02:35:07 +000061
Ronald Oussorenab3f5cb2010-02-07 11:38:28 +000062 if sys.maxint < 2**32:
63 def test_copy(self):
64 try:
65 os.unlink(TESTFN2)
66 except:
67 pass
68 macostools.copy(test_support.TESTFN, TESTFN2)
69 self.assertEqual(self.compareData(), '')
Tim Petersf2715e02003-02-19 02:35:07 +000070
Ronald Oussorenab3f5cb2010-02-07 11:38:28 +000071 if sys.maxint < 2**32:
72 def test_mkalias(self):
73 try:
74 os.unlink(TESTFN2)
75 except:
76 pass
77 macostools.mkalias(test_support.TESTFN, TESTFN2)
78 fss, _, _ = Carbon.File.ResolveAliasFile(TESTFN2, 0)
79 self.assertEqual(fss.as_pathname(), os.path.realpath(test_support.TESTFN))
Tim Petersf2715e02003-02-19 02:35:07 +000080
Ronald Oussorenab3f5cb2010-02-07 11:38:28 +000081 def test_mkalias_relative(self):
82 try:
83 os.unlink(TESTFN2)
84 except:
85 pass
86 # If the directory doesn't exist, then chances are this is a new
87 # install of Python so don't create it since the user might end up
88 # running ``sudo make install`` and creating the directory here won't
89 # leave it with the proper permissions.
90 if not os.path.exists(sys.prefix):
91 return
92 macostools.mkalias(test_support.TESTFN, TESTFN2, sys.prefix)
93 fss, _, _ = Carbon.File.ResolveAliasFile(TESTFN2, 0)
94 self.assertEqual(fss.as_pathname(), os.path.realpath(test_support.TESTFN))
Tim Petersf2715e02003-02-19 02:35:07 +000095
96
Jack Jansen10882f62003-01-28 21:39:28 +000097def test_main():
Benjamin Peterson6f5a2b52008-06-19 21:39:06 +000098 # Skip on wide unicode
99 if len(u'\0'.encode('unicode-internal')) == 4:
Benjamin Peterson888a39b2009-03-26 20:48:25 +0000100 raise unittest.SkipTest("test_macostools is broken in USC4")
Jack Jansen10882f62003-01-28 21:39:28 +0000101 test_support.run_unittest(TestMacostools)
102
103
104if __name__ == '__main__':
105 test_main()