def test_ask_to_select_languages(self):
        languages = [('lang1', 50), ('lang2', 25), ('language3', 25)]
        res = []
        with simulate_console_inputs('1 2') as generator:
            res = ask_to_select_languages(languages, self.printer, False)
            self.assertEqual(generator.last_input, 0)
        self.assertEqual(res, [('lang1', 50), ('lang2', 25)])

        with simulate_console_inputs('6', '1') as generator:
            res = ask_to_select_languages(languages, self.printer, False)
            self.assertEqual(generator.last_input, 1)
        self.assertEqual(res, [('lang1', 50)])

        with simulate_console_inputs('\n') as generator:
            res = ask_to_select_languages(languages, self.printer, False)
            self.assertEqual(generator.last_input, 0)
        self.assertEqual(res, [('lang1', 50), ('lang2', 25),
                               ('language3', 25)])
    def test_ask_to_select_languages(self):
        languages = [('lang1', 50), ('lang2', 25), ('language3', 25)]
        res = []
        with simulate_console_inputs('1 2') as generator:
            res = ask_to_select_languages(languages, self.printer, False)
            self.assertEqual(generator.last_input, 0)
        self.assertEqual(res, [('lang1', 50), ('lang2', 25)])

        with simulate_console_inputs('6', '1') as generator:
            res = ask_to_select_languages(languages, self.printer, False)
            self.assertEqual(generator.last_input, 1)
        self.assertEqual(res, [('lang1', 50)])

        with simulate_console_inputs('\n') as generator:
            res = ask_to_select_languages(languages, self.printer, False)
            self.assertEqual(generator.last_input, 0)
        self.assertEqual(res, [('lang1', 50), ('lang2', 25),
                               ('language3', 25)])
    def test_print_used_languages(self):
        with retrieve_stdout() as custom_stdout:
            print_used_languages(self.printer, [('Python', 100)])
            res = custom_stdout.getvalue()
            self.assertIn("Python", res)
            self.assertIn("100%", res)

            print_used_languages(self.printer, [('Python', 100)], False)
            res = custom_stdout.getvalue()
            self.assertIn("Python", res)
            self.assertIn("100%", res)

            ask_to_select_languages([('Python', 100)], self.printer, True)
            res = custom_stdout.getvalue()
            self.assertIn("Python", res)
            self.assertIn("100%", res)

        with retrieve_stdout() as custom_stdout:
            print_used_languages(self.printer, [('Python', 75), ('C++', 25)])
            self.assertIn("75%\n", custom_stdout.getvalue())
    def test_print_used_languages(self):
        with retrieve_stdout() as custom_stdout:
            print_used_languages(self.printer, [('Python', 100)])
            res = custom_stdout.getvalue()
            self.assertIn("Python", res)
            self.assertIn("100%", res)

            print_used_languages(self.printer, [('Python', 100)], False)
            res = custom_stdout.getvalue()
            self.assertIn("Python", res)
            self.assertIn("100%", res)

            ask_to_select_languages([('Python', 100)], self.printer, True)
            res = custom_stdout.getvalue()
            self.assertIn("Python", res)
            self.assertIn("100%", res)

        with retrieve_stdout() as custom_stdout:
            print_used_languages(self.printer, [('Python', 75), ('C++', 25)])
            self.assertIn("75%\n", custom_stdout.getvalue())
def main():
    global MAX_ARGS_GREEN_MODE, MAX_VALUES_GREEN_MODE
    arg_parser = _get_arg_parser()
    args = arg_parser.parse_args()

    logging.basicConfig(stream=sys.stdout)
    printer = ConsolePrinter()
    logging.getLogger(__name__)

    fpc = None
    project_dir = os.getcwd()

    if args.green_mode:
        args.no_filter_by_capabilities = None
        args.incomplete_sections = None
        if args.max_args:
            MAX_ARGS_GREEN_MODE = args.max_args
        if args.max_values:
            MAX_VALUES_GREEN_MODE = args.max_values

    if not args.green_mode and (args.max_args or args.max_values):
        logging.warning(' --max-args and --max-values can be used '
                        'only with --green-mode. The arguments will '
                        'be ignored.')

    if not args.non_interactive and not args.green_mode:
        fpc = FilePathCompleter()
        fpc.activate()
        print_welcome_message(printer)
        printer.print(PROJECT_DIR_HELP)
        project_dir = ask_question(
            'What is your project directory?',
            default=project_dir,
            typecast=valid_path)
        fpc.deactivate()

    project_files, ignore_globs = get_project_files(
        None,
        printer,
        project_dir,
        fpc,
        args.non_interactive)

    used_languages = list(get_used_languages(project_files))
    used_languages = ask_to_select_languages(used_languages, printer,
                                             args.non_interactive)

    extracted_information = collect_info(project_dir)

    relevant_bears = filter_relevant_bears(
        used_languages, printer, arg_parser, extracted_information)

    if args.green_mode:
        bear_settings_obj = collect_bear_settings(relevant_bears)
        green_mode(
            project_dir, ignore_globs, relevant_bears, bear_settings_obj,
            MAX_ARGS_GREEN_MODE,
            MAX_VALUES_GREEN_MODE,
            project_files,
            printer,
        )
        exit()

    print_relevant_bears(printer, relevant_bears)

    if args.non_interactive and not args.incomplete_sections:
        unusable_bears = get_non_optional_settings_bears(relevant_bears)
        remove_unusable_bears(relevant_bears, unusable_bears)
        print_relevant_bears(printer, relevant_bears, 'usable')

    settings = generate_settings(
        project_dir,
        project_files,
        ignore_globs,
        relevant_bears,
        extracted_information,
        args.incomplete_sections)

    write_coafile(printer, project_dir, settings)
Example #6
0
def main():
    global MAX_ARGS_GREEN_MODE, MAX_VALUES_GREEN_MODE
    arg_parser = _get_arg_parser()
    args = arg_parser.parse_args()

    logging.basicConfig(stream=sys.stdout)
    printer = ConsolePrinter()
    logging.getLogger(__name__)

    fpc = None
    project_dir = os.getcwd()

    if args.green_mode:
        args.no_filter_by_capabilities = None
        args.incomplete_sections = None
        if args.max_args:
            MAX_ARGS_GREEN_MODE = args.max_args
        if args.max_values:
            MAX_VALUES_GREEN_MODE = args.max_values

    if not args.green_mode and (args.max_args or args.max_values):
        logging.warning(' --max-args and --max-values can be used '
                        'only with --green-mode. The arguments will '
                        'be ignored.')

    if not args.non_interactive and not args.green_mode:
        fpc = FilePathCompleter()
        fpc.activate()
        print_welcome_message(printer)
        printer.print(PROJECT_DIR_HELP)
        project_dir = ask_question('What is your project directory?',
                                   default=project_dir,
                                   typecast=valid_path)
        fpc.deactivate()

    project_files, ignore_globs = get_project_files(None, printer, project_dir,
                                                    fpc, args.non_interactive)

    used_languages = list(get_used_languages(project_files))
    used_languages = ask_to_select_languages(used_languages, printer,
                                             args.non_interactive)

    extracted_information = collect_info(project_dir)

    relevant_bears = filter_relevant_bears(used_languages, printer, arg_parser,
                                           extracted_information)

    if args.green_mode:
        bear_settings_obj = collect_bear_settings(relevant_bears)
        green_mode(
            project_dir,
            ignore_globs,
            relevant_bears,
            bear_settings_obj,
            MAX_ARGS_GREEN_MODE,
            MAX_VALUES_GREEN_MODE,
            project_files,
            printer,
        )
        exit()

    print_relevant_bears(printer, relevant_bears)

    if args.non_interactive and not args.incomplete_sections:
        unusable_bears = get_non_optional_settings_bears(relevant_bears)
        remove_unusable_bears(relevant_bears, unusable_bears)
        print_relevant_bears(printer, relevant_bears, 'usable')

    settings = generate_settings(project_dir, project_files, ignore_globs,
                                 relevant_bears, extracted_information,
                                 args.incomplete_sections)

    write_coafile(printer, project_dir, settings)