diff --git a/scripts/west_commands/runners/nrfjprog.py b/scripts/west_commands/runners/nrfjprog.py index d5ada72babe..3baa8afc09f 100644 --- a/scripts/west_commands/runners/nrfjprog.py +++ b/scripts/west_commands/runners/nrfjprog.py @@ -54,11 +54,9 @@ class NrfJprogBinaryRunner(ZephyrBinaryRunner): @classmethod def create(cls, cfg, args): - ret = NrfJprogBinaryRunner(cfg, args.nrf_family, args.softreset, - args.snr, erase=args.erase, - tool_opt=args.tool_opt) - ret.ensure_snr() - return ret + return NrfJprogBinaryRunner(cfg, args.nrf_family, args.softreset, + args.snr, erase=args.erase, + tool_opt=args.tool_opt) def ensure_snr(self): if not self.snr: @@ -102,6 +100,8 @@ class NrfJprogBinaryRunner(ZephyrBinaryRunner): def do_run(self, command, **kwargs): self.require('nrfjprog') + self.ensure_snr() + commands = [] if self.snr is None: raise ValueError("self.snr must not be None") diff --git a/scripts/west_commands/tests/test_nrfjprog.py b/scripts/west_commands/tests/test_nrfjprog.py index 4ced8f4d1b6..5fec8167377 100644 --- a/scripts/west_commands/tests/test_nrfjprog.py +++ b/scripts/west_commands/tests/test_nrfjprog.py @@ -219,11 +219,9 @@ TEST_CASES = [(f, sr, snr, e) for snr in (TEST_OVR_SNR, None) for e in (False, True)] - def get_board_snr_patch(): return TEST_DEF_SNR - def require_patch(program): assert program == 'nrfjprog' @@ -241,7 +239,6 @@ def id_fn(test_case): ret += 'Y' if x else 'N' return ret - @pytest.mark.parametrize('test_case', TEST_CASES, ids=id_fn) @patch('runners.core.ZephyrBinaryRunner.require', side_effect=require_patch) @patch('runners.nrfjprog.NrfJprogBinaryRunner.get_board_snr_from_user', @@ -252,18 +249,16 @@ def test_nrfjprog_init(cc, get_snr, req, test_case, runner_config): runner = NrfJprogBinaryRunner(runner_config, family, softreset, snr, erase=erase) - if snr is None: - with pytest.raises(ValueError) as e: - runner.run('flash') - assert 'snr must not be None' in str(e.value) - else: - with patch('os.path.isfile', side_effect=os_path_isfile_patch): - runner.run('flash') - assert req.called - assert cc.call_args_list == [call(x) for x in - expected_commands(*test_case)] - get_snr.assert_not_called() + with patch('os.path.isfile', side_effect=os_path_isfile_patch): + runner.run('flash') + assert req.called + assert cc.call_args_list == [call(x) for x in + expected_commands(*test_case)] + if snr is None: + get_snr.assert_called_once_with() + else: + get_snr.assert_not_called() @pytest.mark.parametrize('test_case', TEST_CASES, ids=id_fn) @patch('runners.core.ZephyrBinaryRunner.require', side_effect=require_patch)