Add Simple Server and Coverage action (#2168)
* Add Simple Server and CodeCov action * Remove token * Codecov to tox.ini * fix tox * Set coverage location * Add ignore to codecov * Try glob ignore * Setup CodeClimate * Allow coverage check to run * Change coverage check * Add codeclimate exclusions
This commit is contained in:
parent
f39b8b32f7
commit
7c180376d6
12
.codeclimate.yml
Normal file
12
.codeclimate.yml
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
exclude_patterns:
|
||||||
|
- "sanic/__main__.py"
|
||||||
|
- "sanic/reloader_helpers.py"
|
||||||
|
- "sanic/simple.py"
|
||||||
|
- "sanic/utils.py"
|
||||||
|
- ".github/"
|
||||||
|
- "changelogs/"
|
||||||
|
- "docker/"
|
||||||
|
- "docs/"
|
||||||
|
- "examples/"
|
||||||
|
- "hack/"
|
||||||
|
- "scripts/"
|
|
@ -1,7 +1,12 @@
|
||||||
[run]
|
[run]
|
||||||
branch = True
|
branch = True
|
||||||
source = sanic
|
source = sanic
|
||||||
omit = site-packages, sanic/utils.py, sanic/__main__.py
|
omit =
|
||||||
|
site-packages
|
||||||
|
sanic/__main__.py
|
||||||
|
sanic/reloader_helpers.py
|
||||||
|
sanic/simple.py
|
||||||
|
sanic/utils.py
|
||||||
|
|
||||||
[html]
|
[html]
|
||||||
directory = coverage
|
directory = coverage
|
||||||
|
|
40
.github/workflows/coverage.yml
vendored
Normal file
40
.github/workflows/coverage.yml
vendored
Normal file
|
@ -0,0 +1,40 @@
|
||||||
|
name: Coverage check
|
||||||
|
# on:
|
||||||
|
# push:
|
||||||
|
# branches:
|
||||||
|
# - main
|
||||||
|
# tags:
|
||||||
|
# - "!*" # Do not execute on tags
|
||||||
|
# paths:
|
||||||
|
# - sanic/*
|
||||||
|
# - tests/*
|
||||||
|
# pull_request:
|
||||||
|
# paths:
|
||||||
|
# - "!*.MD"
|
||||||
|
on: [push, pull_request]
|
||||||
|
jobs:
|
||||||
|
test:
|
||||||
|
runs-on: ${{ matrix.os }}
|
||||||
|
strategy:
|
||||||
|
matrix:
|
||||||
|
python-version: [3.9]
|
||||||
|
os: [ubuntu-latest]
|
||||||
|
fail-fast: false
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v2
|
||||||
|
|
||||||
|
- uses: actions/setup-python@v1
|
||||||
|
with:
|
||||||
|
python-version: ${{ matrix.python-version }}
|
||||||
|
|
||||||
|
- name: Install dependencies 🔨
|
||||||
|
run: |
|
||||||
|
python -m pip install --upgrade pip
|
||||||
|
pip install tox
|
||||||
|
- uses: paambaati/codeclimate-action@v2.5.3
|
||||||
|
if: always()
|
||||||
|
env:
|
||||||
|
CC_TEST_REPORTER_ID: ${{ secrets.CODECLIMATE }}
|
||||||
|
with:
|
||||||
|
coverageCommand: tox -e coverage
|
5
.github/workflows/pr-python37.yml
vendored
5
.github/workflows/pr-python37.yml
vendored
|
@ -6,6 +6,9 @@ on:
|
||||||
push:
|
push:
|
||||||
branches:
|
branches:
|
||||||
- main
|
- main
|
||||||
|
paths:
|
||||||
|
- sanic/*
|
||||||
|
- tests/*
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
testPy37:
|
testPy37:
|
||||||
|
@ -14,7 +17,7 @@ jobs:
|
||||||
strategy:
|
strategy:
|
||||||
fail-fast: false
|
fail-fast: false
|
||||||
matrix:
|
matrix:
|
||||||
# os: [ubuntu-latest, macos-latest]
|
# os: [ubuntu-latest, macos-latest]
|
||||||
os: [ubuntu-latest]
|
os: [ubuntu-latest]
|
||||||
config:
|
config:
|
||||||
- { python-version: 3.7, tox-env: py37 }
|
- { python-version: 3.7, tox-env: py37 }
|
||||||
|
|
3
.github/workflows/pr-python38.yml
vendored
3
.github/workflows/pr-python38.yml
vendored
|
@ -6,6 +6,9 @@ on:
|
||||||
push:
|
push:
|
||||||
branches:
|
branches:
|
||||||
- main
|
- main
|
||||||
|
paths:
|
||||||
|
- sanic/*
|
||||||
|
- tests/*
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
testPy38:
|
testPy38:
|
||||||
|
|
3
.github/workflows/pr-python39.yml
vendored
3
.github/workflows/pr-python39.yml
vendored
|
@ -6,6 +6,9 @@ on:
|
||||||
push:
|
push:
|
||||||
branches:
|
branches:
|
||||||
- main
|
- main
|
||||||
|
paths:
|
||||||
|
- sanic/*
|
||||||
|
- tests/*
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
testPy39:
|
testPy39:
|
||||||
|
|
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -6,6 +6,7 @@
|
||||||
.coverage
|
.coverage
|
||||||
.coverage.*
|
.coverage.*
|
||||||
coverage
|
coverage
|
||||||
|
coverage.xml
|
||||||
.tox
|
.tox
|
||||||
settings.py
|
settings.py
|
||||||
.idea/*
|
.idea/*
|
||||||
|
|
3
Makefile
3
Makefile
|
@ -49,6 +49,9 @@ test: clean
|
||||||
test-coverage: clean
|
test-coverage: clean
|
||||||
python setup.py test --pytest-args="--cov sanic --cov-report term --cov-append "
|
python setup.py test --pytest-args="--cov sanic --cov-report term --cov-append "
|
||||||
|
|
||||||
|
view-coverage:
|
||||||
|
sanic ./coverage --simple
|
||||||
|
|
||||||
install:
|
install:
|
||||||
python setup.py install
|
python setup.py install
|
||||||
|
|
||||||
|
|
14
codecov.yml
14
codecov.yml
|
@ -1,14 +0,0 @@
|
||||||
codecov:
|
|
||||||
require_ci_to_pass: no
|
|
||||||
coverage:
|
|
||||||
precision: 3
|
|
||||||
round: nearest
|
|
||||||
status:
|
|
||||||
project:
|
|
||||||
default:
|
|
||||||
target: auto
|
|
||||||
threshold: 0.5%
|
|
||||||
patch:
|
|
||||||
default:
|
|
||||||
target: auto
|
|
||||||
threshold: 0.75%
|
|
|
@ -3,6 +3,7 @@ import sys
|
||||||
|
|
||||||
from argparse import ArgumentParser, RawTextHelpFormatter
|
from argparse import ArgumentParser, RawTextHelpFormatter
|
||||||
from importlib import import_module
|
from importlib import import_module
|
||||||
|
from pathlib import Path
|
||||||
from typing import Any, Dict, Optional
|
from typing import Any, Dict, Optional
|
||||||
|
|
||||||
from sanic_routing import __version__ as __routing_version__ # type: ignore
|
from sanic_routing import __version__ as __routing_version__ # type: ignore
|
||||||
|
@ -11,6 +12,7 @@ from sanic import __version__
|
||||||
from sanic.app import Sanic
|
from sanic.app import Sanic
|
||||||
from sanic.config import BASE_LOGO
|
from sanic.config import BASE_LOGO
|
||||||
from sanic.log import error_logger
|
from sanic.log import error_logger
|
||||||
|
from sanic.simple import create_simple_server
|
||||||
|
|
||||||
|
|
||||||
class SanicArgumentParser(ArgumentParser):
|
class SanicArgumentParser(ArgumentParser):
|
||||||
|
@ -37,13 +39,28 @@ def main():
|
||||||
action="version",
|
action="version",
|
||||||
version=f"Sanic {__version__}; Routing {__routing_version__}",
|
version=f"Sanic {__version__}; Routing {__routing_version__}",
|
||||||
)
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"--factory",
|
||||||
|
action="store_true",
|
||||||
|
help=(
|
||||||
|
"Treat app as an application factory, "
|
||||||
|
"i.e. a () -> <Sanic app> callable"
|
||||||
|
),
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"-s",
|
||||||
|
"--simple",
|
||||||
|
dest="simple",
|
||||||
|
action="store_true",
|
||||||
|
help="Run Sanic as a Simple Server (module arg should be a path)\n ",
|
||||||
|
)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"-H",
|
"-H",
|
||||||
"--host",
|
"--host",
|
||||||
dest="host",
|
dest="host",
|
||||||
type=str,
|
type=str,
|
||||||
default="127.0.0.1",
|
default="127.0.0.1",
|
||||||
help="host address [default 127.0.0.1]",
|
help="Host address [default 127.0.0.1]",
|
||||||
)
|
)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"-p",
|
"-p",
|
||||||
|
@ -51,7 +68,7 @@ def main():
|
||||||
dest="port",
|
dest="port",
|
||||||
type=int,
|
type=int,
|
||||||
default=8000,
|
default=8000,
|
||||||
help="port to serve on [default 8000]",
|
help="Port to serve on [default 8000]",
|
||||||
)
|
)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"-u",
|
"-u",
|
||||||
|
@ -62,7 +79,7 @@ def main():
|
||||||
help="location of unix socket\n ",
|
help="location of unix socket\n ",
|
||||||
)
|
)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"--cert", dest="cert", type=str, help="location of certificate for SSL"
|
"--cert", dest="cert", type=str, help="Location of certificate for SSL"
|
||||||
)
|
)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"--key", dest="key", type=str, help="location of keyfile for SSL\n "
|
"--key", dest="key", type=str, help="location of keyfile for SSL\n "
|
||||||
|
@ -70,14 +87,6 @@ def main():
|
||||||
parser.add_bool_arguments(
|
parser.add_bool_arguments(
|
||||||
"--access-logs", dest="access_log", help="display access logs"
|
"--access-logs", dest="access_log", help="display access logs"
|
||||||
)
|
)
|
||||||
parser.add_argument(
|
|
||||||
"--factory",
|
|
||||||
action="store_true",
|
|
||||||
help=(
|
|
||||||
"Treat app as an application factory, "
|
|
||||||
"i.e. a () -> <Sanic app> callable\n "
|
|
||||||
),
|
|
||||||
)
|
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"-w",
|
"-w",
|
||||||
"--workers",
|
"--workers",
|
||||||
|
@ -103,7 +112,12 @@ def main():
|
||||||
help="Extra directories to watch and reload on changes\n ",
|
help="Extra directories to watch and reload on changes\n ",
|
||||||
)
|
)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"module", help="path to your Sanic app. Example: path.to.server:app"
|
"module",
|
||||||
|
help=(
|
||||||
|
"Path to your Sanic app. Example: path.to.server:app\n"
|
||||||
|
"If running a Simple Server, path to directory to serve. "
|
||||||
|
"Example: ./\n"
|
||||||
|
),
|
||||||
)
|
)
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
@ -112,25 +126,29 @@ def main():
|
||||||
if module_path not in sys.path:
|
if module_path not in sys.path:
|
||||||
sys.path.append(module_path)
|
sys.path.append(module_path)
|
||||||
|
|
||||||
delimiter = ":" if ":" in args.module else "."
|
if args.simple:
|
||||||
module_name, app_name = args.module.rsplit(delimiter, 1)
|
path = Path(args.module)
|
||||||
|
app = create_simple_server(path)
|
||||||
|
else:
|
||||||
|
delimiter = ":" if ":" in args.module else "."
|
||||||
|
module_name, app_name = args.module.rsplit(delimiter, 1)
|
||||||
|
|
||||||
if app_name.endswith("()"):
|
if app_name.endswith("()"):
|
||||||
args.factory = True
|
args.factory = True
|
||||||
app_name = app_name[:-2]
|
app_name = app_name[:-2]
|
||||||
|
|
||||||
module = import_module(module_name)
|
module = import_module(module_name)
|
||||||
app = getattr(module, app_name, None)
|
app = getattr(module, app_name, None)
|
||||||
if args.factory:
|
if args.factory:
|
||||||
app = app()
|
app = app()
|
||||||
|
|
||||||
app_type_name = type(app).__name__
|
app_type_name = type(app).__name__
|
||||||
|
|
||||||
if not isinstance(app, Sanic):
|
if not isinstance(app, Sanic):
|
||||||
raise ValueError(
|
raise ValueError(
|
||||||
f"Module is not a Sanic app, it is a {app_type_name}. "
|
f"Module is not a Sanic app, it is a {app_type_name}. "
|
||||||
f"Perhaps you meant {args.module}.app?"
|
f"Perhaps you meant {args.module}.app?"
|
||||||
)
|
)
|
||||||
if args.cert is not None or args.key is not None:
|
if args.cert is not None or args.key is not None:
|
||||||
ssl: Optional[Dict[str, Any]] = {
|
ssl: Optional[Dict[str, Any]] = {
|
||||||
"cert": args.cert,
|
"cert": args.cert,
|
||||||
|
|
21
sanic/simple.py
Normal file
21
sanic/simple.py
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
from sanic import Sanic
|
||||||
|
from sanic.exceptions import SanicException
|
||||||
|
from sanic.response import redirect
|
||||||
|
|
||||||
|
|
||||||
|
def create_simple_server(directory: Path):
|
||||||
|
if not directory.is_dir():
|
||||||
|
raise SanicException(
|
||||||
|
"Cannot setup Sanic Simple Server without a path to a directory"
|
||||||
|
)
|
||||||
|
|
||||||
|
app = Sanic("SimpleServer")
|
||||||
|
app.static("/", directory, name="main")
|
||||||
|
|
||||||
|
@app.get("/")
|
||||||
|
def index(_):
|
||||||
|
return redirect(app.url_for("main", filename="index.html"))
|
||||||
|
|
||||||
|
return app
|
19
tox.ini
19
tox.ini
|
@ -76,6 +76,23 @@ deps =
|
||||||
docutils
|
docutils
|
||||||
pygments
|
pygments
|
||||||
gunicorn==20.0.4
|
gunicorn==20.0.4
|
||||||
|
|
||||||
commands =
|
commands =
|
||||||
make docs-test
|
make docs-test
|
||||||
|
|
||||||
|
[testenv:coverage]
|
||||||
|
usedevelop = True
|
||||||
|
deps =
|
||||||
|
sanic-testing>=0.6.0
|
||||||
|
coverage==5.3
|
||||||
|
pytest==5.2.1
|
||||||
|
pytest-cov
|
||||||
|
pytest-sanic
|
||||||
|
pytest-sugar
|
||||||
|
pytest-benchmark
|
||||||
|
chardet==3.*
|
||||||
|
beautifulsoup4
|
||||||
|
gunicorn==20.0.4
|
||||||
|
uvicorn
|
||||||
|
websockets>=9.0
|
||||||
|
commands =
|
||||||
|
pytest tests --cov=./sanic --cov-report=xml
|
||||||
|
|
Loading…
Reference in New Issue
Block a user