summaryrefslogtreecommitdiff
path: root/unit_tests/test_pdb_plugin.py
blob: cdd43f23c4a6ef6c895a40120d5e477218ef7ac4 (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import sys
import unittest
from nose.config import Config
from nose.plugins import debug
from optparse import OptionParser
from StringIO import StringIO

class StubPdb:
    called = False
    def post_mortem(self, tb):
        self.called = True

class TestPdbPlugin(unittest.TestCase):

    def setUp(self):
        self._pdb = debug.pdb
        self._so = sys.stdout
        debug.pdb = StubPdb()

    def tearDown(self):
        debug.pdb = self._pdb
        sys.stdout = self._so

    def test_plugin_api(self):
        p = debug.Pdb()
        p.addOptions
        p.configure
        p.addError
        p.addFailure

    def test_plugin_calls_pdb(self):
        p = debug.Pdb()

        try:
            raise Exception("oops")
        except:
            err = sys.exc_info()
    
        p.enabled = True
        p.enabled_for_errors = True
        p.enabled_for_failures = True

        p.addError(None, err)
        assert debug.pdb.called, "Did not call pdb.post_mortem on error"

        debug.pdb.called = False
        p.addFailure(None, err)
        assert debug.pdb.called, "Did not call pdb.post_mortem on failure"

    def test_command_line_options_enable(self):
        parser = OptionParser()

        p = debug.Pdb()
        p.addOptions(parser)
        options, args = parser.parse_args(['test_configuration',
                                           '--pdb',
                                           '--pdb-failures'])
        p.configure(options, Config())
        assert p.enabled
        assert p.enabled_for_errors
        assert p.enabled_for_failures

    def test_disabled_by_default(self):
        p = debug.Pdb()
        assert not p.enabled
        assert not p.enabled_for_failures

        parser = OptionParser()
        p.addOptions(parser)
        options, args = parser.parse_args(['test_configuration'])
        p.configure(options, Config())
        assert not p.enabled
        assert not p.enabled_for_errors
        assert not p.enabled_for_failures
        
    def test_env_settings_enable(self):
        p = debug.Pdb()
        assert not p.enabled
        assert not p.enabled_for_failures

        env = {'NOSE_PDB': '1',
               'NOSE_PDB_FAILURES': '1'}

        parser = OptionParser()
        p.addOptions(parser, env)
        options, args = parser.parse_args(['test_configuration'])
        p.configure(options, Config())
        assert p.enabled
        assert p.enabled_for_errors
        assert p.enabled_for_failures

    def test_real_stdout_restored_before_call(self):
        
        class CheckStdout(StubPdb):
            def post_mortem(self, tb):
                assert sys.stdout is sys.__stdout__, \
                       "sys.stdout was not restored to sys.__stdout__ " \
                       "before call"
        debug.pdb = CheckStdout()

        patch = StringIO()
        sys.stdout = patch
        p = debug.Pdb()
        p.enabled = True
        p.enabled_for_errors = True

        try:
            raise Exception("oops")
        except:
            err = sys.exc_info()
    
        p.addError(None, err)    
        assert sys.stdout is patch, "sys.stdout was not reset after call"
        
        
if __name__ == '__main__':
    unittest.main()