pytest_skippy

pytest_skippy.core

pytest_skippy.git

pytest_skippy.git.detect_changed_files(target_branch, base_branch='HEAD', git_repo_dir=None)

Get a list of changed files in a git repo

Parameters:
  • target_branch (str) – The merge target for a branch.
  • base_branch (str) – The branch that’s being merged (default: ‘HEAD’)
  • git_repo_dir (None or str) – The base directory of the git repository. None = current directory. (Default)
Returns:

A set of files that have changed in the git repository.

Return type:

set

pytest_skippy.imp

pytest_skippy.imp.convert_module_to_filename(module_name)

Find a module’s file location

Returns the canonical path to the file that defines a module. The canonical path is retrieved using a call to os.path.realpath()

Parameters:module_name (str) – Full path to a module.
Returns:A string containing the filename of the requested module.
Return type:str

Example:

>>> import os.path
>>> path = convert_module_to_filename('re')
>>> os.path.split(path)[-1]
're.py'

pytest_skippy.parse

pytest_skippy.parse.get_imported_modules(filename)

Return modules that are imported by a file

This function will extract all modules that are imported within a python file. The return value includes imports that happen at scopes other than the top level scope.

This parser does not execute any of the python code in the file.

The parser will also return a set of confirmed modules (strings that are guaranteed to be interpreted as module types at runtime)

from foo import bar  # foo is a confirmed module, bar is a candidate
import bar  # bar is a confirmed module

For example:

>>> import tempfile
>>> with tempfile.NamedTemporaryFile(delete=False) as f:
...     x = f.write(b'''
... import os
...
... def inner():
...    import re
... ''')
>>> modules, _ = get_imported_modules(f.name)
>>> print(sorted([m for m in modules]))
['os', 're']
>>> import os ; os.unlink(f.name)
Parameters:filename (str) – File path to a python file
Returns:(modules, confirmed_modules)
Return type:tuple

pytest_skippy.util

pytest_skippy.util.flatten_imports(imported_module, import_tree)

Returns a set of all modules imported by imported_module

The import tree is a dict in the form {module: set(imported_by)}

Parameters:
  • imported_module (str) – A leaf module name
  • import_tree (str) – A dict in the form of {module: set(imported_by)}
Returns:

A set of modules that import imported_module

Return type:

set

Example: A is imported by B

>>> flat = flatten_imports('A', {'A': {'B'}, 'B': set()})
>>> sorted(list(flat))
['A', 'B']