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

WIP bears/general: Add CheckmanifestBear #1067

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions bear-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@ vim-vint~=0.3.10
vulture~=0.10.0
yamllint~=1.5
yapf~=0.14.0
check-manifest~=0.34
63 changes: 63 additions & 0 deletions bears/general/CheckmanifestBear.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import os

from coalib.bearlib.abstractions.Linter import linter
from dependency_management.requirements.PipRequirement import PipRequirement
from coalib.results.RESULT_SEVERITY import RESULT_SEVERITY
from coalib.bears.GlobalBear import GlobalBear
from coalib.results.Result import Result
from coalib.misc.Shell import run_shell_command
from coalib.results.Diff import Diff


class CheckmanifestBear(GlobalBear):
"""
Check MANIFEST.in in a Python source package for completeness

Check https://pypi.python.org/pypi/rstcheck for more information.
"""

LANGUAGES = {'Python MANIFEST.in'}
REQUIREMENTS = {PipRequirement('check-manifest', '0.34')}
AUTHORS = {'The coala developers'}
AUTHORS_EMAILS = {'[email protected]'}
LICENSE = 'AGPL-3.0'
CAN_DETECT = {'Redundancy'}
EXECUTABLE = 'check-manifest'

def run(self, ignore: list=[]):
"""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

main description missing

Check MANIFEST.in in a Python source package for completeness

Check https://pypi.python.org/pypi/rstcheck for more information.

:param ignore:
ignore files/directories matching these comma-
separated patterns
"""
args = ''
if ignore:
args = ('--ignore=' +
','.join(ignore),)

output_message = 'suggested MANIFEST.in rules:'
output, _ = run_shell_command(
(self.EXECUTABLE,) + tuple(args) +
tuple(os.path.dirname(filename)
for filename in self.file_dict.keys()))
to_search = output_message+'\n'

temp_output = output.splitlines(1)
if to_search in temp_output:
index = temp_output.index(to_search)
new_output = temp_output[index+1:]
final_output = [x.strip(' ') for x in new_output]
else:
final_output = ''
# Ignore LineLengthBear
output_message = 'lists of files in version control and sdist match!'

a = Diff(list(self.file_dict.values())[0])
a.add_lines(len(list(self.file_dict.values())[0]), final_output)

yield Result(self, output_message,
diffs={list(self.file_dict.keys())[0]: a})
72 changes: 72 additions & 0 deletions tests/general/CheckmanifestBearTest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import shutil
import tempfile
import platform
import stat
import os
from os import path

import unittest
from queue import Queue
from coalib.settings.Section import Section
from coalib.misc.Shell import run_shell_command
from bears.general.CheckmanifestBear import CheckmanifestBear


class CheckmanifestBearTest(unittest.TestCase):

@staticmethod
def run_git_command(*args, stdin=None):
run_shell_command(' '.join(('git',) + args), stdin)

@staticmethod
def _windows_rmtree_remove_readonly(func, path, excinfo):
os.chmod(path, stat.S_IWRITE)
func(path)

def setUp(self):
self.queue = Queue()
self.file_dict = {}
self.section = Section('Checkmanifest')
self.uut = CheckmanifestBear(self.file_dict, self.section,
self.queue)
self._old_cwd = os.getcwd()
self.test_dir = tempfile.mkdtemp()
with open(path.join(self.test_dir, 'setup.py'), 'w') as f:
f.write('from setuptools import setup\n')
f.write("setup(name='sample', py_modules=['sample'])\n")
f.close()
with open(path.join(self.test_dir, 'sample.py'), 'w') as f:
f.write('# wow. such code. so amaze\n')
f.close()

f = open(path.join(self.test_dir, 'MANIFEST.in'), 'w')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use with for these.

f.close()
f = open(path.join(self.test_dir, 'unrelated.txt'), 'w')
f.write('Hello from the other side')
f.close()
f = open(path.join(self.test_dir, 'rishu.cpp'), 'w')
f.write('int main')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ouch this burns. Please make it a valid .cpp file.

f.close()
os.chdir(self.test_dir)
self.run_git_command('init')
self.run_git_command('config', 'user.email [email protected]')
self.run_git_command('config', 'user.name coala')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code does not comply to PEP8.

PEP8Bear, severity NORMAL, section autopep8.

The issue can be fixed by applying the following patch:

--- a/tests/general/CheckmanifestBearTest.py
+++ b/tests/general/CheckmanifestBearTest.py
@@ -51,6 +51,7 @@
         self.run_git_command('init')
         self.run_git_command('config', 'user.email [email protected]')
         self.run_git_command('config', 'user.name coala')
+
     def tearDown(self):
         os.chdir(self._old_cwd)
         if platform.system() == 'Windows':

self.run_git_command('add', '--all')

def tearDown(self):
os.chdir(self._old_cwd)
if platform.system() == 'Windows':
onerror = self._windows_rmtree_remove_readonly
else:
onerror = None
shutil.rmtree(self.test_dir, onerror=onerror)

def run_uut(self, *args, **kwargs):
return list(result.message for result in self.uut.run(*args, **kwargs))

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code does not comply to PEP8.

PEP8Bear, severity NORMAL, section autopep8.

The issue can be fixed by applying the following patch:

--- a/tests/general/CheckmanifestBearTest.py
+++ b/tests/general/CheckmanifestBearTest.py
@@ -74,4 +74,3 @@
             invalid_files=(path.join(self.testt_dir, 'MANIFEST.in')),
             settings={'ignore':
                       'unrelated.txt', 'create': 'True', 'update': 'True'})
-

def test_something(self):
self.uut.file_dict = {path.join(self.test_dir, 'MANIFEST.in'):
''}
self.assertEqual(self.run_uut(ignore=['unrelated.txt', 'rishu.cpp']),
['lists of files in version control and sdist match!']) # Ignore LineLengthBear
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are you ignoring LineLengthBear ?? you can split this line into two... 😄

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

formatting is not the concern right now.. You can see

  1. PR is marked WIP -> its not ready.
  2. coverage is creating problems with CI (upstream problem).

self.assertNotEqual(self.run_uut(), [])