Skip to content

Commit 529de1b

Browse files
committed
Add CI and username factors
1 parent 671755d commit 529de1b

2 files changed

Lines changed: 58 additions & 2 deletions

File tree

README.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,22 @@ Any factor can be added with `--prepend-factor` and `--append-factor`.
77
The added factors can be combined with the `-e` argument and
88
`TOXENV` environment variable.
99

10+
## CI factors
11+
12+
The argument `add-ci-factor` will detect the CI from the list below and add
13+
it as a factor or fallback to adding "ci":
14+
15+
- appveyor
16+
- cirrusci
17+
- travis
18+
19+
To always add a "ci" factor, use `--append-factor=ci`.
20+
21+
Many CI services use a username which are the name of the service name.
22+
For those services, the `--prepend-username-factor` may be helpful.
23+
1024
## Platform factors
25+
1126
Three specific factors can be added to simplify selecting OS and arch:
1227
The following example depends on `--prepend-ostype-factor`
1328

@@ -29,7 +44,8 @@ is ever fixed.
2944
The factors `--prepend-cpuarch-factor` and `--prepend-archraw-factor`
3045
are provided by https://github.com/workhorsy/py-cpuinfo .
3146

32-
The cpuarch factor currently only supports
47+
The cpuarch factor currently only supports:
48+
3349
- x86_32
3450
- x86_64
3551
- arm_7

tox_add_factor/hooks.py

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
"""Tox hook implementations."""
22
from __future__ import print_function
3+
34
import os
4-
import sys
5+
56
import tox
67

78
try:
@@ -39,6 +40,18 @@ def tox_addoption(parser):
3940
help="Prepend OS type to factors, such as linux, macos, windows.",
4041
)
4142

43+
parser.add_argument(
44+
"--prepend-username-factor",
45+
action="store_true",
46+
help="Prepend username to factors.",
47+
)
48+
49+
parser.add_argument(
50+
"--add-ci-factor",
51+
action="store_true",
52+
help="Add CI factors if environment variable is set, such as appveyor, travis or fallback ci.",
53+
)
54+
4255

4356
@tox.hookimpl(trylast=True)
4457
def tox_configure(config):
@@ -76,6 +89,33 @@ def tox_configure(config):
7689
else:
7790
config.option.prepend_factor.insert(0, _get_os_type().lower())
7891

92+
if config.option.add_ci_factor and "CI" in os.environ:
93+
extra_factor = None
94+
if "APPVEYOR" in os.environ or "TRAVIS" in os.environ:
95+
config.option.prepend_username_factor = True
96+
elif "CIRRUS_CI" in os.environ:
97+
extra_factor = "cirrusci"
98+
else:
99+
extra_factor = "ci"
100+
101+
if extra_factor:
102+
if not config.option.append_factor:
103+
config.option.append_factor = [extra_factor]
104+
else:
105+
config.option.append_factor.insert(0, extra_factor)
106+
107+
if config.option.prepend_username_factor:
108+
import getpass # noqa
109+
110+
username = getpass.getuser()
111+
if username:
112+
username = username.lower()
113+
114+
if not config.option.prepend_factor:
115+
config.option.prepend_factor = [username]
116+
else:
117+
config.option.prepend_factor.insert(0, username)
118+
79119
if config.option.prepend_factor:
80120
add_factors(config, config.option.prepend_factor, position=BEFORE)
81121
if config.option.append_factor:

0 commit comments

Comments
 (0)