From 44458b3ba947fe009a0cc4588714f2a189a455b6 Mon Sep 17 00:00:00 2001 From: Jon Michael Aanes Date: Wed, 5 Mar 2025 09:52:00 +0100 Subject: [PATCH] Filtering, to avoid changing all tests in the repository --- standardize_test_format/__init__.py | 62 ++++++++++++++++++++--------- standardize_test_format/__main__.py | 33 ++++++++------- 2 files changed, 63 insertions(+), 32 deletions(-) diff --git a/standardize_test_format/__init__.py b/standardize_test_format/__init__.py index 63f997a..d684b8f 100644 --- a/standardize_test_format/__init__.py +++ b/standardize_test_format/__init__.py @@ -4,15 +4,31 @@ Tool for standardizing Partisia's Java tests. Is capable of migrating from Javadoc based property specification to the `@DisplayName` specification and back. + +## Usage + +Convert tests with `@DisplayName` to Javadoc: + +```bash +python -m standardize_java_text --javadoc --only-displayname FILE... +``` + +It it recommended that you do this with no unstaged changes, to more easily +verify the result. + +You may need to call your formatting tool afterwards, as the converter doesn't +take formatting into account. """ import enum import hashlib import re +import dataclasses from ._version import __version__ -__all__ = ['__version__', 'standardize_java_text', 'NamingScheme'] +__all__ = ['__version__', 'standardize_java_text', 'NamingScheme', 'Config'] + class NamingScheme(enum.Enum): @@ -21,10 +37,19 @@ class NamingScheme(enum.Enum): FROM_DESC = 'desc' +@dataclasses.dataclass +class Config: + with_javadoc: bool + with_display_name: bool + only_for_javadoc: bool + only_for_display_name: bool + naming_scheme: NamingScheme + + PATTERN_JAVADOC = r'/\*\*(?P.*(\n\s*\*.*)*?)\s*\*/' PATTERN_DISPLAY_NAME_EXPRESSION = r'\s*(?:"[^"]*?")(?:\s*\+\s*"[^"]*?")*\s*' PATTERN_DISPLAY_NAME = ( - r'@DisplayName\((?P' + PATTERN_DISPLAY_NAME_EXPRESSION + r')\)' + r'@DisplayName\((?P' + PATTERN_DISPLAY_NAME_EXPRESSION + r')\)' ) TEST_PATTERN: re.Pattern = re.compile( @@ -142,25 +167,30 @@ def parse_display_name_to_description(text: str | None): def replace_test_pattern( match: re.Match, - with_javadoc: bool, - with_display_name: bool, - naming_scheme: NamingScheme, + config: Config, ) -> str: javadoc = parse_javadoc_to_description(match.group('javadoc')) annotation = match.group('annotation').strip() visibility = (match.group('visibility') or '').strip() name = match.group('name').strip() - display = parse_display_name_to_description(match.group('display')) + displayname = parse_display_name_to_description(match.group('displayname')) - description = display or javadoc or from_camel_case(name) + # Filtering + if config.only_for_javadoc and not javadoc: + return match.group(0) + if config.only_for_display_name and not displayname: + return match.group(0) + + # Formatting + description = displayname or javadoc or from_camel_case(name) description = re.sub(r'[ \t]+', ' ', description) - if naming_scheme == NamingScheme.PRESERVE: + if config.naming_scheme == NamingScheme.PRESERVE: pass - elif naming_scheme == NamingScheme.FROM_DESC: + elif config.naming_scheme == NamingScheme.FROM_DESC: name = to_camel_case(description) - elif naming_scheme == NamingScheme.HASH_OF_DESC: + elif config.naming_scheme == NamingScheme.HASH_OF_DESC: h = hashlib.sha256() h.update(description.encode('utf8')) name = 'test' + h.hexdigest() @@ -171,8 +201,8 @@ def replace_test_pattern( description, annotation, visibility, - with_javadoc=with_javadoc, - with_display_name=with_display_name, + with_javadoc=config.with_javadoc, + with_display_name=config.with_display_name, ) @@ -181,16 +211,12 @@ IMPORT_DISPLAY_NAME = 'import org.junit.jupiter.api.DisplayName;' def standardize_java_text( text: str, - with_javadoc: bool, - with_display_name: bool, - naming_scheme: NamingScheme, + config: Config, ): text = TEST_PATTERN.sub( lambda m: replace_test_pattern( m, - with_javadoc, - with_display_name, - naming_scheme, + config, ), text, ) diff --git a/standardize_test_format/__main__.py b/standardize_test_format/__main__.py index ebf2f76..c55281d 100644 --- a/standardize_test_format/__main__.py +++ b/standardize_test_format/__main__.py @@ -1,7 +1,7 @@ import argparse import pathlib -from . import NamingScheme, standardize_java_text +from . import NamingScheme, standardize_java_text, Config def test_files(repo: pathlib.Path) -> list[pathlib.Path]: @@ -11,18 +11,14 @@ def test_files(repo: pathlib.Path) -> list[pathlib.Path]: def standardize_in_file( path: pathlib.Path, inline: bool, - with_javadoc: bool, - with_display_name: bool, - naming_scheme: NamingScheme, + config: Config, ): with open(path) as f: text = f.read() text_updated = standardize_java_text( text, - with_javadoc, - with_display_name, - naming_scheme=naming_scheme, + config, ) if text_updated == text: return @@ -36,15 +32,18 @@ def standardize_in_file( def argument_parser(): argparser = argparse.ArgumentParser() - argparser.add_argument('repo', type=pathlib.Path) - argparser.add_argument('-i', action='store_true') - argparser.add_argument('--javadoc', action='store_true') - argparser.add_argument('--displayname', action='store_true') + argparser.add_argument('repo', type=pathlib.Path, help='Repository') + argparser.add_argument('-i', action='store_true', help='Write to files') + argparser.add_argument('--javadoc', action='store_true', help='Generate Javadoc for tests') + argparser.add_argument('--displayname', action='store_true', help='Generate DisplayName for tests') + argparser.add_argument('--only-javadoc', action='store_true', help='Only change tests with an existing Javadoc') + argparser.add_argument('--only-displayname', action='store_true', help='Only change test with an existing DisplayName') argparser.add_argument( '--naming', type=NamingScheme, choices=list(NamingScheme), default=NamingScheme.FROM_DESC, + help='How to derive the name of the test method', ) return argparser @@ -52,13 +51,19 @@ def argument_parser(): def main(): args = argument_parser().parse_args() + config = Config( + with_javadoc=args.javadoc, + with_display_name=args.displayname, + only_for_javadoc=args.only_javadoc, + only_for_display_name=args.only_displayname, + naming_scheme=args.naming, + ) + for path in test_files(args.repo): standardize_in_file( path, inline=args.i, - with_javadoc=args.javadoc, - with_display_name=args.displayname, - naming_scheme=args.naming, + config=config, )