From 9f83139dcb00ec7e26274a76e9b3e7d1a29f38d5 Mon Sep 17 00:00:00 2001 From: Matt Martz Date: Tue, 16 Apr 2019 14:10:14 -0500 Subject: [PATCH] Don't register tests as filters (#55332) * Don't register tests as filters. Fixes #55319 * Remove tests for deprecated functionality * Remove no-tests-as-filters sanity tests * Remove docs too * Revert "Remove docs too" This reverts commit 7daf457a742e67ec6e91964852c7f804507b46b8. * Make no-tests-as-filters doc an orphan --- .../tests-as-filters-deprecation.yaml | 2 + .../testing/sanity/no-tests-as-filters.rst | 2 + lib/ansible/template/__init__.py | 27 ------ .../code-smell/no-tests-as-filters.json | 7 -- test/sanity/code-smell/no-tests-as-filters.py | 82 ------------------- .../template/test_tests_as_filters_warning.py | 35 -------- 6 files changed, 4 insertions(+), 151 deletions(-) create mode 100644 changelogs/fragments/tests-as-filters-deprecation.yaml delete mode 100644 test/sanity/code-smell/no-tests-as-filters.json delete mode 100755 test/sanity/code-smell/no-tests-as-filters.py delete mode 100644 test/units/template/test_tests_as_filters_warning.py diff --git a/changelogs/fragments/tests-as-filters-deprecation.yaml b/changelogs/fragments/tests-as-filters-deprecation.yaml new file mode 100644 index 00000000000..33c15c0d61e --- /dev/null +++ b/changelogs/fragments/tests-as-filters-deprecation.yaml @@ -0,0 +1,2 @@ +minor_changes: +- Jinja tests - Remove deprecated functionality of registering tests as filters (https://github.com/ansible/ansible/issues/55319) diff --git a/docs/docsite/rst/dev_guide/testing/sanity/no-tests-as-filters.rst b/docs/docsite/rst/dev_guide/testing/sanity/no-tests-as-filters.rst index a20c6c5fc73..874521e52d0 100644 --- a/docs/docsite/rst/dev_guide/testing/sanity/no-tests-as-filters.rst +++ b/docs/docsite/rst/dev_guide/testing/sanity/no-tests-as-filters.rst @@ -1,3 +1,5 @@ +:orphan: + Sanity Tests ยป no-tests-as-filters ================================== diff --git a/lib/ansible/template/__init__.py b/lib/ansible/template/__init__.py index c5256c218c7..d754335d5ef 100644 --- a/lib/ansible/template/__init__.py +++ b/lib/ansible/template/__init__.py @@ -178,26 +178,6 @@ def _count_newlines_from_end(in_str): return i -def tests_as_filters_warning(name, func): - ''' - Closure to enable displaying a deprecation warning when tests are used as a filter - - This closure is only used when registering ansible provided tests as filters - - This function should be removed in 2.9 along with registering ansible provided tests as filters - in Templar._get_filters - ''' - @wraps(func) - def wrapper(*args, **kwargs): - display.deprecated( - 'Using tests as filters is deprecated. Instead of using `result|%(name)s` use ' - '`result is %(name)s`' % dict(name=name), - version='2.9' - ) - return func(*args, **kwargs) - return wrapper - - class AnsibleUndefined(StrictUndefined): ''' A custom Undefined class, which returns further Undefined objects on access, @@ -422,13 +402,6 @@ class Templar: self._filters = dict() - # TODO: Remove registering tests as filters in 2.9 - for name, func in self._get_tests().items(): - if name in builtin_filters: - # If we have a custom test named the same as a builtin filter, don't register as a filter - continue - self._filters[name] = tests_as_filters_warning(name, func) - for fp in self._filter_loader.all(): self._filters.update(fp.filters()) diff --git a/test/sanity/code-smell/no-tests-as-filters.json b/test/sanity/code-smell/no-tests-as-filters.json deleted file mode 100644 index 52a4bebdade..00000000000 --- a/test/sanity/code-smell/no-tests-as-filters.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "extensions": [ - ".yml", - ".yaml" - ], - "output": "path-line-column-message" -} diff --git a/test/sanity/code-smell/no-tests-as-filters.py b/test/sanity/code-smell/no-tests-as-filters.py deleted file mode 100755 index 04f50fb88ce..00000000000 --- a/test/sanity/code-smell/no-tests-as-filters.py +++ /dev/null @@ -1,82 +0,0 @@ -#!/usr/bin/env python -# -*- coding: utf-8 -*- -# (c) 2017, Matt Martz -# -# This file is part of Ansible -# -# Ansible is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# Ansible is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with Ansible. If not, see . - -from __future__ import print_function - -import re -import sys - -from ansible.plugins.test import core, files, mathstuff - -TESTS = list(core.TestModule().tests().keys()) + list(files.TestModule().tests().keys()) + list(mathstuff.TestModule().tests().keys()) - - -TEST_MAP = { - 'version_compare': 'version', - 'is_dir': 'directory', - 'is_file': 'file', - 'is_link': 'link', - 'is_abs': 'abs', - 'is_same_file': 'same_file', - 'is_mount': 'mount', - 'issubset': 'subset', - 'issuperset': 'superset', - 'isnan': 'nan', - 'succeeded': 'successful', - 'success': 'successful', - 'change': 'changed', - 'skip': 'skipped', -} - - -FILTER_RE = re.compile(r'(?P[\w .\'"]+)(\s*)\|(\s*)(?P\w+)') - - -def main(): - for path in sys.argv[1:] or sys.stdin.read().splitlines(): - with open(path) as f: - text = f.read() - - lines = text.splitlines(True) - previous = 0 - offset = 0 - lineno = 0 - - for match in FILTER_RE.finditer(text): - filter_name = match.group('filter') - test_name = TEST_MAP.get(filter_name, filter_name) - - if test_name not in TESTS: - continue - - left = match.group('left').strip() - start = match.start('left') - - while start >= offset: - previous = offset - offset += len(lines[lineno]) - lineno += 1 - - colno = start - previous + 1 - - print('%s:%d:%d: use `%s is %s` instead of `%s | %s`' % (path, lineno, colno, left, test_name, left, filter_name)) - - -if __name__ == '__main__': - main() diff --git a/test/units/template/test_tests_as_filters_warning.py b/test/units/template/test_tests_as_filters_warning.py deleted file mode 100644 index e1c379532c5..00000000000 --- a/test/units/template/test_tests_as_filters_warning.py +++ /dev/null @@ -1,35 +0,0 @@ -from ansible.template import Templar, display -from units.mock.loader import DictDataLoader -from jinja2.filters import FILTERS -from os.path import isabs - - -def test_tests_as_filters_warning(mocker): - fake_loader = DictDataLoader({ - "/path/to/my_file.txt": "foo\n", - }) - templar = Templar(loader=fake_loader, variables={}) - filters = templar._get_filters(templar.environment.filters) - - mocker.patch.object(display, 'deprecated') - - # Call successful test, ensure the message is correct - filters['successful']({}) - display.deprecated.assert_called_once_with( - 'Using tests as filters is deprecated. Instead of using `result|successful` use `result is successful`', version='2.9' - ) - - # Call success test, ensure the message is correct - display.deprecated.reset_mock() - filters['success']({}) - display.deprecated.assert_called_once_with( - 'Using tests as filters is deprecated. Instead of using `result|success` use `result is success`', version='2.9' - ) - - # Call bool filter, ensure no deprecation message was displayed - display.deprecated.reset_mock() - filters['bool'](True) - assert display.deprecated.call_count == 0 - - # Ensure custom test does not override builtin filter - assert filters.get('abs') != isabs