def test_handles_additions_tag(self):
        cli.list_region_tags(
            os.path.join(self.smoke_path, 'polyglot_snippet_data.json'),
            self.smoke_path, True, False, True, False)

        out, _ = self.capsys.readouterr()
        assert 'additions_tests (1 test(s))' in out
    def test_shows_test_counts(self):
        cli.list_region_tags(
            os.path.join(self.path, 'polyglot_snippet_data.json'),
            os.path.join(self.path, 'flask'), True, False, True, False)
        out, _ = self.capsys.readouterr()

        assert '1 test(s)' in out
 def test_handle_untested_marker(self):
     cli.list_region_tags(
         os.path.join(self.smoke_path, 'polyglot_snippet_data.json'),
         self.smoke_path, False, False, False, False)
     out, _ = self.capsys.readouterr()
     assert re.search('Ignored region tags.+undetectable_tag', out,
                      re.DOTALL)
    def test_ignores_dotfile_subdirectories(self):
        cli.list_region_tags(
            os.path.join(self.dotfile_path, 'polyglot_snippet_data.json'),
            self.dotfile_path, True, True, False, False)

        out, _ = self.capsys.readouterr()

        assert 'dotfile' not in out
    def test_warn_on_improper_region_tag_usage(self):
        bad_tag_path = os.path.join(self.cli_path, 'bad_region_tag/')
        with self.assertRaises(ValueError) as err:
            cli.list_region_tags(
                os.path.join(bad_tag_path, 'polyglot_snippet_data.json'),
                bad_tag_path, True, False, True, True)

        assert 'Mismatched region tags' in str(err.exception)
    def test_shows_file_names(self):
        cli.list_region_tags(
            os.path.join(self.path, 'polyglot_snippet_data.json'), self.path,
            True, False, True, True)

        out, _ = self.capsys.readouterr()
        assert re.search('sign_handler.+webapp2/webapp2_main.py', out,
                         re.DOTALL)
    def test_test_counts_warns_if_detected_is_false(self):
        cli.list_region_tags(
            os.path.join(self.path, 'polyglot_snippet_data.json'), self.path,
            False, True, True, False)

        out, _ = self.capsys.readouterr()

        assert 'Undetected/ignored region tags do not have test counts' in out
    def test_recursively_lists_detected_region_tags(self):
        cli.list_region_tags(
            os.path.join(self.path, 'polyglot_snippet_data.json'), self.path,
            True, False, False, False)
        out, _ = self.capsys.readouterr()

        assert 'root_tag' in out
        assert 'sample_route' in out
    def test_detects_tests_wrapped_in_classes(self):
        cli.list_region_tags(
            os.path.join(self.parser_path, 'polyglot_snippet_data.json'),
            os.path.join(self.parser_path, 'class_wrapped_tests'), True, False,
            True, False)

        out, _ = self.capsys.readouterr()

        assert '2 test(s)' in out
    def test_sums_test_counts_from_constituents_and_detected_methods(self):
        additions_path = os.path.join(self.cli_path, 'additions')
        cli.list_region_tags(
            os.path.join(additions_path, 'polyglot_snippet_data.json'),
            additions_path, True, False, True, False)

        out, _ = self.capsys.readouterr()

        assert 'untested_method (2 test(s))' in out
    def test_recursively_lists_undetected_region_tags(self):
        cli.list_region_tags(
            os.path.join(self.path, 'polyglot_snippet_data.json'), self.path,
            False, True, True, False)
        out, _ = self.capsys.readouterr()

        assert 'empty_tag' in out

        # Ignored tags should be listed separately
        assert re.search('Ignored.+app', out, re.DOTALL)
    def test_overwrite_should_replace_detected_region_tags(self):
        data_path = os.path.join(TEST_DATA_PATH, 'yaml/overwrite_tests')

        cli.list_region_tags(
            os.path.join(data_path, 'polyglot_snippet_data.json'), data_path,
            True, False, True, False)

        out, _ = self.capsys.readouterr()

        assert 'method_1 (2 test(s))' in out
        assert 'method_2 (1 test(s))' in out
    def test_errors_on_corrupt_repo_json(self):
        path_mock = MagicMock(wraps=os.path)
        path_mock.isfile = MagicMock(return_value=False)

        with patch('ast_parser.core.analyze.path', path_mock, create=True):
            with self.assertRaises(ValueError) as err:
                cli.list_region_tags(
                    os.path.join(self.parser_path,
                                 'polyglot_snippet_data.json'),
                    self.parser_path, True, False, True, False)

        expected = 'Try regenerating polyglot_snippet_data.json'
        assert expected in str(err.exception)
def parse_args(input_args: List[str]) -> None:
    """Parse user-supplied CLI arguments

    This method parses incoming CLI arguments and invokes the appropriate
    polyglot parser functionality. It can also be used to invoke the polyglot
    parser programmatically (which is useful for testing).

    Args:
        input_args: a list of input arguments
    """
    parser = argparse.ArgumentParser(
        description=__doc__,
        formatter_class=argparse.RawDescriptionHelpFormatter)

    subparsers = parser.add_subparsers(dest='command')

    # Syntactic sugar: use helper methods for more complex CLI commands
    _generate_list_region_tags_parser(subparsers)
    _generate_list_source_files_parser(subparsers)

    subparsers.add_parser('inject-snippet-mapping',
                          help=cli.inject_snippet_mapping.__doc__)

    subparsers.add_parser('validate-yaml', help=cli.validate_yaml.__doc__)

    # Add cross-command required parameters
    parser.add_argument('root_dir', help='Root directory')
    parser.add_argument('--output_file',
                        help='File to write output to. Omit to use stdout.',
                        required=False)

    # Route CLI calls
    args = parser.parse_args(input_args)
    data_json = os.path.join(args.root_dir, 'polyglot_snippet_data.json')

    if args.command == 'list-region-tags':
        cli.list_region_tags(data_json, args.root_dir, args.detected,
                             args.undetected, args.show_test_counts,
                             args.show_filenames, args.output_file)
    elif args.command == 'list-source-files':
        cli.list_source_files(data_json, args.root_dir, args.tested_files,
                              args.output_file)
    elif args.command == 'inject-snippet-mapping':
        cli.inject_snippet_mapping(data_json, args.root_dir,
                                   sys.stdin.readlines(), args.output_file)
    elif args.command == 'validate-yaml':
        cli.validate_yaml(data_json, args.root_dir)