|
1 | | -from typing import List, Optional, Union, cast |
| 1 | +from typing import List, Optional, Tuple, Union, cast |
2 | 2 |
|
3 | 3 | from galaxyls.services.tools.constants import ( |
4 | 4 | ARGUMENT, |
@@ -58,20 +58,26 @@ class GalaxyToolTestSnippetGenerator(SnippetGenerator): |
58 | 58 | def __init__(self, tool_document: GalaxyToolXmlDocument, tabSize: int = 4) -> None: |
59 | 59 | super().__init__(tool_document, tabSize) |
60 | 60 |
|
61 | | - def _build_snippet(self) -> Optional[str]: |
| 61 | + def _build_snippet(self) -> Tuple[str, bool]: |
62 | 62 | """This function tries to generate a code snippet in TextMate format with all the tests cases extracted |
63 | 63 | from the inputs and outputs of the tool. |
64 | 64 |
|
65 | 65 | Returns: |
66 | | - Optional[str]: The code snippet in TextMate format or None if the generation failed. |
| 66 | + Tuple[str, bool]: The code snippet in TextMate format or an error message if the generation failed. |
| 67 | + The second value of the tuple indicates if there was an error. |
67 | 68 | """ |
68 | | - input_tree = self.expanded_document.analyze_inputs() |
69 | | - outputs = self.expanded_document.get_outputs() |
70 | | - result_snippet = "\n".join((self._generate_test_case_snippet(input_node, outputs) for input_node in input_tree.leaves)) |
71 | | - tests_section = self.tool_document.find_element(TESTS) |
72 | | - if tests_section and not tests_section.is_self_closed: |
73 | | - return result_snippet |
74 | | - return f"\n<{TESTS}>\n{result_snippet}\n</{TESTS}>" |
| 69 | + try: |
| 70 | + input_tree = self.expanded_document.analyze_inputs() |
| 71 | + outputs = self.expanded_document.get_outputs() |
| 72 | + result_snippet = "\n".join( |
| 73 | + (self._generate_test_case_snippet(input_node, outputs) for input_node in input_tree.leaves) |
| 74 | + ) |
| 75 | + tests_section = self.tool_document.find_element(TESTS) |
| 76 | + if tests_section and not tests_section.is_self_closed: |
| 77 | + return (result_snippet, False) |
| 78 | + return (f"\n<{TESTS}>\n{result_snippet}\n</{TESTS}>", False) |
| 79 | + except BaseException as ex: |
| 80 | + return (f"Automatic Test Case generation failed with reason: {ex}", True) |
75 | 81 |
|
76 | 82 | def _find_snippet_insert_position(self) -> Union[Position, Range]: |
77 | 83 | """Returns the position inside the document where new test cases |
@@ -118,15 +124,12 @@ def _generate_test_case_snippet(self, input_node: InputNode, outputs: List[XmlEl |
118 | 124 | Returns: |
119 | 125 | str: The resulting code snippet in TextMate format. |
120 | 126 | """ |
121 | | - try: |
122 | | - test_element = self._create_test_element() |
123 | | - self._add_inputs_to_test_element(input_node, test_element) |
124 | | - self._add_outputs_to_test_element(outputs, test_element) |
125 | | - etree.indent(test_element, space=self.indent_spaces) |
126 | | - snippet = etree.tostring(test_element, pretty_print=True, encoding=str) |
127 | | - return cast(str, snippet) |
128 | | - except BaseException: |
129 | | - return "" |
| 127 | + test_element = self._create_test_element() |
| 128 | + self._add_inputs_to_test_element(input_node, test_element) |
| 129 | + self._add_outputs_to_test_element(outputs, test_element) |
| 130 | + etree.indent(test_element, space=self.indent_spaces) |
| 131 | + snippet = etree.tostring(test_element, pretty_print=True, encoding=str) |
| 132 | + return cast(str, snippet) |
130 | 133 |
|
131 | 134 | def _create_test_element(self) -> etree._Element: |
132 | 135 | """Returns a XML element representing a <test> tag with the basic information. |
|
0 commit comments