Skip to content

Instantly share code, notes, and snippets.

@AronNovak
Created January 17, 2025 07:59
Show Gist options
  • Select an option

  • Save AronNovak/0cfab9018dc3e74481a6d9d69178111a to your computer and use it in GitHub Desktop.

Select an option

Save AronNovak/0cfab9018dc3e74481a6d9d69178111a to your computer and use it in GitHub Desktop.
import os
import anthropic
import sys
import re
client = anthropic.Client(api_key=sys.argv[1])
wdio_tests_path = "../wdio/specs/backend/"
phpunit_tests_path = "web/modules/custom/server_general/tests/src/ExistingSite/"
# Function to convert a single WDIO test to PHPUnit
def convert_test(wdio_test_path):
# Read the WDIO test file content
with open(wdio_test_path, "r") as file:
wdio_test_content = file.read()
# Create the prompt for the AI
prompt = f"""
You are an AI developer assistant. Your task is to convert this WDIO test to a PHPUnit test.
Here is the WDIO test content:
{wdio_test_content}
The PHPUnit test should follow this sample:
< sample class here >
Please convert the WDIO test to PHPUnit test, ensuring that the test logic is preserved and adapted to the PHPUnit framework.
Only provide the PHP code as a response, nothing else.
"""
# Send the prompt to the AI
message = client.messages.create(
max_tokens=1024,
messages=[
{
"role": "user",
"content": prompt,
}
],
model="claude-3-opus-20240229",
)
# Extract the converted PHPUnit test from the AI response
phpunit_test_content = message.content[0].text if isinstance(message.content, list) else message.content
# Extract the class name from the PHP code
class_name_match = re.search(r'class\s+(\w+)\s+extends\s', phpunit_test_content)
if class_name_match:
class_name = class_name_match.group(1)
# Use the class name as the filename
phpunit_test_name = class_name + ".php"
phpunit_test_path = os.path.join(phpunit_tests_path, phpunit_test_name)
# Ensure the directory exists
os.makedirs(os.path.dirname(phpunit_test_path), exist_ok=True)
with open(phpunit_test_path, "w") as file:
file.write(phpunit_test_content)
print(f"Converted {wdio_test_path} to {phpunit_test_path}")
else:
print(f"Could not find class name in the converted test for {wdio_test_path}")
specific_files = {
"first.js",
"second.js",
}
for filename in os.listdir(wdio_tests_path):
if filename in specific_files:
wdio_test_path = os.path.join(wdio_tests_path, filename)
convert_test(wdio_test_path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment