|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
| 3 | +import builtins |
3 | 4 | from argparse import Namespace |
4 | 5 | from pathlib import Path |
5 | 6 | from unittest import mock |
6 | 7 |
|
7 | 8 | from devservices.commands.purge import purge |
| 9 | +from devservices.utils.state import State |
8 | 10 |
|
9 | 11 |
|
10 | | -def test_purge_no_cache(tmp_path: Path) -> None: |
11 | | - with mock.patch( |
12 | | - "devservices.commands.purge.DEVSERVICES_CACHE_DIR", |
13 | | - str(tmp_path / ".devservices-cache"), |
| 12 | +@mock.patch("devservices.commands.purge.stop_all_running_containers") |
| 13 | +def test_purge_not_confirmed( |
| 14 | + mock_stop_all_running_containers: mock.Mock, tmp_path: Path |
| 15 | +) -> None: |
| 16 | + with ( |
| 17 | + mock.patch( |
| 18 | + "devservices.commands.purge.DEVSERVICES_CACHE_DIR", |
| 19 | + str(tmp_path / ".devservices-cache"), |
| 20 | + ), |
| 21 | + mock.patch("devservices.utils.state.STATE_DB_FILE", str(tmp_path / "state")), |
| 22 | + mock.patch.object(builtins, "input", lambda _: "no"), |
14 | 23 | ): |
15 | 24 | args = Namespace() |
16 | 25 | purge(args) |
17 | 26 |
|
| 27 | + mock_stop_all_running_containers.assert_not_called() |
18 | 28 |
|
19 | | -def test_purge_with_cache(tmp_path: Path) -> None: |
20 | | - with mock.patch( |
21 | | - "devservices.commands.purge.DEVSERVICES_CACHE_DIR", |
22 | | - str(tmp_path / ".devservices-cache"), |
| 29 | + |
| 30 | +@mock.patch("devservices.commands.purge.stop_all_running_containers") |
| 31 | +def test_purge_with_cache_and_state_and_no_running_containers_confirmed( |
| 32 | + mock_stop_all_running_containers: mock.Mock, tmp_path: Path |
| 33 | +) -> None: |
| 34 | + with ( |
| 35 | + mock.patch( |
| 36 | + "devservices.commands.purge.DEVSERVICES_CACHE_DIR", |
| 37 | + str(tmp_path / ".devservices-cache"), |
| 38 | + ), |
| 39 | + mock.patch("devservices.utils.state.STATE_DB_FILE", str(tmp_path / "state")), |
| 40 | + mock.patch.object(builtins, "input", lambda _: "yes"), |
| 41 | + mock.patch( |
| 42 | + "devservices.utils.docker.check_docker_daemon_running", return_value=None |
| 43 | + ), |
23 | 44 | ): |
24 | 45 | # Create a cache file to test purging |
25 | 46 | cache_dir = tmp_path / ".devservices-cache" |
26 | 47 | cache_dir.mkdir(parents=True, exist_ok=True) |
27 | 48 | cache_file = tmp_path / ".devservices-cache" / "test.txt" |
28 | 49 | cache_file.write_text("This is a test cache file.") |
29 | 50 |
|
| 51 | + state = State() |
| 52 | + state.add_started_service("test-service", "test-mode") |
| 53 | + |
30 | 54 | assert cache_file.exists() |
| 55 | + assert state.get_started_services() == ["test-service"] |
31 | 56 |
|
32 | 57 | args = Namespace() |
33 | 58 | purge(args) |
34 | 59 |
|
35 | 60 | assert not cache_file.exists() |
| 61 | + assert state.get_started_services() == [] |
| 62 | + |
| 63 | + mock_stop_all_running_containers.assert_called_once() |
| 64 | + |
| 65 | + |
| 66 | +@mock.patch("devservices.commands.purge.stop_all_running_containers") |
| 67 | +def test_purge_with_cache_and_state_and_running_containers_confirmed( |
| 68 | + mock_stop_all_running_containers: mock.Mock, tmp_path: Path |
| 69 | +) -> None: |
| 70 | + with ( |
| 71 | + mock.patch( |
| 72 | + "devservices.commands.purge.DEVSERVICES_CACHE_DIR", |
| 73 | + str(tmp_path / ".devservices-cache"), |
| 74 | + ), |
| 75 | + mock.patch("devservices.utils.state.STATE_DB_FILE", str(tmp_path / "state")), |
| 76 | + mock.patch.object(builtins, "input", lambda _: "yes"), |
| 77 | + mock.patch( |
| 78 | + "devservices.utils.docker.check_docker_daemon_running", return_value=None |
| 79 | + ), |
| 80 | + ): |
| 81 | + # Create a cache file to test purging |
| 82 | + cache_dir = tmp_path / ".devservices-cache" |
| 83 | + cache_dir.mkdir(parents=True, exist_ok=True) |
| 84 | + cache_file = tmp_path / ".devservices-cache" / "test.txt" |
| 85 | + cache_file.write_text("This is a test cache file.") |
| 86 | + |
| 87 | + state = State() |
| 88 | + state.add_started_service("test-service", "test-mode") |
| 89 | + |
| 90 | + assert cache_file.exists() |
| 91 | + assert state.get_started_services() == ["test-service"] |
| 92 | + |
| 93 | + args = Namespace() |
| 94 | + purge(args) |
| 95 | + |
| 96 | + assert not cache_file.exists() |
| 97 | + assert state.get_started_services() == [] |
| 98 | + |
| 99 | + mock_stop_all_running_containers.assert_called_once() |
| 100 | + |
| 101 | + |
| 102 | +@mock.patch("devservices.commands.purge.stop_all_running_containers") |
| 103 | +def test_purge_with_cache_and_state_and_running_containers_not_confirmed( |
| 104 | + mock_stop_all_running_containers: mock.Mock, tmp_path: Path |
| 105 | +) -> None: |
| 106 | + with ( |
| 107 | + mock.patch( |
| 108 | + "devservices.commands.purge.DEVSERVICES_CACHE_DIR", |
| 109 | + str(tmp_path / ".devservices-cache"), |
| 110 | + ), |
| 111 | + mock.patch("devservices.utils.state.STATE_DB_FILE", str(tmp_path / "state")), |
| 112 | + mock.patch.object(builtins, "input", lambda _: "no"), |
| 113 | + mock.patch( |
| 114 | + "devservices.utils.docker.check_docker_daemon_running", return_value=None |
| 115 | + ), |
| 116 | + ): |
| 117 | + # Create a cache file to test purging |
| 118 | + cache_dir = tmp_path / ".devservices-cache" |
| 119 | + cache_dir.mkdir(parents=True, exist_ok=True) |
| 120 | + cache_file = tmp_path / ".devservices-cache" / "test.txt" |
| 121 | + cache_file.write_text("This is a test cache file.") |
| 122 | + |
| 123 | + state = State() |
| 124 | + state.add_started_service("test-service", "test-mode") |
| 125 | + |
| 126 | + args = Namespace() |
| 127 | + purge(args) |
| 128 | + |
| 129 | + assert cache_file.exists() |
| 130 | + assert state.get_started_services() == ["test-service"] |
| 131 | + |
| 132 | + mock_stop_all_running_containers.assert_not_called() |
0 commit comments