Skip to content

Commit

Permalink
Adding test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
IanWoodard committed Jan 2, 2025
1 parent de42d00 commit d31e15c
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 4 deletions.
25 changes: 24 additions & 1 deletion tests/commands/test_down.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,30 @@ def test_down_simple(
assert "Stopping redis" in captured.out.strip()


@mock.patch("devservices.utils.state.State.remove_started_service")
def test_down_no_config_file(
mock_remove_started_service: mock.Mock,
capsys: pytest.CaptureFixture[str],
tmp_path: Path,
) -> None:
os.chdir(tmp_path)

args = Namespace(service_name=None, debug=False)

with pytest.raises(SystemExit):
down(args)

# Capture the printed output
captured = capsys.readouterr()

assert (
f"No devservices configuration found in {tmp_path}/devservices/config.yml. Please specify a service (i.e. `devservices down sentry`) or run the command from a directory with a devservices configuration."
in captured.out.strip()
)

mock_remove_started_service.assert_not_called()


@mock.patch("devservices.utils.docker_compose.subprocess.run")
@mock.patch("devservices.utils.state.State.remove_started_service")
def test_down_error(
Expand Down Expand Up @@ -150,7 +174,6 @@ def test_down_error(

mock_remove_started_service.assert_not_called()

captured = capsys.readouterr()
assert "Stopping clickhouse" not in captured.out.strip()
assert "Stopping redis" not in captured.out.strip()

Expand Down
21 changes: 21 additions & 0 deletions tests/commands/test_list_dependencies.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import os
from argparse import Namespace
from pathlib import Path
from unittest import mock
Expand All @@ -14,6 +15,26 @@
from devservices.utils.services import Service


def test_list_dependencies_no_config_file(
capsys: pytest.CaptureFixture[str],
tmp_path: Path,
) -> None:
os.chdir(tmp_path)

args = Namespace(service_name=None, debug=False)

with pytest.raises(SystemExit):
list_dependencies(args)

# Capture the printed output
captured = capsys.readouterr()

assert (
f"No devservices configuration found in {tmp_path}/devservices/config.yml. Please specify a service (i.e. `devservices list-dependencies sentry`) or run the command from a directory with a devservices configuration."
in captured.out.strip()
)


@mock.patch("devservices.commands.list_dependencies.find_matching_service")
def test_list_dependencies_service_not_found(
mock_find_matching_service: mock.Mock,
Expand Down
21 changes: 21 additions & 0 deletions tests/commands/test_logs.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import os
import subprocess
from argparse import Namespace
from pathlib import Path
Expand Down Expand Up @@ -152,6 +153,26 @@ def test_logs_no_specified_service_success(
assert captured.out.endswith("redis and clickhouse log output\n")


def test_logs_no_config_file(
capsys: pytest.CaptureFixture[str],
tmp_path: Path,
) -> None:
os.chdir(tmp_path)

args = Namespace(service_name=None, debug=False)

with pytest.raises(SystemExit):
logs(args)

# Capture the printed output
captured = capsys.readouterr()

assert (
f"No devservices configuration found in {tmp_path}/devservices/config.yml. Please specify a service (i.e. `devservices logs sentry`) or run the command from a directory with a devservices configuration."
in captured.out.strip()
)


@mock.patch("devservices.commands.logs.find_matching_service")
def test_logs_config_error(
find_matching_service_mock: mock.Mock,
Expand Down
21 changes: 21 additions & 0 deletions tests/commands/test_status.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import os
import subprocess
from argparse import Namespace
from pathlib import Path
Expand All @@ -15,6 +16,26 @@
from devservices.utils.services import Service


def test_status_no_config_file(
capsys: pytest.CaptureFixture[str],
tmp_path: Path,
) -> None:
os.chdir(tmp_path)

args = Namespace(service_name=None, debug=False)

with pytest.raises(SystemExit):
status(args)

# Capture the printed output
captured = capsys.readouterr()

assert (
f"No devservices configuration found in {tmp_path}/devservices/config.yml. Please specify a service (i.e. `devservices status sentry`) or run the command from a directory with a devservices configuration."
in captured.out.strip()
)


@mock.patch("devservices.commands.status._status")
@mock.patch("devservices.commands.status.find_matching_service")
@mock.patch("devservices.commands.status.install_and_verify_dependencies")
Expand Down
29 changes: 26 additions & 3 deletions tests/commands/test_up.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,30 @@ def test_up_dependency_error(
assert "Starting redis" not in captured.out.strip()


@mock.patch("devservices.utils.state.State.update_started_service")
def test_up_no_config_file(
mock_update_started_service: mock.Mock,
capsys: pytest.CaptureFixture[str],
tmp_path: Path,
) -> None:
os.chdir(tmp_path)

args = Namespace(service_name=None, debug=False)

with pytest.raises(SystemExit):
up(args)

# Capture the printed output
captured = capsys.readouterr()

assert (
f"No devservices configuration found in {tmp_path}/devservices/config.yml. Please specify a service (i.e. `devservices up sentry`) or run the command from a directory with a devservices configuration."
in captured.out.strip()
)

mock_update_started_service.assert_not_called()


@mock.patch("devservices.utils.docker_compose.subprocess.run")
@mock.patch("devservices.utils.state.State.update_started_service")
@mock.patch("devservices.commands.up._create_devservices_network")
Expand Down Expand Up @@ -249,9 +273,8 @@ def test_up_error(
env=mock.ANY,
)

captured = capsys.readouterr()
assert "Retrieving dependencies" not in captured.out.strip()
assert "Starting 'example-service' in mode: 'default'" not in captured.out.strip()
assert "Retrieving dependencies" in captured.out.strip()
assert "Starting 'example-service' in mode: 'default'" in captured.out.strip()
assert "Starting clickhouse" not in captured.out.strip()
assert "Starting redis" not in captured.out.strip()

Expand Down

0 comments on commit d31e15c

Please sign in to comment.