|
36 | 36 |
|
37 | 37 |
|
38 | 38 | def _assert_exception_trace(e, trace): |
39 | | - inner = e |
40 | | - for ctx in trace[:-1]: |
41 | | - assert inner.ctx == ctx |
42 | | - inner = inner.error_msg |
43 | | - assert inner.error_msg == trace[-1] |
| 39 | + parts = [] |
| 40 | + while e.ctx is not None: |
| 41 | + parts.append(e.ctx) |
| 42 | + e = e.error_msg |
| 43 | + parts.append(e.error_msg) |
| 44 | + assert tuple(parts) == trace |
44 | 45 |
|
45 | 46 |
|
46 | 47 | def test_ValidationError_simple_str(): |
@@ -582,6 +583,35 @@ def test_load_from_filename_applies_defaults(tmpdir): |
582 | 583 | assert ret == {'key': False} |
583 | 584 |
|
584 | 585 |
|
| 586 | +def test_load_from_filename_custom_display_no_file(tmp_path): |
| 587 | + with pytest.raises(ValidationError) as excinfo: |
| 588 | + load_from_filename( |
| 589 | + tmp_path.joinpath('cfg.json'), |
| 590 | + map_required, |
| 591 | + json.loads, |
| 592 | + display_filename='cfg.json', |
| 593 | + ) |
| 594 | + _assert_exception_trace(excinfo.value.args[0], ('cfg.json is not a file',)) |
| 595 | + |
| 596 | + |
| 597 | +def test_load_from_filename_custom_display_error(tmp_path): |
| 598 | + f = tmp_path.joinpath('cfg.json') |
| 599 | + f.write_text('{}') |
| 600 | + with pytest.raises(ValidationError) as excinfo: |
| 601 | + load_from_filename( |
| 602 | + f, |
| 603 | + map_required, |
| 604 | + json.loads, |
| 605 | + display_filename='cfg.json', |
| 606 | + ) |
| 607 | + expected = ( |
| 608 | + 'File cfg.json', |
| 609 | + 'At foo(key=MISSING)', |
| 610 | + 'Missing required key: key', |
| 611 | + ) |
| 612 | + _assert_exception_trace(excinfo.value.args[0], expected) |
| 613 | + |
| 614 | + |
585 | 615 | conditional_recurse = Map( |
586 | 616 | 'Map', None, |
587 | 617 |
|
|
0 commit comments