Add utility functions around filename matching

We add utils.fnmatch and utils.filenames_for in anticipation of our
checker manager creating file checkers for each file. We also include
tests for these functions and a couple previously untested utility
functions.
This commit is contained in:
Ian Cordasco 2016-02-22 21:47:43 -06:00
parent a21c328870
commit 8792c30872
2 changed files with 101 additions and 0 deletions

View file

@ -1,5 +1,6 @@
"""Tests for flake8's utils module."""
import os
import mock
import pytest
@ -40,3 +41,48 @@ def test_normalize_path(value, expected):
def test_normalize_paths(value, expected):
"""Verify we normalize comma-separated paths provided to the tool."""
assert utils.normalize_paths(value) == expected
def test_is_windows_checks_for_nt():
"""Verify that we correctly detect Windows."""
with mock.patch.object(os, 'name', 'nt'):
assert utils.is_windows() is True
with mock.patch.object(os, 'name', 'posix'):
assert utils.is_windows() is False
@pytest.mark.parametrize('filename,patterns,expected', [
('foo.py', [], True),
('foo.py', ['*.pyc'], False),
('foo.pyc', ['*.pyc'], True),
('foo.pyc', ['*.swp', '*.pyc', '*.py'], True),
])
def test_fnmatch(filename, patterns, expected):
"""Verify that our fnmatch wrapper works as expected."""
assert utils.fnmatch(filename, patterns) is expected
def test_filenames_from_a_directory():
"""Verify that filenames_from walks a directory."""
filenames = list(utils.filenames_from('flake8/'))
assert len(filenames) > 2
assert 'flake8/__init__.py' in filenames
def test_filenames_from_a_directory_with_a_predicate():
"""Verify that predicates filter filenames_from."""
filenames = list(utils.filenames_from(
arg='flake8/',
predicate=lambda filename: filename == 'flake8/__init__.py',
))
assert len(filenames) > 2
assert 'flake8/__init__.py' not in filenames
def test_filenames_from_a_single_file():
"""Verify that we simply yield that filename."""
filenames = list(utils.filenames_from('flake8/__init__.py'))
assert len(filenames) == 1
assert ['flake8/__init__.py'] == filenames