Skip to content

Run pytest again on CI #40446

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

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions src/sage/cli/notebook_cmd_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ def test_invalid_notebook_choice():


def test_help():
parser = argparse.ArgumentParser()
parser = argparse.ArgumentParser(prog="sage")
JupyterNotebookCmd.extend_parser(parser)
assert parser.format_usage() == "usage: pytest [-h] [-n [{jupyter,jupyterlab}]]\n"
assert parser.format_usage() == "usage: sage [-h] [-n [{jupyter,jupyterlab}]]\n"
61 changes: 26 additions & 35 deletions src/sage/doctest/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import os
import sys

import pytest

# Note: the DOT_SAGE and SAGE_STARTUP_FILE environment variables have already been set by sage-env
DOT_SAGE = os.environ.get('DOT_SAGE', os.path.join(os.environ.get('HOME'),
'.sage'))
Expand Down Expand Up @@ -155,6 +157,9 @@ def __call__(self, parser, namespace, values, option_string=None):


def main():
from sage.doctest.control import DocTestController
from sage.env import SAGE_SRC

parser = _make_parser()
# custom treatment to separate properly
# one or several file names at the end
Expand Down Expand Up @@ -187,44 +192,30 @@ def main():

os.environ["SAGE_NUM_THREADS"] = "2"

from sage.doctest.control import DocTestController
DC = DocTestController(args, args.filenames)
err = DC.run()

# Issue #33521: Do not run pytest if the pytest configuration is not available.
# This happens when the source tree is not available and SAGE_SRC falls back
# to SAGE_LIB.
from sage.env import SAGE_SRC
if not all(os.path.isfile(os.path.join(SAGE_SRC, f))
for f in ["conftest.py", "tox.ini"]):
return err

try:
exit_code_pytest = 0
import pytest
pytest_options = []
if args.verbose:
pytest_options.append("-v")

# #35999: no filename in arguments defaults to "src"
if not args.filenames:
filenames = [SAGE_SRC]
else:
# #31924: Do not run pytest on individual Python files unless
# they match the pytest file pattern. However, pass names
# of directories. We use 'not os.path.isfile(f)' for this so that
# we do not silently hide typos.
filenames = [f for f in args.filenames
if f.endswith("_test.py") or not os.path.isfile(f)]
if filenames:
print(f"Running pytest on {filenames} with options {pytest_options}")
exit_code_pytest = pytest.main(filenames + pytest_options)
if exit_code_pytest == 5:
# Exit code 5 means there were no test files, pass in this case
exit_code_pytest = 0

except ModuleNotFoundError:
print("pytest is not installed in the venv, skip checking tests that rely on it")
exit_code_pytest = 0
pytest_options = []
if args.verbose:
pytest_options.append("-v")

# #35999: no filename in arguments defaults to "src"
if not args.filenames:
filenames = [SAGE_SRC]
else:
# #31924: Do not run pytest on individual Python files unless
# they match the pytest file pattern. However, pass names
# of directories. We use 'not os.path.isfile(f)' for this so that
# we do not silently hide typos.
filenames = [f for f in args.filenames
if f.endswith("_test.py") or not os.path.isfile(f)]
if filenames:
print(f"Running pytest on {filenames} with options {pytest_options}")
exit_code_pytest = pytest.main(filenames + pytest_options)
if exit_code_pytest == 5:
# Exit code 5 means there were no test files, pass in this case
exit_code_pytest = 0

if err == 0:
return exit_code_pytest
Expand Down
Loading