diff --git a/pylink/jlink.py b/pylink/jlink.py index 560cca1..ab22b9d 100644 --- a/pylink/jlink.py +++ b/pylink/jlink.py @@ -830,6 +830,27 @@ def set_log_file(self, file_path): if res: raise errors.JLinkException(res) + @open_required + def set_script_file(self, file_path): + """Sets the custom `Script File`_ to use. + + Args: + self (JLink): the ``JLink`` instance + file_path (str): file path to the JLink script file to be used + + Returns: + ``None`` + + Raises: + JLinkException: if the path specified is invalid. + + .. _Script File: + https://wiki.segger.com/J-Link_script_files + """ + res = self.exec_command('scriptfile = %s' % file_path) + if res: + raise errors.JLinkException('Failed to set JLink Script File: %r' % file_path) + @open_required def invalidate_firmware(self): """Invalidates the emulator's firmware. diff --git a/tests/unit/test_jlink.py b/tests/unit/test_jlink.py index b56e9af..f5f0dce 100644 --- a/tests/unit/test_jlink.py +++ b/tests/unit/test_jlink.py @@ -6260,6 +6260,36 @@ def test_set_log_file_raises_exception_if_SetLogFile_fails(self): with self.assertRaises(JLinkException): self.jlink.set_log_file('my/file/path') + def test_set_script_file_success(self): + """Tests that set_script_file uses provided parameters. + + Args: + self (TestJLink): the ``TestJLink`` instance + + Returns: + ``None`` + """ + args = ['my/file/path'] + expected = ['scriptfile = my/file/path'] + self.jlink.exec_command = mock.Mock() + self.jlink.exec_command.return_value = 0 + + self.jlink.set_script_file(*args) + self.jlink.exec_command.assert_called_once_with(*expected) + + def test_set_script_file_raises_exception_if_exec_command_fails(self): + """Tests that set_script_file raises a JLinkException on failure. + Args: + self (TestJLink): the ``TestJLink`` instance + + Returns: + ``None`` + """ + self.jlink.exec_command = mock.Mock() + self.jlink.exec_command.return_value = -1 + with self.assertRaises(JLinkException): + self.jlink.set_script_file('my/file/path') + if __name__ == '__main__': unittest.main()