• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1import unittest
2from test import test_support
3import os
4import subprocess
5
6MacOS = test_support.import_module('MacOS')
7
8TESTFN2 = test_support.TESTFN + '2'
9
10class TestMacOS(unittest.TestCase):
11    @unittest.skipUnless(os.path.exists('/Developer/Tools/SetFile'),
12                         '/Developer/Tools/SetFile does not exist')
13    def testGetCreatorAndType(self):
14        try:
15            fp = open(test_support.TESTFN, 'w')
16            fp.write('\n')
17            fp.close()
18
19            subprocess.call(
20                    ['/Developer/Tools/SetFile', '-t', 'ABCD', '-c', 'EFGH',
21                        test_support.TESTFN])
22
23            cr, tp = MacOS.GetCreatorAndType(test_support.TESTFN)
24            self.assertEqual(tp, 'ABCD')
25            self.assertEqual(cr, 'EFGH')
26
27        finally:
28            os.unlink(test_support.TESTFN)
29
30    @unittest.skipUnless(os.path.exists('/Developer/Tools/GetFileInfo'),
31                         '/Developer/Tools/GetFileInfo does not exist')
32    def testSetCreatorAndType(self):
33        try:
34            fp = open(test_support.TESTFN, 'w')
35            fp.write('\n')
36            fp.close()
37
38            MacOS.SetCreatorAndType(test_support.TESTFN,
39                    'ABCD', 'EFGH')
40
41            cr, tp = MacOS.GetCreatorAndType(test_support.TESTFN)
42            self.assertEqual(cr, 'ABCD')
43            self.assertEqual(tp, 'EFGH')
44
45            data = subprocess.Popen(["/Developer/Tools/GetFileInfo", test_support.TESTFN],
46                    stdout=subprocess.PIPE).communicate()[0]
47
48            tp = None
49            cr = None
50            for  ln in data.splitlines():
51                if ln.startswith('type:'):
52                    tp = ln.split()[-1][1:-1]
53                if ln.startswith('creator:'):
54                    cr = ln.split()[-1][1:-1]
55
56            self.assertEqual(cr, 'ABCD')
57            self.assertEqual(tp, 'EFGH')
58
59        finally:
60            os.unlink(test_support.TESTFN)
61
62
63    def testOpenRF(self):
64        try:
65            fp = open(test_support.TESTFN, 'w')
66            fp.write('hello world\n')
67            fp.close()
68
69            rfp = MacOS.openrf(test_support.TESTFN, '*wb')
70            rfp.write('goodbye world\n')
71            rfp.close()
72
73
74            fp = open(test_support.TESTFN, 'r')
75            data = fp.read()
76            fp.close()
77            self.assertEqual(data, 'hello world\n')
78
79            rfp = MacOS.openrf(test_support.TESTFN, '*rb')
80            data = rfp.read(100)
81            data2 = rfp.read(100)
82            rfp.close()
83            self.assertEqual(data, 'goodbye world\n')
84            self.assertEqual(data2, '')
85
86
87        finally:
88            os.unlink(test_support.TESTFN)
89
90def test_main():
91    test_support.run_unittest(TestMacOS)
92
93
94if __name__ == '__main__':
95    test_main()
96