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