From 2aad8064e1fc688b3f9b316dd6d5bb1ddbfc339c Mon Sep 17 00:00:00 2001 From: Zhenfei Tai Date: Fri, 19 Jul 2024 21:10:07 -0700 Subject: [PATCH] injection extension update --- .../src/guice_injection_migration.py | 78 +++++++++++++++++++ scripts/testng-junit/src/testng2junit5.py | 23 ++---- .../tests/test_migrate_guice_annotation.py | 21 ++--- 3 files changed, 90 insertions(+), 32 deletions(-) create mode 100644 scripts/testng-junit/src/guice_injection_migration.py diff --git a/scripts/testng-junit/src/guice_injection_migration.py b/scripts/testng-junit/src/guice_injection_migration.py new file mode 100644 index 0000000000..9db29f89fb --- /dev/null +++ b/scripts/testng-junit/src/guice_injection_migration.py @@ -0,0 +1,78 @@ +import os +import sys +import re + + +def migration_injection(content): + # Add import statements + new_imports = [ + "import org.junit.jupiter.api.Test;", + "import com.addepar.infra.library.testing.extention.GuiceInjectionExtension;", + ] + if "org.junit.jupiter.api.extension.ExtendWith" not in content: + new_imports.append("import org.junit.jupiter.api.extension.ExtendWith;") + content_new = re.sub( + "import org.junit.jupiter.api.Test;", + "\n".join(new_imports), + content, + ) + + # Remove @SuppressWarnings("EmptyCatchBlock") + content_new = re.sub( + r" @SuppressWarnings\(\"EmptyCatchBlock\"\)\s*\n", "", content_new + ) + + content_new = re.sub( + r"try {\n\s*injector\.injectMembers\(this\);\n\s*} catch \(RuntimeException e\) {\n\s*}", + "injector.injectMembers(this);", + content_new, + ) + + # Add @ExtendWith(GuiceInjectionExtension.class) + new_lines = [] + content_iter = iter(content_new.split("\n")) + for line in content_iter: + if "public class" in line or "public final class" in line: + left_spaces = " " * (len(line) - len(line.lstrip())) + new_lines.append(left_spaces + "@ExtendWith(GuiceInjectionExtension.class)") + new_lines.append(line) + content_new = "\n".join(new_lines) + + return content_new + + +def migrate_tests(test_dir): + test_files = [] + for path, directory, files in os.walk(test_dir): + # print("p", path) + # print("f", files) + for file in files: + if file.endswith(".java"): + test_files.append(os.path.join(path, file)) + + for file_name in test_files: + # print("f ", file_name) + with open(file_name, "r") as f: + content = f.read() + if ( + "@BeforeAll" in content + and "injector.injectMembers" in content + and not "GuiceInjectionExtension" in content + ): + print("Converting ", file_name) + content_new = migration_injection(content) + with open(file_name, "w") as fn: + fn.write(content_new) + + +def main(): + if len(sys.argv) == 1: + print("usage: {} ".format(sys.argv[0])) + sys.exit(1) + + test_dir = sys.argv[1] + migrate_tests(test_dir) + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/scripts/testng-junit/src/testng2junit5.py b/scripts/testng-junit/src/testng2junit5.py index cc061e5e4f..6870b9b587 100644 --- a/scripts/testng-junit/src/testng2junit5.py +++ b/scripts/testng-junit/src/testng2junit5.py @@ -55,12 +55,8 @@ before_inject_template = ''' @BeforeAll - @SuppressWarnings("EmptyCatchBlock") public void setup() { - try { - injector.injectMembers(this); - } catch (RuntimeException e) { - } + injector.injectMembers(this); } ''' @@ -118,6 +114,8 @@ def migrate_imports(content): # refer to migrate_guice_annotation if '@Guice' in content_new and '@BeforeAll' not in content_new: imports.append('import org.junit.jupiter.api.BeforeAll;') + imports.append('import org.junit.jupiter.api.extension.ExtendWith;') + imports.append('import com.addepar.infra.library.testing.extention.GuiceInjectionExtension;') content_new = re.sub('org.junit.jupiter.api.Test;', '\n'.join(imports), content_new) @@ -387,12 +385,8 @@ def migrate_asserts(content): # private final Injector injector = Guice.createInjector(new SomeModule()); # # @BeforeAll -# @SuppressWarnings("EmptyCatchBlock") # public void someTest() { -# try { -# injector.injectMembers(this); -# } catch (RuntimeException e) { -# } +# injector.injectMembers(this); # } # } # @@ -423,6 +417,7 @@ def migrate_guice_annotation(content): or 'public abstract class' in line): left_spaces = ' ' * (len(line) - len(line.lstrip())) new_content.append(left_spaces + '@TestInstance(Lifecycle.PER_CLASS)') + new_content.append(left_spaces + '@ExtendWith(GuiceInjectionExtension.class)') new_content.append(line) if '{' in line: @@ -439,18 +434,12 @@ def migrate_guice_annotation(content): # ....insert here.... if '@BeforeAll' in line: new_content.append(line) - new_content.append(' @SuppressWarnings("EmptyCatchBlock")') # this should be the line of the method and keep adding the line until we get { # insert injectMember as the first line below the below method. insert_lines_after_method( new_content, content_iter, - [ - " try {", - " injector.injectMembers(this);", - " } catch (RuntimeException e) {", - " }", - ], + [" injector.injectMembers(this);"], ) add_injected_member = True continue diff --git a/scripts/testng-junit/src/tests/test_migrate_guice_annotation.py b/scripts/testng-junit/src/tests/test_migrate_guice_annotation.py index cae1d2c27c..dd68710870 100644 --- a/scripts/testng-junit/src/tests/test_migrate_guice_annotation.py +++ b/scripts/testng-junit/src/tests/test_migrate_guice_annotation.py @@ -18,17 +18,14 @@ expected_1 = """ @TestInstance(Lifecycle.PER_CLASS) + @ExtendWith(GuiceInjectionExtension.class) public class SomeTest { private final Injector injector = Guice.createInjector(new SomeModule()); @BeforeAll - @SuppressWarnings("EmptyCatchBlock") public void someTest() { - try { - injector.injectMembers(this); - } catch (RuntimeException e) { - } + injector.injectMembers(this); } } """ @@ -49,17 +46,14 @@ expected_2 = """ @TestInstance(Lifecycle.PER_CLASS) + @ExtendWith(GuiceInjectionExtension.class) public class SomeTest { private final Injector injector = Guice.createInjector(); @BeforeAll - @SuppressWarnings("EmptyCatchBlock") public void someTest() { - try { - injector.injectMembers(this); - } catch (RuntimeException e) { - } + injector.injectMembers(this); } } """ @@ -81,17 +75,14 @@ expected_3 = """ @TestInstance(Lifecycle.PER_CLASS) + @ExtendWith(GuiceInjectionExtension.class) public class SomeTest { private final Injector injector = Guice.createInjector(new TestModuleA(), new Test.ModuleB()); @BeforeAll - @SuppressWarnings("EmptyCatchBlock") public void someTest() { - try { - injector.injectMembers(this); - } catch (RuntimeException e) { - } + injector.injectMembers(this); } } """