|
| 1 | +import os |
| 2 | +from pathlib import Path |
| 3 | +from unittest import mock |
| 4 | + |
| 5 | +import pytest |
| 6 | +from typer.testing import CliRunner |
| 7 | + |
| 8 | +from taskbadger.cli import app |
| 9 | +from taskbadger.config import write_config, Config |
| 10 | + |
| 11 | +runner = CliRunner() |
| 12 | + |
| 13 | + |
| 14 | +@pytest.fixture |
| 15 | +def test_config(): |
| 16 | + app_dir = Path(__file__).parent / "_test_config" |
| 17 | + with mock.patch("taskbadger.config._get_config_path", return_value=app_dir): |
| 18 | + config = Config(organization_slug="test_org", project_slug="test_project", token="test_token") |
| 19 | + write_config(config) |
| 20 | + yield |
| 21 | + os.remove(app_dir) |
| 22 | + |
| 23 | + |
| 24 | +def test_info_blank(): |
| 25 | + result = runner.invoke(app, ["info"]) |
| 26 | + _check_output(result, "-", "-", "-") |
| 27 | + |
| 28 | + |
| 29 | +def test_info_args(): |
| 30 | + result = runner.invoke(app, ["-o", "org1", "-p", "project1", "info"]) |
| 31 | + _check_output(result, "org1", "project1", "-") |
| 32 | + |
| 33 | + |
| 34 | +@mock.patch.dict(os.environ, { |
| 35 | + "TASKBADGER_ORG": "org2", |
| 36 | + "TASKBADGER_PROJECT": "project2", |
| 37 | + "TASKBADGER_TOKEN": "123", |
| 38 | +}, clear=True) |
| 39 | +def test_info_env(): |
| 40 | + result = runner.invoke(app, ["info"]) |
| 41 | + _check_output(result, "org2", "project2", "123") |
| 42 | + |
| 43 | + |
| 44 | +@mock.patch.dict(os.environ, { |
| 45 | + "TASKBADGER_ORG": "org2", |
| 46 | + "TASKBADGER_PROJECT": "project2", |
| 47 | +}, clear=True) |
| 48 | +def test_info_args_trump_env(): |
| 49 | + result = runner.invoke(app, ["-o", "org1", "-p", "project1", "info"]) |
| 50 | + _check_output(result, "org1", "project1", "-") |
| 51 | + |
| 52 | + |
| 53 | +def test_info_config(test_config): |
| 54 | + result = runner.invoke(app, ["info"]) |
| 55 | + _check_output(result, "test_org", "test_project", "test_token") |
| 56 | + |
| 57 | + |
| 58 | +@mock.patch.dict(os.environ, { |
| 59 | + "TASKBADGER_ORG": "org2", |
| 60 | + "TASKBADGER_PROJECT": "project2", |
| 61 | + "TASKBADGER_TOKEN": "token2", |
| 62 | +}, clear=True) |
| 63 | +def test_info_config_env(test_config): |
| 64 | + result = runner.invoke(app, ["info"]) |
| 65 | + _check_output(result, "org2", "project2", "token2") |
| 66 | + |
| 67 | + |
| 68 | +def test_info_config_args(test_config): |
| 69 | + result = runner.invoke(app, ["-o", "org1", "-p", "project1", "info"]) |
| 70 | + _check_output(result, "org1", "project1", "test_token") |
| 71 | + |
| 72 | + |
| 73 | +@mock.patch.dict(os.environ, { |
| 74 | + "TASKBADGER_ORG": "org2", |
| 75 | + "TASKBADGER_PROJECT": "project2", |
| 76 | + "TASKBADGER_TOKEN": "token2", |
| 77 | +}, clear=True) |
| 78 | +def test_info_config_env_args(test_config): |
| 79 | + result = runner.invoke(app, ["-o", "org1", "-p", "project1", "info"]) |
| 80 | + _check_output(result, "org1", "project1", "token2") |
| 81 | + |
| 82 | + |
| 83 | +def _check_output(result, org, project, token): |
| 84 | + assert result.exit_code == 0 |
| 85 | + assert f"Organization slug: {org}" in result.stdout |
| 86 | + assert f"Project slug: {project}" in result.stdout |
| 87 | + assert f"Auth token: {token}" in result.stdout |
0 commit comments