diff --git a/.gitignore b/.gitignore index 4e9e45c..4162d20 100644 --- a/.gitignore +++ b/.gitignore @@ -166,4 +166,4 @@ cython_debug/ # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore # and can be added to the global gitignore or merged into this file. For a more nuclear # option (not recommended) you can uncomment the following to ignore the entire idea folder. -#.idea/ +.idea/ diff --git a/README.md b/README.md index d5b6af6..eb181fa 100644 --- a/README.md +++ b/README.md @@ -26,7 +26,7 @@ python==3.11 #### 1. Clone the repo ```bash -git clone git@github.com:seyLu/setup-github-label-cli.git +git clone https://github.com/seyLu/setup-github-label-cli.git ``` #### 2. Install dependencies @@ -186,7 +186,7 @@ ghlabel setup -r "Type: Feature Request, Type: Bug" ```bash # -a [valid json string] # will be parsed as list[dict[str, str]] -ghlabel setup -a "[{'name': 'wontfix', 'color': '##ffffff'}, {'name': 'bug', 'color': '#d73a4a', 'description': 'Something isn't working'}]" +ghlabel setup -a "[{'name': 'wontfix', 'color': '#ffffff'}, {'name': 'bug', 'color': '#d73a4a', 'description': 'Something isn't working'}]" ```
diff --git a/cli.py b/cli.py index 3dc7f06..449bbd9 100644 --- a/cli.py +++ b/cli.py @@ -59,7 +59,12 @@ class RemoveAllChoices(str, Enum): silent = "silent" -app = typer.Typer(add_completion=False) +app = typer.Typer( + add_completion=False, + context_settings={ + "help_option_names": ["-h", "--help"], + }, +) @app.command("setup", help="Add/Remove Github labels from config files.") # type: ignore[misc] diff --git a/ghlabel.md b/ghlabel.md index de368d0..45c2afb 100644 --- a/ghlabel.md +++ b/ghlabel.md @@ -117,5 +117,5 @@ ghlabel setup -r "Type: Feature Request, Type: Bug" ```bash # -a [valid json string] # will be parsed as list[dict[str, str]] -ghlabel setup -a "[{'name': 'wontfix', 'color': '##ffffff'}, {'name': 'bug', 'color': '#d73a4a', 'description': 'Something isn't working'}]" +ghlabel setup -a "[{'name': 'wontfix', 'color': '#ffffff'}, {'name': 'bug', 'color': '#d73a4a', 'description': 'Something isn't working'}]" ``` diff --git a/requirements.txt b/requirements.txt index bc4700c..5d0202f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -7,12 +7,17 @@ distlib==0.3.7 filelock==3.12.4 identify==2.5.30 idna==3.4 +iniconfig==2.0.0 markdown-it-py==3.0.0 mdurl==0.1.2 nodeenv==1.8.0 +packaging==23.1 +pluggy==1.3.0 platformdirs==3.11.0 pre-commit==3.5.0 Pygments==2.16.1 +pytest==7.4.2 +pytest-mock==3.11.1 python-dotenv==1.0.0 PyYAML==6.0.1 requests==2.31.0 diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_dump_label.py b/tests/test_dump_label.py new file mode 100644 index 0000000..dd8002a --- /dev/null +++ b/tests/test_dump_label.py @@ -0,0 +1,40 @@ +import logging +import shutil +from pathlib import Path + +import pytest + +from scripts.dump_label import DumpLabel + +logger = logging.getLogger("root") +logger.setLevel(logging.ERROR) + + +@pytest.fixture +def tmp_dir(tmp_path: str) -> Path: + _tmp_dir: Path = Path(tmp_path, "tmp_labels") + _tmp_dir.mkdir() + yield _tmp_dir + shutil.rmtree(_tmp_dir) + + +def test_init_dir(tmp_dir: Path) -> None: + dir: Path = tmp_dir + + tmp_file: Path = Path(dir, "tmp_file.txt") + tmp_file.touch() + + DumpLabel._init_dir(dir=str(dir)) + + assert len(list(dir.iterdir())) == 0 + + +def test_dump(tmp_dir: Path) -> None: + dir: Path = tmp_dir + new: bool = True + ext: str = "yaml" + + DumpLabel.dump(dir=str(dir), new=new, ext=ext) + + assert Path(dir, "_remove_labels.yaml").exists() + assert Path(dir, "default_labels.yaml").exists() diff --git a/tests/test_setup_github_label.py b/tests/test_setup_github_label.py new file mode 100644 index 0000000..4120fab --- /dev/null +++ b/tests/test_setup_github_label.py @@ -0,0 +1,41 @@ +from typing import Any +from unittest.mock import Mock + +import pytest + +from scripts.setup_github_label import GithubLabel + + +@pytest.fixture +def mock_github_config(mocker: Any) -> Any: + github_config = Mock() + github_config.REPO_OWNER = "owner" + github_config.REPO_NAME = "repo" + github_config.PERSONAL_ACCESS_TOKEN = "token" + return github_config + + +@pytest.fixture +def mock_requests(mocker: Any) -> Any: + mock_requests = Mock() + mock_requests.Response.status_code = 200 + return mock_requests + + +def test_init(mock_github_config: Any, mocker: Any) -> None: + mocker.patch("requests.get", return_value=MockResponse) + + github_label = GithubLabel(github_config=mock_github_config) + assert github_label.url == "https://api.github.com/repos/owner/repo/labels" + assert github_label.headers["Authorization"] == "token" + + +class MockResponse: + def json(self) -> list[dict[str, str]]: + return [ + {"name": "label1", "color": "#FF0000", "description": "Description 1."}, + ] + + @property + def status_code(self) -> int: + return 200