From 53d00ac9715a7c934a2355a2aedb26c2d7c3131a Mon Sep 17 00:00:00 2001 From: Tomas Barton Date: Tue, 15 Aug 2023 10:09:48 +0200 Subject: [PATCH] Use flit to build the package, add simple test --- .github/workflows/python.yml | 43 ++++++++++++++++++++++ .gitignore | 3 ++ Makefile | 23 +++++++++--- clickhouse_cli/__main__.py | 16 +++++++++ clickhouse_cli/cli.py | 1 + pyproject.toml | 69 ++++++++++++++++++++++++++++++++++++ tests/__init__.py | 0 tests/test_cli.py | 10 ++++++ tox.ini | 25 +++++++++++++ 9 files changed, 186 insertions(+), 4 deletions(-) create mode 100644 .github/workflows/python.yml create mode 100644 clickhouse_cli/__main__.py create mode 100644 pyproject.toml create mode 100644 tests/__init__.py create mode 100644 tests/test_cli.py create mode 100644 tox.ini diff --git a/.github/workflows/python.yml b/.github/workflows/python.yml new file mode 100644 index 0000000..c796f98 --- /dev/null +++ b/.github/workflows/python.yml @@ -0,0 +1,43 @@ +# This workflow will install Python dependencies, run tests and lint with a single version of Python +# For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions + +name: Python application + +on: + push: + branches: + - master + pull_request: + branches: [ master ] + +jobs: + test: + name: Run tests on ${{ matrix.py }} + runs-on: ubuntu-22.04 + strategy: + matrix: + py: + - "3.11" + - "3.10" + - "3.9" + - "3.8" + - "3.7" + - "pypy-3.9" + - "pypy-3.8" + - "pypy-3.7" + + steps: + - uses: actions/checkout@v3 + - name: Set up Python ${{ matrix.py }} + uses: actions/setup-python@v4 + with: + python-version: ${{ matrix.py }} + check-latest: true + - name: Install dependencies + run: | + python -m pip install --upgrade pip flit + flit install --deps=develop + - name: Lint with flake8 + run: flake8 clickhouse_cli --count --max-complexity=31 --show-source --statistics + - name: Test with pytest + run: pytest --cov=clickhouse_cli diff --git a/.gitignore b/.gitignore index 112fea8..7f2e198 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,6 @@ *.egg-info dist build +.coverage +.tox/* +.python-version diff --git a/Makefile b/Makefile index 4688fb1..56eb4ad 100644 --- a/Makefile +++ b/Makefile @@ -1,11 +1,26 @@ -install: clean - python setup.py install +PYTHON=`which python` + +dev: + $(PYTHON) -m pip install --upgrade pip flit + $(PYTHON) -m flit install --deps=develop + +build: clean + $(PYTHON) -m flit build + +install: build + $(PYTHON) -m flit install --deps=production clean: + $(PYTHON) setup.py clean + find . -name '*.pyc' -delete + find . -name '*~' -delete rm -rf clickhouse_cli.egg-info build dist +test: + tox + register: - python setup.py register -r pypi + $(PYTHON) setup.py register -r pypi upload: - python setup.py sdist upload -r pypi + $(PYTHON) setup.py sdist upload -r pypi diff --git a/clickhouse_cli/__main__.py b/clickhouse_cli/__main__.py new file mode 100644 index 0000000..0f845c6 --- /dev/null +++ b/clickhouse_cli/__main__.py @@ -0,0 +1,16 @@ +#!/usr/bin/env python +# +"""Entrypoint module for `python -m clickhouse_cli`. + +Why does this file exist, and why __main__? For more info, read: +- https://www.python.org/dev/peps/pep-0338/ +- https://docs.python.org/2/using/cmdline.html#cmdoption-m +- https://docs.python.org/3/using/cmdline.html#cmdoption-m +""" + +import sys + +from clickhouse_cli.cli import run_cli + +if __name__ == '__main__': + sys.exit(run_cli()) diff --git a/clickhouse_cli/cli.py b/clickhouse_cli/cli.py index 26932c9..c3dce80 100644 --- a/clickhouse_cli/cli.py +++ b/clickhouse_cli/cli.py @@ -580,6 +580,7 @@ def run_cli(host, port, user, password, arg_password, database, settings, query, host, port, user, password, database, settings, format, format_stdin, multiline, stacktrace, vi_mode, cookie ) cli.run(query, data_input) + return 0 if __name__ == '__main__': diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..40269e8 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,69 @@ +[build-system] +requires = ["flit_core >=3.2,<4"] +build-backend = "flit_core.buildapi" + +[project] +name = "clickhouse-cli" +description = "A third-party client for the Clickhouse DBMS server." +authors = [{name = "Igor Hatarist", email = "igor@hatari.st"}] +readme = "README.md" +dynamic = ["version"] +classifiers = [ + "Development Status :: 5 - Production/Stable", + "Intended Audience :: Developers", + "License :: OSI Approved :: MIT License", + "Operating System :: OS Independent", + "Programming Language :: Python", + "Programming Language :: Python :: 3", + "Programming Language :: Python :: 3 :: Only", + "Programming Language :: Python :: 3.7", + "Programming Language :: Python :: 3.8", + "Programming Language :: Python :: 3.9", + "Programming Language :: Python :: 3.10", + "Programming Language :: Python :: 3.11", + "Programming Language :: Python :: Implementation :: PyPy", + "Topic :: Database", + "Topic :: Software Development", +] +requires-python = ">=3.7" +dependencies = [ + "click>=6.6", + "prompt-toolkit>=2.0", + "pygments>=2.1.3", + "requests>=2.11.1", + "sqlparse>=0.2.2", +] + +[project.urls] +Home = "https://github.com/hatarist/clickhouse-cli" +Documentation = "https://github.com/hatarist/clickhouse-cli" +Source = "https://github.com/hatarist/clickhouse-cli" +Tracker = "https://github.com/hatarist/clickhouse-cli/issues" + +[project.scripts] +clickhouse-cli = "clickhouse_cli.cli:run_cli" + +[project.optional-dependencies] +dev = [ + "flake8", + "build", +] +test = [ + "pytest", + "pytest-cov", +] +doc = [ + "sphinx", +] +tox = [ + "virtualenv", + "tox", +] + +[tool.flit.sdist] +include = [ + "tests/*.py", + "LICENSE.txt", + "Makefile", + "tox.ini", +] diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_cli.py b/tests/test_cli.py new file mode 100644 index 0000000..d581944 --- /dev/null +++ b/tests/test_cli.py @@ -0,0 +1,10 @@ +import pytest + +from clickhouse_cli.cli import run_cli + + +def test_main_help(): + # Call with the --help option as a basic sanity check. + with pytest.raises(SystemExit) as exinfo: + run_cli(["--help", ]) + assert exinfo.value.code == 0 diff --git a/tox.ini b/tox.ini new file mode 100644 index 0000000..67031f4 --- /dev/null +++ b/tox.ini @@ -0,0 +1,25 @@ +[tox] +skip_missing_interpreters = True +envlist = + py37 + py38 + py39 + py310 + py311 + flake8 + +[testenv] +deps = + pytest + pytest-cov +commands = + pytest --cov=clickhouse_cli {posargs} + +[testenv:flake8] +deps = + flake8 +commands = + flake8 clickhouse_cli tests setup.py + +[flake8] +extend-ignore = E501