From 84f1dfcb4bfdbc8a11faa3bafd5b2725e8baf592 Mon Sep 17 00:00:00 2001 From: Bhushan Khanale Date: Sun, 2 Jun 2019 10:25:54 +0530 Subject: [PATCH] Add FileModeBear Closes https://github.com/coala/coala-bears/issues/2370 --- bears/general/FileModeBear.py | 37 ++++++++ tests/general/FileModeBearTest.py | 85 +++++++++++++++++++ .../filemode_test_files/filemode_r_file.txt | 0 .../filemode_test_files/filemode_rw_file.txt | 0 .../filemode_test_files/filemode_rwx_file.txt | 0 .../filemode_test_files/filemode_rx_file.txt | 0 6 files changed, 122 insertions(+) create mode 100644 bears/general/FileModeBear.py create mode 100644 tests/general/FileModeBearTest.py create mode 100644 tests/general/filemode_test_files/filemode_r_file.txt create mode 100644 tests/general/filemode_test_files/filemode_rw_file.txt create mode 100755 tests/general/filemode_test_files/filemode_rwx_file.txt create mode 100755 tests/general/filemode_test_files/filemode_rx_file.txt diff --git a/bears/general/FileModeBear.py b/bears/general/FileModeBear.py new file mode 100644 index 0000000000..a9d393998d --- /dev/null +++ b/bears/general/FileModeBear.py @@ -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)] diff --git a/tests/general/FileModeBearTest.py b/tests/general/FileModeBearTest.py new file mode 100644 index 0000000000..03de075e77 --- /dev/null +++ b/tests/general/FileModeBearTest.py @@ -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) diff --git a/tests/general/filemode_test_files/filemode_r_file.txt b/tests/general/filemode_test_files/filemode_r_file.txt new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/general/filemode_test_files/filemode_rw_file.txt b/tests/general/filemode_test_files/filemode_rw_file.txt new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/general/filemode_test_files/filemode_rwx_file.txt b/tests/general/filemode_test_files/filemode_rwx_file.txt new file mode 100755 index 0000000000..e69de29bb2 diff --git a/tests/general/filemode_test_files/filemode_rx_file.txt b/tests/general/filemode_test_files/filemode_rx_file.txt new file mode 100755 index 0000000000..e69de29bb2