From 19e99432794c6648e436b4447c07bf6aaef3456a Mon Sep 17 00:00:00 2001 From: Guy Taylor Date: Tue, 16 Oct 2018 12:25:31 +0100 Subject: [PATCH 1/3] Move to Python requirements.txt file This also links the requirments.txt file to the Venv via Make. So any change to it will trigger the Make rule to install the new packages. --- Makefile | 4 ++-- requirements.txt | 3 +++ 2 files changed, 5 insertions(+), 2 deletions(-) create mode 100644 requirements.txt diff --git a/Makefile b/Makefile index b460d64d35..589901e044 100644 --- a/Makefile +++ b/Makefile @@ -55,9 +55,9 @@ $(VENV): tar -xzf virtualenv-$(VIRTUALENV_VERSION).tar.gz && mv virtualenv-$(VIRTUALENV_VERSION) ./.virtualenv && rm -f virtualenv-$(VIRTUALENV_VERSION).tar.gz chmod +x $@ -$(PYTHON): $(VENV) +$(PYTHON): $(VENV) requirements.txt $(VENV) -p python2.7 $(VENV_DIR) - $(PIP) install mako pyyaml mkdocs==0.16.3 + $(PIP) install -r requirements.txt $(MAKO_RENDER): $(PYTHON) diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000000..580618b8ba --- /dev/null +++ b/requirements.txt @@ -0,0 +1,3 @@ +mako +pyyaml +mkdocs==0.16.3 From 9867b260e1b4efac96eaae76337f9da61f4a8b9c Mon Sep 17 00:00:00 2001 From: Guy Taylor Date: Tue, 16 Oct 2018 12:41:20 +0100 Subject: [PATCH 2/3] Improve Python testing Introduce pytest, wire it into Make and convert previous tests. Note this is not wired into Travis --- Makefile | 8 ++++- requirements.txt | 1 + src/mako/lib/util.py | 61 +---------------------------------- src/mako/lib/util_test.py | 67 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 76 insertions(+), 61 deletions(-) create mode 100644 src/mako/lib/util_test.py diff --git a/Makefile b/Makefile index 589901e044..a246f4805b 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -.PHONY: help deps regen-apis license clean +.PHONY: help deps regen-apis license test-gen test clean .SUFFIXES: VIRTUALENV_VERSION = 16.0.0 @@ -7,6 +7,7 @@ VENV_DIR := .pyenv-$(shell uname) PYTHON := $(VENV_DIR)/bin/python PIP := $(VENV_DIR)/bin/pip MAKO_RENDER := etc/bin/mako-render +PYTEST := $(PYTHON) -m pytest API_VERSION_GEN := etc/bin/api_version_to_yaml.py TPL := $(PYTHON) $(MAKO_RENDER) MKDOCS := $(shell pwd)/$(VENV_DIR)/bin/mkdocs @@ -81,6 +82,11 @@ license: LICENSE.md regen-apis: | clean-all-api clean-all-cli gen-all-api gen-all-cli license +test-gen: $(PYTHON) + $(PYTEST) src + +test: test-gen + clean: clean-all-api clean-all-cli docs-all-clean -rm -Rf $(VENV_DIR) -rm $(API_DEPS) $(CLI_DEPS) diff --git a/requirements.txt b/requirements.txt index 580618b8ba..4a03020845 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ mako pyyaml mkdocs==0.16.3 +pytest diff --git a/src/mako/lib/util.py b/src/mako/lib/util.py index 8a0cef1070..f0bf78a5ff 100644 --- a/src/mako/lib/util.py +++ b/src/mako/lib/util.py @@ -1037,63 +1037,4 @@ def size_to_bytes(size): if __name__ == '__main__': - def test_to_version(): - for v, want in (('v1.3', '1d3'), - ('v1', '1'), - ('directory_v1', '1_directory'), - ('directory_v1.3', '1d3_directory'), - ('v1beta2', '1_beta2'), - ('v1sandbox', '1_sandbox'), - ('v2.0', '2'), - ('v2.0.1', '2d0d1'), - ('v0.0', '0'), - ('v0.1.0', '0d1'), - ('v2.0beta3', '2_beta3'), - ('alpha', 'alpha'), - ('beta', 'beta'), - ('vm_beta', 'vm_beta')): - res = to_api_version(v) - assert res == want, "%s == %s" % (res, want) - # end for each pair - - for iv in ('some_branch_name', '1.3'): - try: - to_api_version(iv) - except AssertionError: - pass - else: - AssertionError("Call should have failed") - # end for each invalid version - # suite - - def test_library_name(): - for v, want in (('v1', 'oauth2_v1'), - ('v1.4', 'oauth2_v1d4'), - ('alpha', 'oauth2_alpha'), - ('beta', 'oauth2_beta'), - ('vm_beta', 'oauth2_vm_beta')): - res = library_name('oauth2', v) - assert res == want, "%s ~== %s" % (res, want) - - - def test_url_substitution(): - url = "https://www.googleapis.com/resumable/upload/groups/v1/groups/{groupId}/{foo}/archive" - ms = list(re_find_replacements.finditer(url)) - assert len(ms) == 2 - assert ms[0].group(0) == '{groupId}' - assert ms[1].group(0) == '{foo}' - - url = "customer/{customerId}/orgunits{/orgUnitPath*}" - ms = list(re_find_replacements.findall(url)) - assert len(ms) == 2 - assert ms[0] == '{customerId}' - assert ms[1] == '{/orgUnitPath*}' - - url = "{+project}/subscriptions" - ms = list(re_find_replacements.findall(url)) - assert len(ms) == 1 - assert ms[0] == '{+project}' - - test_to_version() - test_library_name() - test_url_substitution() + raise AssertionError('For import only') diff --git a/src/mako/lib/util_test.py b/src/mako/lib/util_test.py new file mode 100644 index 0000000000..7d0a723e2c --- /dev/null +++ b/src/mako/lib/util_test.py @@ -0,0 +1,67 @@ +#!/usr/bin/env python + +import unittest + + +from util import to_api_version, library_name, re_find_replacements + + +class UtilsTest(unittest.TestCase): + + def test_to_version_ok(self): + for v, want in (('v1.3', '1d3'), + ('v1', '1'), + ('directory_v1', '1_directory'), + ('directory_v1.3', '1d3_directory'), + ('v1beta2', '1_beta2'), + ('v1sandbox', '1_sandbox'), + ('v2.0', '2'), + ('v2.0.1', '2d0d1'), + ('v0.0', '0'), + ('v0.1.0', '0d1'), + ('v2.0beta3', '2_beta3'), + ('alpha', 'alpha'), + ('beta', 'beta'), + ('vm_beta', 'vm_beta')): + res = to_api_version(v) + self.assertEqual(res, want) + + def test_to_version_fail(self): + for iv in ('some_branch_name', '1.3'): + with self.assertRaises(AssertionError): + to_api_version(iv) + + def test_library_name(self): + for v, want in (('v1', 'oauth2_v1'), + ('v1.4', 'oauth2_v1d4'), + ('alpha', 'oauth2_alpha'), + ('beta', 'oauth2_beta'), + ('vm_beta', 'oauth2_vm_beta')): + res = library_name('oauth2', v) + self.assertEqual(res, want) + + def test_url_substitution(self): + url = "https://www.googleapis.com/resumable/upload/groups/v1/groups/{groupId}/{foo}/archive" + ms = list(re_find_replacements.finditer(url)) + self.assertEqual(len(ms), 2) + self.assertEqual(ms[0].group(0), '{groupId}') + self.assertEqual(ms[1].group(0), '{foo}') + + url = "customer/{customerId}/orgunits{/orgUnitPath*}" + ms = list(re_find_replacements.findall(url)) + self.assertEqual(len(ms), 2) + self.assertEqual(ms[0], '{customerId}') + self.assertEqual(ms[1], '{/orgUnitPath*}') + + url = "{+project}/subscriptions" + ms = list(re_find_replacements.findall(url)) + self.assertEqual(len(ms), 1) + self.assertEqual(ms[0], '{+project}') + + +def main(): + unittest.main() + + +if __name__ == "__main__": + main() From 081cceecb7ccef1d5d8db4063afe3f3815f78b25 Mon Sep 17 00:00:00 2001 From: Guy Taylor Date: Sun, 21 Oct 2018 14:45:31 +0100 Subject: [PATCH 3/3] Wire Python testing into Travis --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index c38850885d..27b9eef91c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -20,6 +20,7 @@ before_install: - pyenv global "${PYENV_VERSION}" - pyenv version script: + - make test-gen - make gen-all-cli cargo-api ARGS=test - make cargo-api ARGS=doc - "if [[ $TRAVIS_RUST_VERSION = nightly ]]; then cargo test; fi"