forked from coala/coala-bears
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes coala#2370
- Loading branch information
Showing
6 changed files
with
122 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.