Beispiel #1
0
def runner(to_lex=None, out=sys.stdout, only_lex=True):
    sys.argv = [sys.argv[0]]
    if to_lex is not None:
        sys.argv.append("--path")
        sys.argv.append(os.path.abspath(to_lex))
    if only_lex:
        sys.argv.append("--lex")
    parent_conn, child_conn = Pipe()
    interface_handler = InterfaceHandler(out=out, conn=child_conn)
    interface_handler.start()
    setup_test_data(interface_handler, parent_conn)
    return interface_handler
Beispiel #2
0
 def test_printed_message(self):
     out = StringIO()
     self.interface_handler = InterfaceHandler(out)
     error_handling_file_path = os.path.join(TESTS_RESOURCES_PATH,
                                             'error_handling_files')
     self.interface_handler.args = Namespace(lex=True)
     self.interface_handler.lint_all(error_handling_file_path)
     result = out.getvalue()
     out.close()
     second_line = re.match(r".*\n(?P<second_line>.*)\n",
                            result).group("second_line")
     self.assertEqual(
         '\x1b[93mWARNING: Invalid indentation, you must indent with tabs. Line number: 1',
         second_line)
Beispiel #3
0
 def test_file_reader_no_encoding_check(self):
     bslint.load_config_file(default_filepath=TEST_CONFIG_FILE_PATH)
     file_path = self.ascii_chars_file_path
     fo = open(file_path, "r+")
     str_to_lex = fo.read()
     result = InterfaceHandler.file_reader(file_path)
     expected = {"invalid_encoding": None, "file_content": str_to_lex}
     self.assertEqual(expected, result)
 def setUp(self):
     self.interface_handler = InterfaceHandler()
class TestErrorHandling(unittest.TestCase):
    def setUp(self):
        self.interface_handler = InterfaceHandler()

    def test_no_warnings_in_file_with_errors(self):
        skeleton_main_with_errors_path = os.path.join(
            LEXING_TEST_FILES_PATH, 'skeleton-main-with-errors.brs')
        self.interface_handler.args = Namespace(lex=True)
        self.interface_handler.lint_file(skeleton_main_with_errors_path)
        self.assertEqual(1, len(self.interface_handler.messages[const.ERRORS]))
        self.assertEqual(0,
                         len(self.interface_handler.messages[const.WARNINGS]))

    def test_all_lexed_correctly(self):
        out = StringIO()
        brs_file_path = os.path.join(LEXING_TEST_FILES_PATH,
                                     'basic-string-assignment.brs')
        result = bslint.bslint.runner(to_lex=brs_file_path,
                                      out=out).printed_output
        self.assertEqual(
            msg_handler.get_print_msg(print_const.NO_MANIFEST) +
            msg_handler.get_print_msg(print_const.NO_BSLINTRC) +
            msg_handler.get_print_msg(print_const.LINTING_COMPLETE) +
            msg_handler.get_print_msg(print_const.ALL_LINTED_CORRECTLY),
            result)

    def test_all_parsed_correctly(self):
        out = StringIO()
        brs_file_path = os.path.join(LEXING_TEST_FILES_PATH,
                                     'basic-string-assignment.brs')
        result = bslint.bslint.runner(to_lex=brs_file_path,
                                      out=out,
                                      only_lex=False).printed_output
        self.assertEqual(
            msg_handler.get_print_msg(print_const.NO_MANIFEST) +
            msg_handler.get_print_msg(print_const.NO_BSLINTRC) +
            msg_handler.get_print_msg(print_const.LINTING_COMPLETE) +
            msg_handler.get_print_msg(print_const.ALL_LINTED_CORRECTLY),
            result)

    def test_only_warnings_in_file_without_errors(self):
        skeleton_main_path = os.path.join(LEXING_TEST_FILES_PATH,
                                          'skeleton-main.brs')
        self.interface_handler.args = Namespace(lex=True)
        self.interface_handler.lint_file(skeleton_main_path)
        self.assertEqual(0, len(self.interface_handler.messages[const.ERRORS]))
        self.assertEqual(1,
                         len(self.interface_handler.messages[const.WARNINGS]))

    def test_parsing_directory(self):
        self.interface_handler.args = Namespace(lex=True)
        self.interface_handler.lint_all(LEXING_TEST_FILES_PATH)
        self.assertEqual(1,
                         len(self.interface_handler.messages[const.WARNINGS]))
        self.assertEqual(1, len(self.interface_handler.messages[const.ERRORS]))

    def test_warning_message_printed(self):
        out = StringIO()
        warning_file_path = os.path.join(
            TESTS_RESOURCES_PATH, 'error_handling_files/warning-file.brs')
        result = bslint.bslint.runner(to_lex=warning_file_path,
                                      out=out).printed_output
        self.assertEqual(
            msg_handler.get_print_msg(print_const.NO_MANIFEST) +
            msg_handler.get_print_msg(print_const.NO_BSLINTRC) +
            msg_handler.get_print_msg(print_const.FILE_NAME,
                                      ["file://" + warning_file_path]) +
            msg_handler.get_print_msg(
                const.WARNINGS,
                [msg_handler.get_error_msg(err_const.TAB_AND_SPACES, [1])]) +
            msg_handler.get_print_msg(
                const.WARNINGS,
                [msg_handler.get_error_msg(err_const.TAB_AND_SPACES, [2])]) +
            msg_handler.get_print_msg(print_const.WARNINGS_IN_FILE, [2]) +
            msg_handler.get_print_msg(print_const.LINTING_COMPLETE) +
            msg_handler.get_print_msg(print_const.TOTAL_WARNINGS, [2]) +
            msg_handler.get_print_msg(print_const.TOTAL_ERRORS, [0]), result)

    def test_error_message_printed(self):
        out = StringIO()
        error_file_path = os.path.join(TESTS_RESOURCES_PATH,
                                       'error_handling_files/error-file.brs')
        result = bslint.bslint.runner(to_lex=error_file_path,
                                      out=out).printed_output
        print(result)
        self.assertEqual(
            msg_handler.get_print_msg(print_const.NO_MANIFEST) +
            msg_handler.get_print_msg(print_const.NO_BSLINTRC) +
            msg_handler.get_print_msg(print_const.FILE_NAME,
                                      ["file://" + error_file_path]) +
            msg_handler.get_print_msg(const.ERRORS, [
                msg_handler.get_error_msg(err_const.UNMATCHED_QUOTATION_MARK,
                                          ['"error file', 1])
            ]) + msg_handler.get_print_msg(print_const.LINTING_COMPLETE) +
            msg_handler.get_print_msg(print_const.TOTAL_WARNINGS, [0]) +
            msg_handler.get_print_msg(print_const.TOTAL_ERRORS, [1]), result)

    def test_error_handled_on_last_line_without_return(self):
        error_file_path = os.path.join(TESTS_RESOURCES_PATH,
                                       'error_handling_files/error-file.brs')
        chars = open(error_file_path, "r+").read()
        result = Lexer().lex(chars)
        expected = [
            msg_handler.get_error_msg(err_const.UNMATCHED_QUOTATION_MARK,
                                      ['"error file', 1])
        ]
        self.assertEqual(expected, result["Tokens"])
Beispiel #6
0
class TestErrorHandling(unittest.TestCase):
    def setUp(self):
        self.interface_handler = InterfaceHandler()

    def test_no_warnings_in_file_with_errors(self):
        skeleton_main_with_errors_path = os.path.join(
            LEXING_TEST_FILES_PATH, 'skeleton-main-with-errors.brs')
        self.interface_handler.args = Namespace(lex=True)
        self.interface_handler.lint_file(skeleton_main_with_errors_path)
        self.assertEqual(1, len(self.interface_handler.messages[const.ERRORS]))
        self.assertEqual(0,
                         len(self.interface_handler.messages[const.WARNINGS]))

    def test_all_linted_correctly(self):
        out = StringIO()
        brs_file_path = os.path.join(LEXING_TEST_FILES_PATH,
                                     'basic-string-assignment.brs')
        result = bslint.bslint.runner(to_lex=brs_file_path,
                                      out=out).printed_output
        self.assertEqual(
            msg_handler.get_print_msg(print_const.NO_MANIFEST) +
            msg_handler.get_print_msg(print_const.NO_BSLINTRC) +
            msg_handler.get_print_msg(print_const.LINTING_COMPLETE) +
            msg_handler.get_print_msg(print_const.ALL_LINTED_CORRECTLY),
            result)

    def test_only_warnings_in_file_without_errors(self):
        skeleton_main_path = os.path.join(LEXING_TEST_FILES_PATH,
                                          'skeleton-main.brs')
        self.interface_handler.args = Namespace(lex=True)
        self.interface_handler.lint_file(skeleton_main_path)
        self.assertEqual(0, len(self.interface_handler.messages[const.ERRORS]))
        self.assertEqual(1,
                         len(self.interface_handler.messages[const.WARNINGS]))

    def test_parsing_directory(self):
        self.interface_handler.args = Namespace(lex=True)
        self.interface_handler.lint_all(LEXING_TEST_FILES_PATH)
        self.assertEqual(1,
                         len(self.interface_handler.messages[const.WARNINGS]))
        self.assertEqual(1, len(self.interface_handler.messages[const.ERRORS]))

    def test_printed_message(self):
        out = StringIO()
        self.interface_handler = InterfaceHandler(out)
        error_handling_file_path = os.path.join(TESTS_RESOURCES_PATH,
                                                'error_handling_files')
        self.interface_handler.args = Namespace(lex=True)
        self.interface_handler.lint_all(error_handling_file_path)
        result = out.getvalue()
        out.close()
        second_line = re.match(r".*\n(?P<second_line>.*)\n",
                               result).group("second_line")
        self.assertEqual(
            '\x1b[93mWARNING: Invalid indentation, you must indent with tabs. Line number: 1',
            second_line)

    def test_error_handled_on_last_line_without_return(self):
        error_file_path = os.path.join(TESTS_RESOURCES_PATH,
                                       'error_handling_files/error-file.brs')
        chars = open(error_file_path, "r+").read()
        result = Lexer().lex(chars)
        expected = [
            msg_handler.get_error_msg(err_const.UNMATCHED_QUOTATION_MARK,
                                      ['"error file', 1])
        ]
        self.assertEqual(expected, result["Tokens"])
Beispiel #7
0
def main():
    try:
        interface_handler = InterfaceHandler()
        interface_handler.start()
    except KeyboardInterrupt:
        pass
 def setUp(self):
     self.InterfaceHandler = InterfaceHandler()