codeplain 0.4.3.dev64__py3-none-any.whl → 0.4.3.dev66__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (37) hide show
  1. {codeplain-0.4.3.dev64.dist-info → codeplain-0.4.3.dev66.dist-info}/METADATA +1 -1
  2. {codeplain-0.4.3.dev64.dist-info → codeplain-0.4.3.dev66.dist-info}/RECORD +37 -33
  3. codeplain_REST_api.py +12 -0
  4. git_utils.py +66 -0
  5. module_renderer.py +23 -5
  6. partial_rendering.py +34 -10
  7. plain2code.py +18 -4
  8. plain2code_arguments.py +6 -0
  9. plain_modules.py +95 -13
  10. render_machine/actions/commit_conformance_tests_changes.py +7 -3
  11. render_machine/actions/exit_with_error.py +7 -6
  12. render_machine/actions/finish_functional_requirement.py +16 -4
  13. render_machine/actions/fix_unit_tests.py +19 -0
  14. render_machine/actions/prepare_repositories.py +68 -0
  15. render_machine/actions/render_conformance_tests.py +20 -2
  16. render_machine/actions/render_functional_requirement.py +3 -0
  17. render_machine/render_context.py +63 -17
  18. render_machine/render_types.py +4 -1
  19. render_machine/render_utils.py +6 -0
  20. render_machine/state_machine_config.py +2 -4
  21. tests/test_conformance_fix_conversation.py +1 -0
  22. tests/test_conformance_tests_json_order.py +37 -0
  23. tests/test_fix_unit_tests_action.py +35 -0
  24. tests/test_fix_unit_tests_conformance_context.py +1 -0
  25. tests/test_git_utils.py +26 -0
  26. tests/test_partial_rendering.py +3 -0
  27. tests/test_plain_modules.py +240 -0
  28. tests/test_prepare_repositories_layout.py +2 -1
  29. tests/test_regenerate_checks.py +173 -0
  30. tests/test_regenerate_reset.py +167 -0
  31. tests/test_render_context_conformance_regression.py +198 -0
  32. tests/test_render_stop_reason.py +27 -2
  33. tests/test_terminal_attachment.py +1 -0
  34. tui/plain_module_render_choice_tui.py +7 -0
  35. {codeplain-0.4.3.dev64.dist-info → codeplain-0.4.3.dev66.dist-info}/WHEEL +0 -0
  36. {codeplain-0.4.3.dev64.dist-info → codeplain-0.4.3.dev66.dist-info}/entry_points.txt +0 -0
  37. {codeplain-0.4.3.dev64.dist-info → codeplain-0.4.3.dev66.dist-info}/licenses/LICENSE +0 -0
plain_modules.py CHANGED
@@ -156,6 +156,7 @@ class PlainModule:
156
156
  template_dirs: list[str],
157
157
  tests_folders: list[str] | None = None,
158
158
  conformance_tests_enabled: bool = False,
159
+ _modules_cache: dict[str, PlainModule] | None = None,
159
160
  ):
160
161
  self.filename = filename
161
162
  self.build_folder = build_folder
@@ -174,16 +175,22 @@ class PlainModule:
174
175
  plain_spec.collect_linked_resources(plain_source, resources_list, None, True)
175
176
  self.resources_list = resources_list
176
177
  self.functionalities_without_scenarios: list[tuple[str, str]] = []
178
+ if _modules_cache is None:
179
+ _modules_cache = {}
180
+ _modules_cache[self.module_name] = self
177
181
  self.required_modules = []
178
- if len(required_modules_names) > 0:
179
- self.required_modules = [
180
- PlainModule(
181
- plain_file.get_filename_from_module_name(module_name),
182
- self.build_folder,
183
- self.template_dirs,
182
+ for req_name in required_modules_names:
183
+ if req_name in _modules_cache:
184
+ self.required_modules.append(_modules_cache[req_name])
185
+ else:
186
+ self.required_modules.append(
187
+ PlainModule(
188
+ plain_file.get_filename_from_module_name(req_name),
189
+ self.build_folder,
190
+ self.template_dirs,
191
+ _modules_cache=_modules_cache,
192
+ )
184
193
  )
185
- for module_name in required_modules_names
186
- ]
187
194
  if tests_folders is not None:
188
195
  self._bind_test_scenarios(tests_folders, conformance_tests_enabled)
189
196
 
@@ -202,14 +209,20 @@ class PlainModule:
202
209
 
203
210
  @cached_property
204
211
  def all_required_modules(self) -> list[PlainModule]:
205
- all_required_modules = []
212
+ result: list[PlainModule] = []
213
+ seen: set[str] = set()
214
+
206
215
  for required_module in self.required_modules:
207
- if len(required_module.required_modules) > 0:
208
- all_required_modules.extend(required_module.all_required_modules)
216
+ for mod in required_module.all_required_modules:
217
+ if mod.module_name not in seen:
218
+ seen.add(mod.module_name)
219
+ result.append(mod)
209
220
 
210
- all_required_modules.append(required_module)
221
+ if required_module.module_name not in seen:
222
+ seen.add(required_module.module_name)
223
+ result.append(required_module)
211
224
 
212
- return all_required_modules
225
+ return result
213
226
 
214
227
  @property
215
228
  def _default_module_folder(self) -> str:
@@ -361,6 +374,11 @@ class PlainModule:
361
374
  functionalities.append(functionality)
362
375
  metadata[MODULE_FUNCTIONALITIES] = functionalities
363
376
 
377
+ # source_hash claims the code implements the whole spec, so only stamp it when that is
378
+ # true. Regenerating one functionality leaves the other functionalities as they were.
379
+ if self._metadata_matches_spec(metadata):
380
+ metadata["source_hash"] = self.get_module_source_hash()
381
+
364
382
  metadata_utils.write_metadata(self.module_metadata_path(), metadata)
365
383
 
366
384
  def _unique_chain(self) -> list[tuple[str, dict]]:
@@ -380,6 +398,13 @@ class PlainModule:
380
398
  chain.append((module.module_name, module.plain_source))
381
399
  return chain
382
400
 
401
+ def _metadata_matches_spec(self, metadata: dict) -> bool:
402
+ """Whether the given metadata already describes the module's current spec."""
403
+ if metadata.get(MODULE_FUNCTIONALITIES) != self._get_module_functional_requirements():
404
+ return False
405
+
406
+ return metadata.get("non_functional_source_hash") == self.get_module_non_functional_source_hash()
407
+
383
408
  def get_module_source_hash(self) -> str:
384
409
  return plain_spec.get_hash_value([self.plain_source] + self.resources_list)
385
410
 
@@ -567,6 +592,63 @@ class PlainModule:
567
592
  f" codeplain {self.module_name}{plain_file.PLAIN_SOURCE_FILE_EXTENSION} --render-from {frid}"
568
593
  )
569
594
 
595
+ def ensure_frid_can_be_regenerated(self, frid: str, render_conformance_tests: bool) -> None:
596
+ """Stop a regeneration of a functionality that was never fully rendered.
597
+
598
+ Only a rendered functionality can be generated again. This check runs before the required
599
+ modules are rendered, so a regeneration that cannot run uses no credits.
600
+
601
+ A module rendered without conformance tests has no conformance tests folder. A regeneration
602
+ with conformance tests would fail when it commits the new tests, and the earlier
603
+ functionalities would have no tests for the regression pass.
604
+ """
605
+ if not os.path.exists(self.module_build_folder) or not git_utils.has_commit_for_frid(
606
+ self.module_build_folder, frid, self.module_name
607
+ ):
608
+ raise MissingPreviousFunctionalitiesError(
609
+ f"Cannot regenerate functionality {frid} because it has not been fully rendered yet. "
610
+ f"Please render all functionalities first by running:\n"
611
+ f" codeplain {self.module_name}{plain_file.PLAIN_SOURCE_FILE_EXTENSION}"
612
+ )
613
+
614
+ if render_conformance_tests and not os.path.exists(self.module_conformance_tests_folder):
615
+ raise MissingPreviousFunctionalitiesError(
616
+ f"Cannot regenerate functionality {frid} because module {self.module_name} was rendered "
617
+ f"without conformance tests. Render the module again with conformance tests:\n"
618
+ f" codeplain {self.module_name}{plain_file.PLAIN_SOURCE_FILE_EXTENSION}"
619
+ )
620
+
621
+ def get_regenerated_frid_removed_by_render_from(self, start_frid: str) -> str | None:
622
+ """The earliest functionality before `start_frid` whose regeneration a render from `start_frid` removes.
623
+
624
+ A render from `start_frid` resets the code to the "fully implemented" commit of the functionality
625
+ before it. A regeneration made after that commit is removed by the reset. The functionalities from
626
+ `start_frid` on render again, so only a regeneration of an earlier functionality is lost.
627
+ """
628
+ previous_frid = plain_spec.get_previous_frid(self.plain_source, start_frid)
629
+ if previous_frid is None or not os.path.exists(self.module_build_folder):
630
+ return None
631
+
632
+ regenerated_frids = git_utils.get_frids_regenerated_after_frid(
633
+ self.module_build_folder, previous_frid, self.module_name
634
+ )
635
+ for frid in plain_spec.get_frids_before(self.plain_source, start_frid):
636
+ if frid in regenerated_frids:
637
+ return frid
638
+ return None
639
+
640
+ def ensure_render_from_keeps_regenerations(self, start_frid: str) -> None:
641
+ """Stop a render from `start_frid` that would remove the regeneration of an earlier functionality."""
642
+ regenerated_frid = self.get_regenerated_frid_removed_by_render_from(start_frid)
643
+ if regenerated_frid is None:
644
+ return
645
+ raise MissingPreviousFunctionalitiesError(
646
+ f"Cannot render from functionality {start_frid} of module {self.module_name}. "
647
+ f"Functionality {regenerated_frid} was regenerated, and a render from functionality {start_frid} "
648
+ f"would remove that regeneration. Render from functionality {regenerated_frid} instead:\n"
649
+ f" codeplain {self.module_name}{plain_file.PLAIN_SOURCE_FILE_EXTENSION} --render-from {regenerated_frid}"
650
+ )
651
+
570
652
  def ensure_previous_frid_commits_exist(self, render_range: list[str], render_conformance_tests: bool) -> None:
571
653
  """
572
654
  Ensure that all FRID commits before the render_range exist.
@@ -10,9 +10,8 @@ class CommitConformanceTestsChanges(BaseAction):
10
10
  SUCCESSFUL_OUTCOME_IMPLEMENTATION_NOT_UPDATED = "conformance_tests_changes_committed_implementation_not_updated"
11
11
  SUCCESSFUL_OUTCOME_IMPLEMENTATION_UPDATED = "conformance_tests_changes_committed_implementation_updated"
12
12
 
13
- def __init__(self, implementation_code_commit_message: str, conformance_tests_commit_message: str):
13
+ def __init__(self, implementation_code_commit_message: str):
14
14
  self.implementation_code_commit_message = implementation_code_commit_message
15
- self.conformance_tests_commit_message = conformance_tests_commit_message
16
15
 
17
16
  def execute(self, render_context: RenderContext, _previous_action_payload: Any | None):
18
17
  implementation_updated = False
@@ -26,8 +25,13 @@ class CommitConformanceTestsChanges(BaseAction):
26
25
  )
27
26
  implementation_updated = True
28
27
 
28
+ conformance_tests_commit_message = (
29
+ git_utils.FUNCTIONAL_REQUIREMENT_REGENERATED_COMMIT_MESSAGE
30
+ if render_context.is_regenerate
31
+ else git_utils.FUNCTIONAL_REQUIREMENT_FINISHED_COMMIT_MESSAGE
32
+ )
29
33
  functional_requirement_text = render_context.frid_context.specifications[plain_spec.FUNCTIONAL_REQUIREMENTS][-1]
30
- templated_functional_requirement_finished_commit_msg = self.conformance_tests_commit_message.format(
34
+ templated_functional_requirement_finished_commit_msg = conformance_tests_commit_message.format(
31
35
  render_context.frid_context.frid
32
36
  )
33
37
  formatted_conformance_commit_msg = (
@@ -18,13 +18,14 @@ class ExitWithError(BaseAction):
18
18
  render_context.last_error_message = error_message
19
19
  console.error(error_message)
20
20
 
21
- render_context.codeplain_api.fail_functional_requirement(
22
- render_context.frid_context.frid,
23
- module_name=render_context.module_name,
24
- run_state=render_context.run_state,
25
- )
26
-
21
+ # A render can fail before any functionality is started, for example when a regeneration is
22
+ # not possible. There is then no functionality to report as failed.
27
23
  if render_context.frid_context is not None:
24
+ render_context.codeplain_api.fail_functional_requirement(
25
+ render_context.frid_context.frid,
26
+ module_name=render_context.module_name,
27
+ run_state=render_context.run_state,
28
+ )
28
29
  console.info(
29
30
  f"To continue rendering from the last successfully rendered functionality, "
30
31
  f"provide the --render-from {render_context.frid_context.frid} flag."
@@ -1,16 +1,28 @@
1
1
  from typing import Any
2
2
 
3
- from render_machine.actions.commit_implementation_code_changes import CommitImplementationCodeChanges
3
+ import git_utils
4
+ from render_machine.actions.base_action import BaseAction
4
5
  from render_machine.render_context import RenderContext
5
6
 
6
7
 
7
- class FinishFunctionalRequirement(CommitImplementationCodeChanges):
8
+ class FinishFunctionalRequirement(BaseAction):
8
9
  SUCCESSFUL_OUTCOME = "functional_requirement_finished"
9
10
 
10
- def execute(self, render_context: RenderContext, previous_action_payload: Any | None):
11
+ def execute(self, render_context: RenderContext, _previous_action_payload: Any | None):
11
12
  render_context.plain_module.update_frid_in_module_metadata(render_context.frid_context.frid)
12
13
 
13
- super().execute(render_context, previous_action_payload)
14
+ commit_message = (
15
+ git_utils.FUNCTIONAL_REQUIREMENT_REGENERATED_COMMIT_MESSAGE
16
+ if render_context.is_regenerate
17
+ else git_utils.FUNCTIONAL_REQUIREMENT_FINISHED_COMMIT_MESSAGE
18
+ )
19
+ git_utils.add_all_files_and_commit(
20
+ render_context.build_folder,
21
+ commit_message.format(render_context.frid_context.frid),
22
+ render_context.module_name,
23
+ render_context.frid_context.frid,
24
+ render_context.run_state.render_id,
25
+ )
14
26
 
15
27
  render_context.codeplain_api.finish_functional_requirement(
16
28
  render_context.frid_context.frid,
@@ -246,8 +246,27 @@ class FixUnitTests(BaseAction):
246
246
  )
247
247
  if current:
248
248
  sections.append(f"### Module: {render_context.module_name} (Currently Being Implemented)\n{current[-1]}")
249
+ later = FixUnitTests._functionalities_after_regenerated_one(render_context)
250
+ if later:
251
+ sections.append(
252
+ f"### Module: {render_context.module_name} (Already Implemented, for context)\n" + "\n".join(later)
253
+ )
249
254
  return "\n\n".join(sections)
250
255
 
256
+ @staticmethod
257
+ def _functionalities_after_regenerated_one(render_context: RenderContext) -> list[str]:
258
+ """The specs of the functionalities after the regenerated one, as their code implements them.
259
+
260
+ A regeneration renders one functionality again, and the functionalities after it keep their
261
+ code. Without their specs, the agent can change their code to agree with a wrong unit test.
262
+ """
263
+ if not render_context.is_regenerate:
264
+ return []
265
+ stored = render_context.plain_module.load_module_metadata()["functionalities"]
266
+ stored_frids = [functionality["id"] for functionality in stored]
267
+ position = stored_frids.index(render_context.frid_context.frid)
268
+ return [functionality["markdown"] for functionality in stored[position + 1 :]]
269
+
251
270
  @staticmethod
252
271
  def _read_script(script: str | None) -> str:
253
272
  if not script:
@@ -7,12 +7,17 @@ from memory_management import MemoryManager
7
7
  from plain2code_console import console
8
8
  from render_machine.actions.base_action import BaseAction
9
9
  from render_machine.render_context import RenderContext
10
+ from render_machine.render_types import RenderError
10
11
 
11
12
 
12
13
  class PrepareRepositories(BaseAction):
13
14
  SUCCESSFUL_OUTCOME = "repositories_prepared"
15
+ REGENERATE_NOT_POSSIBLE_OUTCOME = "regenerate_not_possible"
14
16
 
15
17
  def execute(self, render_context: RenderContext, _previous_action_payload: Any | None):
18
+ if render_context.is_regenerate:
19
+ return self._prepare_regenerate(render_context)
20
+
16
21
  if render_context.render_range is not None and render_context.render_range[0] != plain_spec.get_first_frid(
17
22
  render_context.plain_source_tree
18
23
  ):
@@ -87,3 +92,66 @@ class PrepareRepositories(BaseAction):
87
92
  )
88
93
 
89
94
  return self.SUCCESSFUL_OUTCOME, None
95
+
96
+ def _regenerate_not_possible(self, render_context: RenderContext, error_message: str):
97
+ # The error goes back as the outcome, so the state machine runs ExitWithError, which logs it.
98
+ # Dispatching the error from inside this action would end the render before that step runs,
99
+ # and the user would see no reason.
100
+ render_context.last_error_message = error_message
101
+ return self.REGENERATE_NOT_POSSIBLE_OUTCOME, RenderError.encode(message=error_message).to_payload()
102
+
103
+ def _prepare_regenerate(self, render_context: RenderContext):
104
+ frid = render_context.render_range[0]
105
+
106
+ if not git_utils.has_commit_for_frid(render_context.build_folder, frid, render_context.module_name):
107
+ return self._regenerate_not_possible(
108
+ render_context,
109
+ f"Cannot regenerate functionality {frid} because it has not been fully rendered yet. "
110
+ f"Please render all functionalities first by running: "
111
+ f"codeplain {render_context.module_name}.plain",
112
+ )
113
+
114
+ render_context.starting_frid = frid
115
+
116
+ module_metadata = render_context.plain_module.load_module_metadata()
117
+ if not module_metadata or "functionalities" not in module_metadata:
118
+ return self._regenerate_not_possible(
119
+ render_context,
120
+ "module_metadata.json is missing or incomplete. Please re-render the module from the beginning.",
121
+ )
122
+
123
+ stored_functionalities = module_metadata["functionalities"]
124
+ old_functionality = next((stored for stored in stored_functionalities if stored["id"] == frid), None)
125
+ if old_functionality is None:
126
+ return self._regenerate_not_possible(
127
+ render_context,
128
+ f"module_metadata.json holds no functionality {frid}. "
129
+ "Please re-render the module from the beginning.",
130
+ )
131
+
132
+ render_context.old_frid_spec = old_functionality["markdown"]
133
+ # The specs the code on disk currently implements (code-truth). Sent to the API so the
134
+ # regeneration prompt shows the old spec for any changed-but-not-yet-regenerated FR,
135
+ # keeping specs and code in sync.
136
+ render_context.code_functional_requirements = [stored["markdown"] for stored in stored_functionalities]
137
+
138
+ # An interrupted regeneration leaves its commits and changes on top of the last finished
139
+ # functionality. Remove them, so that this regeneration starts from finished code and tests.
140
+ git_utils.revert_to_last_finished_commit(render_context.build_folder, render_context.module_name)
141
+ if render_context.render_conformance_tests:
142
+ git_utils.revert_to_last_finished_commit(
143
+ render_context.conformance_tests.get_module_conformance_tests_folder(render_context.module_name),
144
+ render_context.module_name,
145
+ )
146
+ conformance_tests_json = render_context.conformance_tests.get_conformance_tests_json(
147
+ render_context.module_name
148
+ )
149
+ if frid in conformance_tests_json:
150
+ old_folder = conformance_tests_json[frid]["folder_name"]
151
+ file_utils.delete_folder(old_folder)
152
+ del conformance_tests_json[frid]
153
+ render_context.conformance_tests.dump_conformance_tests_json(
154
+ render_context.module_name, conformance_tests_json
155
+ )
156
+
157
+ return self.SUCCESSFUL_OUTCOME, None
@@ -75,14 +75,18 @@ class RenderConformanceTests(BaseAction):
75
75
 
76
76
  console.debug(f"Storing conformance test files in subfolder {conformance_tests_folder_name}/")
77
77
 
78
- render_context.conformance_tests_running_context.get_conformance_tests_json(
78
+ conformance_tests_json = render_context.conformance_tests_running_context.get_conformance_tests_json(
79
79
  render_context.conformance_tests_running_context.current_testing_module_name
80
- )[render_context.conformance_tests_running_context.current_testing_frid] = {
80
+ )
81
+ conformance_tests_json[render_context.conformance_tests_running_context.current_testing_frid] = {
81
82
  "folder_name": conformance_tests_folder_name,
82
83
  "functional_requirement": render_context.frid_context.specifications[
83
84
  plain_spec.FUNCTIONAL_REQUIREMENTS
84
85
  ][-1],
85
86
  }
87
+ RenderConformanceTests._keep_specification_order(
88
+ conformance_tests_json, list(plain_spec.get_frids(render_context.plain_source_tree))
89
+ )
86
90
  else:
87
91
  conformance_tests_folder_name = (
88
92
  render_context.conformance_tests_running_context.get_current_conformance_test_folder_name()
@@ -125,6 +129,7 @@ class RenderConformanceTests(BaseAction):
125
129
  render_context.conformance_tests_running_context.current_testing_module_name
126
130
  ),
127
131
  run_state=render_context.run_state,
132
+ is_regeneration=render_context.is_regenerate,
128
133
  )
129
134
 
130
135
  self._warn_about_untraced_scenarios(render_context, traceability)
@@ -140,6 +145,19 @@ class RenderConformanceTests(BaseAction):
140
145
 
141
146
  return self.SUCCESSFUL_OUTCOME, None
142
147
 
148
+ @staticmethod
149
+ def _keep_specification_order(conformance_tests_json: dict, frids: list[str]) -> None:
150
+ """Put the entries back in the order of the functionalities in the specification.
151
+
152
+ A regeneration, or conformance tests written again, removes the entry and adds it at the end.
153
+ A module that requires this one runs these tests in the order of the entries.
154
+ """
155
+ ordered = sorted(
156
+ conformance_tests_json.items(), key=lambda item: frids.index(item[0]) if item[0] in frids else len(frids)
157
+ )
158
+ conformance_tests_json.clear()
159
+ conformance_tests_json.update(ordered)
160
+
143
161
  @staticmethod
144
162
  def _warn_about_untraced_scenarios(render_context: RenderContext, traceability: dict) -> None:
145
163
  """Warn about a scenario with no test, a test that implements no scenario, and a scenario
@@ -78,6 +78,9 @@ class RenderFunctionalRequirement(BaseAction):
78
78
  render_context.get_required_modules_functionalities(),
79
79
  render_context.should_run_unit_tests(),
80
80
  render_context.run_state,
81
+ is_regeneration=render_context.is_regenerate,
82
+ old_functional_requirement_text=render_context.old_frid_spec,
83
+ code_functional_requirements=render_context.code_functional_requirements,
81
84
  )
82
85
  except FunctionalRequirementTooComplex as e:
83
86
  error_message = f"The functionality:\n{render_context.frid_context.functional_requirement_text}\n is too complex to be implemented. Please break down the functionality into smaller parts."
@@ -50,6 +50,7 @@ class RenderContext:
50
50
  test_script_timeout: Optional[int] = None,
51
51
  stop_event: Optional[threading.Event] = None,
52
52
  enter_pause_event: Optional[threading.Event] = None,
53
+ is_regenerate: bool = False,
53
54
  ):
54
55
  self.codeplain_api: CodeplainAPI = codeplain_api
55
56
  self.memory_manager = memory_manager
@@ -73,6 +74,9 @@ class RenderContext:
73
74
  self.event_bus = event_bus
74
75
  self.stop_event = stop_event
75
76
  self.enter_pause_event = enter_pause_event
77
+ self.is_regenerate = is_regenerate
78
+ self.old_frid_spec: str | None = None
79
+ self.code_functional_requirements: list[str] | None = None
76
80
  self.script_execution_history = ScriptExecutionHistory()
77
81
  # A preparation script run started while the conformance tests are generated (see PrepareTestingEnvironment).
78
82
  self.background_preparation: Any = None
@@ -89,7 +93,7 @@ class RenderContext:
89
93
  self.frid_context: Optional[FridContext] = None
90
94
  self.unit_tests_running_context: Optional[UnitTestsRunningContext] = None
91
95
  self.conformance_tests_running_context: Optional[ConformanceTestsRunningContext] = None
92
- # Constants that should remain for a single frid, but possible over multiple rerenderings of the same frid
96
+ # Constants that should remain for a single frid, but possible over multiple regenerations of the same frid
93
97
  self.functional_requirements_render_attempts_failed_unit_during_conformance_tests = 0
94
98
 
95
99
  # Initialize conformance tests utilities. The resolver lets a required module that ships
@@ -448,11 +452,13 @@ class RenderContext:
448
452
  def _has_reached_implementation_frid(self) -> bool:
449
453
  """Check if regression has reached the FRID being implemented."""
450
454
  ctx = self.conformance_tests_running_context
451
- return (
455
+ if not (
452
456
  ctx.execution_phase == TestExecutionPhase.RUNNING_REGRESSION
453
457
  and ctx.current_testing_module_name == self.module_name
454
- and (ctx.current_testing_frid is None or ctx.current_testing_frid == ctx.frid_being_implemented)
455
- )
458
+ ):
459
+ return False
460
+
461
+ return ctx.current_testing_frid is None or ctx.current_testing_frid == ctx.frid_being_implemented
456
462
 
457
463
  def _setup_test_specifications(self):
458
464
  """Load specifications for the current test."""
@@ -574,25 +580,65 @@ class RenderContext:
574
580
  # Get reference to the updated context
575
581
  ctx = self.conformance_tests_running_context
576
582
 
583
+ if (
584
+ self.is_regenerate
585
+ and ctx.current_testing_module_name == self.module_name
586
+ and ctx.current_testing_frid == ctx.frid_being_implemented
587
+ ):
588
+ # The regenerated functionality was already tested in the initial phase, so move past it.
589
+ # The functionalities after it keep their code, so the run continues to the end of the spec
590
+ # file. Past the last functionality the frid is None, and the run ends below.
591
+ self.conformance_tests_running_context = self._get_next_test_to_run()
592
+ ctx = self.conformance_tests_running_context
593
+
594
+ if ctx.current_testing_frid is None or not ctx.current_conformance_tests_exist():
595
+ self._end_regression_run()
596
+ return
597
+
577
598
  # Set up specs and run test
578
599
  self._setup_test_specifications()
579
600
 
580
- if ctx.current_conformance_tests_exist():
581
- # Check if this is the implementation FRID (last test to run)
582
- if self._has_reached_implementation_frid():
583
- # Reached implementation FRID - only re-run it if code changed during regression
584
- if ctx.code_changed_during_regression:
585
- # Code changed - run the implementation FRID again to verify no regression
586
- # After it passes, mark as completed on next iteration
587
- ctx.execution_phase = TestExecutionPhase.COMPLETED
588
- else:
589
- # No code changes - skip re-running implementation FRID, mark as completed immediately
590
- ctx.execution_phase = TestExecutionPhase.COMPLETED
591
- self.machine.dispatch(triggers.MARK_ALL_CONFORMANCE_TESTS_PASSED)
592
- return
601
+ # Check if this is the implementation FRID (last test to run)
602
+ if not self.is_regenerate and self._has_reached_implementation_frid():
603
+ # Reached implementation FRID - only re-run it if code changed during regression
604
+ if ctx.code_changed_during_regression:
605
+ # Code changed - run the implementation FRID again to verify no regression
606
+ # After it passes, mark as completed on next iteration
607
+ ctx.execution_phase = TestExecutionPhase.COMPLETED
608
+ else:
609
+ # No code changes - skip re-running implementation FRID, mark as completed immediately
610
+ ctx.execution_phase = TestExecutionPhase.COMPLETED
611
+ self.machine.dispatch(triggers.MARK_ALL_CONFORMANCE_TESTS_PASSED)
612
+ return
613
+
614
+ self.machine.dispatch(triggers.MARK_CONFORMANCE_TESTS_READY)
615
+
616
+ def _end_regression_run(self):
617
+ """End the regression run when the next functionality has no conformance tests, or when a
618
+ regeneration has walked past the last functionality.
619
+
620
+ The functionalities of this module are walked through the spec file, so the walk can reach a
621
+ functionality that has none. A project that was rendered only in part has such
622
+ functionalities. Nothing is left to run then, and the run must not end without a trigger:
623
+ the state machine would stay in the same state and generate conformance tests for that
624
+ functionality.
593
625
 
626
+ The running context goes back to the functionality being implemented, because the
627
+ postprocessing steps look their conformance test folder up by it.
628
+ """
629
+ ctx = self.conformance_tests_running_context
630
+ ctx.current_testing_module_name = self.module_name
631
+ ctx.current_testing_frid = ctx.frid_being_implemented
632
+ self._setup_test_specifications()
633
+ ctx.execution_phase = TestExecutionPhase.COMPLETED
634
+
635
+ if ctx.code_changed_during_regression:
636
+ # Code changed during the run, so test the functionality being implemented once more.
594
637
  self.machine.dispatch(triggers.MARK_CONFORMANCE_TESTS_READY)
638
+ else:
639
+ self.machine.dispatch(triggers.MARK_ALL_CONFORMANCE_TESTS_PASSED)
595
640
 
641
+ # TODO why is this in the context and not part of the state machine (in an action)
596
642
  # ========== Main Conformance Test Orchestration ==========
597
643
 
598
644
  def start_conformance_tests_for_frid(self):
@@ -236,7 +236,10 @@ class ConformanceTestsRunningContext:
236
236
  if plain_spec.ACCEPTANCE_TESTS not in self.current_testing_frid_specifications:
237
237
  return None
238
238
  acceptance_tests = self.current_testing_frid_specifications[plain_spec.ACCEPTANCE_TESTS]
239
- if not acceptance_tests or self.acceptance_tests_completed == 0:
239
+ # The counter belongs to the functionality being implemented. The specifications can belong
240
+ # to another functionality, which can have fewer acceptance tests, so the counter alone is
241
+ # not a valid position in this list.
242
+ if self.acceptance_tests_completed == 0 or self.acceptance_tests_completed > len(acceptance_tests):
240
243
  return None
241
244
  return acceptance_tests[self.acceptance_tests_completed - 1]
242
245
 
@@ -12,6 +12,7 @@ if sys.platform == "linux":
12
12
  import fcntl
13
13
 
14
14
  import file_utils
15
+ import git_utils
15
16
  import plain_spec
16
17
  from plain2code_console import MUTED_COLOR, RETRY_COLOR, SUCCESS_COLOR, console
17
18
  from plain2code_exceptions import RenderCancelledError
@@ -36,6 +37,11 @@ PIPE_SIZE_KB = 1024 # 1MB
36
37
 
37
38
 
38
39
  def revert_changes_for_frid(render_context):
40
+ if render_context.is_regenerate:
41
+ # The functionalities after the regenerated one stay implemented, so a reset to the previous
42
+ # functionality would remove their code. Only the changes of this regeneration are removed.
43
+ git_utils.revert_to_last_finished_commit(render_context.build_folder, render_context.module_name)
44
+ return
39
45
  if render_context.frid_context.frid is not None:
40
46
  previous_frid = plain_spec.get_previous_frid(render_context.plain_source_tree, render_context.frid_context.frid)
41
47
  render_context.plain_module.revert_code_to_frid(previous_frid)
@@ -57,12 +57,9 @@ class StateMachineConfig:
57
57
  f"{States.IMPLEMENTING_FRID.value}_{States.PROCESSING_CONFORMANCE_TESTS.value}_{States.POSTPROCESSING_CONFORMANCE_TESTS.value}_{States.CONFORMANCE_TESTS_READY_FOR_SUMMARY.value}": SummarizeConformanceTests(),
58
58
  f"{States.IMPLEMENTING_FRID.value}_{States.PROCESSING_CONFORMANCE_TESTS.value}_{States.POSTPROCESSING_CONFORMANCE_TESTS.value}_{States.CONFORMANCE_TESTS_READY_FOR_COMMIT.value}": CommitConformanceTestsChanges(
59
59
  git_utils.CONFORMANCE_TESTS_PASSED_COMMIT_MESSAGE,
60
- git_utils.FUNCTIONAL_REQUIREMENT_FINISHED_COMMIT_MESSAGE,
61
60
  ),
62
61
  f"{States.IMPLEMENTING_FRID.value}_{States.PROCESSING_CONFORMANCE_TESTS.value}_{States.POSTPROCESSING_CONFORMANCE_TESTS.value}_{States.CONFORMANCE_TESTS_READY_FOR_AMBIGUITY_ANALYSIS.value}": AnalyzeSpecificationAmbiguity(),
63
- f"{States.IMPLEMENTING_FRID.value}_{States.FRID_FULLY_IMPLEMENTED.value}": FinishFunctionalRequirement(
64
- git_utils.FUNCTIONAL_REQUIREMENT_FINISHED_COMMIT_MESSAGE
65
- ),
62
+ f"{States.IMPLEMENTING_FRID.value}_{States.FRID_FULLY_IMPLEMENTED.value}": FinishFunctionalRequirement(),
66
63
  f"{States.IMPLEMENTING_FRID.value}_{States.PROCESSING_CONFORMANCE_TESTS.value}_{States.PROCESSING_UNIT_TESTS.value}_{States.UNIT_TESTS_READY.value}": RunUnitTests(),
67
64
  f"{States.IMPLEMENTING_FRID.value}_{States.PROCESSING_CONFORMANCE_TESTS.value}_{States.PROCESSING_UNIT_TESTS.value}_{States.UNIT_TESTS_FAILED.value}": FixUnitTests(),
68
65
  States.RENDER_COMPLETED.value: CreateDist(),
@@ -73,6 +70,7 @@ class StateMachineConfig:
73
70
  """Get the mapping of action outcomes to state machine triggers."""
74
71
  return {
75
72
  PrepareRepositories.SUCCESSFUL_OUTCOME: triggers.START_RENDER,
73
+ PrepareRepositories.REGENERATE_NOT_POSSIBLE_OUTCOME: triggers.HANDLE_ERROR,
76
74
  RenderFunctionalRequirement.SUCCESSFUL_OUTCOME: triggers.RENDER_FUNCTIONAL_REQUIREMENT,
77
75
  RenderFunctionalRequirement.FUNCTIONAL_REQUIREMENT_TOO_COMPLEX_OUTCOME: triggers.HANDLE_ERROR,
78
76
  RenderFunctionalRequirement.ITERATION_LIMIT_EXCEEDED_OUTCOME: triggers.HANDLE_ERROR,
@@ -137,6 +137,7 @@ def make_render_context(project, api, testing_frid="1", frid="1", testing_module
137
137
  conformance_tests_running_context=ctx,
138
138
  plain_source_tree={},
139
139
  module_name="mod",
140
+ is_regenerate=False,
140
141
  required_modules=[],
141
142
  frid_context=SimpleNamespace(frid=frid, linked_resources={}, changed_files={"app.py"}),
142
143
  get_required_modules_functionalities=lambda: {},
@@ -0,0 +1,37 @@
1
+ """The entries of conformance_tests.json follow the order of the functionalities in the specification.
2
+
3
+ A regeneration removes the entry of the functionality and writes it again, so without this the entry
4
+ moves to the end. A module that requires this one runs these tests in the order of the entries.
5
+ """
6
+
7
+ from render_machine.actions.render_conformance_tests import RenderConformanceTests
8
+
9
+ FRIDS = ["run-cli", "add-command", "list-command", "report-command"]
10
+
11
+
12
+ def test_a_rewritten_entry_goes_back_to_its_position():
13
+ conformance_tests_json = {"run-cli": 1, "list-command": 3, "report-command": 4, "add-command": 2}
14
+
15
+ RenderConformanceTests._keep_specification_order(conformance_tests_json, FRIDS)
16
+
17
+ assert list(conformance_tests_json) == FRIDS
18
+
19
+
20
+ def test_the_same_object_is_reordered():
21
+ """The running context holds this dict, so it must be reordered in place."""
22
+ conformance_tests_json = {"list-command": 3, "run-cli": 1}
23
+ same_object = conformance_tests_json
24
+
25
+ RenderConformanceTests._keep_specification_order(conformance_tests_json, FRIDS)
26
+
27
+ assert same_object is conformance_tests_json
28
+ assert list(same_object) == ["run-cli", "list-command"]
29
+ assert same_object["list-command"] == 3
30
+
31
+
32
+ def test_an_entry_the_specification_no_longer_has_goes_last():
33
+ conformance_tests_json = {"removed-command": 9, "add-command": 2, "run-cli": 1}
34
+
35
+ RenderConformanceTests._keep_specification_order(conformance_tests_json, FRIDS)
36
+
37
+ assert list(conformance_tests_json) == ["run-cli", "add-command", "removed-command"]