|
| 1 | +import pytest |
| 2 | + |
| 3 | +from graphsenselib.cli.common import try_load_config |
| 4 | + |
| 5 | + |
| 6 | +class FakeConfig: |
| 7 | + def __init__(self, underlying_file=None): |
| 8 | + self.underlying_file = underlying_file |
| 9 | + self.model_config = { |
| 10 | + "default_files": ["~/.graphsense.yaml"], |
| 11 | + "file_env_var": "GRAPHSENSE_CONFIG", |
| 12 | + } |
| 13 | + self.load_partial_called = False |
| 14 | + self.load_called = False |
| 15 | + |
| 16 | + def load_partial(self, filename=None): |
| 17 | + self.load_partial_called = True |
| 18 | + return True, [] |
| 19 | + |
| 20 | + def load(self, filename=None): |
| 21 | + self.load_called = True |
| 22 | + |
| 23 | + def generate_yaml(self, DEBUG=False): |
| 24 | + return "" |
| 25 | + |
| 26 | + |
| 27 | +def test_try_load_config_web_is_optional(monkeypatch): |
| 28 | + cfg = FakeConfig(underlying_file=None) |
| 29 | + |
| 30 | + monkeypatch.setattr("graphsenselib.cli.common.get_config", lambda: cfg) |
| 31 | + monkeypatch.setattr( |
| 32 | + "graphsenselib.cli.common.sys.argv", |
| 33 | + ["graphsense-cli", "web", "openapi"], |
| 34 | + ) |
| 35 | + |
| 36 | + loaded_cfg, md5hash = try_load_config(filename=None) |
| 37 | + |
| 38 | + assert loaded_cfg is cfg |
| 39 | + assert md5hash == "no-config-file" |
| 40 | + assert cfg.load_partial_called |
| 41 | + assert not cfg.load_called |
| 42 | + |
| 43 | + |
| 44 | +def test_try_load_config_web_with_global_config_option(monkeypatch): |
| 45 | + cfg = FakeConfig(underlying_file=None) |
| 46 | + |
| 47 | + monkeypatch.setattr("graphsenselib.cli.common.get_config", lambda: cfg) |
| 48 | + monkeypatch.setattr( |
| 49 | + "graphsenselib.cli.common.sys.argv", |
| 50 | + [ |
| 51 | + "graphsense-cli", |
| 52 | + "--config-file", |
| 53 | + "/does/not/exist.yaml", |
| 54 | + "web", |
| 55 | + "openapi", |
| 56 | + ], |
| 57 | + ) |
| 58 | + |
| 59 | + loaded_cfg, md5hash = try_load_config(filename="/does/not/exist.yaml") |
| 60 | + |
| 61 | + assert loaded_cfg is cfg |
| 62 | + assert md5hash == "no-config-file" |
| 63 | + assert cfg.load_partial_called |
| 64 | + assert not cfg.load_called |
| 65 | + |
| 66 | + |
| 67 | +def test_try_load_config_non_optional_command_is_strict(monkeypatch): |
| 68 | + cfg = FakeConfig(underlying_file=None) |
| 69 | + |
| 70 | + monkeypatch.setattr("graphsenselib.cli.common.get_config", lambda: cfg) |
| 71 | + monkeypatch.setattr( |
| 72 | + "graphsenselib.cli.common.sys.argv", |
| 73 | + ["graphsense-cli", "rates", "coin-prices"], |
| 74 | + ) |
| 75 | + |
| 76 | + with pytest.raises(SystemExit) as excinfo: |
| 77 | + try_load_config(filename=None) |
| 78 | + |
| 79 | + assert excinfo.value.code == 10 |
| 80 | + assert not cfg.load_partial_called |
| 81 | + assert cfg.load_called |
0 commit comments