@@ -72,14 +72,24 @@ def _body(actions: Sequence, style: str) -> str:
7272 raise ValueError (f"unknown codegen style: { style !r} " )
7373
7474
75- def _render_pytest (actions : Sequence , name : str , style : str ) -> str :
76- body = textwrap .indent (_body (actions , style ), " " )
75+ def _render_pytest (actions : Sequence , name : str , style : str ,
76+ failure_bundle : bool = False ) -> str :
77+ raw_body = _body (actions , style )
78+ if failure_bundle :
79+ body = (" with ac.failure_bundle_on_error(\n "
80+ f" { (_slug (name ) + '-failure.zip' )!r} ,\n "
81+ f" context={{'generated_test': { _slug (name )!r} }}):\n "
82+ + textwrap .indent (raw_body , " " ))
83+ else :
84+ body = textwrap .indent (raw_body , " " )
7785 return (f'"""{ _HEADER } """\n '
78- "import je_auto_control as ac\n \n \n "
79- f"def test_{ _slug (name )} ():\n { body } \n " )
86+ + ("import je_auto_control.api as ac\n \n \n " if failure_bundle
87+ else "import je_auto_control as ac\n \n \n " )
88+ + f"def test_{ _slug (name )} ():\n { body } \n " )
8089
8190
82- def _render_python (actions : Sequence , name : str , style : str ) -> str :
91+ def _render_python (actions : Sequence , name : str , style : str ,
92+ _failure_bundle : bool = False ) -> str :
8393 slug = _slug (name )
8494 body = textwrap .indent (_body (actions , style ), " " )
8595 return (f'"""{ _HEADER } """\n '
@@ -89,7 +99,8 @@ def _render_python(actions: Sequence, name: str, style: str) -> str:
8999 f" { slug } ()\n " )
90100
91101
92- def _render_robot (actions : Sequence , name : str , _style : str ) -> str :
102+ def _render_robot (actions : Sequence , name : str , _style : str ,
103+ _failure_bundle : bool = False ) -> str :
93104 payload = json .dumps ([list (action ) for action in actions ],
94105 ensure_ascii = False )
95106 test_name = name .replace ("_" , " " ).strip ().title () or "Recorded Flow"
@@ -113,22 +124,27 @@ def _render_robot(actions: Sequence, name: str, _style: str) -> str:
113124
114125
115126def generate_code (actions : Sequence , target : str = "pytest" ,
116- name : str = "recorded_flow" , style : str = "calls" ) -> str :
127+ name : str = "recorded_flow" , style : str = "calls" ,
128+ failure_bundle : bool = False ) -> str :
117129 """Render ``actions`` as source code for ``target`` (pytest/python/robot)."""
118130 if not isinstance (actions , list ) or not actions :
119131 raise ValueError ("actions must be a non-empty list" )
120132 renderer = _RENDERERS .get (target )
121133 if renderer is None :
122134 raise ValueError (f"unknown codegen target: { target !r} " )
123- return renderer (actions , name , style )
135+ if failure_bundle and target != "pytest" :
136+ raise ValueError ("failure_bundle is currently supported for pytest only" )
137+ return renderer (actions , name , style , failure_bundle )
124138
125139
126140def generate_code_file (source , output_path : str , target : str = "pytest" ,
127- name : str = "recorded_flow" , style : str = "calls" ) -> str :
141+ name : str = "recorded_flow" , style : str = "calls" ,
142+ failure_bundle : bool = False ) -> str :
128143 """Generate code from a list or JSON action-file path; write and return it."""
129144 actions = source if isinstance (source , list ) else read_action_json (
130145 os .path .realpath (source ))
131- code = generate_code (actions , target = target , name = name , style = style )
146+ code = generate_code (actions , target = target , name = name , style = style ,
147+ failure_bundle = failure_bundle )
132148 with open (os .path .realpath (output_path ), "w" , encoding = "utf-8" ) as handle :
133149 handle .write (code )
134150 return code
0 commit comments