module pycode.utils_tests_helper
¶
Short summary¶
module pyquickhelper.pycode.utils_tests_helper
This extension contains various functionalities to help unittesting.
Classes¶
class |
truncated documentation |
---|---|
Code or style issues. |
Functions¶
function |
truncated documentation |
---|---|
Private function which does extra checkings when refactoring pyquickhelper. |
|
Looks for development version of a given module and add paths to |
|
Checks if PEP8, the function calls command pycodestyle on a specific folder. |
|
Creates and returns a local temporary folder to store files when unit testing. |
Documentation¶
This extension contains various functionalities to help unittesting.
- exception pyquickhelper.pycode.utils_tests_helper.PEP8Exception[source]¶
Bases:
Exception
Code or style issues.
- pyquickhelper.pycode.utils_tests_helper._extended_refactoring(filename, line)[source]¶
Private function which does extra checkings when refactoring pyquickhelper.
- Parameters:
filename – filename
line – line
- Returns:
None or error message
- pyquickhelper.pycode.utils_tests_helper.add_missing_development_version(names, root, hide=False)[source]¶
Looks for development version of a given module and add paths to
sys.path
after having checked they are working.- Parameters:
names – name or names of the module to import
root – folder where to look (assuming all modules location at the same place in a flat hierarchy)
hide – hide warnings when importing a module (might be a lot)
- Returns:
added paths
- pyquickhelper.pycode.utils_tests_helper.check_pep8(folder, ignore=('E265', 'W504'), skip=None, complexity=-1, stop_after=100, fLOG=None, pylint_ignore=('C0103', 'C1801', 'R1705', 'W0108', 'W0613', 'W0107', 'C0415', 'C0209'), recursive=True, neg_pattern=None, extended=None, max_line_length=143, pattern='.*[.]py$', run_lint=True, verbose=False, run_cmd_filter=None)[source]¶
Checks if PEP8, the function calls command pycodestyle on a specific folder.
- Parameters:
folder – folder to look into
ignore – list of warnings to skip when raising an exception if PEP8 is not verified, see also Error Codes
pylint_ignore – ignore pylint issues, see pylint error codes
complexity – see check_file
stop_after – stop after stop_after issues
skip – skip a warning if a substring in this list is found
neg_pattern – skip files verifying this regular expressions
extended – list of tuple (name, function), see below
max_line_length – maximum allowed length of a line of code
recursive – look into subfolder
pattern – only file matching this pattern will be checked
run_lint – run pylint
verbose – pylint is slow, tells which file is investigated (but it is even slower)
run_cmd_filter – some files makes pylint crashes (
import yaml
), the test for this is run in a separate process if the function run_cmd_filter returns True of the filename, verbose is set to True in that casefLOG – logging function
- Returns:
output
Functions mentioned in extended takes two parameters (file name and line) and they returned None or an error message or a tuple (position in the line, error message). When the return is not empty, a warning will be added to the ones printed by pycodestyle. A few codes to ignore:
E501: line too long (?? characters)
E265: block comments should have a space after #
W504: line break after binary operator, this one is raised after the code is modified by
remove_extra_spaces_and_pep8
.
The full list is available at PEP8 codes. In addition, the function adds its own codes:
ECL1: line too long for a specific reason.
Some errors to disable with pylint:
C0103: variable name is not conform
C0111: missing function docstring
C1801: do not use len(SEQUENCE) to determine if a sequence is empty
R0205: Class ‘?’ inherits from object, can be safely removed from bases in python3 (pylint)
R0901: too many ancestors
R0902: too many instance attributes
R0911: too many return statements
R0912: too many branches
R0913: too many arguments
R0914: too many local variables
R0915: too many statements
R1702: too many nested blocks
R1705: unnecessary “else” after “return”
W0107: unnecessary pass statements
W0108: Lambda may not be necessary
W0613: unused argument
The full list is available at pylint error codes. pylint was added used to check the code. It produces the following list of errors pylint error codes.
If neg_pattern is empty, it populates with a default value which skips unnecessary folders:
".*[/\\\\]((_venv)|([.]git)|(__pycache__)|(temp_)).*"
.
- pyquickhelper.pycode.utils_tests_helper.get_temp_folder(thisfile, name=None, clean=True, create=True, persistent=False, path_name='tpath')[source]¶
Creates and returns a local temporary folder to store files when unit testing.
- Parameters:
thisfile – use
__file__
or the function which runs the testname – name of the temporary folder
clean – if True, clean the folder first, it can also a function called to determine whether or not the folder should be cleaned
create – if True, creates it (empty if clean is True)
persistent – if True, create a folder at root level to reduce path length, the function checks the
MAX_PATH
variable and shorten the test folder is max_path is True on Windows, on Linux, it creates a folder three level aheadpath_name – test path used when max_path is True
- Returns:
temporary folder
The function extracts the file which runs this test and will name the temporary folder base on the name of the method. name must be None.
Parameter clean can be a function. Signature is
def clean(folder)
.