Skip to content

Commit

Permalink
Merge pull request #37 from TGoddessana/dev
Browse files Browse the repository at this point in the history
Updates to test code writing, mypy and flake8 checks
  • Loading branch information
TGoddessana authored Nov 19, 2023
2 parents b656efa + eb25d68 commit c1bbfdf
Show file tree
Hide file tree
Showing 9 changed files with 129 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .flake8
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[flake8]
select = B,B9,C,D,DAR,E,F,N,RST,S,W
ignore = E203,E501,RST201,RST203,RST301,W503,D100,D101,D102,D104
ignore = E203,E501,RST201,RST203,RST301,W503,D100,D101,D102,D103,D104
max-line-length = 80
max-complexity = 10
per-file-ignores = tests/*:S101
Expand Down
8 changes: 7 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ click = ">=8.0.1"
nox-poetry = "^1.0.3"
nox = "^2023.4.22"

[tool.poetry.extras]
ipython = ["ipython"]
ptpython = ["ptpython"]
bpython = ["bpython"]
all = ["ipython", "ptpython", "bpython"]

[tool.poetry.scripts]
flask-moreshell = "flask_moreshell.__main__:main"

Expand Down Expand Up @@ -72,6 +78,7 @@ lines_after_imports = 2

[tool.mypy]
strict = true
exclude = ["tests"]
warn_unreachable = true
pretty = true
show_column_numbers = true
Expand Down
5 changes: 3 additions & 2 deletions src/flask_moreshell/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
type=click.Choice(["ipython", "bpython", "ptpython", "python"]),
default=None,
)
@with_appcontext
@with_appcontext # type: ignore
def shell(shelltype: str) -> None:
"""Run `flask shell` command with IPython, BPython, PTPython.
Expand All @@ -38,7 +38,8 @@ def shell(shelltype: str) -> None:

def try_load_shell(_shelltype: str) -> None:
shell_class = shells.get(_shelltype)
shell_class().load()
assert shell_class is not None # noqa S101
shell_class().load() # type: ignore

# If the user specifies a shell type, try to load it
if shelltype:
Expand Down
6 changes: 3 additions & 3 deletions src/flask_moreshell/shells/ipython_shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ def get_shell_version(self) -> str:

def load(self) -> None:
config = self._get_config()
config.TerminalInteractiveShell.banner1 = self.get_banner()
config.TerminalInteractiveShell.banner1 = self.get_banner() # type: ignore

IPython.start_ipython(
IPython.start_ipython( # type: ignore
argv=[],
user_ns=current_app.make_shell_context(),
config=config,
Expand All @@ -34,4 +34,4 @@ def load(self) -> None:
def _get_config() -> Any | None:
if "IPYTHON_CONFIG" in current_app.config:
return Config(current_app.config["IPYTHON_CONFIG"])
return load_default_config()
return load_default_config() # type: ignore
18 changes: 18 additions & 0 deletions tests/shells/test_bpython_shell.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from importlib.metadata import version

import pytest

from flask_moreshell.shells import BPythonShell


@pytest.fixture
def bpython_shell():
return BPythonShell()


def test_get_shell_name(bpython_shell):
assert bpython_shell.get_shell_name() == "BPython"


def test_get_shell_version(bpython_shell):
assert bpython_shell.get_shell_version() == version("bpython")
18 changes: 18 additions & 0 deletions tests/shells/test_ipython_shell.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import pytest

from flask_moreshell.shells import IpythonShell


@pytest.fixture
def ipython_shell():
return IpythonShell()


def test_get_shell_name(ipython_shell):
assert ipython_shell.get_shell_name() == "IPython"


def test_get_shell_version(ipython_shell):
from IPython import __version__

assert ipython_shell.get_shell_version() == __version__
37 changes: 37 additions & 0 deletions tests/shells/test_ptpython_shell.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from importlib.metadata import version

import pytest
from flask import Flask

from flask_moreshell.shells import PTPythonShell


@pytest.fixture
def app():
app = Flask(__name__)
app.config["TESTING"] = True
return app


@pytest.fixture
def ptpython_shell():
return PTPythonShell()


def test_get_shell_name(ptpython_shell):
assert ptpython_shell.get_shell_name() == "PTPython"


def test_get_shell_version(ptpython_shell):
assert ptpython_shell.get_shell_version() == version("ptpython")


def test_load(ptpython_shell, monkeypatch, capsys, app):
with app.app_context():
ptpython_shell.load()

stdout = capsys.readouterr().out

assert ptpython_shell.get_shell_name() in stdout
assert ptpython_shell.get_shell_version() in stdout
assert "PTPython" in stdout
41 changes: 35 additions & 6 deletions tests/test_main.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,46 @@
"""Test cases for the __main__ module."""
import pytest
from click.testing import CliRunner
from flask import Flask

from flask_moreshell import shell


@pytest.fixture
def runner() -> CliRunner:
"""Fixture for invoking command-line interfaces."""
def app():
app = Flask(__name__)
app.config["TESTING"] = True
return app


@pytest.fixture
def runner(app):
return CliRunner()


def test_main_succeeds(runner: CliRunner) -> None:
"""It exits with a status code of zero."""
result = runner.invoke(shell)
def test_ipython_shell(runner, app):
with app.app_context():
result = runner.invoke(shell, ["--shelltype", "ipython"])
assert result.exit_code == 0
assert "IPython" in result.output


# def test_bpython_shell(runner, app):
# with app.app_context():
# result = runner.invoke(shell, ["--shelltype", "bpython"])
# assert result.output == ""
# assert result.exit_code == 0
# assert "BPython" in result.output


def test_pypython_shell(runner, app):
with app.app_context():
result = runner.invoke(shell, ["--shelltype", "ptpython"])
assert result.exit_code == 0
assert "PTPython" in result.output


def test_python_shell(runner, app):
with app.app_context():
result = runner.invoke(shell, ["--shelltype", "python"])
assert result.exit_code == 0
assert "Python" in result.output

0 comments on commit c1bbfdf

Please sign in to comment.