• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright (C) 2003 Python Software Foundation
2
3import unittest
4import os
5import sys
6from test import test_support
7
8MacOS = test_support.import_module('MacOS')
9#The following modules should exist if MacOS exists.
10import Carbon.File
11import macostools
12
13TESTFN2 = test_support.TESTFN + '2'
14
15requires_32bit = unittest.skipUnless(sys.maxint < 2**32, '32-bit only test')
16
17class 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):
28        test_support.unlink(test_support.TESTFN)
29        test_support.unlink(TESTFN2)
30
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 ''
49
50    def test_touched(self):
51        # This really only tests that nothing unforeseen happens.
52        with test_support.check_warnings(('macostools.touched*',
53                                          DeprecationWarning), quiet=True):
54            macostools.touched(test_support.TESTFN)
55
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(), '')
61
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))
68
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))
82
83
84def test_main():
85    # Skip on wide unicode
86    if len(u'\0'.encode('unicode-internal')) == 4:
87        raise unittest.SkipTest("test_macostools is broken in USC4")
88    test_support.run_unittest(TestMacostools)
89
90
91if __name__ == '__main__':
92    test_main()
93