Skip to content

Commit 778d8be

Browse files
chore: add noxfile parameters for extra dependencies (#906)
Also, add tests for some noxfile parameters for assurance that the template generates valid Python. Co-authored-by: Jeffrey Rennie <[email protected]>
1 parent 3816b08 commit 778d8be

File tree

2 files changed

+109
-4
lines changed

2 files changed

+109
-4
lines changed

synthtool/gcp/templates/python_library/noxfile.py.j2

+27-4
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,21 @@ def default(session):
8888
session.install("asyncmock", "pytest-asyncio")
8989
{% endif %}
9090
session.install("mock", "pytest", "pytest-cov", {% for d in unit_test_external_dependencies %}"{{d}}"{% if not loop.last %},{% endif %}{% endfor %})
91-
session.install("-e", ".")
92-
{% for dependency in unit_test_local_dependencies %}session.install("-e", "{{dependency}}"){% endfor %}
91+
{% for dependency in unit_test_local_dependencies %}session.install("-e", "{{dependency}}")
92+
{% endfor %}
9393
{% for dependency in unit_test_dependencies %}session.install("-e", "{{dependency}}"){% endfor %}
94+
{%- if unit_test_extras_by_python %}
95+
{% for extras_python in unit_test_extras_by_python %}
96+
{%- if not loop.first %}el{% endif %}if session.python == "{{extras_python}}":
97+
extras = "[{{",".join(unit_test_extras_by_python[extras_python])}}]"
98+
{% endfor %}else:
99+
extras = "{%- if unit_test_extras %}[{{",".join(unit_test_extras)}}]{% endif %}"
100+
session.install("-e", f".{extras}")
101+
{% elif unit_test_extras %}
102+
session.install("-e", ".[{{",".join(unit_test_extras)}}]")
103+
{% else %}
104+
session.install("-e", ".")
105+
{% endif %}
94106

95107
# Run py.test against the unit tests.
96108
session.run(
@@ -139,10 +151,21 @@ def system(session):
139151
session.install("mock", "pytest", "google-cloud-testutils", {% for d in system_test_external_dependencies %}"{{d}}"{% if not loop.last %},{% endif %}{% endfor %})
140152

141153
{%- if system_test_local_dependencies %}
142-
session.install("-e", {% for d in system_test_local_dependencies %}"{{d}}"{% if not loop.last %},{% endif %}{% endfor %})
154+
{% for dependency in system_test_local_dependencies %}session.install("-e", "{{dependency}}")
155+
{% endfor %}
143156
{%- endif %}
157+
{%- if system_test_extras_by_python %}
158+
{% for extras_python in system_test_extras_by_python %}
159+
{%- if not loop.first %}el{% endif %}if session.python == "{{extras_python}}":
160+
extras = "[{{",".join(system_test_extras_by_python[extras_python])}}]"
161+
{% endfor %}else:
162+
extras = "{%- if system_test_extras %}[{{",".join(system_test_extras)}}]{% endif %}"
163+
session.install("-e", f".{extras}")
164+
{% elif system_test_extras %}
165+
session.install("-e", ".[{{",".join(system_test_extras)}}]")
166+
{% else %}
144167
session.install("-e", ".")
145-
168+
{% endif %}
146169

147170
# Run py.test against the system tests.
148171
if system_test_exists:

tests/test_python_library.py

+82
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,89 @@
1515
import os
1616
from pathlib import Path
1717

18+
import pytest
19+
1820
from synthtool import gcp
21+
from synthtool.sources import templates
22+
23+
24+
PYTHON_LIBRARY = Path(__file__).parent.parent / "synthtool/gcp/templates/python_library"
25+
26+
27+
@pytest.mark.parametrize(
28+
["template_kwargs", "expected_text"],
29+
[
30+
({}, ["import nox", 'session.install("-e", ".")']),
31+
(
32+
{"unit_test_local_dependencies": ["../testutils", "../unitutils"]},
33+
[
34+
'session.install("-e", "../testutils")',
35+
'session.install("-e", "../unitutils")',
36+
],
37+
),
38+
(
39+
{"system_test_local_dependencies": ["../testutils", "../sysutils"]},
40+
[
41+
'session.install("-e", "../testutils")',
42+
'session.install("-e", "../sysutils")',
43+
],
44+
),
45+
(
46+
{"unit_test_extras": ["abc", "def"]},
47+
['session.install("-e", ".[abc,def]")'],
48+
),
49+
(
50+
{"system_test_extras": ["abc", "def"]},
51+
['session.install("-e", ".[abc,def]")'],
52+
),
53+
(
54+
{"unit_test_extras_by_python": {"3.8": ["abc", "def"]}},
55+
[
56+
'if session.python == "3.8":\n extras = "[abc,def]"',
57+
'else:\n extras = ""',
58+
'session.install("-e", f".{extras}")',
59+
],
60+
),
61+
(
62+
{"system_test_extras_by_python": {"3.8": ["abc", "def"]}},
63+
[
64+
'if session.python == "3.8":\n extras = "[abc,def]"',
65+
'else:\n extras = ""',
66+
'session.install("-e", f".{extras}")',
67+
],
68+
),
69+
(
70+
{
71+
"unit_test_extras": ["tuv", "wxyz"],
72+
"unit_test_extras_by_python": {"3.8": ["abc", "def"]},
73+
},
74+
[
75+
'if session.python == "3.8":\n extras = "[abc,def]"',
76+
'else:\n extras = "[tuv,wxyz]"',
77+
'session.install("-e", f".{extras}")',
78+
],
79+
),
80+
(
81+
{
82+
"system_test_extras": ["tuv", "wxyz"],
83+
"system_test_extras_by_python": {"3.8": ["abc", "def"]},
84+
},
85+
[
86+
'if session.python == "3.8":\n extras = "[abc,def]"',
87+
'else:\n extras = "[tuv,wxyz]"',
88+
'session.install("-e", f".{extras}")',
89+
],
90+
),
91+
],
92+
)
93+
def test_library_noxfile(template_kwargs, expected_text):
94+
t = templates.Templates(PYTHON_LIBRARY)
95+
result = t.render("noxfile.py.j2", **template_kwargs,).read_text()
96+
# Validate Python syntax.
97+
result_code = compile(result, "noxfile.py", "exec")
98+
assert result_code is not None
99+
for expected in expected_text:
100+
assert expected in result
19101

20102

21103
def test_python_library():

0 commit comments

Comments
 (0)