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

Improve test action #42

Merged
merged 8 commits into from
Aug 10, 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- project initialisation
- add the -F/--filter argument
- improve ipynbs detection

[Unreleased]: https://github.com/gepetto/gepetuto/compare/v1.0.0...main
[v1.0.0]: https://github.com/cmake-wheel/cmeel/releases/tag/v1.0.0
2 changes: 1 addition & 1 deletion gepetuto/generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def generate(tp_id: List[int], **kwargs):
def generate_from_id(tp_id: int):
"""Find the corresponding ipynb and folder for a given tp_id."""
folder = Path() / f"tp{tp_id}"
ipynb = next(Path().glob(f"{tp_id}_*.ipynb"))
ipynb = next(Path().glob(f"{tp_id}-*.ipynb"))
generate_ipynb(ipynb, folder)


Expand Down
5 changes: 3 additions & 2 deletions gepetuto/lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
def lint(files, **kwargs):
"""Lint python scripts."""
LOG.info("linting tutorial sources.")
for f in files:
lint_file(f)
for tp_files in files.values():
for tp_file in tp_files:
lint_file(tp_file)
LOG.info("lint done.")


Expand Down
18 changes: 9 additions & 9 deletions gepetuto/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def parse_args(args=None) -> argparse.Namespace:
)
parser.add_argument(
"tp_id",
default=[],
default=get_tp_id(),
type=int,
nargs="*",
help="choose which tp to process. Default to all.",
Expand Down Expand Up @@ -105,14 +105,13 @@ def retrieve_python_interpreter():
return sys.executable


def get_file_list(args):
def get_files(args):
"""Get the list of files we use action on."""
tp_id = args.tp_id or get_tp_id()
file = [Path(f) for f in args.file]
file_list = []
for n in tp_id:
files = {}
for n in args.tp_id:
folder = Path(f"tp{n}")
tp_files = folder.glob("*.py")
tp_files = list(folder.glob("*.py"))
if file != []:
tp_files = [f for f in tp_files if f in file]
if args.filter != []:
Expand All @@ -121,14 +120,15 @@ def get_file_list(args):
for f in tp_files
if any(filter_str in str(f) for filter_str in args.filter)
]
file_list += tp_files
return file_list
if tp_files != []:
files[n] = tp_files
return files


def main():
"""Run command."""
args = parse_args()
files = get_file_list(args)
files = get_files(args)
if args.action == "generate":
generate(**vars(args))
elif args.action == "lint":
Expand Down
33 changes: 21 additions & 12 deletions gepetuto/test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Add "test" action for the "gepetuto" program."""

import logging
from collections import defaultdict
from pathlib import Path
from subprocess import check_call

Expand All @@ -11,23 +12,31 @@ def test(files, **kwargs):
"""Test python scripts."""
python_interpreter = kwargs["python"]
LOG.info("testing tutorial sources.")
tp_id = int(str(files[0])[2]) # get the tp id of the first file
check_ipynb(tp_id, python_interpreter)
for f in files:
LOG.debug(f"Checking {f}")
check_call([python_interpreter, f])
current_tp_id = int(str(f)[2])
if tp_id != current_tp_id:
tp_id = current_tp_id
check_ipynb(current_tp_id, python_interpreter)
for tp_files in files.values():
for tp_file in tp_files:
LOG.debug(f"Checking {tp_file}")
check_call([python_interpreter, tp_file])
ipynbs = get_ipynbs()
for tp_ipynbs in ipynbs.values():
for tp_ipynb in tp_ipynbs:
check_ipynb(tp_ipynb, python_interpreter)
LOG.info("test passed.")


def check_ipynb(tp_number, python_interpreter):
def get_ipynbs():
"""Get the dictionary of ipynbs to test."""
ipynbs = defaultdict(list)
for ipynb in Path().glob("*.ipynb"):
prefix = str(ipynb).split("-")[0]
ipynbs[prefix].append(ipynb)
return ipynbs


def check_ipynb(ipynb, python_interpreter):
"""Check .ipynb files from given tp_number."""
ipynb = next(Path().glob(f"{tp_number}_*.ipynb"))
check_call(["jupyter", "nbconvert", "--to", "script", f"{ipynb}"])
converted_ipynb = next(Path().glob(f"{tp_number}_*.py"))
prefix = str(ipynb).split("-")[0]
converted_ipynb = next(Path().glob(f"{prefix}-*.py"))
LOG.debug(f"Checking temporary file {converted_ipynb}")
check_call([python_interpreter, converted_ipynb])
Path.unlink(converted_ipynb)
File renamed without changes.
File renamed without changes.
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
}
45 changes: 45 additions & 0 deletions tests/appendix_1-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
}
83 changes: 37 additions & 46 deletions tests/test_args.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import unittest
from pathlib import Path

from gepetuto.main import get_file_list, parse_args
from gepetuto.main import get_files, parse_args


class TestGepetutoArguments(unittest.TestCase):
Expand All @@ -11,85 +11,76 @@ class TestGepetutoArguments(unittest.TestCase):
def test_no_arg(self):
"""Check files we work on when no arguments are given."""
arguments = parse_args()
file_list = get_file_list(arguments)
self.assertTrue(Path("tp1/cholesky.py") in file_list)
self.assertTrue(Path("tp1/example_script.py") in file_list)
self.assertTrue(Path("tp2/cholesky.py") in file_list)
self.assertTrue(Path("tp2/another_script.py") in file_list)
file_list = get_files(arguments)
self.assertTrue(Path("tp1/cholesky.py") in file_list[1])
self.assertTrue(Path("tp1/example_script.py") in file_list[1])
self.assertTrue(Path("tp2/cholesky.py") in file_list[2])
self.assertTrue(Path("tp2/another_script.py") in file_list[2])

def test_tp_id_1(self):
"""Check files we work on when we specify tp_id = 1."""
arguments = parse_args(["1"])
file_list = get_file_list(arguments)
self.assertTrue(Path("tp1/cholesky.py") in file_list)
self.assertTrue(Path("tp1/example_script.py") in file_list)
self.assertTrue(Path("tp2/cholesky.py") not in file_list)
self.assertTrue(Path("tp2/another_script.py") not in file_list)
file_list = get_files(arguments)
self.assertTrue(Path("tp1/cholesky.py") in file_list[1])
self.assertTrue(Path("tp1/example_script.py") in file_list[1])
self.assertTrue(2 not in file_list.keys())

def test_file_cholesky(self):
"""Check files we work on when we specify a file with --file."""
arguments = parse_args(["--file", "tp1/cholesky.py"])
file_list = get_file_list(arguments)
self.assertTrue(Path("tp1/cholesky.py") in file_list)
self.assertTrue(Path("tp1/example_script.py") not in file_list)
self.assertTrue(Path("tp2/cholesky.py") not in file_list)
self.assertTrue(Path("tp2/another_script.py") not in file_list)
file_list = get_files(arguments)
self.assertTrue(Path("tp1/cholesky.py") in file_list[1])
self.assertTrue(Path("tp1/example_script.py") not in file_list[1])
self.assertTrue(2 not in file_list.keys())

def test_tp_id_1_file_cholesky(self):
"""Check files we work on when we specify tp_id = 1 and a file in tp1 folder."""
arguments = parse_args(["1", "--file", "tp1/cholesky.py"])
file_list = get_file_list(arguments)
self.assertTrue(Path("tp1/cholesky.py") in file_list)
self.assertTrue(Path("tp1/example_script.py") not in file_list)
self.assertTrue(Path("tp2/cholesky.py") not in file_list)
self.assertTrue(Path("tp2/another_script.py") not in file_list)
file_list = get_files(arguments)
self.assertTrue(Path("tp1/cholesky.py") in file_list[1])
self.assertTrue(Path("tp1/example_script.py") not in file_list[1])
self.assertTrue(2 not in file_list.keys())

def test_no_file_matching(self):
"""Check files we work on when tp_id and --file has no files in common."""
arguments = parse_args(["2", "--file", "tp1/cholesky.py"])
file_list = get_file_list(arguments)
self.assertTrue(Path("tp1/cholesky.py") not in file_list)
self.assertTrue(Path("tp1/example_script.py") not in file_list)
self.assertTrue(Path("tp2/cholesky.py") not in file_list)
self.assertTrue(Path("tp2/another_script.py") not in file_list)
file_list = get_files(arguments)
self.assertTrue(1 not in file_list.keys())
self.assertTrue(2 not in file_list.keys())

def test_filter_cholesky(self):
"""Check files we work on with --filter cholesky argument."""
arguments = parse_args(["--filter", "cholesky"])
file_list = get_file_list(arguments)
self.assertTrue(Path("tp1/cholesky.py") in file_list)
self.assertTrue(Path("tp1/example_script.py") not in file_list)
self.assertTrue(Path("tp2/cholesky.py") in file_list)
self.assertTrue(Path("tp2/another_script.py") not in file_list)
file_list = get_files(arguments)
self.assertTrue(Path("tp1/cholesky.py") in file_list[1])
self.assertTrue(Path("tp1/example_script.py") not in file_list[1])
self.assertTrue(Path("tp2/cholesky.py") in file_list[2])
self.assertTrue(Path("tp2/another_script.py") not in file_list[2])

def test_tp_id_1_filter_cholesky(self):
"""Check files we work on with --filter cholesky argument."""
arguments = parse_args(["1", "--filter", "cholesky"])
file_list = get_file_list(arguments)
self.assertTrue(Path("tp1/cholesky.py") in file_list)
self.assertTrue(Path("tp1/example_script.py") not in file_list)
self.assertTrue(Path("tp2/cholesky.py") not in file_list)
self.assertTrue(Path("tp2/another_script.py") not in file_list)
file_list = get_files(arguments)
self.assertTrue(Path("tp1/cholesky.py") in file_list[1])
self.assertTrue(Path("tp1/example_script.py") not in file_list[1])
self.assertTrue(2 not in file_list.keys())

def test_file_cholesky_filter_cholesky(self):
"""Check files we work on with --filter cholesky and --file on cholesky file."""
arguments = parse_args(["--file", "tp1/cholesky.py", "--filter", "cholesky"])
file_list = get_file_list(arguments)
self.assertTrue(Path("tp1/cholesky.py") in file_list)
self.assertTrue(Path("tp1/example_script.py") not in file_list)
self.assertTrue(Path("tp2/cholesky.py") not in file_list)
self.assertTrue(Path("tp2/another_script.py") not in file_list)
file_list = get_files(arguments)
self.assertTrue(Path("tp1/cholesky.py") in file_list[1])
self.assertTrue(Path("tp1/example_script.py") not in file_list[1])
self.assertTrue(2 not in file_list.keys())

def test_no_file_matching_2(self):
"""Check files we work on when --file and --filter has no files in common."""
arguments = parse_args(
["--file", "tp1/example_script.py", "--filter", "cholesky"],
)
file_list = get_file_list(arguments)
self.assertTrue(Path("tp1/cholesky.py") not in file_list)
self.assertTrue(Path("tp1/example_script.py") not in file_list)
self.assertTrue(Path("tp2/cholesky.py") not in file_list)
self.assertTrue(Path("tp2/another_script.py") not in file_list)
file_list = get_files(arguments)
self.assertTrue(1 not in file_list.keys())
self.assertTrue(2 not in file_list.keys())


if __name__ == "__main__":
Expand Down