|
| 1 | +import os |
| 2 | +import tempfile |
| 3 | +from pathlib import Path |
| 4 | +from unittest.mock import patch |
| 5 | + |
| 6 | +import pytest |
| 7 | + |
| 8 | +from plain2code_arguments import resolve_config_file |
| 9 | +from plain2code_exceptions import AmbiguousConfigFileError |
| 10 | + |
| 11 | + |
| 12 | +@pytest.fixture |
| 13 | +def two_dirs(): |
| 14 | + """Provide two separate temporary directories: one for the plain file, one as CWD.""" |
| 15 | + with tempfile.TemporaryDirectory() as plain_dir: |
| 16 | + with tempfile.TemporaryDirectory() as cwd: |
| 17 | + yield plain_dir, cwd |
| 18 | + |
| 19 | + |
| 20 | +def _plain_file(plain_dir): |
| 21 | + return os.path.join(plain_dir, "module.plain") |
| 22 | + |
| 23 | + |
| 24 | +def test_config_in_plain_file_dir_only(two_dirs): |
| 25 | + plain_dir, cwd = two_dirs |
| 26 | + config = Path(plain_dir) / "config.yaml" |
| 27 | + config.write_text("verbose: true\n") |
| 28 | + |
| 29 | + with patch("os.getcwd", return_value=cwd): |
| 30 | + result = resolve_config_file("config.yaml", _plain_file(plain_dir)) |
| 31 | + |
| 32 | + assert result == os.path.normpath(str(config)) |
| 33 | + |
| 34 | + |
| 35 | +def test_config_in_cwd_only(two_dirs): |
| 36 | + plain_dir, cwd = two_dirs |
| 37 | + config = Path(cwd) / "config.yaml" |
| 38 | + config.write_text("verbose: true\n") |
| 39 | + |
| 40 | + with patch("os.getcwd", return_value=cwd): |
| 41 | + result = resolve_config_file("config.yaml", _plain_file(plain_dir)) |
| 42 | + |
| 43 | + assert result == os.path.normpath(str(config)) |
| 44 | + |
| 45 | + |
| 46 | +def test_config_in_both_locations_raises(two_dirs): |
| 47 | + plain_dir, cwd = two_dirs |
| 48 | + (Path(plain_dir) / "config.yaml").write_text("verbose: true\n") |
| 49 | + (Path(cwd) / "config.yaml").write_text("verbose: false\n") |
| 50 | + |
| 51 | + with patch("os.getcwd", return_value=cwd): |
| 52 | + with pytest.raises(AmbiguousConfigFileError) as exc_info: |
| 53 | + resolve_config_file("config.yaml", _plain_file(plain_dir)) |
| 54 | + |
| 55 | + assert plain_dir in str(exc_info.value) |
| 56 | + assert cwd in str(exc_info.value) |
| 57 | + |
| 58 | + |
| 59 | +def test_config_not_found_returns_none(two_dirs): |
| 60 | + plain_dir, cwd = two_dirs |
| 61 | + |
| 62 | + with patch("os.getcwd", return_value=cwd): |
| 63 | + result = resolve_config_file("config.yaml", _plain_file(plain_dir)) |
| 64 | + |
| 65 | + assert result is None |
| 66 | + |
| 67 | + |
| 68 | +def test_config_same_dir_no_error(): |
| 69 | + """When the plain file and CWD are in the same directory, a single config file is fine.""" |
| 70 | + with tempfile.TemporaryDirectory() as d: |
| 71 | + config = Path(d) / "config.yaml" |
| 72 | + config.write_text("verbose: true\n") |
| 73 | + |
| 74 | + with patch("os.getcwd", return_value=d): |
| 75 | + result = resolve_config_file("config.yaml", os.path.join(d, "module.plain")) |
| 76 | + |
| 77 | + assert result == os.path.normpath(str(config)) |
| 78 | + |
| 79 | + |
| 80 | +def test_custom_config_name_found_in_plain_file_dir(two_dirs): |
| 81 | + """A custom --config-name is also looked up in the two locations.""" |
| 82 | + plain_dir, cwd = two_dirs |
| 83 | + config = Path(plain_dir) / "myconfig.yaml" |
| 84 | + config.write_text("verbose: true\n") |
| 85 | + |
| 86 | + with patch("os.getcwd", return_value=cwd): |
| 87 | + result = resolve_config_file("myconfig.yaml", _plain_file(plain_dir)) |
| 88 | + |
| 89 | + assert result == os.path.normpath(str(config)) |
0 commit comments