def test_nonexistent_coafile(self): old_getcwd = os.getcwd os.getcwd = lambda *args: None with retrieve_stdout() as stdout: retval = coala_delete_orig.main() self.assertIn("Can only delete .orig files if ", stdout.getvalue()) self.assertEqual(retval, 255) os.getcwd = old_getcwd
def test_remove_exception(self, mock_glob): # Non existent file mock_glob.return_value = ['non_existent_file'] with retrieve_stderr() as stderr: retval = coala_delete_orig.main(section=self.section) output = stderr.getvalue() self.assertEqual(retval, 0) self.assertIn("Couldn't delete", output) # Directory instead of file with tempfile.TemporaryDirectory() as filename, \ retrieve_stderr() as stderr: mock_glob.return_value = [filename] retval = coala_delete_orig.main(section=self.section) output = stderr.getvalue() self.assertEqual(retval, 0) self.assertIn("Couldn't delete", output)
def test_remove_exception(self, mock_glob): # Non existent file mock_glob.return_value = ['non_existent_file'] with retrieve_stderr() as stderr: retval = coala_delete_orig.main(section=self.section) output = stderr.getvalue() self.assertEqual(retval, 0) self.assertIn("Couldn't delete", output) # Directory instead of file with tempfile.TemporaryDirectory() as filename: with retrieve_stderr() as stderr: mock_glob.return_value = [filename] retval = coala_delete_orig.main(section=self.section) output = stderr.getvalue() self.assertEqual(retval, 0) self.assertIn("Couldn't delete", output)
def test_normal_running(self): with tempfile.TemporaryDirectory() as directory: temporary = tempfile.mkstemp(suffix='.orig', dir=directory) os.close(temporary[0]) section = Section('') section.append(Setting('project_dir', PathArg(directory))) retval = coala_delete_orig.main(section=section) self.assertEqual(retval, 0) self.assertFalse(os.path.isfile(temporary[1]))
def test_remove_exception(self): old_glob = Globbing.glob # Non existent file with retrieve_stdout() as stdout: Globbing.glob = lambda *args: ["non_existent_file"] retval = coala_delete_orig.main(section=self.section) output = stdout.getvalue() self.assertEqual(retval, 0) self.assertIn("Couldn't delete", output) # Directory instead of file with tempfile.TemporaryDirectory() as filename, \ retrieve_stdout() as stdout: Globbing.glob = lambda *args: [filename] retval = coala_delete_orig.main(section=self.section) output = stdout.getvalue() self.assertEqual(retval, 0) self.assertIn("Couldn't delete", output) Globbing.glob = old_glob
def test_remove_exception(self): old_remove = os.remove old_glob = Globbing.glob Globbing.glob = lambda *args: ["file1", "file2"] os.remove = raise_assertion_error with retrieve_stdout() as stdout: retval = coala_delete_orig.main(section=self.section) output = stdout.getvalue() self.assertEqual(retval, 0) self.assertIn("Couldn't delete... file1", output) self.assertIn("Couldn't delete... file2", output) os.remove = old_remove Globbing.glob = old_glob
def test_nonexistent_coafile(self, mocked_getcwd): mocked_getcwd.return_value = None retval = coala_delete_orig.main() self.assertEqual(retval, 255)
def run_coala(log_printer=None, print_results=do_nothing, acquire_settings=fail_acquire_settings, print_section_beginning=do_nothing, nothing_done=do_nothing, show_bears=do_nothing, autoapply=True): """ This is a main method that should be usable for almost all purposes and reduces executing coala to one function call. :param log_printer: A LogPrinter object to use for logging. :param print_results: A callback that takes a LogPrinter, a section, a list of results to be printed, the file dict and the mutable file diff dict. :param acquire_settings: The method to use for requesting settings. It will get a parameter which is a dictionary with the settings name as key and a list containing a description in [0] and the names of the bears who need this setting in all following indexes. :param print_section_beginning: A callback that will be called with a section name string whenever analysis of a new section is started. :param nothing_done: A callback that will be called with only a log printer that shall indicate that nothing was done. :param show_bears: A callback that will be called with first a list of local bears, second a list of global bears to output them. A third bool parameter may be used to indicate if a compressed output (True) or a normal output (False) is desired, the former being used for showing all available bears to the user. :param autoapply: Set to False to autoapply nothing by default; this is overridable via any configuration file/CLI. :return: A dictionary containing a list of results for all analyzed sections as key. """ log_printer = log_printer or LogPrinter(ConsolePrinter()) exitcode = 0 results = None try: yielded_results = yielded_unfixed_results = False did_nothing = True sections, local_bears, global_bears, targets = gather_configuration( acquire_settings, log_printer, autoapply=autoapply) tag = str(sections['default'].get('tag', None)) dtag = str(sections['default'].get('dtag', None)) config_file = os.path.abspath(str(sections["default"].get("config"))) show_all_bears = bool(sections['default'].get('show_all_bears', False)) show_bears_ = bool(sections["default"].get("show_bears", "False")) # Deleting all .orig files, so the latest files are up to date! coala_delete_orig.main(log_printer, sections["default"]) delete_tagged_results(dtag, config_file, log_printer) if show_bears_ or show_all_bears: if show_all_bears: (local_bears, global_bears) = collect_all_bears_from_sections( sections, log_printer) show_bears(local_bears, global_bears, show_all_bears) did_nothing = False else: results = {} for section_name, section in sections.items(): if not section.is_enabled(targets): continue print_section_beginning(section) section_result = execute_section( section=section, global_bear_list=global_bears[section_name], local_bear_list=local_bears[section_name], print_results=print_results, log_printer=log_printer) yielded, yielded_unfixed, results[section_name] = ( simplify_section_result(section_result)) yielded_results = yielded_results or yielded yielded_unfixed_results = (yielded_unfixed_results or yielded_unfixed) did_nothing = False tag_results(tag, config_file, results, log_printer) if did_nothing: nothing_done(log_printer) elif yielded_unfixed_results: exitcode = 1 elif yielded_results: exitcode = 5 except BaseException as exception: # pylint: disable=broad-except exitcode = exitcode or get_exitcode(exception, log_printer) return results, exitcode
def run_coala(log_printer=None, print_results=do_nothing, acquire_settings=fail_acquire_settings, print_section_beginning=do_nothing, nothing_done=do_nothing, show_bears=do_nothing, autoapply=True): """ This is a main method that should be usable for almost all purposes and reduces executing coala to one function call. :param log_printer: A LogPrinter object to use for logging. :param print_results: A callback that takes a LogPrinter, a section, a list of results to be printed, the file dict and the mutable file diff dict. :param acquire_settings: The method to use for requesting settings. It will get a parameter which is a dictionary with the settings name as key and a list containing a description in [0] and the names of the bears who need this setting in all following indexes. :param print_section_beginning: A callback that will be called with a section name string whenever analysis of a new section is started. :param nothing_done: A callback that will be called with only a log printer that shall indicate that nothing was done. :param show_bears: A callback that will be called with first a list of local bears, second a list of global bears to output them. A third bool parameter may be used to indicate if a compressed output (True) or a normal output (False) is desired, the former being used for showing all available bears to the user. :param autoapply: Set to False to autoapply nothing by default; this is overridable via any configuration file/CLI. :return: A dictionary containing a list of results for all analyzed sections as key. """ log_printer = log_printer or LogPrinter(ConsolePrinter()) exitcode = 0 results = None try: yielded_results = yielded_unfixed_results = False did_nothing = True sections, local_bears, global_bears, targets = gather_configuration( acquire_settings, log_printer, autoapply=autoapply) tag = str(sections['default'].get('tag', None)) dtag = str(sections['default'].get('dtag', None)) config_file = os.path.abspath(str(sections["default"].get("config"))) show_all_bears = bool(sections['default'].get('show_all_bears', False)) show_bears_ = bool(sections["default"].get("show_bears", "False")) # Deleting all .orig files, so the latest files are up to date! coala_delete_orig.main(log_printer, sections["default"]) delete_tagged_results(dtag, config_file, log_printer) if show_bears_ or show_all_bears: if show_all_bears: (local_bears, global_bears) = collect_all_bears_from_sections(sections, log_printer) show_bears(local_bears, global_bears, show_all_bears) did_nothing = False else: results = {} for section_name, section in sections.items(): if not section.is_enabled(targets): continue print_section_beginning(section) section_result = execute_section( section=section, global_bear_list=global_bears[section_name], local_bear_list=local_bears[section_name], print_results=print_results, log_printer=log_printer) yielded, yielded_unfixed, results[section_name] = ( simplify_section_result(section_result)) yielded_results = yielded_results or yielded yielded_unfixed_results = ( yielded_unfixed_results or yielded_unfixed) did_nothing = False tag_results(tag, config_file, results, log_printer) if did_nothing: nothing_done(log_printer) elif yielded_unfixed_results: exitcode = 1 elif yielded_results: exitcode = 5 except BaseException as exception: # pylint: disable=broad-except exitcode = exitcode or get_exitcode(exception, log_printer) return results, exitcode
def run_coala(log_printer=None, print_results=do_nothing, acquire_settings=fail_acquire_settings, print_section_beginning=do_nothing, nothing_done=do_nothing, autoapply=True, arg_parser=None): """ This is a main method that should be usable for almost all purposes and reduces executing coala to one function call. :param log_printer: A LogPrinter object to use for logging. :param print_results: A callback that takes a LogPrinter, a section, a list of results to be printed, the file dict and the mutable file diff dict. :param acquire_settings: The method to use for requesting settings. It will get a parameter which is a dictionary with the settings name as key and a list containing a description in [0] and the names of the bears who need this setting in all following indexes. :param print_section_beginning: A callback that will be called with a section name string whenever analysis of a new section is started. :param nothing_done: A callback that will be called with only a log printer that shall indicate that nothing was done. :param autoapply: Set to False to autoapply nothing by default; this is overridable via any configuration file/CLI. :return: A dictionary containing a list of results for all analyzed sections as key. """ log_printer = log_printer or LogPrinter(ConsolePrinter(), LOG_LEVEL.DEBUG) exitcode = 0 results = {} file_dicts = {} try: yielded_results = yielded_unfixed_results = False did_nothing = True sections, local_bears, global_bears, targets = gather_configuration( acquire_settings, log_printer, autoapply=autoapply, arg_parser=arg_parser) log_printer.debug("Platform {} -- Python {}, pip {}, coalib {}".format( platform.system(), platform.python_version(), pip.__version__, VERSION)) tag = str(sections['default'].get('tag', None)) dtag = str(sections['default'].get('dtag', None)) config_file = os.path.abspath(str(sections["default"].get("config"))) # Deleting all .orig files, so the latest files are up to date! coala_delete_orig.main(log_printer, sections["default"]) delete_tagged_results(dtag, config_file, log_printer) for section_name, section in sections.items(): if not section.is_enabled(targets): continue print_section_beginning(section) section_result = execute_section( section=section, global_bear_list=global_bears[section_name], local_bear_list=local_bears[section_name], print_results=print_results, log_printer=log_printer) yielded, yielded_unfixed, results[section_name] = ( simplify_section_result(section_result)) yielded_results = yielded_results or yielded yielded_unfixed_results = (yielded_unfixed_results or yielded_unfixed) did_nothing = False file_dicts[section_name] = section_result[3] tag_results(tag, config_file, results, log_printer) if did_nothing: nothing_done(log_printer) elif yielded_unfixed_results: exitcode = 1 elif yielded_results: exitcode = 5 except BaseException as exception: # pylint: disable=broad-except exitcode = exitcode or get_exitcode(exception, log_printer) return results, exitcode, file_dicts
def run_coala( log_printer=None, print_results=do_nothing, acquire_settings=fail_acquire_settings, print_section_beginning=do_nothing, nothing_done=do_nothing, autoapply=True, ): """ This is a main method that should be usable for almost all purposes and reduces executing coala to one function call. :param log_printer: A LogPrinter object to use for logging. :param print_results: A callback that takes a LogPrinter, a section, a list of results to be printed, the file dict and the mutable file diff dict. :param acquire_settings: The method to use for requesting settings. It will get a parameter which is a dictionary with the settings name as key and a list containing a description in [0] and the names of the bears who need this setting in all following indexes. :param print_section_beginning: A callback that will be called with a section name string whenever analysis of a new section is started. :param nothing_done: A callback that will be called with only a log printer that shall indicate that nothing was done. :param autoapply: Set to False to autoapply nothing by default; this is overridable via any configuration file/CLI. :return: A dictionary containing a list of results for all analyzed sections as key. """ log_printer = log_printer or LogPrinter(ConsolePrinter()) exitcode = 0 results = {} file_dicts = {} try: yielded_results = yielded_unfixed_results = False did_nothing = True sections, local_bears, global_bears, targets = gather_configuration( acquire_settings, log_printer, autoapply=autoapply ) log_printer.debug( "Platform {} -- Python {}, pip {}, coalib {}".format( platform.system(), platform.python_version(), pip.__version__, VERSION ) ) tag = str(sections["default"].get("tag", None)) dtag = str(sections["default"].get("dtag", None)) config_file = os.path.abspath(str(sections["default"].get("config"))) # Deleting all .orig files, so the latest files are up to date! coala_delete_orig.main(log_printer, sections["default"]) delete_tagged_results(dtag, config_file, log_printer) for section_name, section in sections.items(): if not section.is_enabled(targets): continue print_section_beginning(section) section_result = execute_section( section=section, global_bear_list=global_bears[section_name], local_bear_list=local_bears[section_name], print_results=print_results, log_printer=log_printer, ) yielded, yielded_unfixed, results[section_name] = simplify_section_result(section_result) yielded_results = yielded_results or yielded yielded_unfixed_results = yielded_unfixed_results or yielded_unfixed did_nothing = False file_dicts[section_name] = section_result[3] tag_results(tag, config_file, results, log_printer) if did_nothing: nothing_done(log_printer) elif yielded_unfixed_results: exitcode = 1 elif yielded_results: exitcode = 5 except BaseException as exception: # pylint: disable=broad-except exitcode = exitcode or get_exitcode(exception, log_printer) return results, exitcode, file_dicts
def test_nonexistent_coafile(self): old_getcwd = os.getcwd os.getcwd = lambda *args: None retval = coala_delete_orig.main() self.assertEqual(retval, 255) os.getcwd = old_getcwd