From 4d152f747f3f51498e94624387b0fe3f7c4d415b Mon Sep 17 00:00:00 2001 From: Martine Lenders Date: Tue, 9 Mar 2021 17:43:05 +0100 Subject: [PATCH 1/4] riotctrl.ctrl: add flash method for RIOTCtrl --- riotctrl/ctrl.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/riotctrl/ctrl.py b/riotctrl/ctrl.py index dc5dea1..693d7f8 100644 --- a/riotctrl/ctrl.py +++ b/riotctrl/ctrl.py @@ -88,6 +88,7 @@ class RIOTCtrl(): TERM_STARTED_DELAY = int(os.environ.get('RIOT_TERM_START_DELAY') or 3) MAKE_ARGS = () + FLASH_TARGETS = ('flash',) RESET_TARGETS = ('reset',) TERM_TARGETS = ('cleanterm',) @@ -110,6 +111,20 @@ def board(self): """Return board type.""" return self.env['BOARD'] + def flash(self, *runargs, stdout=DEVNULL, stderr=DEVNULL, **runkwargs): + """Flash application in ``ctrl.application_directory`` to ctrl. + + :param stdout: stdout parameter passed to ctrl.make_run + (default: DEVNULL) + :param stderr: stdout parameter passed to ctrl.make_run + (default: DEVNULL) + :param *runargs: args passed to subprocess.run + :param *runkwargs: kwargs passed to subprocess.run + :return: subprocess.CompletedProcess object + """ + self.make_run(self.FLASH_TARGETS, *runargs, + stdout=stdout, stderr=stderr, **runkwargs) + def reset(self): """Reset current ctrl.""" # Make reset yields error on some boards even if successful From 1b23aace48c5877e840bac97fd07fca483c56489 Mon Sep 17 00:00:00 2001 From: Martine Lenders Date: Tue, 9 Mar 2021 17:43:24 +0100 Subject: [PATCH 2/4] riotctrl.tests: add test for RIOTCtrl.flash --- riotctrl/tests/ctrl_test.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/riotctrl/tests/ctrl_test.py b/riotctrl/tests/ctrl_test.py index 57a8162..13a48f0 100644 --- a/riotctrl/tests/ctrl_test.py +++ b/riotctrl/tests/ctrl_test.py @@ -52,6 +52,28 @@ def test_riotctrl_curdir(): os.environ.update(_environ) +def test_riotctrl_flash(monkeypatch): + """Test the flash method of a riotctrl.""" + args = [] + kwargs = [] + + def make_run_mock(self, *_args, **_kwargs): + """ + Mocks RIOTCtrl's make_run method + """ + # pylint: disable=unused-argument + args.append(_args) + kwargs.append(_kwargs) + + monkeypatch.setattr(riotctrl.ctrl.RIOTCtrl, 'make_run', make_run_mock) + ctrl = riotctrl.ctrl.RIOTCtrl(APPLICATIONS_DIR) + ctrl.flash() + assert len(args) == len(kwargs) == 1 + assert args[-1] == (riotctrl.ctrl.RIOTCtrl.FLASH_TARGETS,) + assert kwargs[-1] == {'stderr': riotctrl.ctrl.DEVNULL, + 'stdout': riotctrl.ctrl.DEVNULL} + + @pytest.fixture(name='app_pidfile_env') def fixture_app_pidfile_env(): """Environment to use application pidfile""" From e74d91944370180c441d44c301279a1d4393c24d Mon Sep 17 00:00:00 2001 From: Martine Lenders Date: Thu, 9 Sep 2021 15:40:09 +0200 Subject: [PATCH 3/4] riotctrl.ctrl: use subprocess's DEVNULL for DEVNULL --- riotctrl/ctrl.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/riotctrl/ctrl.py b/riotctrl/ctrl.py index 693d7f8..949ab5c 100644 --- a/riotctrl/ctrl.py +++ b/riotctrl/ctrl.py @@ -13,7 +13,7 @@ import pexpect -DEVNULL = open(os.devnull, 'w') +DEVNULL = subprocess.DEVNULL MAKE = os.environ.get('MAKE', 'make') From da33e0b6d9da1c426f38a7e6861b134fdc26fcca Mon Sep 17 00:00:00 2001 From: Martine Lenders Date: Thu, 9 Sep 2021 15:44:54 +0200 Subject: [PATCH 4/4] tests.ctrl_tests: fix pylint warnings --- riotctrl/tests/ctrl_test.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/riotctrl/tests/ctrl_test.py b/riotctrl/tests/ctrl_test.py index 13a48f0..ce714cf 100644 --- a/riotctrl/tests/ctrl_test.py +++ b/riotctrl/tests/ctrl_test.py @@ -224,12 +224,13 @@ def test_term_cleanup(app_pidfile_env): with ctrl.run_term(logfile=sys.stdout) as child: child.expect_exact('Running') # Ensure script is started correctly - content = open(tmpfile.name, 'r', encoding='utf-8').read() - assert content == 'Running\n' + with open(tmpfile.name, 'r', encoding='utf-8') as tempfile_r: + assert tempfile_r.read() == 'Running\n' # File should not exist anymore so no error to create one # File must exist to be cleaned by tempfile - open(tmpfile.name, 'x') + with open(tmpfile.name, 'x', encoding='utf-8'): + pass class CtrlMock1(riotctrl.ctrl.RIOTCtrl):