|
| 1 | +# Copyright (c) 2025, NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +""" |
| 16 | +Custom nose2 plugin to filter generator test functions by attributes |
| 17 | +before they are called (preventing imports of optional dependencies or other code execution). |
| 18 | +
|
| 19 | +This plugin monkey-patches the Generators plugin's _testsFromGeneratorFunc |
| 20 | +method to check attributes before calling generator functions. |
| 21 | +""" |
| 22 | +from nose2.events import Plugin |
| 23 | +import logging |
| 24 | + |
| 25 | +log = logging.getLogger(__name__) |
| 26 | + |
| 27 | + |
| 28 | +class AttributeGeneratorFilter(Plugin): |
| 29 | + """Filter generator functions by attributes before calling them.""" |
| 30 | + |
| 31 | + configSection = "attrib-generators" |
| 32 | + alwaysOn = True |
| 33 | + |
| 34 | + def __init__(self): |
| 35 | + super().__init__() |
| 36 | + self._patched = False |
| 37 | + |
| 38 | + def _get_attrib_plugin(self): |
| 39 | + """Get the attrib plugin from the session.""" |
| 40 | + for plugin in self.session.plugins: |
| 41 | + if plugin.__class__.__name__ == "AttributeSelector": |
| 42 | + return plugin |
| 43 | + return None |
| 44 | + |
| 45 | + def _build_attribs_list(self, attrib_plugin): |
| 46 | + """Build the attribs list from the attrib plugin's -A configuration. |
| 47 | +
|
| 48 | + This replicates the logic from AttributeSelector.moduleLoadedSuite |
| 49 | + for -A filters only (not -E eval filters). |
| 50 | + """ |
| 51 | + attribs = [] |
| 52 | + |
| 53 | + # Handle -A (attribute) filters |
| 54 | + for attr in attrib_plugin.attribs: |
| 55 | + attr_group = [] |
| 56 | + for attrib in attr.strip().split(","): |
| 57 | + if not attrib: |
| 58 | + continue |
| 59 | + items = attrib.split("=", 1) |
| 60 | + if len(items) > 1: |
| 61 | + # "name=value" |
| 62 | + key, value = items |
| 63 | + else: |
| 64 | + key = items[0] |
| 65 | + if key[0] == "!": |
| 66 | + # "!name" |
| 67 | + key = key[1:] |
| 68 | + value = False |
| 69 | + else: |
| 70 | + # "name" |
| 71 | + value = True |
| 72 | + attr_group.append((key, value)) |
| 73 | + attribs.append(attr_group) |
| 74 | + |
| 75 | + return attribs |
| 76 | + |
| 77 | + def _matches_attrib_filter(self, test_func, attrib_plugin): |
| 78 | + """Check if test_func matches the attribute filter from attrib plugin.""" |
| 79 | + if not attrib_plugin: |
| 80 | + return True |
| 81 | + |
| 82 | + if not attrib_plugin.attribs: |
| 83 | + return True |
| 84 | + |
| 85 | + # Build attribs list using attrib plugin's logic |
| 86 | + attribs = self._build_attribs_list(attrib_plugin) |
| 87 | + |
| 88 | + if not attribs: |
| 89 | + return True |
| 90 | + |
| 91 | + # Use the plugin's validateAttrib method |
| 92 | + return attrib_plugin.validateAttrib(test_func, attribs) |
| 93 | + |
| 94 | + def _patch_generator_plugin(self): |
| 95 | + """Monkey-patch the Generators plugin to check attributes first.""" |
| 96 | + if self._patched: |
| 97 | + return |
| 98 | + |
| 99 | + # Find the Generators plugin |
| 100 | + gen_plugin = None |
| 101 | + for plugin in self.session.plugins: |
| 102 | + if plugin.__class__.__name__ == "Generators": |
| 103 | + gen_plugin = plugin |
| 104 | + break |
| 105 | + |
| 106 | + if not gen_plugin: |
| 107 | + log.warning("Could not find Generators plugin to patch") |
| 108 | + return |
| 109 | + |
| 110 | + # Save original method |
| 111 | + original_tests_from_gen = gen_plugin._testsFromGeneratorFunc |
| 112 | + attrib_filter_self = self |
| 113 | + |
| 114 | + # Create patched method |
| 115 | + def patched_tests_from_gen(event, obj): |
| 116 | + """Check attributes before calling generator function.""" |
| 117 | + attrib_plugin = attrib_filter_self._get_attrib_plugin() |
| 118 | + |
| 119 | + # Check if generator function matches attribute filter |
| 120 | + if not attrib_filter_self._matches_attrib_filter(obj, attrib_plugin): |
| 121 | + log.debug(f"Skipping generator {obj.__name__} due to attribute filter") |
| 122 | + return [] # Return empty list |
| 123 | + |
| 124 | + # Call original method |
| 125 | + return original_tests_from_gen(event, obj) |
| 126 | + |
| 127 | + # Monkey-patch it |
| 128 | + gen_plugin._testsFromGeneratorFunc = patched_tests_from_gen |
| 129 | + self._patched = True |
| 130 | + log.debug("Patched Generators plugin to check attributes") |
| 131 | + |
| 132 | + def handleArgs(self, event): |
| 133 | + """Patch right after argument handling, before test discovery.""" |
| 134 | + self._patch_generator_plugin() |
0 commit comments