-
-
Notifications
You must be signed in to change notification settings - Fork 808
/
Copy pathconftest.py
150 lines (119 loc) · 4.77 KB
/
conftest.py
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
# Copyright (c) 2014-present PlatformIO <contact@platformio.org>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import email
import functools
import imaplib
import os
import time
import pytest
from click.testing import CliRunner
from platformio import http
from platformio.package.meta import PackageSpec, PackageType
from platformio.registry.client import RegistryClient
def pytest_configure(config):
config.addinivalue_line("markers", "skip_ci: mark a test that will not run in CI")
@pytest.fixture(scope="session")
def validate_cliresult():
def decorator(result):
assert result.exit_code == 0, "{} => {}".format(result.exception, result.output)
assert not result.exception, "{} => {}".format(result.exception, result.output)
return decorator
@pytest.fixture(scope="session")
def clirunner(request, tmpdir_factory):
cache_dir = tmpdir_factory.mktemp(".cache")
backup_env_vars = {
"PLATFORMIO_CACHE_DIR": {"new": str(cache_dir)},
"PLATFORMIO_WORKSPACE_DIR": {"new": None},
}
for key, item in backup_env_vars.items():
# pylint: disable=unnecessary-dict-index-lookup
backup_env_vars[key]["old"] = os.environ.get(key)
if item["new"] is not None:
os.environ[key] = item["new"]
elif key in os.environ:
del os.environ[key]
def fin():
for key, item in backup_env_vars.items():
if item["old"] is not None:
os.environ[key] = item["old"]
elif key in os.environ:
del os.environ[key]
request.addfinalizer(fin)
return CliRunner()
def _isolated_pio_core(request, tmpdir_factory):
core_dir = tmpdir_factory.mktemp(".platformio")
os.environ["PLATFORMIO_CORE_DIR"] = str(core_dir)
def fin():
if "PLATFORMIO_CORE_DIR" in os.environ:
del os.environ["PLATFORMIO_CORE_DIR"]
request.addfinalizer(fin)
return core_dir
@pytest.fixture(scope="module")
def isolated_pio_core(request, tmpdir_factory):
return _isolated_pio_core(request, tmpdir_factory)
@pytest.fixture(scope="function")
def func_isolated_pio_core(request, tmpdir_factory):
return _isolated_pio_core(request, tmpdir_factory)
@pytest.fixture(scope="function")
def without_internet(monkeypatch):
monkeypatch.setattr(http, "_internet_on", lambda: False)
@pytest.fixture
def receive_email(): # pylint:disable=redefined-outer-name, too-many-locals
def _receive_email(from_who):
test_email = os.environ["TEST_EMAIL_LOGIN"]
test_password = os.environ["TEST_EMAIL_PASSWORD"]
imap_server = os.environ["TEST_EMAIL_IMAP_SERVER"]
def get_body(msg):
if msg.is_multipart():
return get_body(msg.get_payload(0))
return msg.get_payload(None, True)
result = None
start_time = time.time()
while not result:
time.sleep(5)
server = imaplib.IMAP4_SSL(imap_server)
server.login(test_email, test_password)
server.select("INBOX")
_, mails = server.search(None, "ALL")
for index in mails[0].split():
status, data = server.fetch(index, "(RFC822)")
if status != "OK" or not data or not isinstance(data[0], tuple):
continue
msg = email.message_from_string(
data[0][1].decode("ASCII", errors="surrogateescape")
)
if from_who not in msg.get("To"):
continue
if "gmail" in imap_server:
server.store(index, "+X-GM-LABELS", "\\Trash")
server.store(index, "+FLAGS", "\\Deleted")
server.expunge()
result = get_body(msg).decode()
if time.time() - start_time > 120:
break
server.close()
server.logout()
return result
return _receive_email
@pytest.fixture(scope="session")
def get_pkg_latest_version():
@functools.lru_cache()
def wrap(spec, pkg_type=None):
if not isinstance(spec, PackageSpec):
spec = PackageSpec(spec)
pkg_type = pkg_type or PackageType.LIBRARY
client = RegistryClient()
pkg = client.get_package(pkg_type, spec.owner, spec.name)
return pkg["version"]["name"]
return wrap