File: test_fdcapture.py

package info (click to toggle)
codespeak-lib 0.9.1-3
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 3,212 kB
  • ctags: 5,409
  • sloc: python: 33,390; ansic: 961; xml: 582; makefile: 90
file content (59 lines) | stat: -rw-r--r-- 1,785 bytes parent folder | download
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
import os, sys
import py

class TestFDCapture: 
    def test_basic(self): 
        tmpfile = py.std.os.tmpfile() 
        fd = tmpfile.fileno()
        cap = py.io.FDCapture(fd)
        os.write(fd, "hello")
        f = cap.done()
        s = f.read()
        assert s == "hello"

    def test_stderr(self): 
        cap = py.io.FDCapture(2)
        cap.setasfile('stderr')
        print >>sys.stderr, "hello"
        f = cap.done()
        s = f.read()
        assert s == "hello\n"

    def test_stdin(self): 
        f = os.tmpfile()
        print >>f, "3"
        f.seek(0)
        cap = py.io.FDCapture(0, tmpfile=f)
        # check with os.read() directly instead of raw_input(), because
        # sys.stdin itself may be redirected (as py.test now does by default)
        x = os.read(0, 100).strip()
        f = cap.done()
        assert x == "3"

    def test_writeorg(self):
        tmppath = py.test.ensuretemp('test_writeorg').ensure('stderr',
                                                             file=True)
        tmpfp = tmppath.open('w+b')
        try:
            cap = py.io.FDCapture(tmpfp.fileno())
            print >>tmpfp, 'foo'
            cap.writeorg('bar\n')
        finally:
            tmpfp.close()
        f = cap.done()
        scap = f.read()
        assert scap == 'foo\n'
        stmp = tmppath.read()
        assert stmp == "bar\n"

    def test_writeorg_wrongtype(self):
        tmppath = py.test.ensuretemp('test_writeorg').ensure('stdout',
                                                             file=True)
        tmpfp = tmppath.open('r')
        try:
            cap = py.io.FDCapture(tmpfp.fileno())
            py.test.raises(IOError, "cap.writeorg('bar\\n')")
        finally:
            tmpfp.close()
        f = cap.done()