From ce1e5cd54edd1f232ec3cedc1a2fbd9baba94e64 Mon Sep 17 00:00:00 2001 From: Antti Soininen Date: Mon, 2 Sep 2024 14:46:36 +0300 Subject: [PATCH] Move Jump's default condition to class level field, add unit test No functional changes intended. I was hunting a transient bug in Jump which turned out to be a "feature" when executing jumps under PyCharm's debugger. Re spine-tools/Spine-Toolbox#2925 --- spine_engine/project_item/connection.py | 10 +++++---- tests/project_item/test_connection.py | 28 ++++++++++++++++++++++++- 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/spine_engine/project_item/connection.py b/spine_engine/project_item/connection.py index 64d071c..0f28bed 100644 --- a/spine_engine/project_item/connection.py +++ b/spine_engine/project_item/connection.py @@ -589,8 +589,10 @@ def from_dict(cls, connection_dict): class Jump(ConnectionBase): """Represents a conditional jump between two project items.""" + _DEFAULT_CONDITION = {"type": "python-script", "script": "exit(1)", "specification": ""} + def __init__( - self, source_name, source_position, destination_name, destination_position, condition={}, cmd_line_args=() + self, source_name, source_position, destination_name, destination_position, condition=None, cmd_line_args=() ): """ Args: @@ -598,11 +600,11 @@ def __init__( source_position (str): source anchor's position destination_name (str): destination project item's name destination_position (str): destination anchor's position - condition (dict): jump condition + condition (dict, optional): jump condition + cmd_line_args (Iterable of str): command line arguments """ super().__init__(source_name, source_position, destination_name, destination_position) - default_condition = {"type": "python-script", "script": "exit(1)", "specification": ""} - self.condition = condition if condition else default_condition + self.condition = condition if condition is not None else self._DEFAULT_CONDITION self._resources_from_source = set() self._resources_from_destination = set() self.cmd_line_args = list(cmd_line_args) diff --git a/tests/project_item/test_connection.py b/tests/project_item/test_connection.py index a98bda5..4d1f812 100644 --- a/tests/project_item/test_connection.py +++ b/tests/project_item/test_connection.py @@ -11,11 +11,12 @@ ###################################################################################################################### """ Uni tests for the ``connection`` module. """ import os.path +import pathlib from tempfile import TemporaryDirectory import unittest from unittest.mock import Mock from spine_engine.project_item.connection import Connection, FilterSettings, Jump -from spine_engine.project_item.project_item_resource import database_resource +from spine_engine.project_item.project_item_resource import LabelArg, database_resource, file_resource from spinedb_api import DatabaseMapping, import_alternatives, import_entity_classes, import_scenarios from spinedb_api.filters.scenario_filter import SCENARIO_FILTER_TYPE @@ -169,6 +170,31 @@ def test_counter_passed_to_condition(self): jump.make_logger(Mock()) self.assertTrue(jump.is_condition_true(23)) + def test_command_line_args_with_whitespace_are_not_broken_into_tokens(self): + # Curiously, this test fails when run under PyCharm's debugger. + with TemporaryDirectory() as temp_dir: + path = pathlib.Path(temp_dir) / "path with spaces" / "file name.txt" + condition = { + "type": "python-script", + "script": "\n".join( + ( + "from pathlib import Path", + "import sys", + "if len(sys.argv) != 3:", + " exit(1)", + f"expected_path = Path(r'{str(path)}').resolve()", + "if Path(sys.argv[1]).resolve() != expected_path:", + " exit(1)", + "exit(0 if int(sys.argv[2]) == 23 else 1)", + ) + ), + } + jump = Jump("source", "bottom", "destination", "top", condition, [LabelArg("arg_label")]) + resource = file_resource("provider: unit test", str(path), "arg_label") + jump.receive_resources_from_source([resource]) + jump.make_logger(Mock()) + self.assertTrue(jump.is_condition_true(23)) + def test_dictionary(self): jump = Jump("source", "bottom", "destination", "top", {"type": "python-script", "script": "exit(23)"}) jump_dict = jump.to_dict()