codeplain 0.4.3.dev60__py3-none-any.whl → 0.4.3.dev62__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.
@@ -1,4 +1,7 @@
1
- from typing import Any
1
+ import os
2
+ import threading
3
+ from dataclasses import dataclass, field
4
+ from typing import Any, Optional
2
5
 
3
6
  import render_machine.render_utils as render_utils
4
7
  from plain2code_console import console
@@ -7,27 +10,98 @@ from render_machine.render_context import RenderContext
7
10
  from render_machine.render_types import RenderError
8
11
 
9
12
 
13
+ def _build_folder_signature(build_folder: str) -> tuple:
14
+ """What the preparation script reads: the files of the build folder (outside .git), by size and change time."""
15
+ entries = []
16
+ for root, dirs, files in os.walk(build_folder):
17
+ dirs[:] = [d for d in dirs if d != ".git"]
18
+ for name in files:
19
+ path = os.path.join(root, name)
20
+ try:
21
+ stat = os.stat(path)
22
+ except OSError:
23
+ continue
24
+ entries.append((os.path.relpath(path, build_folder), stat.st_size, stat.st_mtime_ns))
25
+ return tuple(sorted(entries))
26
+
27
+
28
+ @dataclass
29
+ class BackgroundPreparation:
30
+ """A run of the preparation script started ahead of time, and the build folder it prepared (as the script left
31
+ it once it finished)."""
32
+
33
+ signature: tuple
34
+ done: threading.Event = field(default_factory=threading.Event)
35
+ result: Optional[tuple[int, str, Optional[str]]] = None
36
+ error: Optional[BaseException] = None
37
+
38
+
39
+ def _should_prepare(render_context: RenderContext) -> bool:
40
+ return (
41
+ render_context.prepare_environment_script is not None
42
+ and render_context.conformance_tests_running_context is not None
43
+ and render_context.conformance_tests_running_context.should_prepare_testing_environment
44
+ )
45
+
46
+
47
+ def _run_script(render_context: RenderContext) -> tuple[int, str, Optional[str]]:
48
+ console.info(
49
+ f"Running testing environment preparation script {render_context.prepare_environment_script} for build folder {render_context.build_folder}."
50
+ )
51
+ return render_utils.execute_script(
52
+ render_context.prepare_environment_script,
53
+ [render_context.build_folder],
54
+ "Testing Environment Preparation",
55
+ timeout=render_context.test_script_timeout,
56
+ stop_event=render_context.stop_event,
57
+ )
58
+
59
+
60
+ def start_in_background(render_context: RenderContext) -> None:
61
+ """Start the preparation script now when it will be due, so it runs while the conformance tests are being
62
+ generated: it reads only the build folder, which the generation does not change. PrepareTestingEnvironment
63
+ uses its result if the build folder is still the same, and runs the script again otherwise."""
64
+ if not _should_prepare(render_context) or render_context.background_preparation is not None:
65
+ return
66
+ preparation = BackgroundPreparation(signature=_build_folder_signature(render_context.build_folder))
67
+
68
+ def run() -> None:
69
+ try:
70
+ preparation.result = _run_script(render_context)
71
+ # The script may write into the build folder (node_modules, target/, ...): compare against the folder
72
+ # as it left it, or its own output would always look like a change and the script would run again.
73
+ preparation.signature = _build_folder_signature(render_context.build_folder)
74
+ except BaseException as e: # noqa: B036 - handed to the waiting action
75
+ preparation.error = e
76
+ finally:
77
+ preparation.done.set()
78
+
79
+ render_context.background_preparation = preparation
80
+ threading.Thread(target=run, name="prepare-testing-environment", daemon=True).start()
81
+
82
+
10
83
  class PrepareTestingEnvironment(BaseAction):
11
84
  SUCCESSFUL_OUTCOME = "testing_environment_prepared"
12
85
  FAILED_OUTCOME = "testing_environment_preparation_failed"
13
86
 
14
87
  def execute(self, render_context: RenderContext, _previous_action_payload: Any | None):
15
- if (
16
- render_context.prepare_environment_script is None
17
- or not render_context.conformance_tests_running_context.should_prepare_testing_environment
18
- ):
88
+ preparation = render_context.background_preparation
89
+ render_context.background_preparation = None
90
+ if not _should_prepare(render_context):
91
+ if preparation is not None:
92
+ preparation.done.wait()
19
93
  return self.SUCCESSFUL_OUTCOME, None
20
94
 
21
- console.info(
22
- f"Running testing environment preparation script {render_context.prepare_environment_script} for build folder {render_context.build_folder}."
23
- )
24
- exit_code, _, preparation_temp_file_path = render_utils.execute_script(
25
- render_context.prepare_environment_script,
26
- [render_context.build_folder],
27
- "Testing Environment Preparation",
28
- timeout=render_context.test_script_timeout,
29
- stop_event=render_context.stop_event,
30
- )
95
+ result = None
96
+ if preparation is not None:
97
+ preparation.done.wait()
98
+ if preparation.error is not None:
99
+ raise preparation.error
100
+ if preparation.signature == _build_folder_signature(render_context.build_folder):
101
+ result = preparation.result
102
+ else:
103
+ console.debug("The build folder changed while the environment was being prepared; preparing again.")
104
+ exit_code, _, preparation_temp_file_path = result if result is not None else _run_script(render_context)
31
105
 
32
106
  render_context.conformance_tests_running_context.should_prepare_testing_environment = False
33
107
  render_context.script_execution_history.latest_testing_environment_output_path = preparation_temp_file_path
@@ -5,6 +5,7 @@ import file_utils
5
5
  import plain_spec
6
6
  from memory_management import MemoryManager
7
7
  from plain2code_console import console
8
+ from render_machine.actions import prepare_testing_environment
8
9
  from render_machine.actions.base_action import BaseAction
9
10
  from render_machine.implementation_code_helpers import ImplementationCodeHelpers
10
11
  from render_machine.render_context import RenderContext
@@ -15,6 +16,7 @@ class RenderConformanceTests(BaseAction):
15
16
  SUCCESSFUL_OUTCOME = "conformance_test_rendered"
16
17
 
17
18
  def execute(self, render_context: RenderContext, _previous_action_payload: Any | None):
19
+ prepare_testing_environment.start_in_background(render_context)
18
20
  if self._should_render_conformance_tests(render_context):
19
21
  return self._render_conformance_tests(render_context)
20
22
  else:
@@ -56,6 +56,11 @@ class RunConformanceTests(BaseAction):
56
56
  render_context.script_execution_history.should_update_script_outputs = True
57
57
 
58
58
  if exit_code == 0:
59
+ ctx = render_context.conformance_tests_running_context
60
+ agents = ctx.conformance_fix_agents
61
+ if agents is not None and agents.tested == (ctx.current_testing_module_name, str(ctx.current_testing_frid)):
62
+ # The fix is approved and the tests pass: the fixer and the reviewer are done.
63
+ ctx.conformance_fix_agents = None
59
64
  render_context.memory_manager.journal.record_result(
60
65
  render_context.conformance_tests_running_context.current_testing_module_name,
61
66
  render_context.conformance_tests_running_context.current_testing_frid,