Skip to content

Commit

Permalink
AlexBear: Check if wrong alex is installed
Browse files Browse the repository at this point in the history
Alex has a name clash with another tool in the haskell toolkit. Hence,
we check here by reading the `--help` of `alex` and checking whether
this is indeed the correct tool or not.

Fixes #776
  • Loading branch information
AbdealiLoKo committed Sep 3, 2016
1 parent bfd61fb commit fa70e92
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
27 changes: 27 additions & 0 deletions bears/natural_language/AlexBear.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import subprocess
import sys

from coalib.bearlib.abstractions.Linter import linter
from coalib.bears.requirements.NpmRequirement import NpmRequirement

Expand All @@ -18,6 +21,30 @@ class AlexBear:
AUTHORS_EMAILS = {'[email protected]'}
LICENSE = 'AGPL-3.0'

@classmethod
def check_prerequisites(cls):
parent_prereqs = super().check_prerequisites()
if parent_prereqs is not True: # pragma: no cover
return parent_prereqs

incorrect_pkg_msg = (
"Please ensure that the package that has been installed is the "
"one to 'Catch insensitive, inconsiderate writing'. This can be "
"verified by running `alex --help` and seeing what it does.")
try:
output = subprocess.check_output(("alex", "--help"),
stderr=subprocess.STDOUT)
except (OSError, subprocess.CalledProcessError):
return ("The `alex` package could not be verified. " +
incorrect_pkg_msg)
else:
output = output.decode(sys.getfilesystemencoding())
if "Catch insensitive, inconsiderate writing" in output:
return True
else:
return ("The `alex` package that's been installed seems to "
"be incorrect. " + incorrect_pkg_msg)

@staticmethod
def create_arguments(filename, file, config_file):
return filename,
27 changes: 27 additions & 0 deletions tests/natural_language/AlexBearTest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import unittest
from unittest.mock import patch

from bears.natural_language.AlexBear import AlexBear
from tests.BearTestHelper import generate_skip_decorator
from tests.LocalBearTestHelper import verify_local_bear

good_file = "Their network looks good."
Expand All @@ -9,3 +13,26 @@
AlexBearTest = verify_local_bear(AlexBear,
valid_files=(good_file,),
invalid_files=(bad_file,))


@generate_skip_decorator(AlexBear)
@patch('bears.natural_language.AlexBear.subprocess.check_output')
class AlexBearPrereqTest(unittest.TestCase):

def test_unverified_alex_installed(self, check_output_mock):
check_output_mock.side_effect = OSError
self.assertIn('The `alex` package could not be verified',
AlexBear.check_prerequisites())

def test_wrong_alex_installed(self, check_output_mock):
check_output_mock.return_value = b'Unexpected output from package'
self.assertIn("The `alex` package that's been installed seems to "
"be incorrect",
AlexBear.check_prerequisites())

def test_right_alex_installed(self, check_output_mock):
check_output_mock.return_value = (
b'Some text here\n'
b' Catch insensitive, inconsiderate writing\n'
b'Usage instructions and examples here ....')
self.assertTrue(AlexBear.check_prerequisites())

0 comments on commit fa70e92

Please sign in to comment.