Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 0 additions & 75 deletions flask_to_fastapi_migration/guide.md

This file was deleted.

100 changes: 60 additions & 40 deletions freezegun_to_timemachine_migration/run.py
Original file line number Diff line number Diff line change
@@ -1,42 +1,62 @@
import codegen
from codegen import Codebase
codebase = Codebase.from_repo("getmoto/moto", commit="786a8ada7ed0c7f9d8b04d49f24596865e4b7901")

print("🚀 Starting FreezeGun to TimeMachine conversion...")

for file in codebase.files:
if "tests" not in file.filepath:
continue
print(f"📝 Processing: {file.filepath}")
# Update imports
for imp in file.imports:
if imp.symbol_name and 'freezegun' in imp.source:
if imp.name == 'freeze_time':
# required due to Codegen limitations
imp.edit('from time_machine import travel')
else:
imp.set_import_module('time_machine')
# Find all function calls in the file
for fcall in file.function_calls:
# Skip if not a freeze_time call
if 'freeze_time' not in fcall.source:


@codegen.function("freezegun-to-timemachine")
def run(codebase: Codebase):
"""Convert FreezeGun usage to TimeMachine in test files.

This script:
1. Identifies test files using FreezeGun.
2. Updates imports from FreezeGun to TimeMachine.
3. Modifies function calls to include necessary parameters.
"""
print("🚀 Starting FreezeGun to TimeMachine conversion...")

for file in codebase.files:
if "tests" not in file.filepath:
continue
# Get original source and prepare new source
new_source = fcall.source
# Add tick parameter if not present
if not fcall.get_arg_by_parameter_name('tick'):
if new_source.endswith(')'):
new_source = new_source[:-1]
if not new_source.endswith('('):
new_source += ','
new_source += ' tick=False)'
# Replace freeze_time with travel
if '.' in new_source:
new_source = new_source.replace(
'freeze_time', 'travel').replace('freezegun', 'time_machine')
else:
new_source = 'travel' + new_source[len('freeze_time'):]
# Make single edit with complete changes
fcall.edit(new_source)
codebase.commit()

print("✅ FreezeGun to TimeMachine conversion completed successfully!")
print(f"📝 Processing: {file.filepath}")

# Update imports
for imp in file.imports:
if imp.symbol_name and "freezegun" in imp.source:
if imp.name == "freeze_time":
# required due to Codegen limitations
imp.edit("from time_machine import travel")
else:
imp.set_import_module("time_machine")

# Find all function calls in the file
for fcall in file.function_calls:
# Skip if not a freeze_time call
if "freeze_time" not in fcall.source:
continue

# Get original source and prepare new source
new_source = fcall.source

# Add tick parameter if not present
if not fcall.get_arg_by_parameter_name("tick"):
if new_source.endswith(")"):
new_source = new_source[:-1]
if not new_source.endswith("("):
new_source += ","
new_source += " tick=False)"

# Replace freeze_time with travel
if "." in new_source:
new_source = new_source.replace("freeze_time", "travel").replace("freezegun", "time_machine")
else:
new_source = "travel" + new_source[len("freeze_time") :]

# Make single edit with complete changes
fcall.edit(new_source)

codebase.commit()
print("✅ FreezeGun to TimeMachine conversion completed successfully!")


if __name__ == "__main__":
codebase = Codebase.from_repo("getmoto/moto", commit="786a8ada7ed0c7f9d8b04d49f24596865e4b7901")
run(codebase)
39 changes: 0 additions & 39 deletions python2_to_python3/guide.md

This file was deleted.

11 changes: 7 additions & 4 deletions python2_to_python3/run.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import codegen
from codegen import Codebase

# Initialize codebase
codebase = Codebase("./")

# Define the target directory
TARGET_DIR = "repo-before"
TARGET_DIR = "input_repo"


def convert_print_statements(file):
Expand Down Expand Up @@ -115,7 +115,8 @@ def update_iterators(file):
stmt.edit(new_stmt)


def main():
@codegen.function("python2-to-python3")
def run():
"""Main function to run the Python 2 to 3 conversion"""
print("🚀 Starting Python 2 to 3 conversion...\n")

Expand Down Expand Up @@ -149,4 +150,6 @@ def main():


if __name__ == "__main__":
main()
codebase = Codebase("./")

run(codebase)
27 changes: 18 additions & 9 deletions unittest_to_pytest/README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
# unittest to pytest Migration Example
# Unittest to Pytest Migration Example

[![Documentation](https://img.shields.io/badge/docs-docs.codegen.com-blue)](https://docs.codegen.com/tutorials/unittest-to-pytest)
This codemod demonstrates how to automatically migrate `unittest` test suites to `pytest` using Codegen. The migration script simplifies the process by handling all the tedious manual updates automatically.

This example demonstrates how to use Codegen to automatically migrate unittest test suites to pytest. For a complete walkthrough, check out our [tutorial](https://docs.codegen.com/tutorials/unittest-to-pytest).
## How the Migration Script Works

## What This Example Does

The migration script handles four key transformations:
The script automates the entire migration process in a few key steps:

1. **Convert Test Classes and Setup Methods**
```python
Expand All @@ -29,6 +27,8 @@ The migration script handles four key transformations:
user = db.create_user("test")
assert user.name == "test"
```
- Converts `unittest.TestCase` classes to standalone functions
- Replaces `setUp` methods with `pytest` fixtures

2. **Update Assertions**
```python
Expand All @@ -45,6 +45,8 @@ The migration script handles four key transformations:
with pytest.raises(ValueError):
parse_id("invalid")
```
- Replaces `unittest` assertions with `pytest` assertions
- Uses `pytest.raises` for exception testing

3. **Convert Test Discovery**
```python
Expand All @@ -55,6 +57,8 @@ The migration script handles four key transformations:
# To:
# Remove unittest.main() and rename files to test_*.py
```
- Removes `unittest.main()` calls
- Ensures files are named for `pytest` discovery

4. **Modernize Fixtures**
```python
Expand All @@ -68,8 +72,9 @@ The migration script handles four key transformations:
def conn():
return create_db()
```
- Converts class-level setup to session-scoped fixtures

## Running the Example
## Running the Migration

```bash
# Install Codegen
Expand All @@ -84,12 +89,16 @@ The script will process all Python test files in the `repo-before` directory and
## Understanding the Code

- `run.py` - The migration script
- `repo-before/` - Sample unittest test suite to migrate
- `repo-before/` - Sample `unittest` test suite to migrate
- `guide.md` - Additional notes and explanations

## Learn More

- [Full Tutorial](https://docs.codegen.com/tutorials/unittest-to-pytest)
- [pytest Documentation](https://docs.pytest.org/)
- [unittest Documentation](https://docs.python.org/3/library/unittest.html)
- [Codegen Documentation](https://docs.codegen.com)
- [Codegen Documentation](https://docs.codegen.com)

## Contributing

Feel free to submit issues and enhancement requests!
41 changes: 0 additions & 41 deletions unittest_to_pytest/guide.md

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# jj_classes/castle.py


class Castle:
"""Defines the Castle class."""

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# jj_classes/character.py


class Character:
"""Defines the Character class."""

Expand Down
Loading