Skip to content

Commit

Permalink
support recursively migrate all tests in directory
Browse files Browse the repository at this point in the history
  • Loading branch information
ztai-add committed Aug 27, 2024
1 parent b5417d9 commit 1f96297
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 22 deletions.
59 changes: 37 additions & 22 deletions scripts/testng-junit/src/testng2junit5.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
def migrate_imports(content):
# Updates import statements from TestNG to JUnit.
content_new = re.sub('org.testng.annotations.Test', 'org.junit.jupiter.api.Test', content)
content_new = re.sub('org.junit.Test', 'org.junit.jupiter.api.Test', content_new)

# Before
content_new = re.sub('org.testng.annotations.BeforeSuite',
Expand All @@ -76,6 +77,7 @@ def migrate_imports(content):
'org.junit.jupiter.api.BeforeAll', content_new)

content_new = re.sub('org.testng.annotations.BeforeTest', 'org.junit.jupiter.api.BeforeAll', content_new)
content_new = re.sub('org.junit.Before', 'org.junit.jupiter.api.BeforeEach', content_new)

# After
content_new = re.sub('org.testng.annotations.AfterMethod', 'org.junit.jupiter.api.AfterEach', content_new)
Expand Down Expand Up @@ -106,6 +108,7 @@ def migrate_imports(content):
imports = ['org.junit.jupiter.api.Test;']
if re.compile(r'@Test\s?\(enabled').search(content_new):
imports.append('import org.junit.jupiter.api.Disabled;')
content_new = re.sub('import org.junit.Ignore;', 'import org.junit.jupiter.api.Disabled;', content_new)

if 'expectedExceptionsMessageRegExp' in content_new or 'expectedExceptions' in content_new:
imports.append('import static org.junit.jupiter.api.Assertions.assertThrows;')
Expand Down Expand Up @@ -139,6 +142,7 @@ def migrate_testng_annotations(content):
content_new = re.sub(r'@BeforeMethod\s+(public|protected|private)', r'@BeforeEach\n public', content_new)
content_new = re.sub(r'@BeforeEach(\(alwaysRun\s+=\s+true\))?', '@BeforeEach', content_new)
content_new = re.sub(r'@BeforeMethod', '@BeforeEach', content_new)
content_new = re.sub(r'@Before\n', r'@BeforeEach\n', content_new)

content_new = re.sub(r'@AfterMethod\s+(public|protected|private)', r'@AfterEach\n public', content_new)

Expand Down Expand Up @@ -167,6 +171,7 @@ def migrate_testng_annotations(content):
content_new = re.sub('BaseJerseyTestNG', 'BaseJerseyJUnit', content_new)

content_new = re.sub(r'@Test\s?\(enabled(\s*)=(\s*)false\)', '@Disabled @Test', content_new)
content_new = re.sub(r'@Ignore\n', r'@Disabled\n', content_new)

# Ensure test methods are public
content_new = re.sub('@Test\n void', '@Test\n public void', content_new)
Expand Down Expand Up @@ -196,9 +201,14 @@ def migrate_mockito_rule_annotation(content):

def migrate_data_providers(content):
"""TestNG allows a DataProvider to be renamed."""
if '@DataProvider' not in content:
if 'dataProvider' not in content:
return content

if 'import org.junit.jupiter.params.ParameterizedTest' not in content:
content = re.sub('org.junit.jupiter.api.Test;', '''org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;''', content)

'''
Make a list of tuples mapping the new name to original name.
Expand All @@ -222,7 +232,14 @@ def migrate_data_providers(content):
content_new = re.sub(r'@Test\(dataProvider\s*=\s*(.*?)\s*(?:,\s*(.*))?\)',
'@ParameterizedTest(\\2)\n @MethodSource(\\1)', content_new)

# Convert @ParameterizedTest() to @ParameterizedTest
# Convert @ParameterizedTest(*) to @ParameterizedTest
matches = re.findall(r'@ParameterizedTest\((\s*enabled\s*=\s*(true|false)\s*)?\)', content_new)
if any(match[1] == 'false' for match in matches):
if 'import org.junit.jupiter.api.Disabled;' not in content_new:
content_new = re.sub('import org.junit.jupiter.api.Test;',
'''import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Disabled;''', content_new)
content_new = re.sub(r'@ParameterizedTest\(\s*enabled\s*=\s*false\s*\)', '@Disabled\n @ParameterizedTest', content_new)
content_new = re.sub(r'@ParameterizedTest\(\)', '@ParameterizedTest', content_new)

# Use provider function name in @MethodSource
Expand Down Expand Up @@ -624,27 +641,29 @@ def insert_lines_after_method(contents, content_iter, new_lines):


def migrate_buck(buck_module):
buck_file = buck_module + "/BUCK"
if os.path.isfile(buck_file):
with open(buck_file, 'r') as f_in:
content = f_in.read()
if 'java_test_internal' in content and 'test_type = "junit"' not in content:
content = re.sub(r'java_test_internal\(',
'java_test_internal(\n test_type = "junit",', content)
for path, _, files in os.walk(buck_module):
for file in files:
if file.endswith('BUCK'):
file_path = os.path.join(path, file)
with open(file_path, 'r') as f_in:
content = f_in.read()
if 'java_test_internal' in content and 'test_type = "junit"' not in content:
content = re.sub(r'java_test_internal\(',
'java_test_internal(\n test_type = "junit",', content)

if 'TEST_DEPS' in content and '//infra/library/lang:test_utils' not in content:
content = re.sub(r'TEST_DEPS = \[',
'TEST_DEPS = [\n "//infra/library/lang:test_utils",', content)
if 'TEST_DEPS' in content and '//infra/library/lang:test_utils' not in content:
content = re.sub(r'TEST_DEPS = \[',
'TEST_DEPS = [\n "//infra/library/lang:test_utils",', content)

with open(buck_file, 'w') as fn_out:
fn_out.write(content)
with open(file_path, 'w') as fn_out:
fn_out.write(content)


def migrate_tests(test_dir):
test_files = []
for path, directory, files in os.walk(test_dir):
for path, _, files in os.walk(test_dir):
for file in files:
if file.endswith('.java') \
if 'src/test' in path and file.endswith('.java') \
and not file.endswith('AbstractJerseyTestNG.java') \
and not file.endswith('Assert.java') \
and not file.endswith('AssertionUtils.java') \
Expand Down Expand Up @@ -681,12 +700,8 @@ def main():
sys.exit(1)

buck_module = sys.argv[1]
test_dir = buck_module
if 'src/test' not in buck_module:
test_dir = buck_module + '/src/test'
migrate_buck(buck_module)

migrate_tests(test_dir)
migrate_buck(buck_module)
migrate_tests(buck_module)


if __name__ == '__main__':
Expand Down
10 changes: 10 additions & 0 deletions scripts/testng-junit/src/tests/test_migrate_data_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@
public void parsesUrlPrefixFromValidAdiaHost(URI adiaHost) {
assertThat(AdiaReverseProxyServletModule.getUrlPrefix(adiaHost), is("/proxy-adia1"));
}
@Test(dataProvider = "notFound", enabled = false)
public void testNotFoundProvider(String name) {
}
"""

expected = """
Expand Down Expand Up @@ -83,6 +87,12 @@
public void parsesUrlPrefixFromValidAdiaHost(URI adiaHost) {
assertThat(AdiaReverseProxyServletModule.getUrlPrefix(adiaHost), is("/proxy-adia1"));
}
@Disabled
@ParameterizedTest
@MethodSource("notFound")
public void testNotFoundProvider(String name) {
}
"""


Expand Down

0 comments on commit 1f96297

Please sign in to comment.