Skip to content

Commit

Permalink
Test CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
jwodder committed Sep 30, 2023
1 parent faf79c8 commit 7363ddc
Show file tree
Hide file tree
Showing 2 changed files with 172 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/ghtoken/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ def main() -> None:
hub_oauthtoken=hub_oauthtoken,
)
except GHTokenNotFound as e:
sys.exit(str(e))
print(e, file=sys.stderr)
sys.exit(1)
else:
print(token)

Expand Down
170 changes: 170 additions & 0 deletions test/test_cmd.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
from __future__ import annotations
from collections.abc import Callable
from pathlib import Path
import shutil
import sys
from typing import Any
import pytest
from ghtoken.__main__ import main

needs_gh = pytest.mark.skipif(shutil.which("gh") is None, reason="gh not installed")
needs_git = pytest.mark.skipif(shutil.which("git") is None, reason="git not installed")


def make_dotenv(tmp_path: Path, **_: Any) -> None:
(tmp_path / ".env").write_text("GITHUB_TOKEN=dotenv_token\n", encoding="us-ascii")


def make_dotenv_custom(tmp_path: Path, **_: Any) -> None:
(tmp_path / "custom.env").write_text(
"GITHUB_TOKEN=dotenv_custom_token\n", encoding="us-ascii"
)


def make_environ(monkeypatch: pytest.MonkeyPatch, **_: Any) -> None:
monkeypatch.setenv("GITHUB_TOKEN", "environ_token")


def make_gh(tmp_home: Path, **_: Any) -> None:
hosts_file = tmp_home / ".config" / "gh" / "hosts.yml"
hosts_file.parent.mkdir(parents=True, exist_ok=True)
hosts_file.write_text(
"github.com:\n oauth_token: gh_token\n",
encoding="us-ascii",
)


def make_hub(tmp_home: Path, **_: Any) -> None:
(tmp_home / ".config").mkdir(exist_ok=True)
(tmp_home / ".config" / "hub").write_text(
"github.com:\n- oauth_token: hub_token\n",
encoding="us-ascii",
)


def make_hub_oauthtoken(tmp_home: Path, **_: Any) -> None:
(tmp_home / ".gitconfig").write_text(
"[hub]\noauthtoken = hub_oauthtoken_token\n",
encoding="us-ascii",
)


@pytest.mark.parametrize(
"opts,makers,token",
[
([], [], None),
(
[],
[make_dotenv, make_environ, make_gh, make_hub, make_hub_oauthtoken],
"dotenv_token",
),
(
["--env", "custom.env"],
[make_dotenv_custom, make_environ, make_gh, make_hub, make_hub_oauthtoken],
"dotenv_custom_token",
),
(
["--env", "custom.env"],
[make_dotenv, make_environ, make_gh, make_hub, make_hub_oauthtoken],
"environ_token",
),
(
[],
[make_environ, make_gh, make_hub, make_hub_oauthtoken],
"environ_token",
),
(
["--no-dotenv"],
[make_dotenv, make_environ, make_gh, make_hub, make_hub_oauthtoken],
"environ_token",
),
pytest.param(
["--no-dotenv"],
[make_dotenv, make_gh, make_hub, make_hub_oauthtoken], # type: ignore[list-item]
"gh_token",
marks=[needs_gh],
),
pytest.param(
[],
[make_gh, make_hub, make_hub_oauthtoken],
"gh_token",
marks=[needs_gh],
),
(
["--no-dotenv", "--no-environ"],
[make_dotenv, make_hub, make_hub_oauthtoken], # type: ignore[list-item]
"hub_token",
),
(
["--no-dotenv", "--no-environ", "--no-gh"],
[make_dotenv, make_environ, make_gh, make_hub, make_hub_oauthtoken],
"hub_token",
),
(
[],
[make_hub, make_hub_oauthtoken],
"hub_token",
),
pytest.param(
["--no-dotenv", "--no-environ", "--no-gh"],
[make_dotenv, make_environ, make_gh, make_hub_oauthtoken],
"hub_oauthtoken_token",
marks=[needs_git],
),
pytest.param(
["--no-dotenv", "--no-environ", "--no-gh", "--no-hub"],
[make_dotenv, make_environ, make_gh, make_hub, make_hub_oauthtoken],
"hub_oauthtoken_token",
marks=[needs_git],
),
pytest.param(
[],
[make_hub_oauthtoken],
"hub_oauthtoken_token",
marks=[needs_git],
),
(
["--no-dotenv", "--no-environ", "--no-gh", "--no-hub"],
[make_dotenv, make_environ, make_gh, make_hub],
None,
),
(
[
"--no-dotenv",
"--no-environ",
"--no-gh",
"--no-hub",
"--no-hub-oauthtoken",
],
[make_dotenv, make_environ, make_gh, make_hub, make_hub_oauthtoken],
None,
),
],
)
def test_get_github_token(
opts: list[str],
makers: list[Callable],
token: str | None,
capsys: pytest.CaptureFixture[str],
monkeypatch: pytest.MonkeyPatch,
tmp_home: Path,
tmp_path: Path,
) -> None:
monkeypatch.delenv("GH_TOKEN", raising=False)
monkeypatch.delenv("GITHUB_TOKEN", raising=False)
monkeypatch.chdir(tmp_path)
for m in makers:
m(monkeypatch=monkeypatch, tmp_home=tmp_home, tmp_path=tmp_path)
monkeypatch.setattr(sys, "argv", ["ghtoken"] + opts)
if token is not None:
main()
out, err = capsys.readouterr()
assert out == f"{token}\n"
assert err == ""
else:
with pytest.raises(SystemExit) as excinfo:
main()
assert excinfo.value.args == (1,)
out, err = capsys.readouterr()
assert out == ""
assert err == "GitHub access token not found\n"

0 comments on commit 7363ddc

Please sign in to comment.