|
4 | 4 | import novelforge.config as cfg |
5 | 5 |
|
6 | 6 |
|
| 7 | +# --------------------------------------------------------------------------- |
| 8 | +# _resolve_dir helper |
| 9 | +# --------------------------------------------------------------------------- |
| 10 | + |
| 11 | +class TestResolveDir: |
| 12 | + """Unit tests for the _resolve_dir() helper.""" |
| 13 | + |
| 14 | + def test_returns_project_root_relative_path_when_unset(self, monkeypatch): |
| 15 | + monkeypatch.delenv("NF_TEST_DIR", raising=False) |
| 16 | + result = cfg._resolve_dir("NF_TEST_DIR", "my/subdir") |
| 17 | + assert result == str(cfg.PROJECT_ROOT / "my/subdir") |
| 18 | + |
| 19 | + def test_returns_project_root_relative_path_for_relative_env_var(self, monkeypatch): |
| 20 | + monkeypatch.setenv("NF_TEST_DIR", "custom/path") |
| 21 | + result = cfg._resolve_dir("NF_TEST_DIR", "default/path") |
| 22 | + assert result == str(cfg.PROJECT_ROOT / "custom/path") |
| 23 | + |
| 24 | + def test_raises_for_absolute_path(self, monkeypatch): |
| 25 | + monkeypatch.setenv("NF_TEST_DIR", "/etc/passwd") |
| 26 | + with pytest.raises(ValueError, match="NF_TEST_DIR"): |
| 27 | + cfg._resolve_dir("NF_TEST_DIR", "default") |
| 28 | + |
| 29 | + def test_error_message_contains_bad_value(self, monkeypatch): |
| 30 | + monkeypatch.setenv("NF_TEST_DIR", "/absolute/path") |
| 31 | + with pytest.raises(ValueError, match="/absolute/path"): |
| 32 | + cfg._resolve_dir("NF_TEST_DIR", "default") |
| 33 | + |
| 34 | + def test_raises_for_root_path(self, monkeypatch): |
| 35 | + monkeypatch.setenv("NF_TEST_DIR", "/") |
| 36 | + with pytest.raises(ValueError): |
| 37 | + cfg._resolve_dir("NF_TEST_DIR", "default") |
| 38 | + |
| 39 | + def test_relative_default_accepted_when_env_var_unset(self, monkeypatch): |
| 40 | + monkeypatch.delenv("NF_TEST_DIR", raising=False) |
| 41 | + # Should not raise; default is relative |
| 42 | + result = cfg._resolve_dir("NF_TEST_DIR", "logs") |
| 43 | + assert result.endswith("logs") |
| 44 | + |
| 45 | + def test_result_is_string(self, monkeypatch): |
| 46 | + monkeypatch.delenv("NF_TEST_DIR", raising=False) |
| 47 | + result = cfg._resolve_dir("NF_TEST_DIR", "data") |
| 48 | + assert isinstance(result, str) |
| 49 | + |
| 50 | + |
7 | 51 | # --------------------------------------------------------------------------- |
8 | 52 | # get_env_int helper |
9 | 53 | # --------------------------------------------------------------------------- |
|
0 commit comments