def test_reporter(self):
        """
        Test for LimitedReporter.
        Use test file of indentation to test
        whether limited messages are returned when using LimitedReporter.

        Complete run on the test file will return two warnings:
        W0311 and W0312, but with limited report it returns only one.
        """
        moduleTestIndentation = "twistedchecker.functionaltests.indentation"
        pathTestIndentation = os.path.join(twistedchecker.abspath,
                                      "functionaltests", "indentation.py")
        # assert the test file exists
        self.assertTrue(os.path.exists(pathTestIndentation))
        streamTestResult = NativeStringIO()
        runner = Runner()
        runner.setOutput(streamTestResult)
        # defaultly, runner will use LimitedReporter as its output reporter
        # set allowed messages for it
        runner.linter.reporter.messagesAllowed = set(["W0311"])

        exitResult = self.assertRaises(
            SystemExit, runner.run, [moduleTestIndentation])

        # check the results to see only W0311 is reported
        resultTest = streamTestResult.getvalue()
        self.assertTrue("W0311" in resultTest)
        self.assertTrue("W0312" not in resultTest)
        self.assertEqual(4, exitResult.code)
    def test_unregisterChecker(self):
        """
        Test for method unregisterChecker.

        Remove HeaderChecker from registered,
        and make sure it was removed.
        """
        runner = Runner()
        registeredCheckers = sum(list(runner.linter._checkers.values()), [])
        # Make sure an instance of HeaderChecker in registered checkers
        headerCheckerList = [ckr
                             for ckr in registeredCheckers
                             if type(ckr) == HeaderChecker]
        self.assertTrue(headerCheckerList)
        headerChecker = headerCheckerList[0]
        # Make sure it in option providers
        self.assertTrue(headerChecker in runner.linter.options_providers)
        runner.unregisterChecker(headerChecker)
        # Make sure the instance of HeaderChecker was removed
        registeredCheckers = sum(list(runner.linter._checkers.values()), [])
        self.assertFalse(headerChecker in registeredCheckers)
        # Could not check reports because HeaderChecker is not be
        # recorded in that list
        # Make sure it was removed from option providers
        self.assertFalse(headerChecker in runner.linter.options_providers)
Exemple #3
0
 def makeRunner(self):
     """
     Return a runner instance.
     """
     runner = Runner()
     runner.setOutput(self.outputStream)
     return runner
Exemple #4
0
    def test_setNameExceptions(self):
        """
        Test for twistedchecker.core.runner.Runner.setNameExceptions.
        """
        pathTestFiles = createTestFilesForFindingExceptions(self.mktemp())
        self.clearOutputStream()
        runner = Runner()
        runner.setOutput(self.outputStream)
        # Set the reporter to C{twistedchecker.reporters.test.TestReporter}.
        runner.setReporter(TestReporter())
        # Limit messages.
        runner.linter.disable_noerror_messages()
        runner.linter.enable("C0103")

        workingDir = os.getcwd()
        os.chdir(os.path.dirname(pathTestFiles))
        moduleName = os.path.basename(pathTestFiles)

        exitResult = self.assertRaises(SystemExit, runner.run, [moduleName])

        os.chdir(workingDir)
        predictResult = "11:C0103\n14:C0103\n15:C0103\n"
        outputResult = self.outputStream.getvalue()
        self.assertEqual(outputResult, predictResult)
        self.assertEqual(16, exitResult.code)
Exemple #5
0
 def makeRunner(self):
     """
     Return a runner instance.
     """
     runner = Runner()
     runner.setOutput(self.outputStream)
     return runner
Exemple #6
0
    def test_unregisterChecker(self):
        """
        Test for method unregisterChecker.

        Remove HeaderChecker from registered,
        and make sure it was removed.
        """
        runner = Runner()
        registeredCheckers = sum(list(runner.linter._checkers.values()), [])
        # Make sure an instance of HeaderChecker in registered checkers
        headerCheckerList = [
            ckr for ckr in registeredCheckers if type(ckr) == HeaderChecker
        ]
        self.assertTrue(headerCheckerList)
        headerChecker = headerCheckerList[0]
        # Make sure it in option providers
        self.assertTrue(headerChecker in runner.linter.options_providers)
        runner.unregisterChecker(headerChecker)
        # Make sure the instance of HeaderChecker was removed
        registeredCheckers = sum(list(runner.linter._checkers.values()), [])
        self.assertFalse(headerChecker in registeredCheckers)
        # Could not check reports because HeaderChecker is not be
        # recorded in that list
        # Make sure it was removed from option providers
        self.assertFalse(headerChecker in runner.linter.options_providers)
Exemple #7
0
    def test_runWithErrors(self):
        """
        When checked file is not clean it will exit with non zero exit code.
        """
        runner = Runner()
        runner.setOutput(self.outputStream)

        # The comments functional test is assumed to have at lest one error.
        exitResult = self.assertRaises(SystemExit, runner.run, [
            "twistedchecker.functionaltests.comments"])

        self.assertNotEqual(0, exitResult.code)
Exemple #8
0
    def test_runWithErrors(self):
        """
        When checked file is not clean it will exit with non zero exit code.
        """
        runner = Runner()
        runner.setOutput(self.outputStream)

        # The comments functional test is assumed to have at lest one error.
        exitResult = self.assertRaises(
            SystemExit, runner.run,
            ["twistedchecker.functionaltests.comments"])

        self.assertNotEqual(0, exitResult.code)
Exemple #9
0
    def test_runVersion(self):
        """
        Pass argument "--version" to C{runner.run}, and it should show
        a version infomation, then exit. So that I could know it called pylint.
        """
        runner = Runner()
        runner.setOutput(self.outputStream)

        exitResult = self.assertRaises(SystemExit, runner.run, ["--version"])

        self.assertTrue(self.outputStream.getvalue().count("Python") > 0, \
                        msg="failed to call pylint")
        self.assertIsNone(runner.diffOption)
        self.assertEqual(0, exitResult.code)
Exemple #10
0
    def test_runVersion(self):
        """
        Pass argument "--version" to C{runner.run}, and it should show
        a version infomation, then exit. So that I could know it called pylint.
        """
        runner = Runner()
        runner.setOutput(self.outputStream)

        exitResult = self.assertRaises(SystemExit, runner.run, ["--version"])

        self.assertTrue(self.outputStream.getvalue().count("Python") > 0, \
                        msg="failed to call pylint")
        self.assertIsNone(runner.diffOption)
        self.assertEqual(0, exitResult.code)
Exemple #11
0
    def test_runNoError(self):
        """
        When checked file is clean and has no errors it exit with code 0
        without any other output.
        """
        runner = Runner()
        runner.setOutput(self.outputStream)

        # The twistedchecker/checkers/__init__.py is assumed to be clean.
        exitResult = self.assertRaises(SystemExit, runner.run,
                                       ["twistedchecker.checkers.__init__"])

        self.assertEqual('', self.outputStream.getvalue())
        self.assertEqual(0, exitResult.code)
Exemple #12
0
    def test_runNoError(self):
        """
        When checked file is clean and has no errors it exit with code 0
        without any other output.
        """
        runner = Runner()
        runner.setOutput(self.outputStream)

        # The twistedchecker/checkers/__init__.py is assumed to be clean.
        exitResult = self.assertRaises(SystemExit, runner.run, [
            "twistedchecker.checkers.__init__"])

        self.assertEqual('', self.outputStream.getvalue())
        self.assertEqual(0, exitResult.code)
Exemple #13
0
    def test_restrictCheckers(self):
        """
        Test for method restrictCheckers.

        Manually set allowed messages,
        then check for the result of registered checkers
        after run this method.
        """
        runner = Runner()
        runner.restrictCheckers(HeaderChecker.msgs.keys()[:1])
        # After run it, only HeaderChecker should be left in
        # registered checkers
        registeredCheckers = sum(runner.linter._checkers.values(), [])
        self.assertEqual(len(registeredCheckers), 1)
        self.assertEqual(type(registeredCheckers[0]), HeaderChecker)
Exemple #14
0
    def test_restrictCheckers(self):
        """
        Test for method restrictCheckers.

        Manually set allowed messages,
        then check for the result of registered checkers
        after run this method.
        """
        runner = Runner()
        runner.restrictCheckers(list(HeaderChecker.msgs.keys())[:1])
        # After run it, only HeaderChecker should be left in
        # registered checkers
        registeredCheckers = sum(list(runner.linter._checkers.values()), [])
        self.assertEqual(len(registeredCheckers), 1)
        self.assertEqual(type(registeredCheckers[0]), HeaderChecker)
Exemple #15
0
 def test_findUselessCheckers(self):
     """
     Test for method findUselessCheckers
     """
     runner = Runner()
     registeredCheckers = sum(runner.linter._checkers.values(), [])
     # remove checkers other than header checker
     headerCheckerList = filter(lambda ckr: type(ckr) == HeaderChecker,
                                registeredCheckers)
     self.assertTrue(headerCheckerList)
     headerChecker = headerCheckerList[0]
     uselessCheckers = runner.findUselessCheckers(
                         headerChecker.msgs.keys()[:1])
     self.assertEqual(len(uselessCheckers) + 1, len(registeredCheckers))
     self.assertTrue(headerChecker not in uselessCheckers)
Exemple #16
0
    def test_generateDiff(self):
        """
        Test for twistedchecker.core.runner.Runner.generateDiff.
        """
        oldWarnings = {
            "foo": {"W9001:  1,0: Missing copyright header"},
            "bar": {
                "W9002:  1,0: Missing a reference to test module in header",
                "C0111:  10,0: Missing docstring"
            }
        }

        newWarnings = {
            "foo": {
                "W9001:  1,0: Missing copyright header",
                "C0301: 10,0: Line too long"
            },
            "bar": {
                "W9002:  1,0: Missing a reference to test module in header",
                "C0111:  10,0: Missing docstring"
            },
            "baz": {"W9001:  1,0: Missing copyright header"}
        }

        diffCorrect = {
            "foo": {"C0301: 10,0: Line too long"},
            "baz": {"W9001:  1,0: Missing copyright header"}
        }

        # Make sure generated diff is correct.
        diff = Runner().generateDiff(oldWarnings, newWarnings)
        self.assertEqual(diff, diffCorrect)
Exemple #17
0
 def test_findUselessCheckers(self):
     """
     Test for method findUselessCheckers
     """
     runner = Runner()
     registeredCheckers = sum(list(runner.linter._checkers.values()), [])
     # remove checkers other than header checker
     headerCheckerList = [
         ckr for ckr in registeredCheckers if type(ckr) == HeaderChecker
     ]
     self.assertTrue(headerCheckerList)
     headerChecker = headerCheckerList[0]
     uselessCheckers = runner.findUselessCheckers(
         list(headerChecker.msgs.keys())[:1])
     self.assertEqual(len(uselessCheckers) + 1, len(registeredCheckers))
     self.assertTrue(headerChecker not in uselessCheckers)
Exemple #18
0
 def test_allMessagesAreRegistered(self):
     """
     A test to assume all tests are registered to reporter.
     """
     linter = Runner().linter
     messagesFromTests = self._loadAllowedMessages()
     messagesFromReporter = linter.reporter.messagesAllowed
     messagesDisabled = set(
         linter.cfgfile_parser.get("TWISTEDCHECKER",
                                   "disable").replace(" ", "").split(","))
     self.assertEqual(messagesFromTests - messagesDisabled,
                      messagesFromReporter)
def _runTest(testCase, testFilePath):
    """
    Run a functional test.

    @param testCase: The test case on which to call assertions.
    @type testCase: L{unittest.TestCase}

    @param testFilePath: The path to the module to test.
    @type testFilePath: L{str}
    """
    pathResultFile = testFilePath.replace(".py", ".result")
    moduleName = filenameToModuleName(testFilePath)
    outputStream = NativeStringIO()

    runner = Runner()
    runner.allowOptions = False
    runner.setOutput(outputStream)
    runner.setReporter(TestReporter())

    limits = _parseLimitMessages(testFilePath)
    if limits is not None:
        action, messages = limits
        _setLinterLimits(runner.linter, action, messages)

    exitCode = None
    try:
        runner.run([moduleName])
    except SystemExit as error:
        exitCode = error.code

    # Check the results
    with open(pathResultFile) as f:
        expectedResult = sorted(f.read().strip().splitlines())

    outputResult = sorted(outputStream.getvalue().strip().splitlines())

    try:
        testCase.assertEqual(expectedResult, outputResult)
    except unittest.FailTest:
        testCase.fail(_formatResults(moduleName, expectedResult, outputResult))

    if not expectedResult:
        testCase.assertEqual(0, exitCode)
    else:
        testCase.assertNotEqual(0, exitCode)
Exemple #20
0
    def test_setNameExceptions(self):
        """
        Test for twistedchecker.core.runner.Runner.setNameExceptions.
        """
        pathTestFiles = createTestFilesForFindingExceptions(self.mktemp())
        self.clearOutputStream()
        runner = Runner()
        runner.setOutput(self.outputStream)
        # Set the reporter to C{twistedchecker.reporters.test.TestReporter}.
        runner.setReporter(TestReporter())
        # Limit messages.
        runner.linter.disable_noerror_messages()
        # Enable invalid function names.
        runner.linter.enable("C0103")
        # Enable invalid method names.
        runner.linter.enable("C9302")

        workingDir = os.getcwd()
        os.chdir(os.path.dirname(pathTestFiles))
        moduleName = os.path.basename(pathTestFiles)

        exitResult = self.assertRaises(SystemExit, runner.run, [moduleName])

        os.chdir(workingDir)
        predictResult = "7:C9302\n11:C0103\n14:C0103\n15:C9302\n"
        outputResult = self.outputStream.getvalue()
        self.assertEqual(outputResult, predictResult)
        self.assertEqual(16, exitResult.code)
def _runTest(testCase, testFilePath):
    """
    Run a functional test.

    @param testCase: The test case on which to call assertions.
    @type testCase: L{unittest.TestCase}

    @param testFilePath: The path to the module to test.
    @type testFilePath: L{str}
    """
    pathResultFile = testFilePath.replace(".py", ".result")
    moduleName = filenameToModuleName(testFilePath)
    outputStream = io.BytesIO()

    runner = Runner()
    runner.allowOptions = False
    runner.setOutput(outputStream)
    runner.setReporter(TestReporter())

    limits = _parseLimitMessages(testFilePath)
    if limits is not None:
        action, messages = limits
        _setLinterLimits(runner.linter, action, messages)

    _enablePEP8Checker(runner.linter)

    exitCode = None
    try:
        runner.run([moduleName])
    except SystemExit as error:
        exitCode = error.code

    # Check the results
    expectedResult = open(pathResultFile).read().strip()
    outputResult = outputStream.getvalue().strip()

    try:
        testCase.assertEqual(expectedResult, outputResult)
    except unittest.FailTest:
        testCase.fail(_formatResults(moduleName, expectedResult, outputResult))

    if not expectedResult:
        testCase.assertEqual(0, exitCode)
    else:
        testCase.assertNotEqual(0, exitCode)
Exemple #22
0
    def test_getPathList(self):
        """
        Test for twistedchecker.core.runner.Runner.getPathList.
        """
        workingDir = os.getcwd()
        pathTwistedchecker = os.path.dirname(twistedchecker.__path__[0])
        inputList = [
            os.path.join("twistedchecker", "functionaltests"),
            "twistedchecker.core.util"
        ]
        correctPaths = [
            os.path.join("twistedchecker", "functionaltests"),
            os.path.join("twistedchecker", "core", "util.py")
        ]
        os.chdir(pathTwistedchecker)
        result = Runner().getPathList(inputList)
        # transform them to relative path.
        result = [os.path.relpath(path) for path in result]
        os.chdir(workingDir)

        self.assertEqual(result, correctPaths)
Exemple #23
0
    def test_parseWarnings(self):
        """
        Test for twistedchecker.core.runner.Runner.parseWarnings.
        """
        textWarnings = """
************* Module foo
W9001:  1,0: Missing copyright header
************* Module bar
W9002:  1,0: Missing a reference to test module in header
C0111:  10,0: Missing docstring
        """.strip()

        warningsCorrect = {
            "foo": {
                "W9001:  1,0: Missing copyright header",
            },
            "bar": {
                "W9002:  1,0: Missing a reference "
                "to test module in header", "C0111:  10,0: Missing docstring"
            }
        }

        warnings = Runner().parseWarnings(textWarnings)
        self.assertEqual(warnings, warningsCorrect)
Exemple #24
0
    def test_formatWarnings(self):
        """
        Test for twistedchecker.core.runner.Runner.formatWarnings.
        """
        warnings = {
            "foo": {
                "W9001:  1,0: Missing copyright header",
            },
            "bar": {
                "W9002:  1,0: Missing a reference "
                "to test module in header", "C0111:  10,0: Missing docstring"
            }
        }

        resultCorrect = """
************* Module bar
W9002:  1,0: Missing a reference to test module in header
C0111:  10,0: Missing docstring
************* Module foo
W9001:  1,0: Missing copyright header
        """.strip()

        result = Runner().formatWarnings(warnings)
        self.assertEqual(result, resultCorrect)