Skip to content

Commit

Permalink
chore: add noxfile parameters for extra dependencies (#906)
Browse files Browse the repository at this point in the history
Also, add tests for some noxfile parameters for assurance that the
template generates valid Python.

Co-authored-by: Jeffrey Rennie <[email protected]>
  • Loading branch information
tswast and SurferJeffAtGoogle authored Jan 26, 2021
1 parent 3816b08 commit 778d8be
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 4 deletions.
31 changes: 27 additions & 4 deletions synthtool/gcp/templates/python_library/noxfile.py.j2
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,21 @@ def default(session):
session.install("asyncmock", "pytest-asyncio")
{% endif %}
session.install("mock", "pytest", "pytest-cov", {% for d in unit_test_external_dependencies %}"{{d}}"{% if not loop.last %},{% endif %}{% endfor %})
session.install("-e", ".")
{% for dependency in unit_test_local_dependencies %}session.install("-e", "{{dependency}}"){% endfor %}
{% for dependency in unit_test_local_dependencies %}session.install("-e", "{{dependency}}")
{% endfor %}
{% for dependency in unit_test_dependencies %}session.install("-e", "{{dependency}}"){% endfor %}
{%- if unit_test_extras_by_python %}
{% for extras_python in unit_test_extras_by_python %}
{%- if not loop.first %}el{% endif %}if session.python == "{{extras_python}}":
extras = "[{{",".join(unit_test_extras_by_python[extras_python])}}]"
{% endfor %}else:
extras = "{%- if unit_test_extras %}[{{",".join(unit_test_extras)}}]{% endif %}"
session.install("-e", f".{extras}")
{% elif unit_test_extras %}
session.install("-e", ".[{{",".join(unit_test_extras)}}]")
{% else %}
session.install("-e", ".")
{% endif %}

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

{%- if system_test_local_dependencies %}
session.install("-e", {% for d in system_test_local_dependencies %}"{{d}}"{% if not loop.last %},{% endif %}{% endfor %})
{% for dependency in system_test_local_dependencies %}session.install("-e", "{{dependency}}")
{% endfor %}
{%- endif %}
{%- if system_test_extras_by_python %}
{% for extras_python in system_test_extras_by_python %}
{%- if not loop.first %}el{% endif %}if session.python == "{{extras_python}}":
extras = "[{{",".join(system_test_extras_by_python[extras_python])}}]"
{% endfor %}else:
extras = "{%- if system_test_extras %}[{{",".join(system_test_extras)}}]{% endif %}"
session.install("-e", f".{extras}")
{% elif system_test_extras %}
session.install("-e", ".[{{",".join(system_test_extras)}}]")
{% else %}
session.install("-e", ".")

{% endif %}

# Run py.test against the system tests.
if system_test_exists:
Expand Down
82 changes: 82 additions & 0 deletions tests/test_python_library.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,89 @@
import os
from pathlib import Path

import pytest

from synthtool import gcp
from synthtool.sources import templates


PYTHON_LIBRARY = Path(__file__).parent.parent / "synthtool/gcp/templates/python_library"


@pytest.mark.parametrize(
["template_kwargs", "expected_text"],
[
({}, ["import nox", 'session.install("-e", ".")']),
(
{"unit_test_local_dependencies": ["../testutils", "../unitutils"]},
[
'session.install("-e", "../testutils")',
'session.install("-e", "../unitutils")',
],
),
(
{"system_test_local_dependencies": ["../testutils", "../sysutils"]},
[
'session.install("-e", "../testutils")',
'session.install("-e", "../sysutils")',
],
),
(
{"unit_test_extras": ["abc", "def"]},
['session.install("-e", ".[abc,def]")'],
),
(
{"system_test_extras": ["abc", "def"]},
['session.install("-e", ".[abc,def]")'],
),
(
{"unit_test_extras_by_python": {"3.8": ["abc", "def"]}},
[
'if session.python == "3.8":\n extras = "[abc,def]"',
'else:\n extras = ""',
'session.install("-e", f".{extras}")',
],
),
(
{"system_test_extras_by_python": {"3.8": ["abc", "def"]}},
[
'if session.python == "3.8":\n extras = "[abc,def]"',
'else:\n extras = ""',
'session.install("-e", f".{extras}")',
],
),
(
{
"unit_test_extras": ["tuv", "wxyz"],
"unit_test_extras_by_python": {"3.8": ["abc", "def"]},
},
[
'if session.python == "3.8":\n extras = "[abc,def]"',
'else:\n extras = "[tuv,wxyz]"',
'session.install("-e", f".{extras}")',
],
),
(
{
"system_test_extras": ["tuv", "wxyz"],
"system_test_extras_by_python": {"3.8": ["abc", "def"]},
},
[
'if session.python == "3.8":\n extras = "[abc,def]"',
'else:\n extras = "[tuv,wxyz]"',
'session.install("-e", f".{extras}")',
],
),
],
)
def test_library_noxfile(template_kwargs, expected_text):
t = templates.Templates(PYTHON_LIBRARY)
result = t.render("noxfile.py.j2", **template_kwargs,).read_text()
# Validate Python syntax.
result_code = compile(result, "noxfile.py", "exec")
assert result_code is not None
for expected in expected_text:
assert expected in result


def test_python_library():
Expand Down

0 comments on commit 778d8be

Please sign in to comment.