summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_macos.py
blob: ce5f858dde9fbb573891062fc40bc3a148559d33 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import unittest
from test import test_support
import os
import subprocess

MacOS = test_support.import_module('MacOS')
#The following should exist if MacOS exists.
import Carbon.File

TESTFN2 = test_support.TESTFN + '2'

class TestMacOS(unittest.TestCase):

    def testGetCreatorAndType(self):
        if not os.path.exists('/Developer/Tools/SetFile'):
            return

        try:
            fp = open(test_support.TESTFN, 'w')
            fp.write('\n')
            fp.close()

            subprocess.call(
                    ['/Developer/Tools/SetFile', '-t', 'ABCD', '-c', 'EFGH',
                        test_support.TESTFN])

            cr, tp = MacOS.GetCreatorAndType(test_support.TESTFN)
            self.assertEquals(tp, 'ABCD')
            self.assertEquals(cr, 'EFGH')

        finally:
            os.unlink(test_support.TESTFN)

    def testSetCreatorAndType(self):
        if not os.path.exists('/Developer/Tools/GetFileInfo'):
            return

        try:
            fp = open(test_support.TESTFN, 'w')
            fp.write('\n')
            fp.close()

            MacOS.SetCreatorAndType(test_support.TESTFN,
                    'ABCD', 'EFGH')

            cr, tp = MacOS.GetCreatorAndType(test_support.TESTFN)
            self.assertEquals(cr, 'ABCD')
            self.assertEquals(tp, 'EFGH')

            data = subprocess.Popen(["/Developer/Tools/GetFileInfo", test_support.TESTFN],
                    stdout=subprocess.PIPE).communicate()[0]

            tp = None
            cr = None
            for  ln in data.splitlines():
                if ln.startswith('type:'):
                    tp = ln.split()[-1][1:-1]
                if ln.startswith('creator:'):
                    cr = ln.split()[-1][1:-1]

            self.assertEquals(cr, 'ABCD')
            self.assertEquals(tp, 'EFGH')

        finally:
            os.unlink(test_support.TESTFN)


    def testOpenRF(self):
        try:
            fp = open(test_support.TESTFN, 'w')
            fp.write('hello world\n')
            fp.close()

            rfp = MacOS.openrf(test_support.TESTFN, '*wb')
            rfp.write('goodbye world\n')
            rfp.close()


            fp = open(test_support.TESTFN, 'r')
            data = fp.read()
            fp.close()
            self.assertEquals(data, 'hello world\n')

            rfp = MacOS.openrf(test_support.TESTFN, '*rb')
            data = rfp.read(100)
            data2 = rfp.read(100)
            rfp.close()
            self.assertEquals(data, 'goodbye world\n')
            self.assertEquals(data2, '')


        finally:
            os.unlink(test_support.TESTFN)

def test_main():
    test_support.run_unittest(TestMacOS)


if __name__ == '__main__':
    test_main()