File: test_file_jy.py

package info (click to toggle)
jython 2.7.3%2Brepack1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 62,820 kB
  • sloc: python: 641,384; java: 306,981; xml: 2,066; sh: 514; ansic: 126; makefile: 77
file content (73 lines) | stat: -rw-r--r-- 2,243 bytes parent folder | download | duplicates (3)
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
"""Misc file tests.

Made for Jython.
"""
import os
import unittest
from test import test_support
from java.lang import System

class FileTestCase(unittest.TestCase):

    def tearDown(self):
        if os.path.exists(test_support.TESTFN):
            os.remove(test_support.TESTFN)

    def test_append(self):
        # http://bugs.jython.org/issue1466
        mode = 'ab'
        fp1 = open(test_support.TESTFN, mode)
        fp1.write('test1\n')
        fp2 = open(test_support.TESTFN, mode)
        fp2.write('test2\n')
        fp1.close()
        fp2.close()
        with open(test_support.TESTFN) as fp:
            self.assertEqual('test1\ntest2\n', fp.read())

    def test_appendplus(self):
        # regression with the test_append fix:
        # http://bugs.jython.org/issue1576
        with open(test_support.TESTFN, 'ab+') as fp:
            fp.write('test1\n')
            fp.seek(0)
            self.assertEqual(fp.read(), 'test1\n')

    def test_issue1825(self):
        testfnu = unicode(test_support.TESTFN)
        try:
            open(testfnu)
        except IOError, e:
            self.assertTrue(isinstance(e.filename, unicode))
            self.assertEqual(e.filename, testfnu)
        else:
            self.assertTrue(False)

    @unittest.skipUnless(hasattr(os, 'chmod'), 'chmod() support required for this test')
    def test_issue2081(self):
        with open(test_support.TESTFN, 'wb'):
            pass
        os.chmod(test_support.TESTFN, 200)      # write-only
        with open(test_support.TESTFN, 'w'):    # should succeed, raised IOError (permission denied) prior to fix
            pass

    # http://bugs.jython.org/issue2358
    def test_read_empty_file(self):
        with open(test_support.TESTFN, 'w'):
            pass
        with open(test_support.TESTFN) as f:
            self.assertEqual(f.read(), '')

    # http://bugs.jython.org/issue2358
    @unittest.skipUnless(System.getProperty('os.name') == u'Linux', 'Linux required')
    def test_can_read_proc_filesystem(self):
        with open('/proc/{}/cmdline'.format(os.getpid())) as f:
            self.assertIn('jython', f.read())


def test_main():
    test_support.run_unittest(FileTestCase)


if __name__ == '__main__':
    test_main()