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
|
"""Tests for the clear cache operation from proselint.command_line."""
import os
import unittest
from proselint import command_line as cl
try:
from unittest import mock
except ImportError:
# Py2.x
from unittest import mock
try:
from builtins import PermissionError
except ImportError:
class PermissionError(OSError):
"""Introduced in Py3.3, emulate for earlier versions."""
def __init__(self, *args, **kwargs):
"""Constructor."""
OSError.__init__(self, *args, **kwargs)
try:
from builtins import FileNotFoundError
except ImportError:
class FileNotFoundError(OSError):
"""Introduced in Py3.3, emulate for earlier versions."""
def __init__(self, *args, **kwargs):
"""Constructor."""
OSError.__init__(self, *args, **kwargs)
try:
from builtins import IsADirectoryError
except ImportError:
class IsADirectoryError(OSError):
"""Introduced in Py3.3, emulate for earlier versions."""
def __init__(self, *args, **kwargs):
"""Constructor."""
OSError.__init__(self, *args, **kwargs)
class Test__delete_compiled_python_files(unittest.TestCase):
"""proselint.command_line._delete_compiled_python_files()."""
def setUp(self):
"""init common data."""
self.base_dir = '.'
self.python_file = 'a.py'
self.pyc_file = 'a.pyc'
self.dot_pyc = '.pyc'
self.files = [
(self.base_dir, ('dummy',), (self.pyc_file,
self.python_file,
self.dot_pyc))
]
self.pyc_file_path = os.path.join(self.base_dir, self.pyc_file)
self.python_file_path = os.path.join(self.base_dir, self.python_file)
self.dot_pyc_path = os.path.join(self.base_dir, self.python_file)
@mock.patch('os.walk')
@mock.patch('os.remove')
def test_delete_pyc_file(self, mock_remove, mock_walk):
"""Ensure 'pyc' files are removed."""
mock_walk.return_value = self.files
cl._delete_compiled_python_files()
mock_remove.assert_called_with(self.pyc_file_path)
@mock.patch('os.walk')
@mock.patch('os.remove')
def test_files_not_deleted(self, mock_remove, mock_walk):
"""Ensure non 'pyc' files are not removed."""
mock_walk.return_value = self.files
cl._delete_compiled_python_files()
with self.assertRaises(AssertionError):
mock_remove.assert_called_with(self.python_file_path)
with self.assertRaises(AssertionError):
mock_remove.assert_called_with(self.dot_pyc_path)
@mock.patch('os.walk')
@mock.patch('os.remove', side_effect=PermissionError)
def test_no_permission(self, mock_remove, mock_walk):
"""Ignore if unable to delete files."""
mock_walk.return_value = self.files
cl._delete_compiled_python_files()
@mock.patch('os.walk')
@mock.patch('os.remove', side_effect=OSError)
def test_on_oserror(self, mock_remove, mock_walk):
"""Ignore if OSError."""
mock_walk.return_value = self.files
cl._delete_compiled_python_files()
@mock.patch('os.walk')
@mock.patch('os.remove', side_effect=FileNotFoundError)
def test_files_not_found(self, mock_remove, mock_walk):
"""Ignore if file not found."""
mock_walk.return_value = self.files
cl._delete_compiled_python_files()
@mock.patch('os.walk')
@mock.patch('os.remove', side_effect=IsADirectoryError)
def test__remove_dir(self, mock_remove, mock_walk):
"""Ignore if attempt to delete a directory."""
mock_walk.return_value = self.files
cl._delete_compiled_python_files()
class Test__delete_cache(unittest.TestCase):
"""proselint.command_line.__delete_cache()."""
def setUp(self):
"""Init common data."""
self.cache_path = os.path.join("proselint", "cache")
@mock.patch('shutil.rmtree')
def test_rm_cache(self, mock_rmtree):
"""Correct directory is removed."""
cl._delete_cache()
mock_rmtree.assert_called_with(self.cache_path)
@mock.patch('shutil.rmtree', side_effect=PermissionError)
def test_no_permission(self, mock_rmtree):
"""Ignore if unable to delete."""
cl._delete_cache()
@mock.patch('shutil.rmtree', side_effect=OSError)
def test_on_oserror(self, mock_rmtree):
"""Ignore if general OSError."""
cl._delete_cache()
|