Skip to content

Commit a498455

Browse files
committed
Add tests for pip's vcs module from pip
WARNING: tests will fail on this commit. Next commit will fix import paths and remove unnecessary stuff for the tests to work. File structure has been modified from pip, below is the mapping of fetchcode to pip. Files were copied without any modifications in this commit. Paths from respective repositories roots: { "tests/conftest.py":"tests/conftest.py", "tests/test_vcs_pip_bazaar.py":"tests/functional/test_vcs_bazaar.py", "tests/test_vcs_pip_git.py":"tests/functional/test_vcs_git.py", "tests/test_vcs_pip_mercurial.py":"tests/functional/test_vcs_mercurial.py", "tests/test_vcs_pip.py_subversion":"tests/functional/test_vcs_subversion.py", "tests/test_vcs_pip.py":"tests/unit/test_vcs.py", "tests/test_vcs_pip_mercurial_unit.py":"tests/unit/test_vcs.py", "tests/lib/__init__.py":"tests/lib/__init_.py", "tests/lib/git_submodule_helpers.py":"tests/lib/git_submodule_helpers.py", "tests/lib/local_repos.py":"tests/lib/local_repos.py", "tests/lib/path.py":"tests/lib/path.py", } It has been agreed with @pombredanne & @TG1999 that history from pip will be rebased on fetchcode by @pombredanne (thanks!). It will be done only for the files that are of concern to fetchcode. I'm leaving this commit without SoB intentionally, as this is not my work, but that of the many pip's authors: https://github.com/pypa/pip/blob/21.2.4/AUTHORS.txt License of pip: MIT (https://pypi.org/project/pip/) add conftest Signed-off-by: Alexander Mazuruk <a.mazuruk@samsung.com> Add dependencies for pip's vcs tests Signed-off-by: Alexander Mazuruk <a.mazuruk@samsung.com> Fix imports in tests find tests -type f -name '*.py' \ -exec sed -i 's/from\ pip._/from\ fetchcode.vcs.pip._/g' {} + \ -exec sed -i 's/import\ pip._/import\ fetchcode.vcs.pip._/g' {} + \ -exec sed -i 's/"pip._/"fetchcode.vcs.pip._/g' {} + \ -exec sed -i "s/'pip._/'fetchcode.vcs.pip._/g" {} + Signed-off-by: Alexander Mazuruk <a.mazuruk@samsung.com> Fix pip tests to run in fetchcode Fix pytest opts Register pytest.markers used by pip's tests Remove unused markers Fix tests to run in CI Signed-off-by: Alexander Mazuruk <a.mazuruk@samsung.com>
1 parent d2ee74f commit a498455

15 files changed

+2156
-2
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,3 +134,6 @@ dmypy.json
134134

135135
# pytype static type analyzer
136136
.pytype/
137+
138+
#pytest test lib
139+
!tests/lib

pyproject.toml

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ norecursedirs = [
3434
"thirdparty",
3535
"tmp",
3636
"tests/data",
37+
"tests/lib",
3738
".eggs",
3839
"src/fetchcode/vcs/pip",
3940
]
@@ -44,7 +45,18 @@ python_classes="Test"
4445
python_functions="test"
4546

4647
addopts = [
47-
"-rfExXw",
48-
"--strict",
48+
"-r fExXw",
49+
"--strict-markers",
4950
"--doctest-modules"
5051
]
52+
53+
markers = [
54+
"network: tests that need network",
55+
"no_auto_tempdir_manager",
56+
"unit: unit tests",
57+
"integration: integration tests",
58+
"bzr: VCS: Bazaar",
59+
"svn: VCS: Subversion",
60+
"mercurial: VCS: Mercurial",
61+
"git: VCS: git",
62+
]

setup.cfg

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ where=src
3636
testing =
3737
# upstream
3838
pytest
39+
pytest-rerunfailures
3940
pytest-xdist
41+
scripttest
4042
docs =
4143
Sphinx>=3.3.1
4244
sphinx-rtd-theme>=0.5.0

tests/conftest.py

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
import os
2+
import shutil
3+
import sys
4+
5+
import pytest
6+
7+
from fetchcode.vcs.pip._internal.utils.temp_dir import global_tempdir_manager
8+
from lib import VCSTestEnvironment
9+
from lib.path import Path
10+
from lib.compat import nullcontext
11+
12+
13+
def pytest_addoption(parser):
14+
parser.addoption(
15+
"--keep-tmpdir",
16+
action="store_true",
17+
default=False,
18+
help="keep temporary test directories",
19+
)
20+
21+
22+
@pytest.fixture(scope="session")
23+
def tmpdir_factory(request, tmpdir_factory):
24+
"""Modified `tmpdir_factory` session fixture
25+
that will automatically cleanup after itself.
26+
"""
27+
yield tmpdir_factory
28+
if not request.config.getoption("--keep-tmpdir"):
29+
shutil.rmtree(
30+
tmpdir_factory.getbasetemp(),
31+
ignore_errors=True,
32+
)
33+
34+
35+
@pytest.fixture
36+
def tmpdir(request, tmpdir):
37+
"""
38+
Return a temporary directory path object which is unique to each test
39+
function invocation, created as a sub directory of the base temporary
40+
directory. The returned object is a ``tests.lib.path.Path`` object.
41+
42+
This uses the built-in tmpdir fixture from pytest itself but modified
43+
to return our typical path object instead of py.path.local as well as
44+
deleting the temporary directories at the end of each test case.
45+
"""
46+
assert tmpdir.isdir()
47+
yield Path(str(tmpdir))
48+
# Clear out the temporary directory after the test has finished using it.
49+
# This should prevent us from needing a multiple gigabyte temporary
50+
# directory while running the tests.
51+
if not request.config.getoption("--keep-tmpdir"):
52+
tmpdir.remove(ignore_errors=True)
53+
54+
55+
@pytest.fixture(autouse=True)
56+
def isolate(tmpdir, monkeypatch):
57+
"""
58+
Isolate our tests so that things like global configuration files and the
59+
like do not affect our test results.
60+
61+
We use an autouse function scoped fixture because we want to ensure that
62+
every test has it's own isolated home directory.
63+
"""
64+
65+
# TODO: Figure out how to isolate from *system* level configuration files
66+
# as well as user level configuration files.
67+
68+
# Create a directory to use as our home location.
69+
home_dir = os.path.join(str(tmpdir), "home")
70+
os.makedirs(home_dir)
71+
72+
# Create a directory to use as a fake root
73+
fake_root = os.path.join(str(tmpdir), "fake-root")
74+
os.makedirs(fake_root)
75+
76+
if sys.platform == "win32":
77+
# Note: this will only take effect in subprocesses...
78+
home_drive, home_path = os.path.splitdrive(home_dir)
79+
monkeypatch.setenv("USERPROFILE", home_dir)
80+
monkeypatch.setenv("HOMEDRIVE", home_drive)
81+
monkeypatch.setenv("HOMEPATH", home_path)
82+
for env_var, sub_path in (
83+
("APPDATA", "AppData/Roaming"),
84+
("LOCALAPPDATA", "AppData/Local"),
85+
):
86+
path = os.path.join(home_dir, *sub_path.split("/"))
87+
monkeypatch.setenv(env_var, path)
88+
os.makedirs(path)
89+
else:
90+
# Set our home directory to our temporary directory, this should force
91+
# all of our relative configuration files to be read from here instead
92+
# of the user's actual $HOME directory.
93+
monkeypatch.setenv("HOME", home_dir)
94+
95+
# Configure git, because without an author name/email git will complain
96+
# and cause test failures.
97+
monkeypatch.setenv("GIT_CONFIG_NOSYSTEM", "1")
98+
monkeypatch.setenv("GIT_AUTHOR_NAME", "pip")
99+
monkeypatch.setenv("GIT_AUTHOR_EMAIL", "distutils-sig@python.org")
100+
101+
# FIXME: Windows...
102+
#os.makedirs(os.path.join(home_dir, ".gitconfig", "git"))
103+
with open(os.path.join(home_dir, ".gitconfig"), "wb") as fp:
104+
fp.write(b"[user]\n\tname = pip\n\temail = distutils-sig@python.org\n")
105+
106+
107+
@pytest.fixture(autouse=True)
108+
def scoped_global_tempdir_manager(request):
109+
"""Make unit tests with globally-managed tempdirs easier
110+
111+
Each test function gets its own individual scope for globally-managed
112+
temporary directories in the application.
113+
"""
114+
if "no_auto_tempdir_manager" in request.keywords:
115+
ctx = nullcontext
116+
else:
117+
ctx = global_tempdir_manager
118+
119+
with ctx():
120+
yield
121+
122+
123+
@pytest.fixture(scope="session")
124+
def script_factory():
125+
def factory(tmpdir):
126+
return VCSTestEnvironment(
127+
tmpdir,
128+
ignore_hidden=False,
129+
start_clear=False,
130+
capture_temp=True,
131+
assert_no_temp=True)
132+
return factory
133+
134+
135+
@pytest.fixture
136+
#def script(tmpdir, virtualenv, script_factory):
137+
def script(tmpdir, script_factory):
138+
"""
139+
Return a PipTestEnvironment which is unique to each test function and
140+
will execute all commands inside of the unique virtual environment for this
141+
test function. The returned object is a
142+
``tests.lib.PipTestEnvironment``.
143+
"""
144+
#return script_factory(tmpdir.joinpath("workspace"), virtualenv)
145+
return script_factory(tmpdir.joinpath("workspace"))

0 commit comments

Comments
 (0)