Skip to content

Commit

Permalink
Ensure that config build fails on unit collection errors
Browse files Browse the repository at this point in the history
  • Loading branch information
jvansanten committed Dec 13, 2024
1 parent 8011b4a commit 53e3149
Show file tree
Hide file tree
Showing 4 changed files with 321 additions and 299 deletions.
4 changes: 4 additions & 0 deletions ampel/config/collector/UnitConfigCollector.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ def add(self, # type: ignore[override]
)
except Exception:
self.logger.info(f'Units auto-registration has failed for package {el}')
self.has_error = True
continue

# Standart unit definition (ex: ampel.t3.stage.T3AggregatingStager)
Expand Down Expand Up @@ -138,6 +139,8 @@ def add(self, # type: ignore[override]
exc_info=e
)

self.has_error = True

if isinstance(self.logger.handlers[0], AggregatingLoggingHandlerProtocol):
self.logger.handlers[0].aggregate_interval = agg_int

Expand Down Expand Up @@ -172,6 +175,7 @@ def get_mro(self, module_fqn: str, class_name: str) -> None | tuple[int, list[st
except Exception as e:

self.err_fqns.append((module_fqn, e))
self.has_error = True

if self.options.hide_module_not_found_errors and isinstance(e, ModuleNotFoundError):
return None
Expand Down
18 changes: 18 additions & 0 deletions ampel/test/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@
import pytest
import yaml
from pydantic import ValidationError
from pytest_mock import MockerFixture

from ampel.abstract.AbsEventUnit import AbsEventUnit
from ampel.base.BadConfig import BadConfig
from ampel.config.builder.ConfigChecker import ConfigChecker
from ampel.config.builder.ConfigValidator import ConfigValidator
from ampel.config.builder.DisplayOptions import DisplayOptions
from ampel.config.builder.DistConfigBuilder import DistConfigBuilder
from ampel.core.UnitLoader import UnitLoader
from ampel.test.test_JobCommand import run
from ampel.util.mappings import set_by_path
Expand Down Expand Up @@ -146,3 +149,18 @@ def test_validate_config(testing_config, tmpdir, patch, result):
)
== result
)

def test_collect_bad_unit(tmp_path: Path, mocker: MockerFixture) -> None:
cb = DistConfigBuilder(DisplayOptions(verbose=True, debug=True))

bad_unit_config = tmp_path / "unit.yaml"
with bad_unit_config.open("w") as f:
yaml.dump(["flerhherher.Floop"], f)

mocker.patch("ampel.config.builder.DistConfigBuilder.get_files", return_value=[str(bad_unit_config)])

cb.load_distributions()
assert cb.first_pass_config.has_nested_error()

with pytest.raises(ValueError):
cb.build_config(stop_on_errors=2)
Loading

0 comments on commit 53e3149

Please sign in to comment.