fix: preserve missing explicit factor rules path
This commit is contained in:
parent
f7f99395ec
commit
80f03f2a13
|
|
@ -100,6 +100,26 @@ class FactorRulesPathTests(unittest.TestCase):
|
||||||
self.assertEqual(rules, [])
|
self.assertEqual(rules, [])
|
||||||
self.assertIsNone(loaded_path)
|
self.assertIsNone(loaded_path)
|
||||||
|
|
||||||
|
def test_missing_explicit_rule_path_does_not_fall_back_silently(self):
|
||||||
|
with tempfile.TemporaryDirectory() as tmpdir:
|
||||||
|
project_dir = Path(tmpdir)
|
||||||
|
examples_dir = project_dir / "examples"
|
||||||
|
examples_dir.mkdir()
|
||||||
|
default_rule_path = examples_dir / "factor_rules.json"
|
||||||
|
default_rule_path.write_text(json.dumps({"rules": [{"name": "Default"}]}), encoding="utf-8")
|
||||||
|
missing_explicit_path = project_dir / "factor_rules.json"
|
||||||
|
missing_explicit_path.unlink(missing_ok=True)
|
||||||
|
|
||||||
|
rules, loaded_path = load_factor_rules(
|
||||||
|
{
|
||||||
|
"project_dir": str(project_dir),
|
||||||
|
"factor_rules_path": str(missing_explicit_path),
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
self.assertEqual(rules, [])
|
||||||
|
self.assertEqual(Path(loaded_path), missing_explicit_path.resolve())
|
||||||
|
|
||||||
def test_invalid_rules_object_raises_value_error(self):
|
def test_invalid_rules_object_raises_value_error(self):
|
||||||
with tempfile.TemporaryDirectory() as tmpdir:
|
with tempfile.TemporaryDirectory() as tmpdir:
|
||||||
project_dir = Path(tmpdir)
|
project_dir = Path(tmpdir)
|
||||||
|
|
|
||||||
|
|
@ -51,7 +51,18 @@ def _candidate_rule_paths(config: Optional[Dict[str, Any]] = None) -> List[Path]
|
||||||
|
|
||||||
|
|
||||||
def load_factor_rules(config: Optional[Dict[str, Any]] = None) -> Tuple[List[Dict[str, Any]], Optional[str]]:
|
def load_factor_rules(config: Optional[Dict[str, Any]] = None) -> Tuple[List[Dict[str, Any]], Optional[str]]:
|
||||||
|
config = config or {}
|
||||||
|
explicit = config.get("factor_rules_path")
|
||||||
|
explicit_path = None
|
||||||
|
if explicit:
|
||||||
|
try:
|
||||||
|
explicit_path = Path(explicit).resolve()
|
||||||
|
except Exception:
|
||||||
|
explicit_path = None
|
||||||
|
|
||||||
for path in _candidate_rule_paths(config):
|
for path in _candidate_rule_paths(config):
|
||||||
|
if explicit_path and path == explicit_path and not path.exists():
|
||||||
|
return [], str(path)
|
||||||
if not path.exists():
|
if not path.exists():
|
||||||
continue
|
continue
|
||||||
with open(path, "r", encoding="utf-8") as f:
|
with open(path, "r", encoding="utf-8") as f:
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue