Home | History | Annotate | Download | only in test
      1 import unittest
      2 from test import test_support
      3 import os
      4 import subprocess
      5 
      6 MacOS = test_support.import_module('MacOS')
      7 
      8 TESTFN2 = test_support.TESTFN + '2'
      9 
     10 class TestMacOS(unittest.TestCase):
     11 
     12     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.assertEqual(tp, 'ABCD')
     27             self.assertEqual(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.assertEqual(cr, 'ABCD')
     46             self.assertEqual(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.assertEqual(cr, 'ABCD')
     60             self.assertEqual(tp, 'EFGH')
     61 
     62         finally:
     63             os.unlink(test_support.TESTFN)
     64 
     65 
     66     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.assertEqual(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.assertEqual(data, 'goodbye world\n')
     87             self.assertEqual(data2, '')
     88 
     89 
     90         finally:
     91             os.unlink(test_support.TESTFN)
     92 
     93 def test_main():
     94     test_support.run_unittest(TestMacOS)
     95 
     96 
     97 if __name__ == '__main__':
     98     test_main()
     99