Files
kevinveenbirkenbachandClaude Opus 5.5 052365e9eb test(web-app-openproject): quote the scratch path and require an unconditional initializer write
Addresses the two CodeRabbit findings on #2147:
- The scratch directory reaches sh -c as $0 and is quoted at the redirect, so a TMPDIR containing a space no longer sends the write to another path.
- A RUN instruction that writes an initializer must be a plain && chain. A || or ; in it fails the check, because the build could then succeed without the write. A new test appends || true and ; true.

🎯 Fix: 93 %
💥 Break: 1 %
💸 Tokens: –
⏱️ Velocity: –
🛡️ Security: 100 %
🏆 Score: 92
🚦 Gate: ✅ 90/5, tokens and velocity <= 100, security 100 %

Created-with: https://s.veen.world/skills
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-18 15:26:04 +00:00

110 lines
3.6 KiB
Python

import ast
import subprocess
import tempfile
import unittest
from pathlib import Path
from utils.cache.files import PROJECT_ROOT, read_text
DOCKERFILE = PROJECT_ROOT / "roles/web-app-openproject/files/Dockerfile"
INITIALIZERS = "/app/config/initializers/"
LOAD_FLAGS = "ActiveRecord::Tasks::DatabaseTasks.structure_load_flags"
CONDITIONAL = ("||", ";")
def _steps(dockerfile: str) -> list[list[str]]:
"""Return every RUN instruction as its list of `&&`-chained shell steps.
Args:
dockerfile: text of the Dockerfile.
"""
instructions = dockerfile.replace("\\\n", " ").splitlines()
return [
[step.strip() for step in line.removeprefix("RUN ").split("&&")]
for line in instructions
if line.startswith("RUN ")
]
def _without_initializers(dockerfile: str) -> str:
"""Return the RUN instructions with every step that writes an initializer dropped.
Args:
dockerfile: text of the Dockerfile.
"""
runs = [
" && ".join(step for step in steps if INITIALIZERS not in step)
for steps in _steps(dockerfile)
]
return "\n".join(f"RUN {run}" for run in runs if run)
def _initializers(dockerfile: str) -> dict[str, str]:
"""
Args:
dockerfile: text of the Dockerfile.
Returns:
The written initializers, keyed by file name.
Raises:
AssertionError: a RUN instruction that writes an initializer is no plain
`&&` chain, so the build could succeed without the write.
"""
runs = [
steps
for steps in _steps(dockerfile)
if any(INITIALIZERS in step for step in steps)
]
for steps in runs:
if any(operator in step for step in steps for operator in CONDITIONAL):
raise AssertionError(f"build may skip the write: RUN {' && '.join(steps)}")
commands = [step for steps in runs for step in steps if INITIALIZERS in step]
with tempfile.TemporaryDirectory() as tmp:
for command in commands:
subprocess.run(
["sh", "-c", command.replace(INITIALIZERS, '"$0"/'), tmp], check=True
)
return {path.name: read_text(str(path)) for path in Path(tmp).iterdir()}
def _load_flags(dockerfile: str) -> list[str] | None:
"""Return the psql flags the image adds to the Rails schema load.
Args:
dockerfile: text of the Dockerfile.
"""
for source in _initializers(dockerfile).values():
target, _, value = source.strip().partition(" = ")
if target == LOAD_FLAGS:
return ast.literal_eval(value)
return None
class TestSchemaLoad(unittest.TestCase):
def test_the_schema_load_runs_in_one_transaction(self):
flags = _load_flags(read_text(str(DOCKERFILE)))
self.assertEqual(flags, ["--single-transaction"])
def test_the_initializer_loads_after_the_upstream_ones(self):
names = list(_initializers(read_text(str(DOCKERFILE))))
self.assertEqual(len(names), 1)
self.assertTrue(names[0].startswith("zz_"))
self.assertTrue(names[0].endswith(".rb"))
def test_negative_control_without_the_initializer_the_load_autocommits(self):
dockerfile = read_text(str(DOCKERFILE))
upstream = _without_initializers(dockerfile)
self.assertNotEqual(_steps(upstream), _steps(dockerfile))
self.assertIsNone(_load_flags(upstream))
def test_a_write_the_build_may_skip_fails_the_check(self):
dockerfile = read_text(str(DOCKERFILE)).rstrip("\n")
for suffix in (" || true", "; true"):
with self.subTest(suffix=suffix), self.assertRaises(AssertionError):
_initializers(f"{dockerfile}{suffix}\n")
if __name__ == "__main__":
unittest.main()