|
1 | | -import click.testing |
| 1 | +"""Test cases for the console module.""" |
| 2 | +from unittest.mock import Mock |
| 3 | + |
| 4 | +from click.testing import CliRunner |
2 | 5 | import pytest |
| 6 | +from pytest_mock import MockFixture |
3 | 7 |
|
4 | 8 | from toml_validator import console |
5 | 9 |
|
6 | 10 |
|
7 | 11 | @pytest.fixture |
8 | 12 | def runner(): |
9 | | - return click.testing.CliRunner() |
| 13 | + return CliRunner() |
| 14 | + |
| 15 | + |
| 16 | +@pytest.fixture |
| 17 | +def mock_validation_validate_extension(mocker: MockFixture) -> Mock: |
| 18 | + """Fixture for mocking validation.validate_extension.""" |
| 19 | + return mocker.patch("toml_validator.validation.validate_extension") |
| 20 | + |
| 21 | + |
| 22 | +@pytest.fixture |
| 23 | +def mock_validation_validate_toml_no_error(mocker: MockFixture) -> Mock: |
| 24 | + """Fixture for mocking validation.validate_toml with no errors.""" |
| 25 | + mock = mocker.patch("toml_validator.validation.validate_toml") |
| 26 | + mock.return_value = "" |
| 27 | + return mock |
10 | 28 |
|
11 | 29 |
|
12 | | -# TODO make the two success cases |
13 | | -# (one using mock file and one running a valid file in e2e) |
| 30 | +@pytest.fixture |
| 31 | +def mock_validation_validate_toml_with_error(mocker: MockFixture) -> Mock: |
| 32 | + """Fixture for mocking validation.validate_toml with error.""" |
| 33 | + mock = mocker.patch("toml_validator.validation.validate_toml") |
| 34 | + mock.return_value = "|some error description|" |
| 35 | + return mock |
14 | 36 |
|
15 | 37 |
|
16 | | -def test_main_without_arguments(runner): |
| 38 | +def test_main_without_argument(runner: CliRunner): |
17 | 39 | result = runner.invoke(console.main) |
18 | 40 | assert result.exit_code == 2 |
19 | 41 |
|
20 | 42 |
|
| 43 | +def test_main_with_argument_success( |
| 44 | + runner: CliRunner, |
| 45 | + mock_validation_validate_extension: Mock, |
| 46 | + mock_validation_validate_toml_no_error: Mock, |
| 47 | +): |
| 48 | + with runner.isolated_filesystem(): |
| 49 | + with open("file.toml", "w") as f: |
| 50 | + f.write("content doesnt matter") |
| 51 | + |
| 52 | + result = runner.invoke(console.main, ["file.toml"]) |
| 53 | + assert result.output == ( |
| 54 | + "Reading file file.toml.\n" "No problems found parsing file file.toml!\n" |
| 55 | + ) |
| 56 | + assert result.exit_code == 0 |
| 57 | + |
| 58 | + |
| 59 | +def test_main_with_argument_fail( |
| 60 | + runner: CliRunner, |
| 61 | + mock_validation_validate_extension: Mock, |
| 62 | + mock_validation_validate_toml_with_error: Mock, |
| 63 | +): |
| 64 | + with runner.isolated_filesystem(): |
| 65 | + with open("file.toml", "w") as f: |
| 66 | + f.write("content doesnt matter") |
| 67 | + |
| 68 | + result = runner.invoke(console.main, ["file.toml"]) |
| 69 | + assert result.output == ( |
| 70 | + "Reading file file.toml.\n" "Error(s) found: |some error description|.\n" |
| 71 | + ) |
| 72 | + assert result.exit_code == 0 |
| 73 | + |
| 74 | + |
21 | 75 | @pytest.mark.e2e |
22 | | -def test_main_without_arguments_in_production_env(runner): |
| 76 | +def test_main_without_arguments_in_production_env(runner: CliRunner): |
23 | 77 | result = runner.invoke(console.main) |
24 | 78 | assert result.exit_code == 2 |
0 commit comments