Skip to content

Commit

Permalink
bears/general: Add FileModeBear
Browse files Browse the repository at this point in the history
Closes #2370
  • Loading branch information
bkhanale committed Jul 21, 2019
1 parent fd5a5a7 commit 70a08d9
Show file tree
Hide file tree
Showing 4 changed files with 264 additions and 0 deletions.
1 change: 1 addition & 0 deletions bear-languages.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ ESLintBear:
- Typescript
ElmLintBear:
- Elm
FileModeBear:
FilenameBear:
FormatRBear:
- R
Expand Down
51 changes: 51 additions & 0 deletions bears/general/FileModeBear.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
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):
LANGUAGES = {'All'}
AUTHORS = {'The coala developers'}
AUTHORS_EMAILS = {'coala-devel@googlegroups.com'}
LICENSE = 'AGPL-3.0'

def run(self,
filename,
file,
filemode: str,
):
"""
The bear will check if the file has required permissions provided by
the user.
:param filename:
Name of the file that needs to be checked.
:param file:
File that needs to be checked in the form of a list of strings.
:param filemode:
Filemode to check, e.g. `rw`, `rwx`, etc.
"""
st = os.stat(filename)
permissions = {'r': stat.S_IRUSR,
'w': stat.S_IWUSR,
'x': stat.S_IXUSR,
}

for char in filemode:
if char not in permissions:
raise ValueError('Unable to recognize character `{}` in '
'filemode `{}`.'.format(char, filemode))

mode = st.st_mode
for char in filemode:
if not mode & permissions[char]:
message = ('The file permissions are not adequate. The '
'permissions are set to {}'.format(stat.filemode(
mode)))
return [Result.from_values(origin=self,
message=message,
severity=RESULT_SEVERITY.INFO,
file=filename)]
212 changes: 212 additions & 0 deletions tests/general/FileModeBearTest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
import os
import platform
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.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)


FILE_PATH = get_testfile_path('test_file.txt')


class FileModeBearTest(LocalBearTestHelper):

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

def test_r_to_r_permissions(self):
os.chmod(FILE_PATH, stat.S_IRUSR)
self.section.append(Setting('filemode', 'r'))
self.check_results(
self.uut,
[],
[],
filename=FILE_PATH,
)

def test_w_to_w_permissions(self):
os.chmod(FILE_PATH, stat.S_IWUSR)
self.section.append(Setting('filemode', 'w'))
self.check_results(
self.uut,
[],
[],
filename=FILE_PATH,
)

def test_x_to_x_permissions(self):
os.chmod(FILE_PATH, stat.S_IXUSR)
if platform.system() != 'Windows':
self.section.append(Setting('filemode', 'x'))
self.check_results(
self.uut,
[],
[],
filename=FILE_PATH,
)

def test_rw_to_rw_permissions(self):
os.chmod(FILE_PATH, stat.S_IRUSR | stat.S_IWUSR)
self.section.append(Setting('filemode', 'rw'))
self.check_results(
self.uut,
[],
[],
filename=FILE_PATH,
)

def test_wx_to_wx_permissions(self):
os.chmod(FILE_PATH, stat.S_IWUSR | stat.S_IXUSR)
if platform.system() != 'Windows':
self.section.append(Setting('filemode', 'wx'))
self.check_results(
self.uut,
[],
[],
filename=FILE_PATH,
)

def test_rx_to_rx_permissions(self):
os.chmod(FILE_PATH, stat.S_IRUSR | stat.S_IXUSR)
if platform.system() != 'Windows':
self.section.append(Setting('filemode', 'rx'))
self.check_results(
self.uut,
[],
[],
filename=FILE_PATH,
)

def test_rwx_to_rwx_permissions(self):
os.chmod(FILE_PATH, stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR)
if platform.system() != 'Windows':
self.section.append(Setting('filemode', 'rwx'))
self.check_results(
self.uut,
[],
[],
filename=FILE_PATH,
settings={'filemode': 'rwx'})

def test_r_to_rwx_permissions(self):
os.chmod(FILE_PATH, stat.S_IRUSR)
filemode = '-r--------'
if platform.system() == 'Windows':
filemode = '-r--r--r--'
message = ('The file permissions are not adequate. The '
'permissions are set to {}'.format(filemode))
self.section.append(Setting('filemode', 'rwx'))
self.check_results(
self.uut,
[],
[Result.from_values('FileModeBear',
message,
file=FILE_PATH,
severity=RESULT_SEVERITY.INFO)],
filename=FILE_PATH,
settings={'filemode': 'rwx'})

def test_w_to_rwx_permissions(self):
os.chmod(FILE_PATH, stat.S_IWUSR)
filemode = '--w-------'
if platform.system() == 'Windows':
filemode = '-rw-rw-rw-'
message = ('The file permissions are not adequate. The '
'permissions are set to {}'.format(filemode))
self.section.append(Setting('filemode', 'rwx'))
self.check_results(
self.uut,
[],
[Result.from_values('FileModeBear',
message,
file=FILE_PATH,
severity=RESULT_SEVERITY.INFO)],
filename=FILE_PATH,
)

def test_x_to_rwx_permissions(self):
os.chmod(FILE_PATH, stat.S_IXUSR)
filemode = '---x------'
if platform.system() != 'Windows':
message = ('The file permissions are not adequate. The '
'permissions are set to {}'.format(filemode))
self.section.append(Setting('filemode', 'rwx'))
self.check_results(
self.uut,
[],
[Result.from_values('FileModeBear',
message,
file=FILE_PATH,
severity=RESULT_SEVERITY.INFO)],
filename=FILE_PATH,
)

def test_rx_to_rwx_permissions(self):
os.chmod(FILE_PATH, stat.S_IRUSR | stat.S_IXUSR)
filemode = '-r-x------'
if platform.system() != 'Windows':
message = ('The file permissions are not adequate. The '
'permissions are set to {}'.format(filemode))
self.section.append(Setting('filemode', 'rwx'))
self.check_results(
self.uut,
[],
[Result.from_values('FileModeBear',
message,
file=FILE_PATH,
severity=RESULT_SEVERITY.INFO)],
filename=FILE_PATH,
)

def test_wx_to_rwx_permissions(self):
os.chmod(FILE_PATH, stat.S_IWUSR | stat.S_IXUSR)
filemode = '--wx------'
if platform.system() != 'Windows':
message = ('The file permissions are not adequate. The '
'permissions are set to {}'.format(filemode))
self.section.append(Setting('filemode', 'rwx'))
self.check_results(
self.uut,
[],
[Result.from_values('FileModeBear',
message,
file=FILE_PATH,
severity=RESULT_SEVERITY.INFO)],
filename=FILE_PATH,
)

def test_rw_to_rwx_permissions(self):
os.chmod(FILE_PATH, stat.S_IRUSR | stat.S_IWUSR)
filemode = '-rw-------'
if platform.system() == 'Windows':
filemode = '-rw-rw-rw-'
message = ('The file permissions are not adequate. The '
'permissions are set to {}'.format(filemode))
self.section.append(Setting('filemode', 'rwx'))
self.check_results(
self.uut,
[],
[Result.from_values('FileModeBear',
message,
file=FILE_PATH,
severity=RESULT_SEVERITY.INFO)],
filename=FILE_PATH,
)

def test_invalid_char_in_filemode(self):
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=FILE_PATH)
Empty file.

0 comments on commit 70a08d9

Please sign in to comment.