-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathtest_index.py
More file actions
169 lines (143 loc) · 7 KB
/
test_index.py
File metadata and controls
169 lines (143 loc) · 7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import os
import pytest
import shutil
import glob
import buildtools.index
from tests.buildtools.helper import in_fresh_temp_dir
# we use `monkeypatch.setenv` instead of setting `os.environ` directly, since that produces
# cross-talk between tests. (using mock.patch.dict is only available for Python 3)
class TestIncludeOptions:
@staticmethod
def test_LGTM_SRC(monkeypatch):
monkeypatch.setenv("LGTM_SRC", "path/src")
assert buildtools.index.get_include_options() == ["-R", "path/src"]
@staticmethod
def test_LGTM_INDEX_INCLUDE(monkeypatch):
monkeypatch.setenv("LGTM_INDEX_INCLUDE", "/foo\n/bar")
assert buildtools.index.get_include_options() == ["-R", "/foo", "-R", "/bar"]
class TestPip21_3:
@staticmethod
def test_no_build_dir(monkeypatch):
with in_fresh_temp_dir() as path:
os.makedirs(os.path.join(path, "src"))
monkeypatch.setenv("LGTM_SRC", path)
assert buildtools.index.exclude_pip_21_3_build_dir_options() == []
@staticmethod
def test_faked_build_dir(monkeypatch):
# since I don't want to introduce specific pip version on our
# testing infrastructure, I'm just going to fake that `pip install .` had
# been called.
with in_fresh_temp_dir() as path:
os.makedirs(os.path.join(path, "build", "lib"))
monkeypatch.setenv("LGTM_SRC", path)
expected = ["-Y", os.path.join(path, "build")]
assert buildtools.index.exclude_pip_21_3_build_dir_options() == expected
@staticmethod
def test_disable_environment_variable(monkeypatch):
monkeypatch.setenv(
"CODEQL_EXTRACTOR_PYTHON_DISABLE_AUTOMATIC_PIP_BUILD_DIR_EXCLUDE", "1"
)
with in_fresh_temp_dir() as path:
os.makedirs(os.path.join(path, "build", "lib"))
monkeypatch.setenv("LGTM_SRC", path)
assert buildtools.index.exclude_pip_21_3_build_dir_options() == []
@staticmethod
def test_code_build_dir(monkeypatch):
# simulating that you have the module `mypkg.build.lib.foo`
with in_fresh_temp_dir() as path:
os.makedirs(os.path.join(path, "mypkg", "build", "lib"))
open(os.path.join(path, "mypkg", "build", "lib", "foo.py"), "wt").write("print(42)")
open(os.path.join(path, "mypkg", "build", "lib", "__init__.py"), "wt").write("")
open(os.path.join(path, "mypkg", "build", "__init__.py"), "wt").write("")
open(os.path.join(path, "mypkg", "__init__.py"), "wt").write("")
monkeypatch.setenv("LGTM_SRC", path)
assert buildtools.index.exclude_pip_21_3_build_dir_options() == []
def create_fake_venv(path, is_unix):
os.makedirs(path)
open(os.path.join(path, "pyvenv.cfg"), "wt").write("")
if is_unix:
os.mkdir(os.path.join(path, "bin"))
open(os.path.join(path, "bin", "activate"), "wt").write("")
os.makedirs(os.path.join(path, "lib", "python3.10", "site-packages"))
else:
os.mkdir(os.path.join(path, "Scripts"))
open(os.path.join(path, "Scripts", "activate.bat"), "wt").write("")
os.makedirs(os.path.join(path, "Lib", "site-packages"))
class TestVenvIgnore:
@staticmethod
def test_no_venv(monkeypatch):
with in_fresh_temp_dir() as path:
monkeypatch.setenv("LGTM_SRC", path)
assert buildtools.index.exclude_venvs_options() == []
@staticmethod
@pytest.mark.parametrize("is_unix", [True,False])
def test_faked_venv_dir(monkeypatch, is_unix):
with in_fresh_temp_dir() as path:
create_fake_venv(os.path.join(path, "venv"), is_unix=is_unix)
monkeypatch.setenv("LGTM_SRC", path)
assert buildtools.index.exclude_venvs_options() == ["-Y", os.path.join(path, "venv")]
@staticmethod
@pytest.mark.parametrize("is_unix", [True,False])
def test_multiple_faked_venv_dirs(monkeypatch, is_unix):
with in_fresh_temp_dir() as path:
create_fake_venv(os.path.join(path, "venv"), is_unix=is_unix)
create_fake_venv(os.path.join(path, "venv2"), is_unix=is_unix)
monkeypatch.setenv("LGTM_SRC", path)
expected = [
"-Y", os.path.join(path, "venv"),
"-Y", os.path.join(path, "venv2"),
]
actual = buildtools.index.exclude_venvs_options()
assert sorted(actual) == sorted(expected)
@staticmethod
def test_faked_venv_dir_no_pyvenv_cfg(monkeypatch):
"""
Some times, the `pyvenv.cfg` file is not included when a virtual environment is
added to a git-repo, but we should be able to ignore the venv anyway.
See
- https://github.com/FiacreT/M-moire/tree/4089755191ffc848614247e98bbb641c1933450d/osintplatform/testNeo/venv
- https://github.com/Lynchie/KCM/tree/ea9eeed07e0c9eec41f9fc7480ce90390ee09876/VENV
"""
with in_fresh_temp_dir() as path:
create_fake_venv(os.path.join(path, "venv"), is_unix=True)
monkeypatch.setenv("LGTM_SRC", path)
os.remove(os.path.join(path, "venv", "pyvenv.cfg"))
assert buildtools.index.exclude_venvs_options() == ["-Y", os.path.join(path, "venv")]
@staticmethod
def test_faked_venv_no_bin_dir(monkeypatch):
"""
Some times, the activate script is not included when a virtual environment is
added to a git-repo, but we should be able to ignore the venv anyway.
"""
with in_fresh_temp_dir() as path:
create_fake_venv(os.path.join(path, "venv"), is_unix=True)
monkeypatch.setenv("LGTM_SRC", path)
bin_dir = os.path.join(path, "venv", "bin")
assert os.path.isdir(bin_dir)
shutil.rmtree(bin_dir)
assert buildtools.index.exclude_venvs_options() == ["-Y", os.path.join(path, "venv")]
@staticmethod
def test_faked_venv_dir_no_lib_python(monkeypatch):
"""
If there are no `lib/pyhton*` dirs within a unix venv, then it doesn't
constitute a functional virtual environment, and we don't exclude it. That's not
going to hurt, since it won't contain any installed packages.
"""
with in_fresh_temp_dir() as path:
create_fake_venv(os.path.join(path, "venv"), is_unix=True)
monkeypatch.setenv("LGTM_SRC", path)
glob_res = glob.glob(os.path.join(path, "venv", "lib", "python*"))
assert glob_res
for d in glob_res:
shutil.rmtree(d)
assert buildtools.index.exclude_venvs_options() == []
@staticmethod
@pytest.mark.parametrize("is_unix", [True,False])
def test_disable_environment_variable(monkeypatch, is_unix):
monkeypatch.setenv(
"CODEQL_EXTRACTOR_PYTHON_DISABLE_AUTOMATIC_VENV_EXCLUDE", "1"
)
with in_fresh_temp_dir() as path:
create_fake_venv(os.path.join(path, "venv"), is_unix=is_unix)
monkeypatch.setenv("LGTM_SRC", path)
assert buildtools.index.exclude_venvs_options() == []