Skip to content

Commit

Permalink
Add FileModeBear
Browse files Browse the repository at this point in the history
Closes coala#2370
  • Loading branch information
bkhanale committed Jun 2, 2019
1 parent fd5a5a7 commit 84f1dfc
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 0 deletions.
37 changes: 37 additions & 0 deletions bears/general/FileModeBear.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import logging
import os
import stat

from coalib.bears.LocalBear import LocalBear
from coalib.results.Result import Result
from coalib.results.RESULT_SEVERITY import RESULT_SEVERITY


class FileModeBear(LocalBear):
def run(self,
filename,
file,
filemode: str,
):
"""
The bear will check for the file permissions provided by the user.
"""
st = os.stat(filename)
logging.debug(stat.filemode(st.st_mode))
permissions = {'r': stat.S_IRUSR,
'w': stat.S_IWUSR,
'x': stat.S_IXUSR,
}
val = st.st_mode
for char in filemode:
if not char in permissions:
raise ValueError('Unable to recognize character `{}` in '
'filemode `{}`.'.format(char, filemode))
if not val & permissions[char]:
message = ('The file permissions are not adequate. The '
'permissions are set to {}'.format(stat.filemode(
st.st_mode)))
return [Result.from_values(origin=self,
message=message,
severity=RESULT_SEVERITY.INFO,
file=filename)]
85 changes: 85 additions & 0 deletions tests/general/FileModeBearTest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import os
import stat

from queue import Queue

from bears.general.FileModeBear import FileModeBear
from coalib.testing.LocalBearTestHelper import LocalBearTestHelper
from coalib.results.Result import RESULT_SEVERITY, Result
from coalib.output.printers.LOG_LEVEL import LOG_LEVEL
from coalib.settings.Section import Section
from coalib.settings.Setting import Setting


def get_testfile_path(file):
return os.path.join(os.path.dirname(__file__),
'filemode_test_files', file)


class FileModeBearTest(LocalBearTestHelper):

def setUp(self):
self.section = Section('')
self.uut = FileModeBear(self.section, Queue())

def test_r_to_rwx_permissions(self):
filename = get_testfile_path('filemode_r_file.txt')
message = ('The file permissions are not adequate. The '
'permissions are set to -r--------')
self.section.append(Setting('filemode', 'rwx'))
self.check_results(
self.uut,
[],
[Result.from_values('FileModeBear',
message,
file=filename,
severity=RESULT_SEVERITY.INFO)],
filename=filename,
settings={'filemode': 'rwx'})

def test_rw_to_rwx_permissions(self):
filename = get_testfile_path('filemode_rw_file.txt')
message = ('The file permissions are not adequate. The '
'permissions are set to -rw-------')
self.section.append(Setting('filemode', 'rwx'))
self.check_results(
self.uut,
[],
[Result.from_values('FileModeBear',
message,
file=filename,
severity=RESULT_SEVERITY.INFO)],
filename=filename,
settings={'filemode': 'rwx'})

def test_rx_to_rwx_permissions(self):
filename = get_testfile_path('filemode_rx_file.txt')
message = ('The file permissions are not adequate. The '
'permissions are set to -r-x------')
self.section.append(Setting('filemode', 'rwx'))
self.check_results(
self.uut,
[],
[Result.from_values('FileModeBear',
message,
file=filename,
severity=RESULT_SEVERITY.INFO)],
filename=filename,
settings={'filemode': 'rwx'})

def test_rwx_to_rwx_permissions(self):
filename = get_testfile_path('filemode_rwx_file.txt')
self.section.append(Setting('filemode', 'rwx'))
self.check_results(
self.uut,
[],
[],
filename=filename,
settings={'filemode': 'rwx'})

def test_invalid_char_in_filemode(self):
filename = get_testfile_path('filemode_rwx_file.txt')
self.section.append(Setting('filemode', 'rwm'))
error_msg = 'ValueError: Unable to recognize character `m` in filemode `rwm`.'
with self.assertRaisesRegex(AssertionError, error_msg):
self.check_validity(self.uut, [], filename=filename)
Empty file.
Empty file.
Empty file.
Empty file.

0 comments on commit 84f1dfc

Please sign in to comment.