This file is indexed.

/usr/lib/python3/dist-packages/pytest_bdd/generation.py is in python3-pytest-bdd 2.18.2-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
"""pytest-bdd missing test code generation."""

import itertools
import os.path

from mako.lookup import TemplateLookup
import py

from .scenario import (
    find_argumented_step_fixture_name,
    make_python_name,
)
from .steps import get_step_fixture_name
from .feature import get_features
from .types import STEP_TYPES


template_lookup = TemplateLookup(directories=[os.path.join(os.path.dirname(__file__), "templates")])


def add_options(parser):
    """Add pytest-bdd options."""
    group = parser.getgroup("bdd", "Generation")

    group._addoption(
        "--generate-missing",
        action="store_true",
        dest="generate_missing",
        default=False,
        help="Generate missing bdd test code for given feature files and exit.",
    )

    group._addoption(
        "--feature",
        metavar="FILE_OR_DIR",
        action="append",
        dest="features",
        help="Feature file or directory to generate missing code for. Multiple allowed.",
    )


def cmdline_main(config):
    """Check config option to show missing code."""
    if config.option.generate_missing:
        return show_missing_code(config)


def generate_code(features, scenarios, steps):
    """Generate test code for the given filenames."""
    grouped_steps = group_steps(steps)
    template = template_lookup.get_template("test.py.mak")
    return template.render(
        features=features, scenarios=scenarios, steps=grouped_steps, make_python_name=make_python_name)


def show_missing_code(config):
    """Wrap pytest session to show missing code."""
    from _pytest.main import wrap_session
    return wrap_session(config, _show_missing_code_main)


def print_missing_code(scenarios, steps):
    """Print missing code with TerminalWriter."""
    tw = py.io.TerminalWriter()
    scenario = step = None

    for scenario in scenarios:
        tw.line()
        tw.line(
            'Scenario "{scenario.name}" is not bound to any test in the feature "{scenario.feature.name}"'
            " in the file {scenario.feature.filename}:{scenario.line_number}".format(scenario=scenario),
            red=True,
        )

    if scenario:
        tw.sep("-", red=True)

    for step in steps:
        tw.line()
        if step.scenario is not None:
            tw.line(
                """Step {step} is not defined in the scenario "{step.scenario.name}" in the feature"""
                """ "{step.scenario.feature.name}" in the file"""
                """ {step.scenario.feature.filename}:{step.line_number}""".format(step=step),
                red=True,
            )
        elif step.background is not None:
            tw.line(
                """Step {step} is not defined in the background of the feature"""
                """ "{step.background.feature.name}" in the file"""
                """ {step.background.feature.filename}:{step.line_number}""".format(step=step),
                red=True,
            )

    if step:
        tw.sep("-", red=True)

    tw.line("Please place the code above to the test file(s):")
    tw.line()

    features = sorted(
        set(scenario.feature for scenario in scenarios),
        key=lambda feature: feature.name or feature.filename
    )
    code = generate_code(features, scenarios, steps)
    tw.write(code)


def _find_step_fixturedef(fixturemanager, item, name, type_, encoding="utf-8"):
    """Find step fixturedef.

    :param request: PyTest Item object.
    :param step: `Step`.

    :return: Step function.
    """
    fixturedefs = fixturemanager.getfixturedefs(get_step_fixture_name(name, type_, encoding), item.nodeid)
    if not fixturedefs:
        name = find_argumented_step_fixture_name(name, type_, fixturemanager)
        if name:
            return _find_step_fixturedef(fixturemanager, item, name, encoding)
    else:
        return fixturedefs


def parse_feature_files(paths):
    """Parse feature files of given paths.

    :param paths: `list` of paths (file or dirs)

    :return: `list` of `tuple` in form:
             (`list` of `Feature` objects, `list` of `Scenario` objects, `list` of `Step` objects).
    """
    features = get_features(paths)
    scenarios = sorted(
        itertools.chain.from_iterable(feature.scenarios.values() for feature in features),
        key=lambda scenario: (
            scenario.feature.name or scenario.feature.filename, scenario.name))
    steps = sorted(
        set(itertools.chain.from_iterable(scenario.steps for scenario in scenarios)),
        key=lambda step: step.name,
    )
    return features, scenarios, steps


def group_steps(steps):
    """Group steps by type."""
    steps = sorted(steps, key=lambda step: step.type)
    seen_steps = set()
    grouped_steps = []
    for step in (itertools.chain.from_iterable(
            sorted(group, key=lambda step: step.name)
            for _, group in itertools.groupby(steps, lambda step: step.type))):
        if step.name not in seen_steps:
            grouped_steps.append(step)
            seen_steps.add(step.name)
    grouped_steps.sort(key=lambda step: STEP_TYPES.index(step.type))
    return grouped_steps


def _show_missing_code_main(config, session):
    """Preparing fixture duplicates for output."""
    tw = py.io.TerminalWriter()
    session.perform_collect()

    fm = session._fixturemanager

    if config.option.features is None:
        tw.line("The --feature parameter is required.", red=True)
        session.exitstatus = 100
        return

    features, scenarios, steps = parse_feature_files(config.option.features)

    for item in session.items:
        scenario = getattr(item.obj, "__scenario__", None)
        if scenario:
            if scenario in scenarios:
                scenarios.remove(scenario)
            for step in scenario.steps:
                fixturedefs = _find_step_fixturedef(fm, item, step.name, step.type)
                if fixturedefs:
                    try:
                        steps.remove(step)
                    except ValueError:
                        pass
    for scenario in scenarios:
        for step in scenario.steps:
            if step.background is None:
                steps.remove(step)
    grouped_steps = group_steps(steps)
    print_missing_code(scenarios, grouped_steps)

    if scenarios or steps:
        session.exitstatus = 100