Skip to content
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

Unit tests #35

Merged
merged 5 commits into from
Aug 8, 2023
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: 2 additions & 0 deletions .github/workflows/python-app.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,5 @@ jobs:
run: python -m pip install -U pip && python -m pip install .
- name: check with gepetuto
run: cd tests && gepetuto -vva all
- name: run unit tests
run: cd tests && python unit_tests.py
129 changes: 1 addition & 128 deletions gepetuto/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,134 +3,7 @@
This can be started with `python -m gepetuto`, or simply `gepetuto`.
"""

import argparse
import logging
import os
import sys
from pathlib import Path
from subprocess import check_call

from .generate import generate
from .lint import lint
from .test import test

LOG = logging.getLogger("gepetuto")


def parse_args(args=None) -> argparse.Namespace:
"""Check what the user want."""
parser = argparse.ArgumentParser(prog="gepetuto", description="gepetuto tools")
parser.add_argument(
"-v",
"--verbose",
action="count",
default=0,
help="increment verbosity level",
)
parser.add_argument(
"-a",
"--action",
default="generate",
choices=["lint", "test", "generate", "all"],
nargs="?",
help="choose what to do. Default to 'generate'.",
)
parser.add_argument(
"-f",
"--file",
default=[],
type=str,
nargs="*",
help="choose which files to process.",
)
parser.add_argument(
"tp_id",
default=[],
type=int,
nargs="*",
help="choose which tp to process. Default to all.",
)
parser.add_argument(
"-p",
"--python",
default=retrieve_python_interpreter(),
help="choose python interpreter to use.",
)

args = parser.parse_args(args=args)

if args.verbose == 0:
level = os.environ.get("GEPETUTO_LOG_LEVEL", "WARNING")
else:
level = 30 - 10 * args.verbose
logging.basicConfig(level=level)

LOG.debug("parsed arguments: %s", args)

return args


def get_tp_id():
"""Find tp to process."""
tp_id = []
current_tp_id = 0
while True:
folder = Path(f"tp{current_tp_id}")
if folder.exists():
tp_id.append(current_tp_id)
elif current_tp_id != 0:
return tp_id
current_tp_id += 1


def retrieve_python_interpreter():
"""Retrieve installed python interpreter."""
try:
check_call(["python3", "--version"])
return "python3"
except FileNotFoundError:
try:
check_call(["python", "--version"])
return "python"
except FileNotFoundError:
LOG.warn(
"Didn't found 'python3' or 'python' executable, using ",
sys.executable,
)
return sys.executable


def get_file_list(tp_id, file):
"""Get the list of files we use action on."""
file = [Path(f) for f in file]
file_list = []
if tp_id == []:
tp_id = get_tp_id()
for n in tp_id:
folder = Path(f"tp{n}")
if file == []:
file_list += folder.glob("*.py")
else:
file_list += list(filter(lambda f: f in file, folder.glob("*.py")))
return file_list


def main():
"""Run command."""
args = parse_args()
files = get_file_list(args.tp_id, args.file)
if args.action == "generate":
generate(**vars(args))
elif args.action == "lint":
lint(files, **vars(args))
elif args.action == "test":
test(files, **vars(args))
elif args.action == "all":
LOG.debug("no action specified, running all 3.")
lint(files, **vars(args))
test(files, **vars(args))
generate(**vars(args))

from .main import main

if __name__ == "__main__":
main()
6 changes: 3 additions & 3 deletions gepetuto/lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ def lint(files, **kwargs):
"""Lint python scripts."""
LOG.info("linting tutorial sources.")
for f in files:
lint_folder(f)
lint_file(f)
LOG.info("lint done.")


def lint_folder(file):
"""Lint python scripts in folder."""
def lint_file(file):
"""Lint python script."""
LOG.debug(f"Checking {file}")
check_call(["isort", file])
check_call(["black", file])
Expand Down
130 changes: 130 additions & 0 deletions gepetuto/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
"""Gepetuto main program source code."""

import argparse
import logging
import os
import sys
from pathlib import Path
from subprocess import check_call

from .generate import generate
from .lint import lint
from .test import test

LOG = logging.getLogger("gepetuto")


def parse_args(args=None) -> argparse.Namespace:
"""Check what the user want."""
parser = argparse.ArgumentParser(prog="gepetuto", description="gepetuto tools")
parser.add_argument(
"-v",
"--verbose",
action="count",
default=0,
help="increment verbosity level",
)
parser.add_argument(
"-a",
"--action",
default="generate",
choices=["lint", "test", "generate", "all"],
nargs="?",
help="choose what to do. Default to 'generate'.",
)
parser.add_argument(
"-f",
"--file",
default=[],
type=str,
nargs="*",
help="choose which files to process.",
)
parser.add_argument(
"tp_id",
default=[],
type=int,
nargs="*",
help="choose which tp to process. Default to all.",
)
parser.add_argument(
"-p",
"--python",
default=retrieve_python_interpreter(),
help="choose python interpreter to use.",
)

args = parser.parse_args(args=args)

if args.verbose == 0:
level = os.environ.get("GEPETUTO_LOG_LEVEL", "WARNING")
else:
level = 30 - 10 * args.verbose
logging.basicConfig(level=level)

LOG.debug("parsed arguments: %s", args)

return args


def get_tp_id():
"""Find tp to process."""
tp_id = []
current_tp_id = 0
while True:
folder = Path(f"tp{current_tp_id}")
if folder.exists():
tp_id.append(current_tp_id)
elif current_tp_id != 0:
return tp_id
current_tp_id += 1


def retrieve_python_interpreter():
"""Retrieve installed python interpreter."""
try:
check_call(["python3", "--version"])
return "python3"
except FileNotFoundError:
try:
check_call(["python", "--version"])
return "python"
except FileNotFoundError:
LOG.warn(
"Didn't found 'python3' or 'python' executable, using ",
sys.executable,
)
return sys.executable


def get_file_list(tp_id, file):
"""Get the list of files we use action on."""
file = [Path(f) for f in file]
file_list = []
if tp_id == []:
tp_id = get_tp_id()
for n in tp_id:
folder = Path(f"tp{n}")
if file == []:
file_list += folder.glob("*.py")
else:
file_list += list(filter(lambda f: f in file, folder.glob("*.py")))
return file_list


def main():
"""Run command."""
print("here2")
args = parse_args()
files = get_file_list(args.tp_id, args.file)
if args.action == "generate":
generate(**vars(args))
elif args.action == "lint":
lint(files, **vars(args))
elif args.action == "test":
test(files, **vars(args))
elif args.action == "all":
LOG.debug("no action specified, running all 3.")
lint(files, **vars(args))
test(files, **vars(args))
generate(**vars(args))
45 changes: 45 additions & 0 deletions tests/2_other_notebook.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "f2fe3084",
"metadata": {},
"outputs": [],
"source": [
"def hypotenuse(a, b):\n",
" \"\"\"Return hypotenuse of triangle.\"\"\"\n",
" return (a**2 + b**2) ** 0.5\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9bc368d8",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.10"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
6 changes: 6 additions & 0 deletions tests/tp1/cholesky.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
"""Little example program."""


def hypotenuse(a, b):
"""Return hypotenuse of triangle."""
return (a**2 + b**2) ** 0.5
10 changes: 10 additions & 0 deletions tests/tp2/another_script.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
"""Little example program."""


# %jupyter_snippet example_snippet
def hypotenuse(a, b):
"""Return hypotenuse of triangle."""
return (a**2 + b**2) ** 0.5


# %end_jupyter_snippet
6 changes: 6 additions & 0 deletions tests/tp2/cholesky.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
"""Little example program."""


def hypotenuse(a, b):
"""Return hypotenuse of triangle."""
return (a**2 + b**2) ** 0.5
3 changes: 3 additions & 0 deletions tests/tp2/generated/another_script_example_snippet
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
def hypotenuse(a, b):
"""Return hypotenuse of triangle."""
return (a**2 + b**2) ** 0.5
Loading