Skip to content

Commit d29e6b0

Browse files
[ISSUE-188] Add jlink script file support via set_script_file() (#210)
Adds support to specify a script file via `set_script_file()` as detailed in ISSUE-188.
1 parent 1cb1253 commit d29e6b0

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

pylink/jlink.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -830,6 +830,27 @@ def set_log_file(self, file_path):
830830
if res:
831831
raise errors.JLinkException(res)
832832

833+
@open_required
834+
def set_script_file(self, file_path):
835+
"""Sets the custom `Script File`_ to use.
836+
837+
Args:
838+
self (JLink): the ``JLink`` instance
839+
file_path (str): file path to the JLink script file to be used
840+
841+
Returns:
842+
``None``
843+
844+
Raises:
845+
JLinkException: if the path specified is invalid.
846+
847+
.. _Script File:
848+
https://wiki.segger.com/J-Link_script_files
849+
"""
850+
res = self.exec_command('scriptfile = %s' % file_path)
851+
if res:
852+
raise errors.JLinkException('Failed to set JLink Script File: %r' % file_path)
853+
833854
@open_required
834855
def invalidate_firmware(self):
835856
"""Invalidates the emulator's firmware.

tests/unit/test_jlink.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6260,6 +6260,36 @@ def test_set_log_file_raises_exception_if_SetLogFile_fails(self):
62606260
with self.assertRaises(JLinkException):
62616261
self.jlink.set_log_file('my/file/path')
62626262

6263+
def test_set_script_file_success(self):
6264+
"""Tests that set_script_file uses provided parameters.
6265+
6266+
Args:
6267+
self (TestJLink): the ``TestJLink`` instance
6268+
6269+
Returns:
6270+
``None``
6271+
"""
6272+
args = ['my/file/path']
6273+
expected = ['scriptfile = my/file/path']
6274+
self.jlink.exec_command = mock.Mock()
6275+
self.jlink.exec_command.return_value = 0
6276+
6277+
self.jlink.set_script_file(*args)
6278+
self.jlink.exec_command.assert_called_once_with(*expected)
6279+
6280+
def test_set_script_file_raises_exception_if_exec_command_fails(self):
6281+
"""Tests that set_script_file raises a JLinkException on failure.
6282+
Args:
6283+
self (TestJLink): the ``TestJLink`` instance
6284+
6285+
Returns:
6286+
``None``
6287+
"""
6288+
self.jlink.exec_command = mock.Mock()
6289+
self.jlink.exec_command.return_value = -1
6290+
with self.assertRaises(JLinkException):
6291+
self.jlink.set_script_file('my/file/path')
6292+
62636293

62646294
if __name__ == '__main__':
62656295
unittest.main()

0 commit comments

Comments
 (0)