Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions ccflow/publishers/composite.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
59 changes: 59 additions & 0 deletions ccflow/tests/publishers/test_composite.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading