Skip to content

Py.test improperly handles command line options with spaces #1471

@nolar

Description

@nolar

A sample script:

#conftest.py
import pytest


@pytest.hookimpl
def pytest_addoption(parser):
    parser.addoption('--env', action='store', dest='env')
#mytest.py
def test_me(request):
    print(request.config.option.env)

A sample command that fails:

$ touch pytest.ini
$ py.test --env ../111/env.xml --capture=no mytest.py
usage: py.test [options] [file_or_dir] [file_or_dir] [...]
py.test: error: unrecognized arguments: --env mytest.py
  inifile: None
  rootdir: /Users/svasilyev/parent

Where /Users/svasilyev/parent is a parent dir for the sample scripts dir (they are in /Users/svasilyev/parent/pytest-cmdline-sample).

The error happens only when ../111/env.xml actually exists, and it is outside of the current dir with conftest.py & pytest.ini.

The error does NOT happen when:

  • ... the target file does not exists (even if it points to outer dir), or this is not a filename (just by accident, like "no" in --capture no notation).
  • ... the file exists, but it is in the local dir or any subdir: py.test --env ./env.xml --capture=no mytest.py.
  • ... cmdline options are in this order: py.test --capture=no --env ../111/env.xml mytest.py (surprise!)
  • … the same space-notation is used for built-in options (e.g., --junit-xml ../111/junit.xml)

I guess the error is caused by py.test's cmdline pre-parsing and rootdir detection: it treats the standalone cmdline option ../111/env.xml as a reference to a test file, and this is done before --env is actually added to the parser by the plugins.

I understand that plugins cannot be properly loaded before the cmdline is pre-parsed, and thus it is impossible to add all cmdline options before cmdline parsing.

Yet, python's argparse accepts both --opt=val and --opt val notations, so this is the expected behaviour. So, probably, preparsing or rootdir detection should be done a bit more carefully.

PS:

  • Mac OS X 10.11 (does not matter)
  • Python-2.7.10
  • pytest-2.9.1
  • no other packages needed

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions