Skip to content

Commit f54c1a7

Browse files
authored
Make naming of task files more flexible. (#25)
1 parent eff1930 commit f54c1a7

File tree

7 files changed

+97
-7
lines changed

7 files changed

+97
-7
lines changed

docs/changes.rst

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,13 @@ chronological order. Releases follow `semantic versioning <https://semver.org/>`
66
all releases are available on `Anaconda.org <https://anaconda.org/pytask/pytask>`_.
77

88

9-
0.0.6 - 2020-xx-xx
9+
0.0.7 - 2020-xx-xx
10+
------------------
11+
12+
- :gh:`25` allows to customize the names of the task files.
13+
14+
15+
0.0.6 - 2020-09-12
1016
------------------
1117

1218
- :gh:`16` reduces the traceback generated from tasks, failure section in report, fix

docs/reference_guides/configuration.rst

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ The options
100100

101101
.. code-block:: console
102102
103-
pytask --pdb
103+
pytask build --pdb
104104
105105
or use a truthy configuration value.
106106

@@ -115,7 +115,7 @@ The options
115115

116116
.. code-block:: console
117117
118-
pytask --strict-markers
118+
pytask build --strict-markers
119119
120120
or set the option to a truthy value.
121121

@@ -124,13 +124,26 @@ The options
124124
strict_markers = True
125125
126126
127+
.. confval:: task_files
128+
129+
Change the pattern which identify task files.
130+
131+
.. code-block:: ini
132+
133+
task_files = task_*.py # default
134+
135+
task_files =
136+
task_*.py
137+
tasks_*.py
138+
139+
127140
.. confval:: trace
128141

129142
If you want to enter the interactive debugger in the beginning of each task, use
130143

131144
.. code-block:: console
132145
133-
pytask --trace
146+
pytask build --trace
134147
135148
or set this option to a truthy value.
136149

src/_pytask/collect.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ def pytask_collect_file_protocol(session, path, reports):
9191

9292
@hookimpl
9393
def pytask_collect_file(session, path, reports):
94-
if path.name.startswith("task_") and path.suffix == ".py":
94+
if any(path.match(pattern) for pattern in session.config["task_files"]):
9595
spec = importlib.util.spec_from_file_location(path.stem, str(path))
9696

9797
if spec is None:

src/_pytask/config.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@ def pytask_parse_config(config, config_from_cli, config_from_file):
9595
config_from_file["ignore"] = parse_value_or_multiline_option(
9696
config_from_file.get("ignore")
9797
)
98-
9998
config["ignore"] = (
10099
to_list(
101100
get_first_non_none_value(
@@ -119,6 +118,15 @@ def pytask_parse_config(config, config_from_cli, config_from_file):
119118
config["pm"].trace.root.setwriter(click.echo)
120119
config["pm"].enable_tracing()
121120

121+
config_from_file["task_files"] = parse_value_or_multiline_option(
122+
config_from_file.get("task_files")
123+
)
124+
config["task_files"] = to_list(
125+
get_first_non_none_value(
126+
config_from_file, key="task_files", default="task_*.py"
127+
)
128+
)
129+
122130

123131
@hookimpl
124132
def pytask_post_parse(config):

src/_pytask/shared.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,12 @@ def get_first_non_none_value(*configs, key, default=None, callback=None):
8888

8989

9090
def parse_value_or_multiline_option(value):
91-
if isinstance(value, str) and "\n" in value:
91+
if value in ["none", "None", None, ""]:
92+
value = None
93+
elif isinstance(value, str) and "\n" in value:
9294
value = [v.strip() for v in value.split("\n") if v.strip()]
95+
elif isinstance(value, str) and "n" not in value:
96+
value = value.strip()
9397
return value
9498

9599

tests/test_collect.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,44 @@ def task_dummy():
9797

9898
assert session.exit_code == 0
9999
assert len(session.tasks) == 1
100+
101+
102+
@pytest.mark.end_to_end
103+
@pytest.mark.parametrize(
104+
"task_files, pattern, expected_collected_tasks",
105+
[
106+
(["dummy_task.py"], "*_task.py", 1),
107+
(["tasks_dummy.py"], "tasks_*", 1),
108+
(["dummy_tasks.py"], "*_tasks.py", 1),
109+
(["task_dummy.py", "tasks_dummy.py"], "None", 1),
110+
(["task_dummy.py", "tasks_dummy.py"], "tasks_*.py", 1),
111+
(
112+
["task_dummy.py", "tasks_dummy.py"],
113+
"\n task_*.py\n tasks_*.py",
114+
2,
115+
),
116+
],
117+
)
118+
@pytest.mark.parametrize("config_name", ["pytask.ini", "tox.ini", "setup.cfg"])
119+
def test_collect_files_w_custom_file_name_pattern(
120+
tmp_path, config_name, task_files, pattern, expected_collected_tasks
121+
):
122+
config = textwrap.dedent(
123+
f"""
124+
[pytask]
125+
task_files = {pattern}
126+
"""
127+
)
128+
tmp_path.joinpath(config_name).write_text(config)
129+
130+
source = """
131+
def task_dummy():
132+
pass
133+
"""
134+
for file in task_files:
135+
tmp_path.joinpath(file).write_text(textwrap.dedent(source))
136+
137+
session = main({"paths": tmp_path})
138+
139+
assert session.exit_code == 0
140+
assert len(session.tasks) == expected_collected_tasks

tests/test_shared.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import pytest
22
from _pytask.shared import convert_truthy_or_falsy_to_bool
3+
from _pytask.shared import parse_value_or_multiline_option
34

45

56
@pytest.mark.unit
@@ -32,3 +33,20 @@ def test_convert_truthy_or_falsy_to_bool(value, expected):
3233
def test_raise_error_convert_truthy_or_falsy_to_bool(value, expectation):
3334
with expectation:
3435
convert_truthy_or_falsy_to_bool(value)
36+
37+
38+
@pytest.mark.unit
39+
@pytest.mark.parametrize(
40+
"value, expected",
41+
[
42+
(None, None),
43+
("None", None),
44+
("none", None),
45+
("first\nsecond", ["first", "second"]),
46+
("first", "first"),
47+
("", None),
48+
],
49+
)
50+
def test_parse_value_or_multiline_option(value, expected):
51+
result = parse_value_or_multiline_option(value)
52+
assert result == expected

0 commit comments

Comments
 (0)