scripts: runners: defer ensure_snr() to run in nrfjprog

The create() classmethod should not be doing any I/O -- its only job
is to create the ZephyrBinaryRunner instance. It's currently trying to
figure out what the serial number of the board is, though. Let's defer
that work to do_run(), so it gets handled by run_common's exception
handler.

Signed-off-by: Martí Bolívar <marti.bolivar@nordicsemi.no>
This commit is contained in:
Martí Bolívar 2020-01-09 14:26:39 -08:00 committed by Anas Nashif
commit ae2f27cf16
2 changed files with 14 additions and 19 deletions

View file

@ -54,11 +54,9 @@ class NrfJprogBinaryRunner(ZephyrBinaryRunner):
@classmethod @classmethod
def create(cls, cfg, args): def create(cls, cfg, args):
ret = NrfJprogBinaryRunner(cfg, args.nrf_family, args.softreset, return NrfJprogBinaryRunner(cfg, args.nrf_family, args.softreset,
args.snr, erase=args.erase, args.snr, erase=args.erase,
tool_opt=args.tool_opt) tool_opt=args.tool_opt)
ret.ensure_snr()
return ret
def ensure_snr(self): def ensure_snr(self):
if not self.snr: if not self.snr:
@ -102,6 +100,8 @@ class NrfJprogBinaryRunner(ZephyrBinaryRunner):
def do_run(self, command, **kwargs): def do_run(self, command, **kwargs):
self.require('nrfjprog') self.require('nrfjprog')
self.ensure_snr()
commands = [] commands = []
if self.snr is None: if self.snr is None:
raise ValueError("self.snr must not be None") raise ValueError("self.snr must not be None")

View file

@ -219,11 +219,9 @@ TEST_CASES = [(f, sr, snr, e)
for snr in (TEST_OVR_SNR, None) for snr in (TEST_OVR_SNR, None)
for e in (False, True)] for e in (False, True)]
def get_board_snr_patch(): def get_board_snr_patch():
return TEST_DEF_SNR return TEST_DEF_SNR
def require_patch(program): def require_patch(program):
assert program == 'nrfjprog' assert program == 'nrfjprog'
@ -241,7 +239,6 @@ def id_fn(test_case):
ret += 'Y' if x else 'N' ret += 'Y' if x else 'N'
return ret return ret
@pytest.mark.parametrize('test_case', TEST_CASES, ids=id_fn) @pytest.mark.parametrize('test_case', TEST_CASES, ids=id_fn)
@patch('runners.core.ZephyrBinaryRunner.require', side_effect=require_patch) @patch('runners.core.ZephyrBinaryRunner.require', side_effect=require_patch)
@patch('runners.nrfjprog.NrfJprogBinaryRunner.get_board_snr_from_user', @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, runner = NrfJprogBinaryRunner(runner_config, family, softreset, snr,
erase=erase) erase=erase)
if snr is None: with patch('os.path.isfile', side_effect=os_path_isfile_patch):
with pytest.raises(ValueError) as e: runner.run('flash')
runner.run('flash') assert req.called
assert 'snr must not be None' in str(e.value) assert cc.call_args_list == [call(x) for x in
else: expected_commands(*test_case)]
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()
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) @pytest.mark.parametrize('test_case', TEST_CASES, ids=id_fn)
@patch('runners.core.ZephyrBinaryRunner.require', side_effect=require_patch) @patch('runners.core.ZephyrBinaryRunner.require', side_effect=require_patch)