mirror of
https://github.com/TauricResearch/TradingAgents.git
synced 2026-09-27 15:02:39 +03:00
test: keep the CLI's saved selections out of the user's home (#1395)
- every test saves and reads CLI selections under its own tmp_path
This commit is contained in:
@@ -45,6 +45,12 @@ def _no_network(request, monkeypatch):
|
|||||||
monkeypatch.setattr(socket.socket, "connect_ex", refuse)
|
monkeypatch.setattr(socket.socket, "connect_ex", refuse)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture(autouse=True)
|
||||||
|
def _own_cli_prefs(tmp_path, monkeypatch):
|
||||||
|
"""The CLI keeps the last run's selections in the user's home; tests keep theirs apart."""
|
||||||
|
monkeypatch.setattr("cli.prefs._PREFS_PATH", tmp_path / "cli_prefs.json")
|
||||||
|
|
||||||
|
|
||||||
_API_KEY_ENV_VARS = (
|
_API_KEY_ENV_VARS = (
|
||||||
"OPENAI_API_KEY",
|
"OPENAI_API_KEY",
|
||||||
"GOOGLE_API_KEY",
|
"GOOGLE_API_KEY",
|
||||||
|
|||||||
+4
-10
@@ -27,12 +27,6 @@ SAVED = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(autouse=True)
|
|
||||||
def _home(tmp_path, monkeypatch):
|
|
||||||
monkeypatch.setattr("cli.prefs._PREFS_PATH", tmp_path / "cli_prefs.json")
|
|
||||||
return tmp_path
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.unit
|
@pytest.mark.unit
|
||||||
def test_round_trip():
|
def test_round_trip():
|
||||||
save_last_run(SAVED)
|
save_last_run(SAVED)
|
||||||
@@ -45,18 +39,18 @@ def test_missing_file_is_not_an_error():
|
|||||||
|
|
||||||
|
|
||||||
@pytest.mark.unit
|
@pytest.mark.unit
|
||||||
def test_a_corrupt_file_degrades_to_no_memory(_home):
|
def test_a_corrupt_file_degrades_to_no_memory(tmp_path):
|
||||||
(_home / "cli_prefs.json").write_text("{not json")
|
(tmp_path / "cli_prefs.json").write_text("{not json")
|
||||||
assert load_last_run() == {}
|
assert load_last_run() == {}
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.unit
|
@pytest.mark.unit
|
||||||
def test_a_half_written_file_cannot_be_observed(_home):
|
def test_a_half_written_file_cannot_be_observed(tmp_path):
|
||||||
"""Two runs finishing together must never leave a torn file behind."""
|
"""Two runs finishing together must never leave a torn file behind."""
|
||||||
save_last_run(SAVED)
|
save_last_run(SAVED)
|
||||||
save_last_run({**SAVED, "research_depth": 5})
|
save_last_run({**SAVED, "research_depth": 5})
|
||||||
assert load_last_run()["research_depth"] == 5
|
assert load_last_run()["research_depth"] == 5
|
||||||
assert list((_home).glob("*.tmp*")) == []
|
assert list(tmp_path.glob("*.tmp*")) == []
|
||||||
|
|
||||||
|
|
||||||
# --- validation against the current choices ---------------------------------
|
# --- validation against the current choices ---------------------------------
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
"""The suite runs the same on any machine: no test reaches the network."""
|
"""The suite runs the same on any machine: no test reaches the network or the user's files."""
|
||||||
|
|
||||||
import socket
|
import socket
|
||||||
|
|
||||||
@@ -15,3 +15,15 @@ def test_a_test_cannot_reach_the_network(connect):
|
|||||||
machine it runs on; conftest refuses the connection instead."""
|
machine it runs on; conftest refuses the connection instead."""
|
||||||
with pytest.raises(OSError, match="reach the network"):
|
with pytest.raises(OSError, match="reach the network"):
|
||||||
connect()
|
connect()
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.unit
|
||||||
|
def test_a_test_saves_cli_selections_to_its_own_directory(tmp_path):
|
||||||
|
"""The CLI remembers the last run's selections in the user's home; a test
|
||||||
|
that runs the selection flow would otherwise overwrite them."""
|
||||||
|
from cli import prefs
|
||||||
|
|
||||||
|
prefs.save_last_run({"analysts": ["market"]})
|
||||||
|
|
||||||
|
assert prefs._PREFS_PATH.is_relative_to(tmp_path)
|
||||||
|
assert prefs._PREFS_PATH.exists()
|
||||||
|
|||||||
Reference in New Issue
Block a user