forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathtest_installed_check.py
More file actions
138 lines (123 loc) · 3.47 KB
/
test_installed_check.py
File metadata and controls
138 lines (123 loc) · 3.47 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
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
import contextlib
import json
import os
import pathlib
import subprocess
import sys
from typing import Dict, List, Optional, Union
import pytest
SCRIPT_PATH = pathlib.Path(__file__).parent.parent / "installed_check.py"
TEST_DATA = pathlib.Path(__file__).parent / "test_data"
DEFAULT_SEVERITY = 3
@contextlib.contextmanager
def generate_file(base_file: pathlib.Path):
basename = "pyproject.toml" if "pyproject" in base_file.name else "requirements.txt"
fullpath = base_file.parent / basename
if fullpath.exists():
fullpath.unlink()
fullpath.write_text(base_file.read_text(encoding="utf-8"))
try:
yield fullpath
finally:
fullpath.unlink()
def run_on_file(
file_path: pathlib.Path, severity: Optional[str] = None
) -> List[Dict[str, Union[str, int]]]:
env = os.environ.copy()
if severity:
env["VSCODE_MISSING_PGK_SEVERITY"] = severity
result = subprocess.run(
[
sys.executable,
os.fspath(SCRIPT_PATH),
os.fspath(file_path),
],
capture_output=True,
check=True,
env=env,
)
assert result.returncode == 0
assert result.stderr == b""
return json.loads(result.stdout)
EXPECTED_DATA = {
"missing-deps": [
{
"line": 6,
"character": 0,
"endLine": 6,
"endCharacter": 10,
"package": "flake8-csv",
"code": "not-installed",
"severity": 3,
},
{
"line": 10,
"character": 0,
"endLine": 10,
"endCharacter": 11,
"package": "levenshtein",
"code": "not-installed",
"severity": 3,
},
],
"no-missing-deps": [],
"pyproject-missing-deps": [
{
"line": 8,
"character": 34,
"endLine": 8,
"endCharacter": 44,
"package": "flake8-csv",
"code": "not-installed",
"severity": 3,
}
],
"pyproject-no-missing-deps": [],
}
@pytest.mark.parametrize("test_name", EXPECTED_DATA.keys())
def test_installed_check(test_name: str):
base_file = TEST_DATA / f"{test_name}.data"
with generate_file(base_file) as file_path:
result = run_on_file(file_path)
assert result == EXPECTED_DATA[test_name]
EXPECTED_DATA2 = {
"missing-deps": [
{
"line": 6,
"character": 0,
"endLine": 6,
"endCharacter": 10,
"package": "flake8-csv",
"code": "not-installed",
"severity": 0,
},
{
"line": 10,
"character": 0,
"endLine": 10,
"endCharacter": 11,
"package": "levenshtein",
"code": "not-installed",
"severity": 0,
},
],
"pyproject-missing-deps": [
{
"line": 8,
"character": 34,
"endLine": 8,
"endCharacter": 44,
"package": "flake8-csv",
"code": "not-installed",
"severity": 0,
}
],
}
@pytest.mark.parametrize("test_name", EXPECTED_DATA2.keys())
def test_with_severity(test_name: str):
base_file = TEST_DATA / f"{test_name}.data"
with generate_file(base_file) as file_path:
result = run_on_file(file_path, severity="0")
assert result == EXPECTED_DATA2[test_name]