From 1963777937a82b109db88e910ca6e2d2b45f6a33 Mon Sep 17 00:00:00 2001 From: Pengyi Peng Date: Sat, 14 Nov 2026 15:13:06 +0800 Subject: [PATCH] Fix shared CompositePublisher template data binding Signed-off-by: Pengyi Peng --- ccflow/publishers/composite.py | 7 +-- ccflow/tests/publishers/test_composite.py | 59 +++++++++++++++++++++++ 2 files changed, 63 insertions(+), 3 deletions(-) diff --git a/ccflow/publishers/composite.py b/ccflow/publishers/composite.py index 4ddd6f8d..c6fecb22 100644 --- a/ccflow/publishers/composite.py +++ b/ccflow/publishers/composite.py @@ -59,8 +59,9 @@ def _get_publishers(self, data): if not publisher: for try_publisher in self.default_publishers: try: - try_publisher.data = value - publishers[field] = try_publisher.model_copy() + candidate = try_publisher.model_copy() + candidate.data = value + publishers[field] = candidate publishers[field].name = full_name publishers[field].name_params = self.name_params break @@ -76,9 +77,9 @@ def _get_publishers(self, data): else: # If value is the wrong type for the configured publisher, it will raise # User should provide the right type of publisher for a given field. + publisher = publisher.model_copy() publisher.data = value if not publisher.name: - publisher = publisher.model_copy() publisher.name = full_name publisher.name_params = self.name_params publishers[field] = publisher diff --git a/ccflow/tests/publishers/test_composite.py b/ccflow/tests/publishers/test_composite.py index c534ec19..08da0bdf 100644 --- a/ccflow/tests/publishers/test_composite.py +++ b/ccflow/tests/publishers/test_composite.py @@ -125,6 +125,65 @@ def test_root_publisher(self): } self.assertDictEqual(paths, target) + def test_shared_field_and_default_publisher(self): + with tempfile.TemporaryDirectory() as tempdir: + root = Path(tempdir) + shared = GenericFilePublisher(name=str(root / "custom_a"), suffix=".txt") + publisher = CompositePublisher( + name=str(root / "batch"), + data={"a": "ALPHA", "b": "BETA"}, + field_publishers={"a": shared}, + default_publishers=[shared], + ) + + paths = publisher() + + self.assertEqual(paths, {"a": root / "custom_a.txt", "b": root / "batch" / "b.txt"}) + self.assertEqual({field: path.read_text() for field, path in paths.items()}, {"a": "ALPHA", "b": "BETA"}) + + def test_publisher_templates_are_not_modified(self): + for named in (False, True): + with self.subTest(named=named), tempfile.TemporaryDirectory() as tempdir: + root = Path(tempdir) + template = GenericFilePublisher( + name=str(root / "{{tag}}") if named else None, + name_params={"tag": "template"}, + data="SEED", + suffix=".txt", + ) + original_name = template.name + publisher = CompositePublisher( + name=str(root / "batch"), + name_params={"tag": "composite"}, + data={"a": "ALPHA", "b": "BETA"}, + field_publishers={"a": template}, + default_publishers=[template], + ) + + paths = publisher() + + self.assertEqual({field: path.read_text() for field, path in paths.items()}, {"a": "ALPHA", "b": "BETA"}) + self.assertEqual(template.data, "SEED") + self.assertEqual(template.name, original_name) + self.assertEqual(template.name_params, {"tag": "template"}) + + def test_default_validation_fallback_preserves_templates(self): + with tempfile.TemporaryDirectory() as tempdir: + rejected = DummyPublisher(data=(42,)) + accepted = GenericFilePublisher(data="SEED", suffix=".txt") + publisher = CompositePublisher( + name=str(Path(tempdir) / "batch"), + data={"a": "ALPHA", "b": "BETA"}, + default_publishers=[rejected, accepted], + ) + + paths = publisher() + + self.assertEqual({field: path.read_text() for field, path in paths.items()}, {"a": "ALPHA", "b": "BETA"}) + self.assertEqual(rejected.data, (42,)) + self.assertEqual(accepted.data, "SEED") + self.assertIsNone(accepted.name) + def test_skip_root_publisher(self): with tempfile.TemporaryDirectory() as tempdir: os.chdir(tempdir)