Skip to content

Commit 0607235

Browse files
committed
add tests
1 parent 9c50e75 commit 0607235

3 files changed

Lines changed: 36 additions & 3 deletions

File tree

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ test:
1616
$(PYTHON) test/test_memory_usage.py
1717
$(PYTHON) test/test_precision_import.py
1818
$(PYTHON) test/test_exception.py
19+
$(PYTHON) test/test_exit_code.py
1920

2021
develop:
2122
pip install -e .

mprof.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,7 @@ def run_action():
189189
help="""Monitors forked processes as well (sum up all process memory)""")
190190
parser.add_argument("--multiprocess", "-M", dest="multiprocess", action="store_true",
191191
help="""Monitors forked processes creating individual plots for each child (disables --python features)""")
192-
parser.add_argument("--exit-code", "-E", dest="exit_code", action="store_true",
193-
help="""Propagate the exit code""")
192+
parser.add_argument("--exit-code", "-E", dest="exit_code", action="store_true", help="""Propagate the exit code""")
194193
parser.add_argument("--output", "-o", dest="filename",
195194
default="mprofile_%s.dat" % time.strftime("%Y%m%d%H%M%S", time.localtime()),
196195
help="""File to store results in, defaults to 'mprofile_<YYYYMMDDhhmmss>.dat' in the current directory,
@@ -203,7 +202,7 @@ def run_action():
203202
'Option 4: (--python flag present) "<PYTHON_MODULE> <ARG1> <ARG2>..." - profile python module\n'
204203
)
205204
args = parser.parse_args()
206-
205+
print(args)
207206
if len(args.program) == 0:
208207
print("A program to run must be provided. Use -h for help")
209208
sys.exit(1)

test/test_exit_code.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import unittest
2+
import sys
3+
import tempfile
4+
5+
class TestExitCode(unittest.TestCase):
6+
7+
def setUp(self):
8+
# to be able to import mprof
9+
sys.path.append('.')
10+
from mprof import run_action
11+
self.run_action = run_action
12+
13+
def test_exit_code_success(self):
14+
s = "1+1"
15+
tmpfile = tempfile.NamedTemporaryFile('w', suffix='.py')
16+
with tmpfile as ofile:
17+
ofile.write(s)
18+
ofile.flush()
19+
sys.argv = ['<ignored>', '--exit-code', tmpfile.name]
20+
self.assertRaisesRegex(SystemExit, '0', self.run_action)
21+
22+
def test_exit_code_fail(self):
23+
s = "raise RuntimeError('I am not working nicely')"
24+
tmpfile = tempfile.NamedTemporaryFile('w', suffix='.py')
25+
with tmpfile as ofile:
26+
ofile.write(s)
27+
ofile.flush()
28+
sys.argv = ['<ignored>', '--exit-code', tmpfile.name]
29+
self.assertRaisesRegex(SystemExit, '1', self.run_action)
30+
31+
32+
if __name__ == '__main__':
33+
unittest.main()

0 commit comments

Comments
 (0)