|
10 | 10 | import pytest |
11 | 11 |
|
12 | 12 | import dotenv |
13 | | -from dotenv.main import DotEnv |
| 13 | +from dotenv.main import DotEnv, resolve_variables |
14 | 14 |
|
15 | 15 |
|
16 | 16 | def test_set_key_no_file(tmp_path): |
@@ -756,3 +756,50 @@ def test_dotenv_values_empty_value_with_inline_comment(string, expected): |
756 | 756 | result = dotenv.dotenv_values(stream=io.StringIO(string)) |
757 | 757 |
|
758 | 758 | assert result == expected |
| 759 | + |
| 760 | + |
| 761 | +@pytest.mark.parametrize( |
| 762 | + "env,values,override,expected", |
| 763 | + [ |
| 764 | + # Interpolation sees earlier file bindings; file wins with override=True |
| 765 | + ( |
| 766 | + {"A": "env"}, |
| 767 | + [("A", "file"), ("B", "${A}")], |
| 768 | + True, |
| 769 | + {"A": "file", "B": "file"}, |
| 770 | + ), |
| 771 | + # ... while the environment wins with override=False |
| 772 | + ( |
| 773 | + {"A": "env"}, |
| 774 | + [("A", "file"), ("B", "${A}")], |
| 775 | + False, |
| 776 | + {"A": "file", "B": "env"}, |
| 777 | + ), |
| 778 | + # File values stay visible with override=False when the environment |
| 779 | + # does not define the interpolated key |
| 780 | + ( |
| 781 | + {}, |
| 782 | + [("A", "file"), ("B", "${A}")], |
| 783 | + False, |
| 784 | + {"A": "file", "B": "file"}, |
| 785 | + ), |
| 786 | + # Duplicate keys re-resolve against the live, winning source |
| 787 | + ( |
| 788 | + {"A": "env"}, |
| 789 | + [("A", "first"), ("A", "${A}-more")], |
| 790 | + True, |
| 791 | + {"A": "first-more"}, |
| 792 | + ), |
| 793 | + ( |
| 794 | + {"A": "env"}, |
| 795 | + [("A", "first"), ("A", "${A}-more")], |
| 796 | + False, |
| 797 | + {"A": "env-more"}, |
| 798 | + ), |
| 799 | + # Key without value stays None and resolves to "" elsewhere |
| 800 | + ({}, [("A", None), ("B", "${A}")], True, {"A": None, "B": ""}), |
| 801 | + ], |
| 802 | +) |
| 803 | +def test_resolve_variables_override_precedence(env, values, override, expected): |
| 804 | + with mock.patch.dict(os.environ, env, clear=True): |
| 805 | + assert dict(resolve_variables(values, override=override)) == expected |
0 commit comments