Skip to content

7119: data loss with mistyped --basetemp #7170

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

Merged
merged 17 commits into from
Jun 9, 2020
Merged
Show file tree
Hide file tree
Changes from 15 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
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ Pedro Algarvio
Philipp Loose
Pieter Mulder
Piotr Banaszkiewicz
Prashant Anand
Pulkit Goyal
Punyashloka Biswal
Quentin Pradet
Expand Down
1 change: 1 addition & 0 deletions changelog/7119.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Raise exception if ``basetemp`` argument is empty string or . or parent folder of current working directory.
31 changes: 31 additions & 0 deletions src/_pytest/main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
""" core implementation of testing process: init, session, runtest loop. """
import argparse
import fnmatch
import functools
import importlib
Expand Down Expand Up @@ -26,6 +27,7 @@
from _pytest.config import UsageError
from _pytest.fixtures import FixtureManager
from _pytest.outcomes import exit
from _pytest.pathlib import Path
from _pytest.reports import CollectReport
from _pytest.runner import collect_one_node
from _pytest.runner import SetupState
Expand Down Expand Up @@ -167,6 +169,7 @@ def pytest_addoption(parser):
"--basetemp",
dest="basetemp",
default=None,
type=validate_basetemp,
metavar="dir",
help=(
"base temporary directory for this test run."
Expand All @@ -175,6 +178,34 @@ def pytest_addoption(parser):
)


def validate_basetemp(path: str) -> str:
# GH 7119
msg = "basetemp must not be empty, the current working directory or any parent directory of it"

# empty path
if not path:
raise argparse.ArgumentTypeError(msg)

def is_ancestor(base: Path, query: Path) -> bool:
""" return True if query is an ancestor of base, else False."""
if base == query:
return True
for parent in base.parents:
if parent == query:
return True
return False

# check if path is an ancestor of cwd
if is_ancestor(Path.cwd(), Path(path).absolute()):
raise argparse.ArgumentTypeError(msg)

# check symlinks for ancestors
if is_ancestor(Path.cwd().resolve(), Path(path).resolve()):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a fair warning - this should probably be put in line with #5905 via #6523 due to windows aliasing behaviours

raise argparse.ArgumentTypeError(msg)

return path


def wrap_session(
config: Config, doit: Callable[[Config, "Session"], Optional[Union[int, ExitCode]]]
) -> Union[int, ExitCode]:
Expand Down
23 changes: 23 additions & 0 deletions testing/test_main.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import argparse
from typing import Optional

import pytest
from _pytest.config import ExitCode
from _pytest.main import validate_basetemp
from _pytest.pytester import Testdir


Expand Down Expand Up @@ -75,3 +77,24 @@ def pytest_sessionfinish():
assert result.ret == ExitCode.NO_TESTS_COLLECTED
assert result.stdout.lines[-1] == "collected 0 items"
assert result.stderr.lines == ["Exit: exit_pytest_sessionfinish"]


@pytest.mark.parametrize("basetemp", ["foo", "foo/bar"])
def test_validate_basetemp_ok(tmp_path, basetemp, monkeypatch):
monkeypatch.chdir(str(tmp_path))
validate_basetemp(tmp_path / basetemp)


@pytest.mark.parametrize("basetemp", ["", ".", ".."])
def test_validate_basetemp_fails(tmp_path, basetemp, monkeypatch):
monkeypatch.chdir(str(tmp_path))
msg = "basetemp must not be empty, the current working directory or any parent directory of it"
with pytest.raises(argparse.ArgumentTypeError, match=msg):
if basetemp:
basetemp = tmp_path / basetemp
validate_basetemp(basetemp)


def test_validate_basetemp_integration(testdir):
result = testdir.runpytest("--basetemp=.")
result.stderr.fnmatch_lines("*basetemp must not be*")