Skip to content

Commit 892e2e1

Browse files
committed
Expose options to customize tox-gh-action's behavior
1 parent e3dfde2 commit 892e2e1

File tree

3 files changed

+94
-17
lines changed

3 files changed

+94
-17
lines changed

Diff for: README.md

+36
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ please check [the tox4 branch](https://github.com/ymyzk/tox-gh-actions/tree/tox4
2121
- [Advanced Examples](#advanced-examples)
2222
- [Factor-Conditional Settings: Python Version](#factor-conditional-settings-python-version)
2323
- [Factor-Conditional Settings: Environment Variable](#factor-conditional-settings-environment-variable)
24+
- [Fail when no environments are matched](#fail-when-no-environments-are-matched)
25+
- [Disable problem matchers](#disable-problem-matchers)
2426
- [tox requires](#tox-requires)
2527
- [Overriding Environments to Run](#overriding-environments-to-run)
2628
- [Versioning](#versioning)
@@ -300,6 +302,40 @@ deps =
300302

301303
See [tox's documentation about factor-conditional settings](https://tox.readthedocs.io/en/latest/config.html#factors-and-factor-conditional-settings) as well.
302304

305+
#### Fail when no environments are matched
306+
By default, tox-gh-actions won't fail the run even if it cannot find environments matching the criteria.
307+
If you want to fail the run in such a case, you can tune the `fail_on_no_env` option.
308+
309+
`tox.ini`:
310+
```ini
311+
[tox]
312+
envlist = py{38,39}
313+
314+
[gh-actions]
315+
python =
316+
3.8: py38
317+
3.9: py39
318+
# tox run using Python 3.10 will fail because tox-gh-actions cannot find an environment contains py310 in the envlist.
319+
3.10: py310
320+
fail_on_no_env = True
321+
```
322+
323+
#### Disable problem matchers
324+
For annotating error messages on GitHub Actions, tox-gh-actions uses [the problem matcher functionality](https://github.com/actions/toolkit/blob/main/docs/problem-matchers.md).
325+
However, there is a case that GitHub Actions reports an error like the following in certain environments.
326+
327+
```
328+
Error: Could not find a part of the path '/usr/local/lib/python3.10/site-packages/tox_gh_actions/matcher.json'.
329+
```
330+
331+
To prevent such errors, you can explicitly disable the problem matcher using the `problem_matcher` option.
332+
333+
`tox.ini`:
334+
```ini
335+
[gh-actions]
336+
problem_matcher = False
337+
```
338+
303339
#### tox requires
304340
If your project uses [tox's `requires` configuration](https://tox.wiki/en/latest/config.html#conf-requires),
305341
you must add `tox-gh-actions` to the `requires` configuration as well. Otherwise, tox-gh-actions won't be loaded as a tox plugin.

Diff for: src/tox_gh_actions/plugin.py

+47-16
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
import os
44
import sys
55
import threading
6-
from typing import Any, Dict, Iterable, List
6+
from typing import Any, Dict, Iterable, List, NoReturn
77

88
import importlib_resources
99
import pluggy
1010
from tox.action import Action
1111
from tox.config import Config, TestenvConfig, _split_env as split_env
12-
from tox.reporter import verbosity1, verbosity2, warning
12+
from tox.reporter import verbosity1, verbosity2, warning, error
1313
from tox.venv import VirtualEnv
1414

1515

@@ -26,17 +26,6 @@ def tox_configure(config):
2626
# type: (Config) -> None
2727
verbosity1("running tox-gh-actions")
2828

29-
if is_running_on_container():
30-
verbosity2(
31-
"not enabling problem matcher as tox seems to be running on a container"
32-
)
33-
# Trying to add a problem matcher from a container without proper host mount can
34-
# cause an error like the following:
35-
# Unable to process command '::add-matcher::/.../matcher.json' successfully.
36-
else:
37-
verbosity2("enabling problem matcher")
38-
print("::add-matcher::" + get_problem_matcher_file_path())
39-
4029
if not is_log_grouping_enabled(config):
4130
verbosity2(
4231
"disabling log line grouping on GitHub Actions based on the configuration"
@@ -65,6 +54,22 @@ def tox_configure(config):
6554
gh_actions_config = parse_config(config._cfg.sections)
6655
verbosity2("tox-gh-actions config: {}".format(gh_actions_config))
6756

57+
if is_running_on_container():
58+
verbosity2(
59+
"not enabling problem matcher as tox seems to be running on a container"
60+
)
61+
# Trying to add a problem matcher from a container without proper host mount can
62+
# cause an error like the following:
63+
# Unable to process command '::add-matcher::/.../matcher.json' successfully.
64+
elif not gh_actions_config["problem_matcher"]:
65+
verbosity2(
66+
"not enabling problem matcher as it's disabled by the problem_matcher "
67+
"option"
68+
)
69+
else:
70+
verbosity2("enabling problem matcher")
71+
print("::add-matcher::" + get_problem_matcher_file_path())
72+
6873
factors = get_factors(gh_actions_config, versions)
6974
verbosity2("using the following factors to decide envlist: {}".format(factors))
7075

@@ -75,6 +80,9 @@ def tox_configure(config):
7580
"tox-gh-actions couldn't find environments matching the provided factors "
7681
"from envlist. Please use `tox -vv` to get more detailed logs."
7782
)
83+
if gh_actions_config["fail_on_no_env"]:
84+
error("Failing the run because the fail_on_no_env option is enabled.")
85+
abort_tox()
7886
verbosity1("overriding envlist with: {}".format(envlist))
7987

8088

@@ -108,9 +116,12 @@ def tox_runtest_post(venv):
108116

109117
@hookimpl
110118
def tox_cleanup(session):
119+
gh_actions_config = parse_config(session.config._cfg.sections)
111120
# This hook can be called multiple times especially when using parallel mode
112121
if not is_running_on_actions():
113122
return
123+
if not gh_actions_config["problem_matcher"]:
124+
return
114125
verbosity2("disabling problem matcher")
115126
for owner in get_problem_matcher_owners():
116127
print("::remove-matcher owner={}::".format(owner))
@@ -148,15 +159,18 @@ def start_grouping_if_necessary(venv):
148159
def parse_config(config):
149160
# type: (Dict[str, Dict[str, str]]) -> Dict[str, Dict[str, Any]]
150161
"""Parse gh-actions section in tox.ini"""
151-
config_python = parse_dict(config.get("gh-actions", {}).get("python", ""))
162+
main_config = config.get("gh-actions", {})
163+
config_python = parse_dict(main_config.get("python", ""))
152164
config_env = {
153165
name: {k: split_env(v) for k, v in parse_dict(conf).items()}
154166
for name, conf in config.get("gh-actions:env", {}).items()
155167
}
156-
# Example of split_env:
157-
# "py{27,38}" => ["py27", "py38"]
158168
return {
169+
# Example of split_env:
170+
# "py{27,38}" => ["py27", "py38"]
159171
"python": {k: split_env(v) for k, v in config_python.items()},
172+
"fail_on_no_env": parse_bool(main_config.get("fail_on_no_env", "false")),
173+
"problem_matcher": parse_bool(main_config.get("problem_matcher", "true")),
160174
"env": config_env,
161175
}
162176

@@ -306,6 +320,23 @@ def get_problem_matcher_owners():
306320
return [m["owner"] for m in matcher["problemMatcher"]]
307321

308322

323+
def parse_bool(value):
324+
# type: (str) -> bool
325+
"""Parse a boolean value in the tox config."""
326+
clean_value = value.strip().lower()
327+
if clean_value in {"true", "1"}:
328+
return True
329+
elif clean_value in {"false", "0"}:
330+
return False
331+
error("Failed to parse a boolean value in tox-gh-actions config: {}")
332+
abort_tox()
333+
334+
335+
def abort_tox():
336+
# type: () -> NoReturn
337+
raise SystemExit(1)
338+
339+
309340
# The following function was copied from
310341
# https://github.com/tox-dev/tox-travis/blob/0.12/src/tox_travis/utils.py#L11-L32
311342
# which is licensed under MIT LICENSE

Diff for: tests/test_plugin.py

+11-1
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,17 @@ def test_start_grouping_ignores_isolated_build_env(capsys, mocker):
5656
"3.7": ["py37", "flake8"],
5757
},
5858
"env": {},
59+
"fail_on_no_env": False,
60+
"problem_matcher": True,
5961
},
6062
),
6163
(
6264
{
6365
"gh-actions": {
6466
"python": """2.7: py27
65-
3.8: py38"""
67+
3.8: py38""",
68+
"fail_on_no_env": "True",
69+
"problem_matcher": "False",
6670
},
6771
"gh-actions:env": {
6872
"PLATFORM": """ubuntu-latest: linux
@@ -82,20 +86,26 @@ def test_start_grouping_ignores_isolated_build_env(capsys, mocker):
8286
"windows-latest": ["windows"],
8387
},
8488
},
89+
"fail_on_no_env": True,
90+
"problem_matcher": False,
8591
},
8692
),
8793
(
8894
{"gh-actions": {}},
8995
{
9096
"python": {},
9197
"env": {},
98+
"fail_on_no_env": False,
99+
"problem_matcher": True,
92100
},
93101
),
94102
(
95103
{},
96104
{
97105
"python": {},
98106
"env": {},
107+
"fail_on_no_env": False,
108+
"problem_matcher": True,
99109
},
100110
),
101111
],

0 commit comments

Comments
 (0)