ci(molecule): fail the Molecule result when select's output is lost
An empty select output meant "nothing selected", so an output lost on the way from select to result also passed the required check. select now writes `none` for an empty selection; scenarios runs for neither `none` nor an empty output, and result passes a skipped scenarios only for `none`. The result-logic test runs under sh, as act runs a job container's step without a shell, and a new test parses what select writes to GITHUB_OUTPUT. The fork-guard test compares each job with the literal guard. Assisted-by: Claude Code
This commit is contained in:
1 parent
2d1f142662
commit
479efe1ddc
3 files changed
+61
-23
No files matched your search
@@ -540,12 +540,38 @@ class WorkflowJobsTest(unittest.TestCase):
|
||||
SELECTED=selected,
|
||||
)
|
||||
return subprocess.run(
|
||||
["bash", "-e", "-c", step["run"]],
|
||||
# act runs a job container's step with sh when the step sets no shell
|
||||
["sh", "-e", "-c", step["run"]],
|
||||
check=False,
|
||||
env=env,
|
||||
capture_output=True,
|
||||
).returncode
|
||||
|
||||
def written_output(self, selected):
|
||||
"""The `scenarios` output select's run step writes for that selector output."""
|
||||
step = next(s for s in self.JOBS["select"]["steps"] if s.get("id") == "select")
|
||||
block = step["run"].split("|| exit 1\n", 1)[1]
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
out = pathlib.Path(tmp, "output")
|
||||
subprocess.run(
|
||||
["sh", "-e", "-c", 'selected="$1"\n' + block, "sh", selected],
|
||||
check=True,
|
||||
env=dict(os.environ, GITHUB_OUTPUT=str(out)),
|
||||
)
|
||||
lines = out.read_text().splitlines()
|
||||
# GITHUB_OUTPUT's `name<<delimiter` form: the value is every line up to the delimiter
|
||||
name, delim = lines[0].split("<<", 1)
|
||||
self.assertEqual((name, lines[-1]), ("scenarios", delim))
|
||||
self.assertNotIn(delim, lines[1:-1])
|
||||
return "\n".join(lines[1:-1])
|
||||
|
||||
def test_select_writes_none_for_an_empty_selection(self):
|
||||
self.assertEqual(self.written_output(""), "none")
|
||||
|
||||
def test_select_writes_the_selection_one_directory_per_line(self):
|
||||
selected = "extensions/molecule/image-smoke\nroles/a/molecule/default"
|
||||
self.assertEqual(self.written_output(selected), selected)
|
||||
|
||||
def test_three_jobs_each_on_its_runner(self):
|
||||
self.assertEqual(
|
||||
{name: job["runs-on"] for name, job in self.JOBS.items()},
|
||||
@@ -563,15 +589,25 @@ class WorkflowJobsTest(unittest.TestCase):
|
||||
self.assertNotIn("container", self.JOBS["scenarios"])
|
||||
|
||||
def test_every_job_has_the_fork_guard(self):
|
||||
guard = self.JOBS["select"]["if"]
|
||||
self.assertIn("github.repository == 'mortis/kamino'", guard)
|
||||
guard = (
|
||||
"github.repository == 'mortis/kamino' && "
|
||||
"(github.event_name != 'pull_request' || "
|
||||
"github.event.pull_request.head.repo.full_name == github.repository)"
|
||||
)
|
||||
self.assertEqual(self.JOBS["select"]["if"], guard)
|
||||
for name, job in self.JOBS.items():
|
||||
self.assertTrue(job["if"].endswith(guard), name)
|
||||
|
||||
def test_the_scenarios_run_only_for_a_selection(self):
|
||||
job = self.JOBS["scenarios"]
|
||||
self.assertEqual(job["needs"], "select")
|
||||
self.assertTrue(job["if"].startswith("needs.select.outputs.scenarios != '' &&"))
|
||||
# false for `none` and for an empty or missing output alike
|
||||
self.assertTrue(
|
||||
job["if"].startswith(
|
||||
"needs.select.outputs.scenarios != 'none' && "
|
||||
"needs.select.outputs.scenarios != '' &&"
|
||||
)
|
||||
)
|
||||
self.assertEqual(
|
||||
self.JOBS["select"]["outputs"]["scenarios"],
|
||||
"${{ steps.select.outputs.scenarios }}",
|
||||
@@ -587,11 +623,13 @@ class WorkflowJobsTest(unittest.TestCase):
|
||||
self.assertEqual(job["needs"], ["select", "scenarios"])
|
||||
self.assertTrue(job["if"].startswith("always() &&"))
|
||||
|
||||
def test_the_result_passes_a_run_or_an_empty_selection_only(self):
|
||||
def test_the_result_passes_a_run_or_a_none_selection_only(self):
|
||||
self.assertEqual(
|
||||
self.result("success", "success", "roles/a/molecule/default"), 0
|
||||
)
|
||||
self.assertEqual(self.result("success", "skipped", ""), 0)
|
||||
self.assertEqual(self.result("success", "skipped", "none"), 0)
|
||||
# an empty output was lost, not an empty selection
|
||||
self.assertNotEqual(self.result("success", "skipped", ""), 0)
|
||||
self.assertNotEqual(
|
||||
self.result("success", "skipped", "roles/a/molecule/default"), 0
|
||||
)
|
||||
@@ -601,8 +639,8 @@ class WorkflowJobsTest(unittest.TestCase):
|
||||
self.assertNotEqual(
|
||||
self.result("success", "cancelled", "roles/a/molecule/default"), 0
|
||||
)
|
||||
self.assertNotEqual(self.result("failure", "skipped", ""), 0)
|
||||
self.assertNotEqual(self.result("skipped", "skipped", ""), 0)
|
||||
self.assertNotEqual(self.result("failure", "skipped", "none"), 0)
|
||||
self.assertNotEqual(self.result("skipped", "skipped", "none"), 0)
|
||||
|
||||
|
||||
class ScenarioSequenceTest(unittest.TestCase):
|
||||
|
||||
Reference in new issue
Block a user