Skip to content

replace use of scripts with entry_points #311

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Apr 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
python-version: 3.8
- name: Install dependencies
run: |
python -m pip install --upgrade pip
python -m pip install --upgrade pip wheel
pip install tox
- name: Run lint and static type checks
run: tox
Expand Down
10 changes: 5 additions & 5 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
python -m pip install --upgrade pip wheel
pip install tox tox-gh-actions
- name: Test with tox
run: tox
Expand All @@ -52,7 +52,7 @@ jobs:
python-version: 3.8
- name: Install dependencies with only ${{ matrix.dependency }} extra dependency
run: |
python -m pip install --upgrade pip
python -m pip install --upgrade pip wheel
pip install .[${{ matrix.dependency }},test_no_transport]
- name: Test with --${{ matrix.dependency }}-only
run: pytest tests --${{ matrix.dependency }}-only
Expand All @@ -68,9 +68,9 @@ jobs:
python-version: 3.8
- name: Install test dependencies
run: |
python -m pip install --upgrade pip
pip install .[test]
python -m pip install --upgrade pip wheel
pip install -e.[test]
- name: Test with coverage
run: pytest --cov=gql --cov-report=xml tests
run: pytest --cov=gql --cov-report=xml --cov-report=term-missing tests
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v1
2 changes: 0 additions & 2 deletions MANIFEST.in
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ include Makefile

include tox.ini

include scripts/gql-cli

include gql/py.typed

recursive-include tests *.py *.graphql *.cnf *.yaml *.pem
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
.PHONY: clean tests docs

SRC_PYTHON := gql tests scripts/gql-cli docs/code_examples
SRC_PYTHON := gql tests docs/code_examples

dev-setup:
python pip install -e ".[test]"
Expand Down
45 changes: 45 additions & 0 deletions gql/cli.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import asyncio
import json
import logging
import signal as signal_module
import sys
from argparse import ArgumentParser, Namespace, RawDescriptionHelpFormatter
from typing import Any, Dict, Optional
Expand Down Expand Up @@ -407,3 +409,46 @@ async def main(args: Namespace) -> int:
exit_code = 1

return exit_code


def gql_cli() -> None:
"""Synchronously invoke ``main`` with the parsed command line arguments.

Formerly ``scripts/gql-cli``, now registered as an ``entry_point``
"""
# Get arguments from command line
parser = get_parser(with_examples=True)
args = parser.parse_args()

try:
# Create a new asyncio event loop
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)

# Create a gql-cli task with the supplied arguments
main_task = asyncio.ensure_future(main(args), loop=loop)

# Add signal handlers to close gql-cli cleanly on Control-C
for signal_name in ["SIGINT", "SIGTERM", "CTRL_C_EVENT", "CTRL_BREAK_EVENT"]:
signal = getattr(signal_module, signal_name, None)

if signal is None:
continue

try:
loop.add_signal_handler(signal, main_task.cancel)
except NotImplementedError: # pragma: no cover
# not all signals supported on all platforms
pass

# Run the asyncio loop to execute the task
exit_code = 0
try:
exit_code = loop.run_until_complete(main_task)
finally:
loop.close()

# Return with the correct exit code
sys.exit(exit_code)
except KeyboardInterrupt: # pragma: no cover
pass
34 changes: 0 additions & 34 deletions scripts/gql-cli

This file was deleted.

7 changes: 4 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@
"yarl>=1.6,<2.0",
]

scripts = [
"scripts/gql-cli",
console_scripts = [
"gql-cli=gql.cli:gql_cli",
]

tests_requires = [
"parse==1.15.0",
"pytest==6.2.5",
"pytest-asyncio==0.16.0",
"pytest-console-scripts==1.3.1",
"pytest-cov==3.0.0",
"mock==4.0.2",
"vcrpy==4.0.2",
Expand Down Expand Up @@ -106,5 +107,5 @@
include_package_data=True,
zip_safe=False,
platforms="any",
scripts=scripts,
entry_points={"console_scripts": console_scripts},
)
38 changes: 38 additions & 0 deletions tests/test_aiohttp.py
Original file line number Diff line number Diff line change
Expand Up @@ -1016,6 +1016,44 @@ async def handler(request):
assert received_answer == expected_answer


@pytest.mark.asyncio
@pytest.mark.script_launch_mode("subprocess")
async def test_aiohttp_using_cli_ep(
event_loop, aiohttp_server, monkeypatch, script_runner, run_sync_test
):
from aiohttp import web

async def handler(request):
return web.Response(text=query1_server_answer, content_type="application/json")

app = web.Application()
app.router.add_route("POST", "/", handler)
server = await aiohttp_server(app)

url = str(server.make_url("/"))

def test_code():

monkeypatch.setattr("sys.stdin", io.StringIO(query1_str))

ret = script_runner.run(
"gql-cli", url, "--verbose", stdin=io.StringIO(query1_str)
)

assert ret.success

# Check that the result has been printed on stdout
captured_out = str(ret.stdout).strip()

expected_answer = json.loads(query1_server_answer_data)
print(f"Captured: {captured_out}")
received_answer = json.loads(captured_out)

assert received_answer == expected_answer

await run_sync_test(event_loop, server, test_code)


@pytest.mark.asyncio
async def test_aiohttp_using_cli_invalid_param(
event_loop, aiohttp_server, monkeypatch, capsys
Expand Down
10 changes: 10 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import pytest

from gql import __version__
from gql.cli import (
get_execute_args,
get_parser,
Expand Down Expand Up @@ -347,3 +348,12 @@ def test_cli_get_transport_no_protocol(parser):

with pytest.raises(ValueError):
get_transport(args)


def test_cli_ep_version(script_runner):
ret = script_runner.run("gql-cli", "--version")

assert ret.success

assert ret.stdout == f"v{__version__}\n"
assert ret.stderr == ""