File: test_exit_code.py

package info (click to toggle)
python-memory-profiler 0.61-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 540 kB
  • sloc: python: 2,220; makefile: 33
file content (42 lines) | stat: -rw-r--r-- 1,347 bytes parent folder | download | duplicates (2)
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
import unittest
import sys
import tempfile


class TestExitCode(unittest.TestCase):

    def setUp(self):
        # to be able to import mprof
        sys.path.append('.')
        from mprof import run_action
        self.run_action = run_action

    def test_exit_code_success(self):
        s = "1+1"
        tmpfile = tempfile.NamedTemporaryFile('w', suffix='.py')
        with tmpfile as ofile:
            ofile.write(s)
            ofile.flush()
            sys.argv = ['<ignored>', '--exit-code', tmpfile.name]
            self.assertRaisesRegex(SystemExit, '0', self.run_action)

    def test_exit_code_fail(self):
        s = "raise RuntimeError('I am not working nicely')"
        tmpfile = tempfile.NamedTemporaryFile('w', suffix='.py')
        with tmpfile as ofile:
            ofile.write(s)
            ofile.flush()
            sys.argv = ['<ignored>', '--exit-code', tmpfile.name]
            self.assertRaisesRegex(SystemExit, '1', self.run_action)

    def test_no_exit_code_success(self):
        s = "raise RuntimeError('I am not working nicely')"
        tmpfile = tempfile.NamedTemporaryFile('w', suffix='.py')
        with tmpfile as ofile:
            ofile.write(s)
            ofile.flush()
            sys.argv = ['<ignored>', tmpfile.name]
            self.run_action()

if __name__ == '__main__':
    unittest.main()