-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathplugin.py
44 lines (30 loc) · 1.63 KB
/
plugin.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
import os, hashlib
class CircleCIError(Exception):
"""Raised for problems running the CirleCI py.test plugin"""
def read_circleci_env_variables():
"""Read and convert CIRCLE_* environment variables"""
circle_node_total = int(os.environ.get("CIRCLE_NODE_TOTAL", "1").strip() or "1")
circle_node_index = int(os.environ.get("CIRCLE_NODE_INDEX", "0").strip() or "0")
if circle_node_index >= circle_node_total:
raise CircleCIError("CIRCLE_NODE_INDEX={} >= CIRCLE_NODE_TOTAL={}, should be less".format(circle_node_index, circle_node_total))
return (circle_node_total, circle_node_index)
def pytest_report_header(config):
"""Add CircleCI information to report"""
circle_node_total, circle_node_index = read_circleci_env_variables()
return "CircleCI total nodes: {}, this node index: {}".format(circle_node_total, circle_node_index)
def pytest_collection_modifyitems(session, config, items):
"""
Use CircleCI env vars to determine which tests to run
- CIRCLE_NODE_TOTAL indicates total number of nodes tests are running on
- CIRCLE_NODE_INDEX indicates which node this is
Will run a subset of tests based on the node index.
"""
circle_node_total, circle_node_index = read_circleci_env_variables()
deselected = []
for index, item in enumerate(list(items)):
item_location = ':'.join(map(str, item.location)).encode('utf-8')
item_hash = int(hashlib.sha1(item_location).hexdigest(), 16)
if (item_hash % circle_node_total) != circle_node_index:
deselected.append(item)
items.remove(item)
config.hook.pytest_deselected(items=deselected)