blob: ce5f858dde9fbb573891062fc40bc3a148559d33 [file] [log] [blame]
Ronald Oussoren5640ce22008-06-05 12:58:24 +00001import unittest
Ronald Oussoren5640ce22008-06-05 12:58:24 +00002from test import test_support
3import os
Ronald Oussoren0238497e2009-01-02 14:10:20 +00004import subprocess
Ronald Oussoren5640ce22008-06-05 12:58:24 +00005
R. David Murray59beec32009-03-30 19:04:00 +00006MacOS = test_support.import_module('MacOS')
7#The following should exist if MacOS exists.
8import Carbon.File
9
Ronald Oussoren5640ce22008-06-05 12:58:24 +000010TESTFN2 = test_support.TESTFN + '2'
11
12class TestMacOS(unittest.TestCase):
13
Ronald Oussoren0238497e2009-01-02 14:10:20 +000014 def testGetCreatorAndType(self):
15 if not os.path.exists('/Developer/Tools/SetFile'):
16 return
17
18 try:
19 fp = open(test_support.TESTFN, 'w')
20 fp.write('\n')
21 fp.close()
22
23 subprocess.call(
24 ['/Developer/Tools/SetFile', '-t', 'ABCD', '-c', 'EFGH',
25 test_support.TESTFN])
26
27 cr, tp = MacOS.GetCreatorAndType(test_support.TESTFN)
28 self.assertEquals(tp, 'ABCD')
29 self.assertEquals(cr, 'EFGH')
30
31 finally:
32 os.unlink(test_support.TESTFN)
33
34 def testSetCreatorAndType(self):
35 if not os.path.exists('/Developer/Tools/GetFileInfo'):
36 return
37
38 try:
39 fp = open(test_support.TESTFN, 'w')
40 fp.write('\n')
41 fp.close()
42
43 MacOS.SetCreatorAndType(test_support.TESTFN,
44 'ABCD', 'EFGH')
45
46 cr, tp = MacOS.GetCreatorAndType(test_support.TESTFN)
47 self.assertEquals(cr, 'ABCD')
48 self.assertEquals(tp, 'EFGH')
49
50 data = subprocess.Popen(["/Developer/Tools/GetFileInfo", test_support.TESTFN],
51 stdout=subprocess.PIPE).communicate()[0]
52
53 tp = None
54 cr = None
55 for ln in data.splitlines():
56 if ln.startswith('type:'):
57 tp = ln.split()[-1][1:-1]
58 if ln.startswith('creator:'):
59 cr = ln.split()[-1][1:-1]
60
61 self.assertEquals(cr, 'ABCD')
62 self.assertEquals(tp, 'EFGH')
63
64 finally:
65 os.unlink(test_support.TESTFN)
66
67
Ronald Oussoren5640ce22008-06-05 12:58:24 +000068 def testOpenRF(self):
69 try:
70 fp = open(test_support.TESTFN, 'w')
71 fp.write('hello world\n')
72 fp.close()
73
74 rfp = MacOS.openrf(test_support.TESTFN, '*wb')
75 rfp.write('goodbye world\n')
76 rfp.close()
77
78
79 fp = open(test_support.TESTFN, 'r')
80 data = fp.read()
81 fp.close()
82 self.assertEquals(data, 'hello world\n')
83
84 rfp = MacOS.openrf(test_support.TESTFN, '*rb')
85 data = rfp.read(100)
86 data2 = rfp.read(100)
87 rfp.close()
88 self.assertEquals(data, 'goodbye world\n')
89 self.assertEquals(data2, '')
90
91
92 finally:
93 os.unlink(test_support.TESTFN)
94
95def test_main():
96 test_support.run_unittest(TestMacOS)
97
98
99if __name__ == '__main__':
100 test_main()