Skip to content

Exit code 1 when pytest crashes on loading plugin #3106

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
genestack-solomatin opened this issue Jan 11, 2018 · 6 comments
Open

Exit code 1 when pytest crashes on loading plugin #3106

genestack-solomatin opened this issue Jan 11, 2018 · 6 comments
Labels
topic: config related to config handling, argument parsing and config file type: bug problem that needs to be addressed

Comments

@genestack-solomatin
Copy link

Steps to reproduce

execute pytest -p not-installed-plugin

Observed result

Exit code is 1

  ...
  File "/usr/local/lib/python2.7/site-packages/_pytest/config.py", line 435, in import_plugin
    six.reraise(new_exc_type, new_exc, sys.exc_info()[2])
  File "/usr/local/lib/python2.7/site-packages/_pytest/config.py", line 429, in import_plugin
    __import__(importspec)
   ImportError: Error importing plugin "not-installed-plugin": No module named not-installed-plugin

Expected result

Exit code 2 or 4 (https://docs.pytest.org/en/latest/usage.html#possible-exit-codes)

System

platform darwin -- Python 2.7.13, pytest-3.3.2, py-1.5.2, pluggy-0.6.0

@pytestbot
Copy link
Contributor

GitMate.io thinks the contributor most likely able to help you is @nicoddemus.

@pytestbot pytestbot added the type: bug problem that needs to be addressed label Jan 11, 2018
@nicoddemus
Copy link
Member

Hi @genestack-solomatin thanks for writing.

I agree 1 is incorrect, but 2 and 4 doesn't ring correct either to me:

Exit code 2: Test execution was interrupted by the user
Exit code 4: pytest command line usage error

Perhaps we need a new error code?

@nicoddemus nicoddemus added the topic: config related to config handling, argument parsing and config file label Jan 11, 2018
@RonnyPfannschmidt
Copy link
Member

i propose we either generalize the exit code 3 or we introduce a new exit code that indicates "pytest crashed due to a plugin or internal error"

im leaning slightly torwards a new error code

@nicoddemus nicoddemus changed the title Exit code 1 when pytest crushes on loading plugin Exit code 1 when pytest crashes on loading plugin Jan 12, 2018
@feuillemorte
Copy link
Contributor

I can see this code in config.py (433 line):

  except ImportError as e:`
            new_exc_type = ImportError
            new_exc_message = 'Error importing plugin "%s": %s' % (modname, safe_str(e.args[0]))
            new_exc = new_exc_type(new_exc_message)
            six.reraise(new_exc_type, new_exc, sys.exc_info()[2])

So, the solusion maybe replace six.reraise(new_exc_type, new_exc, sys.exc_info()[2]) to this code:

            import traceback
            traceback.print_exception(new_exc_type, new_exc, sys.exc_info()[2])
            sys.exit(EXIT_MODULE_IMPORT_ERROR)

What do you think about it?

@genestack-solomatin
Copy link
Author

Should we return special exit status in case of other errors?

except Exception as e:

except Exception as e:
            import pytest
            if not hasattr(pytest, 'skip') or not isinstance(e, pytest.skip.Exception):
                  raise

@nicoddemus
Copy link
Member

@feuillemorte I don't think that's a good approach: we should not call sys.exit() from within pytest, we should just return the error code from main() instead because people might be using pytest by calling pytest.main(); in that case users expect pytest to return an error code, not raising SystemExit.

Not sure how easy is to change this behavior, because we let the ImportError propagate upwards on purpose so we don't lose the traceback, and Python returns 1 in that case:

$ python -c "raise RuntimeError"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
RuntimeError

$ echo %errorlevel%
1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: config related to config handling, argument parsing and config file type: bug problem that needs to be addressed
Projects
None yet
Development

No branches or pull requests

5 participants