Skip to content

Commit 1ab0887

Browse files
committed
Allow configuring (run|rebot)_cli to not always exit.
Both functions got optional `exit=True` argument. Fixes robotframework#2504.
1 parent cdaa471 commit 1ab0887

File tree

4 files changed

+32
-14
lines changed

4 files changed

+32
-14
lines changed

src/robot/rebot.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -353,22 +353,22 @@ def main(self, datasources, **options):
353353
return rc
354354

355355

356-
def rebot_cli(arguments):
356+
def rebot_cli(arguments, exit=True):
357357
"""Command line execution entry point for running rebot.
358358
359359
:param arguments: Command line arguments as a list of strings.
360+
:param exit: If `True`, call `sys.exit` with the return code denoting
361+
execution status, otherwise just return the rc. New in 3.0.1.
360362
361-
For programmatic usage the :func:`rebot` method is typically better. It has
362-
a better API for that usage and does not call :func:`sys.exit` like this
363-
method.
363+
For programmatic usage the :func:`rebot` method is typically better.
364364
365365
Example::
366366
367367
from robot import rebot_cli
368368
369369
rebot_cli(['--report', 'r.html', '--log', 'NONE', 'o1.xml', 'o2.xml'])
370370
"""
371-
Rebot().execute_cli(arguments)
371+
return Rebot().execute_cli(arguments, exit=exit)
372372

373373

374374
def rebot(*datasources, **options):

src/robot/run.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -449,22 +449,22 @@ def _filter_options_without_value(self, options):
449449
if value not in (None, []))
450450

451451

452-
def run_cli(arguments):
452+
def run_cli(arguments, exit=True):
453453
"""Command line execution entry point for running tests.
454454
455455
:param arguments: Command line arguments as a list of strings.
456+
:param exit: If `True`, call `sys.exit` with the return code denoting
457+
execution status, otherwise just return the rc. New in 3.0.1.
456458
457-
For programmatic usage the :func:`run` function is typically better. It has
458-
a better API for that usage and does not call :func:`sys.exit` like this
459-
function.
459+
For programmatic usage the :func:`run` function is typically better.
460460
461461
Example::
462462
463463
from robot import run_cli
464464
465465
run_cli(['--include', 'tag', 'path/to/tests.html'])
466466
"""
467-
RobotFramework().execute_cli(arguments)
467+
return RobotFramework().execute_cli(arguments, exit=exit)
468468

469469

470470
def run(*datasources, **options):

src/robot/utils/application.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,14 @@ def main(self, arguments, **options):
3939
def validate(self, options, arguments):
4040
return options, arguments
4141

42-
def execute_cli(self, cli_arguments):
42+
def execute_cli(self, cli_arguments, exit=True):
4343
with self._logger:
4444
self._logger.info('%s %s' % (self._ap.name, self._ap.version))
4545
options, arguments = self._parse_arguments(cli_arguments)
4646
rc = self._execute(arguments, options)
47-
self._exit(rc)
47+
if exit:
48+
self._exit(rc)
49+
return rc
4850

4951
def console(self, msg):
5052
if msg:

utest/api/test_run_and_rebot.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99
from os.path import abspath, dirname, join, exists, curdir
1010
from os import chdir
1111

12-
from robot import run, rebot
12+
from robot import run, run_cli, rebot, rebot_cli
1313
from robot.model import SuiteVisitor
1414
from robot.running import namespace
1515
from robot.utils import StringIO
16-
from robot.utils.asserts import assert_equal, assert_true
16+
from robot.utils.asserts import assert_equal, assert_raises, assert_true
1717

1818
from resources.runningtestcase import RunningTestCase
1919
from resources.Listener import Listener
@@ -146,6 +146,14 @@ def test_invalid_modifier(self):
146146
[("[ ERROR ] Executing model modifier 'integer' "
147147
"failed: AttributeError: ", 1)])
148148

149+
def test_run_cli_system_exits_by_default(self):
150+
exit = assert_raises(SystemExit, run_cli, [self.data])
151+
assert_equal(exit.code, 1)
152+
153+
def test_run_cli_optionally_returns_rc(self):
154+
rc = run_cli([self.data], exit=False)
155+
assert_equal(rc, 1)
156+
149157

150158
class TestRebot(RunningTestCase):
151159
data = join(ROOT, 'atest', 'testdata', 'rebot', 'created_normal.xml')
@@ -198,6 +206,14 @@ def visit_test(self, test):
198206
prerebotmodifier=modifier), 3)
199207
assert_equal(modifier.tests, ['Test 1.1', 'Test 1.2', 'Test 2.1'])
200208

209+
def test_rebot_cli_system_exits_by_default(self):
210+
exit = assert_raises(SystemExit, rebot_cli, [self.data])
211+
assert_equal(exit.code, 1)
212+
213+
def test_rebot_cli_optionally_returns_rc(self):
214+
rc = rebot_cli([self.data], exit=False)
215+
assert_equal(rc, 1)
216+
201217

202218
class TestStateBetweenTestRuns(RunningTestCase):
203219
data = join(ROOT, 'atest', 'testdata', 'misc', 'normal.robot')

0 commit comments

Comments
 (0)