From 895119af66bbfd4ba44ef576e6e3dc88bae311fd Mon Sep 17 00:00:00 2001 From: Chris Dent Date: Thu, 16 Jun 2016 13:41:56 +0100 Subject: [PATCH] Send a warning when a yaml filename has an underscore (#157) TestSuites are grouped by test runners by the name that is generated for the tests within. To ease that grouping it is useful to have filenames with a known set of characters. Grouping is also used for py.test fixture handling. This change raises a warning if a filename has an '_' in it, effectively declaring that though it is possible to use '_', it can impact grouping. Fixes #144 --- gabbi/driver.py | 7 +++- gabbi/exception.py | 5 +++ ...n_extensions.yaml => json-extensions.yaml} | 0 .../{last_url.yaml => last-url.yaml} | 0 ...hod_shortcut.yaml => method-shortcut.yaml} | 0 gabbi/tests/test_syntax_warning.py | 41 +++++++++++++++++++ .../warning_gabbits/underscore_sample.yaml | 6 +++ 7 files changed, 58 insertions(+), 1 deletion(-) rename gabbi/tests/gabbits_intercept/{json_extensions.yaml => json-extensions.yaml} (100%) rename gabbi/tests/gabbits_intercept/{last_url.yaml => last-url.yaml} (100%) rename gabbi/tests/gabbits_intercept/{method_shortcut.yaml => method-shortcut.yaml} (100%) create mode 100644 gabbi/tests/test_syntax_warning.py create mode 100644 gabbi/tests/warning_gabbits/underscore_sample.yaml diff --git a/gabbi/driver.py b/gabbi/driver.py index 9cb88fe..22a48c4 100644 --- a/gabbi/driver.py +++ b/gabbi/driver.py @@ -29,8 +29,10 @@ import os import unittest from unittest import suite import uuid +import warnings from gabbi import case +from gabbi import exception from gabbi import handlers from gabbi import reporter from gabbi import suitemaker @@ -83,6 +85,10 @@ def build_tests(path, loader, host=None, port=8001, intercept=None, top_suite = suite.TestSuite() for test_file in glob.iglob('%s/*.yaml' % path): + if '_' in os.path.basename(test_file): + warnings.warn(exception.GabbiSyntaxWarning( + "'_' in test filename %s. This can break suite grouping." + % test_file)) if intercept: host = str(uuid.uuid4()) suite_dict = utils.load_yaml(yaml_file=test_file) @@ -134,7 +140,6 @@ def py_test_generator(test_dir, host=None, port=8001, intercept=None, def test_suite_from_yaml(loader, test_base_name, test_yaml, test_directory, host, port, fixture_module, intercept, prefix=''): """Legacy wrapper retained for backwards compatibility.""" - import warnings with warnings.catch_warnings(): # ensures warnings filter is restored warnings.simplefilter('default', DeprecationWarning) diff --git a/gabbi/exception.py b/gabbi/exception.py index 3d4ef45..2bc93e4 100644 --- a/gabbi/exception.py +++ b/gabbi/exception.py @@ -16,3 +16,8 @@ class GabbiFormatError(ValueError): """An exception to encapsulate poorly formed test data.""" pass + + +class GabbiSyntaxWarning(SyntaxWarning): + """A warning about syntax that is not desirable.""" + pass diff --git a/gabbi/tests/gabbits_intercept/json_extensions.yaml b/gabbi/tests/gabbits_intercept/json-extensions.yaml similarity index 100% rename from gabbi/tests/gabbits_intercept/json_extensions.yaml rename to gabbi/tests/gabbits_intercept/json-extensions.yaml diff --git a/gabbi/tests/gabbits_intercept/last_url.yaml b/gabbi/tests/gabbits_intercept/last-url.yaml similarity index 100% rename from gabbi/tests/gabbits_intercept/last_url.yaml rename to gabbi/tests/gabbits_intercept/last-url.yaml diff --git a/gabbi/tests/gabbits_intercept/method_shortcut.yaml b/gabbi/tests/gabbits_intercept/method-shortcut.yaml similarity index 100% rename from gabbi/tests/gabbits_intercept/method_shortcut.yaml rename to gabbi/tests/gabbits_intercept/method-shortcut.yaml diff --git a/gabbi/tests/test_syntax_warning.py b/gabbi/tests/test_syntax_warning.py new file mode 100644 index 0000000..529dbf6 --- /dev/null +++ b/gabbi/tests/test_syntax_warning.py @@ -0,0 +1,41 @@ +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +"""Test that the driver warns on bad yaml name.""" + +import os +import unittest +import warnings + +from gabbi import driver +from gabbi import exception + + +TESTS_DIR = 'warning_gabbits' + + +class DriverTest(unittest.TestCase): + + def setUp(self): + super(DriverTest, self).setUp() + self.loader = unittest.defaultTestLoader + self.test_dir = os.path.join(os.path.dirname(__file__), TESTS_DIR) + + def test_driver_warngs_on_files(self): + with warnings.catch_warnings(record=True) as the_warnings: + driver.build_tests( + self.test_dir, self.loader, host='localhost', port=8001) + self.assertEqual(1, len(the_warnings)) + the_warning = the_warnings[-1] + self.assertEqual( + the_warning.category, exception.GabbiSyntaxWarning) + self.assertIn("'_' in test filename", str(the_warning.message)) diff --git a/gabbi/tests/warning_gabbits/underscore_sample.yaml b/gabbi/tests/warning_gabbits/underscore_sample.yaml new file mode 100644 index 0000000..185e378 --- /dev/null +++ b/gabbi/tests/warning_gabbits/underscore_sample.yaml @@ -0,0 +1,6 @@ + +tests: + - name: one + url: / + - name: two + url: http://example.com/moo