-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
#1642 add rootdir option #3127
#1642 add rootdir option #3127
Changes from 10 commits
d784155
86f0196
4a18d76
a7c39c8
83034bb
a7066ba
0cfa975
503e00f
3a004a4
ace772c
741b571
9f1772e
9366517
3eb6cad
37d836d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -990,11 +990,14 @@ def pytest_load_initial_conftests(self, early_config): | |
|
||
def _initini(self, args): | ||
ns, unknown_args = self._parser.parse_known_and_unknown_args(args, namespace=self.option.copy()) | ||
r = determine_setup(ns.inifilename, ns.file_or_dir + unknown_args, warnfunc=self.warn) | ||
rootdir = ns.rootdir if ns.rootdir else None | ||
r = determine_setup(ns.inifilename, ns.file_or_dir + unknown_args, warnfunc=self.warn, rootdir_cmd_arg=rootdir) | ||
self.rootdir, self.inifile, self.inicfg = r | ||
self._parser.extra_info['rootdir'] = self.rootdir | ||
self._parser.extra_info['inifile'] = self.inifile | ||
self.invocation_dir = py.path.local() | ||
if ns.rootdir: | ||
self.invocation_dir = self.rootdir | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's the reason for this change? I'm not sure we should be changing the |
||
self._parser.addini('addopts', 'extra command line options', 'args') | ||
self._parser.addini('minversion', 'minimally required pytest version') | ||
self._override_ini = ns.override_ini or () | ||
|
@@ -1323,7 +1326,7 @@ def get_dir_from_path(path): | |
] | ||
|
||
|
||
def determine_setup(inifile, args, warnfunc=None): | ||
def determine_setup(inifile, args, warnfunc=None, rootdir_cmd_arg=None): | ||
dirs = get_dirs_from_args(args) | ||
if inifile: | ||
iniconfig = py.iniconfig.IniConfig(inifile) | ||
|
@@ -1346,6 +1349,11 @@ def determine_setup(inifile, args, warnfunc=None): | |
is_fs_root = os.path.splitdrive(str(rootdir))[1] == '/' | ||
if is_fs_root: | ||
rootdir = ancestor | ||
if rootdir_cmd_arg: | ||
rootdir_abs_path = py.path.local(rootdir_cmd_arg) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should call |
||
if not os.path.isdir(str(rootdir_abs_path)): | ||
raise UsageError("Directory '{}' not found. Check your '--rootdir' option.".format(rootdir_abs_path)) | ||
rootdir = rootdir_abs_path | ||
return rootdir, inifile, inicfg or {} | ||
|
||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Add ``--rootdir`` command-line option to override the rules for discovering the root directory. See `customize <https://docs.pytest.org/en/latest/customize.html>`_ in the documentation for details. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -331,3 +331,9 @@ Builtin configuration file options | |
# content of pytest.ini | ||
[pytest] | ||
console_output_style = classic | ||
|
||
.. confval:: rootdir | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I just realized that this is not really a configuration variable (e.g goes into a I think it makes more sense to add a paragraph to the |
||
|
||
Sets a :ref:`rootdir <rootdir>` directory. Directory may be relative or absolute path. | ||
Additionally path may contain environment variables, that will be expanded. | ||
For more information about rootdir please refer to :ref:`rootdir <rootdir>`. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -253,3 +253,28 @@ def pytest_sessionfinish(): | |
""") | ||
res = testdir.runpytest("--collect-only") | ||
assert res.ret == EXIT_NOTESTSCOLLECTED | ||
|
||
|
||
def test_rootdir_option_arg(testdir): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please also add tests for absolute and paths containing environment variables. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. tests on absolute path added (not commites yet) paths containing environment variables not working in tests, but working when I test locally :( tests:
locally directory changed:
Please, help)) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You need to explicitly call When you say "locally" I suppose you mean the command line? In that case is your shell doing the expansion, not pytest. 😉 |
||
rootdir = testdir.mkdir("root") | ||
rootdir.mkdir("tests") | ||
testdir.makepyfile(""" | ||
import os | ||
def test_one(): | ||
assert os.path.isdir('.cache') | ||
""") | ||
|
||
result = testdir.runpytest() | ||
result.stdout.fnmatch_lines(["*AssertionError*"]) | ||
|
||
result = testdir.runpytest("--rootdir=root") | ||
result.stdout.fnmatch_lines(["*1 passed*"]) | ||
|
||
|
||
def test_rootdir_wrong_option_arg(testdir): | ||
rootdir = testdir.mkdir("root") | ||
testsdir = rootdir.mkdir("tests") | ||
testsdir.join("test_one.py").write("def test_one():\n assert 1") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can be written as: testdir.makepyfile("""
def test_one():
assert 1
""") Which is more readable IMO. This call will create a file Actually I don't even think the file or the directories is relevant for the test at all, so we can remove this preparation from this test. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems that I can't create file in root/tests folder by this code. It will created in the testdir folder. But tests run fine, so, fixed =) |
||
|
||
result = testdir.runpytest("--rootdir=wrong_dir") | ||
result.stderr.fnmatch_lines(["*Directory *wrong_dir* not found. Check your '--rootdir' option.*"]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could be written as:
Besides shorter, this doesn't left a
rootdir
local variable to be used by the unaware. 😁