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.
DeleteFileAction: New action for DuplicateFileBear
Added a new action DeleteFileAction for DuplicateFileBear. Two instances of this action are added corresponding to the files. On applying it will delete one the file.
- Loading branch information
1 parent
993e86c
commit a7fa4bf
Showing
4 changed files
with
59 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,26 @@ | ||
import os | ||
from coalib.results.result_actions.ResultAction import ResultAction | ||
|
||
|
||
class DeleteFileAction(ResultAction): | ||
""" | ||
Deletes a file | ||
""" | ||
|
||
SUCCESS_MESSAGE = 'File deleted successfully.' | ||
|
||
def __init__(self, filename): | ||
self.filename = filename | ||
self.description = ('Delete {} [Note: This will ' | ||
'delete the file permanently]').format(filename) | ||
|
||
@staticmethod | ||
def is_applicable(result, | ||
original_file_dict, | ||
file_diff_dict, | ||
applied_actions=()): | ||
return 'DeleteFileAction' not in applied_actions | ||
|
||
def apply(self, result, original_file_dict, file_diff_dict): | ||
os.remove(self.filename) | ||
return file_diff_dict |
Empty file.
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,33 @@ | ||
import os | ||
import unittest | ||
from unittest.mock import patch | ||
from coalib.results.Result import Result | ||
from bears.general.actions.DeleteFileAction import DeleteFileAction | ||
from coala_utils.ContextManagers import retrieve_stdout | ||
|
||
def get_path(file): | ||
return os.path.join( | ||
os.getcwd(), 'tests', 'general', 'duplicate_test_files', file) | ||
|
||
class DeleteFileActionTest(unittest.TestCase): | ||
|
||
def setUp(self): | ||
self.file1 = 'complexFirst.txt' | ||
self.file2 = 'complexSecond.txt' | ||
self.result = Result('origin', 'message') | ||
self.uut1 = DeleteFileAction(self.file1) | ||
self.uut2 = DeleteFileAction(self.file2) | ||
|
||
def test_is_applicable(self): | ||
self.assertTrue(self.uut1.is_applicable(self.result, {}, {})) | ||
self.assertFalse(self.uut1.is_applicable( | ||
self.result, {}, {}, applied_actions=('DeleteFileAction'))) | ||
|
||
def test_apply(self): | ||
with retrieve_stdout() as stdout: | ||
patcher = ('bears.general.actions.DeleteFileAction.' | ||
'os.remove') | ||
with patch(patcher): | ||
ret = self.uut1.apply(self.result, {}, {'file': 'diff'}) | ||
self.assertEqual(ret, {'file': 'diff'}) | ||
self.assertEqual(stdout.getvalue(), '') |
Empty file.