Skip to content

Commit

Permalink
injection extension update
Browse files Browse the repository at this point in the history
  • Loading branch information
ztai-add committed Jul 20, 2024
1 parent 74bef24 commit 2aad806
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 32 deletions.
78 changes: 78 additions & 0 deletions scripts/testng-junit/src/guice_injection_migration.py
Original file line number Diff line number Diff line change
@@ -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: {} <directory_to_migrate>".format(sys.argv[0]))
sys.exit(1)

test_dir = sys.argv[1]
migrate_tests(test_dir)


if __name__ == "__main__":
sys.exit(main())
23 changes: 6 additions & 17 deletions scripts/testng-junit/src/testng2junit5.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,8 @@


before_inject_template = ''' @BeforeAll
@SuppressWarnings("EmptyCatchBlock")
public void setup() {
try {
injector.injectMembers(this);
} catch (RuntimeException e) {
}
injector.injectMembers(this);
}
'''

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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);
# }
# }
#
Expand Down Expand Up @@ -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:
Expand All @@ -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
Expand Down
21 changes: 6 additions & 15 deletions scripts/testng-junit/src/tests/test_migrate_guice_annotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
"""
Expand All @@ -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);
}
}
"""
Expand All @@ -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);
}
}
"""
Expand Down

0 comments on commit 2aad806

Please sign in to comment.