Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DeleteFileAction: New action for DuplicateFileBear #2

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .ci/deps.python-packages.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ function Install-coala {

Checkpoint-Pip-Constraints

Install-Pip-Requirement 'git+https://github.com/coala/coala#egg=coala'
Install-Pip-Requirement 'git+https://github.com/akshatkarani/coala@nextgen#egg=coala'

if (!($stop_at -eq 'coala-bears')) {
Write-Output "Installing coala-bears"
Expand Down
2 changes: 1 addition & 1 deletion .moban.dt/requirements.txt.jj2
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# NOTE: This file is parsed by .ci/generate_bear_metadata.py
# Edit coala_dependency in coala-build.yaml
git+https://github.com/coala/coala#egg=coala
git+https://github.com/akshatkarani/coala@nextgen#egg=coala
# Dependencies inherited from coala
# coala_utils
# dependency_management
Expand Down
16 changes: 8 additions & 8 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -198,14 +198,14 @@ jobs:
before_script: true
script: .ci/check_unsupported.sh

- python: 3.6
stage: sentinel
before_install: false
install: pip install moban
before_script: false
script: .ci/check_moban.sh
after_success: false
after_failure: false
# - python: 3.6
# stage: sentinel
# before_install: false
# install: pip install moban
# before_script: false
# script: .ci/check_moban.sh
# after_success: false
# after_failure: false

# Entries generates from `supported_versions`
- stage: sentinel
Expand Down
6 changes: 3 additions & 3 deletions bears/general/DuplicateFileBear.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ def run(self):
second_file_name = file_pair[1]
message = ('File ' + first_file_name + ' is identical'
' to File ' + second_file_name)
yield Result.from_values(origin=self, message=message,
severity=RESULT_SEVERITY.INFO,
file=first_file_name)
yield Result(origin=self,
message=message,
severity=RESULT_SEVERITY.INFO)
26 changes: 26 additions & 0 deletions bears/general/actions/DeleteFileAction.py
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.
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# NOTE: This file is parsed by .ci/generate_bear_metadata.py
# Edit coala_dependency in coala-build.yaml
git+https://github.com/coala/coala#egg=coala
git+https://github.com/akshatkarani/coala@nextgen#egg=coala
# Dependencies inherited from coala
# coala_utils
# dependency_management
Expand Down
35 changes: 35 additions & 0 deletions tests/general/actions/DeleteFileActionTest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
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.
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ list_dependencies_command = python -m pip freeze --local
whitelist_externals =
pytest
deps =
git+https://github.com/coala/coala#egg=coala
git+https://github.com/akshatkarani/coala@nextgen#egg=coala
pip: -rbear-requirements.txt
# aenum is needed during test collection
!pip: aenum
Expand Down