From 66e9a794726fd4ba6dc7e28962cdb46b46297e9b Mon Sep 17 00:00:00 2001 From: Dirk Thomas Date: Tue, 10 Oct 2017 07:56:46 -0700 Subject: [PATCH 1/4] get PYTEST_ADDOPTS before calling _initini --- _pytest/config.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/_pytest/config.py b/_pytest/config.py index 2f28bb7bf..1c0cd185d 100644 --- a/_pytest/config.py +++ b/_pytest/config.py @@ -1055,9 +1055,10 @@ class Config(object): "(are you using python -O?)\n") def _preparse(self, args, addopts=True): - self._initini(args) if addopts: args[:] = shlex.split(os.environ.get('PYTEST_ADDOPTS', '')) + args + self._initini(args) + if addopts: args[:] = self.getini("addopts") + args self._checkversion() self._consider_importhook(args) From ed7aa074aa6b17befed57264a2414e633f792109 Mon Sep 17 00:00:00 2001 From: Dirk Thomas Date: Tue, 10 Oct 2017 08:13:07 -0700 Subject: [PATCH 2/4] add changelog file for #2824 --- changelog/2824.feature | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog/2824.feature diff --git a/changelog/2824.feature b/changelog/2824.feature new file mode 100644 index 000000000..690ca939a --- /dev/null +++ b/changelog/2824.feature @@ -0,0 +1 @@ +Allow setting ``file_or_dir``, ``-c``, and ``-o`` in PYTEST_ADDOPTS. From ce8c829945ef30352cc7bec750b91eda85eba8f9 Mon Sep 17 00:00:00 2001 From: Dirk Thomas Date: Tue, 10 Oct 2017 10:03:15 -0700 Subject: [PATCH 3/4] add test for #2824 --- testing/test_config.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/testing/test_config.py b/testing/test_config.py index d049725cd..93b288215 100644 --- a/testing/test_config.py +++ b/testing/test_config.py @@ -843,3 +843,11 @@ class TestOverrideIniArgs(object): rootdir, inifile, inicfg = determine_setup(None, ['a/exist']) assert rootdir == tmpdir assert inifile is None + + def test_addopts_before_initini(self, testdir, tmpdir, monkeypatch): + cache_dir = testdir.tmpdir.join('.custom_cache') + monkeypatch.setenv('PYTEST_ADDOPTS', '-o cache_dir=%s' % cache_dir) + from _pytest.config import get_config + config = get_config() + config._preparse([], addopts=True) + assert config._override_ini == [['cache_dir=%s' % cache_dir]] From 10a3b9118b70ae287b32c218d89a5a5e0c145127 Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Wed, 11 Oct 2017 19:37:17 -0300 Subject: [PATCH 4/4] Use a relative cache_dir in test because of how arguments are parsed on Windows We use shlex to parse command-line arguments and PYTEST_ADDOPTS, so passing a full path with '\' arguments produces incorrect results on Windows Anyway users are advised to use relative paths for portability --- testing/test_config.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/testing/test_config.py b/testing/test_config.py index 93b288215..9881a5d41 100644 --- a/testing/test_config.py +++ b/testing/test_config.py @@ -845,7 +845,7 @@ class TestOverrideIniArgs(object): assert inifile is None def test_addopts_before_initini(self, testdir, tmpdir, monkeypatch): - cache_dir = testdir.tmpdir.join('.custom_cache') + cache_dir = '.custom_cache' monkeypatch.setenv('PYTEST_ADDOPTS', '-o cache_dir=%s' % cache_dir) from _pytest.config import get_config config = get_config()