import os
import re
import glob
def process_file(filepath):
with open(filepath, 'r') as f:
content = f.read()
pattern = r'#\[cfg\(test\)\]\s*mod\s+([a-zA-Z0-9_]+)\s*\{'
matches = list(re.finditer(pattern, content))
if not matches:
return
extracted_tests = []
new_content = ""
last_idx = 0
mod_names_extracted = []
for match in matches:
start_idx = match.end() - 1 mod_name = match.group(1)
brace_count = 0
end_idx = -1
for i in range(start_idx, len(content)):
if content[i] == '{':
brace_count += 1
elif content[i] == '}':
brace_count -= 1
if brace_count == 0:
end_idx = i
break
if end_idx != -1:
test_content = content[start_idx+1:end_idx].strip() + '\n'
extracted_tests.append((mod_name, test_content))
new_content += content[last_idx:match.start()]
mod_names_extracted.append(mod_name)
test_filename = f"{os.path.splitext(os.path.basename(filepath))[0]}_test.rs"
new_content += f'#[cfg(test)]\n#[path = "{test_filename}"]\nmod {mod_name};\n'
last_idx = end_idx + 1
break
if extracted_tests:
new_content += content[last_idx:]
test_filename = f"{os.path.splitext(os.path.basename(filepath))[0]}_test.rs"
test_filepath = os.path.join(os.path.dirname(filepath), test_filename)
with open(filepath, 'w') as f:
f.write(new_content)
with open(test_filepath, 'w') as f:
f.write(extracted_tests[0][1])
print(f"Processed {filepath} -> {test_filepath}")
for root, dirs, files in os.walk("src"):
for file in files:
if file.endswith(".rs") and not file.endswith("_test.rs"):
process_file(os.path.join(root, file))