Skip to content

Commit

Permalink
fix(ux): allow both removed-in and deprecated-in as well as multiple …
Browse files Browse the repository at this point in the history
…arguments for both
  • Loading branch information
akx authored and browniebroke committed Oct 13, 2020
1 parent ab4c20e commit d89676b
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 23 deletions.
22 changes: 15 additions & 7 deletions django_codemod/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,30 +108,38 @@ def djcodemod():
"removed_in",
help="The version of Django where feature are removed.",
type=VersionParamType(REMOVED_IN),
multiple=True,
)
@click.option(
"--deprecated-in",
"deprecated_in",
help="The version of Django where deprecations started.",
type=VersionParamType(DEPRECATED_IN),
multiple=True,
)
def run(
removed_in: Tuple[int, int], deprecated_in: Tuple[int, int], path: List[str]
removed_in: List[Tuple[int, int]],
deprecated_in: List[Tuple[int, int]],
path: List[str],
) -> None:
"""
Automatically fixes deprecations removed Django deprecations.
This command takes the path to target as argument and a version of
Django to select code modifications to apply.
"""
if not any((removed_in, deprecated_in)) or all((removed_in, deprecated_in)):
codemodders_set = set()
for version in removed_in:
codemodders_set |= set(REMOVED_IN[version])
for version in deprecated_in:
codemodders_set |= set(DEPRECATED_IN[version])
if not codemodders_set:
raise click.UsageError(
"You must specify either '--removed-in' or '--deprecated-in' but not both."
"No codemods were selected. "
"Specify '--removed-in' and/or '--deprecated-in'."
)
if removed_in:
codemodders_list = REMOVED_IN[removed_in]
else:
codemodders_list = DEPRECATED_IN[deprecated_in]
codemodders_list = sorted(codemodders_set, key=lambda m: m.__name__)
click.echo(f"Running codemods: {', '.join(m.__name__ for m in codemodders_list)}")
command_instance = BaseCodemodCommand(codemodders_list, CodemodContext())
call_command(command_instance, path)

Expand Down
6 changes: 5 additions & 1 deletion docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ This package provides a `djcodemod` command line tool with 2 main supported work
- Prepare future upgrades by modifying code which is deprecated in a given version using the `deprecated-in` option
- Fix previous deprecated code which is removed in a given version using the `removed-in` option

## Workflows
## Workflows

### Deprecations

Expand All @@ -25,6 +25,10 @@ This is more a just in time operation, assuming you haven't kept up to date with
djcodemod run --removed-in 4.0 .
```

### Mix and match

Both `--deprecated-in` and `--removed-in` can be passed at once, and both accept multiple repetitions.

## Next steps

In either case, the tool will take a few minutes and apply a set of modifications to your code to fix deprecated or removed usages of Django. This should be much faster than doing it manually and much robust than a simple find & replace.
Expand Down
18 changes: 3 additions & 15 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,24 +41,12 @@ def test_missing_argument(cli_runner):
assert "Error: Missing argument 'PATH" in result.output


@pytest.mark.parametrize(
"command_line",
[
# Missing options
["run", "."],
# Too many options
["run", "--removed-in", "3.0", "--deprecated-in", "2.0", "."],
],
)
def test_invalid_options(cli_runner, command_line):
def test_no_mods_selected(cli_runner):
"""Should explain missing option."""
result = cli_runner.invoke(cli.djcodemod, command_line)
result = cli_runner.invoke(cli.djcodemod, ["run", "."])

assert result.exit_code == 2
assert (
"Error: You must specify either '--removed-in' or "
"'--deprecated-in' but not both." in result.output
)
assert "No codemods were selected" in result.output


def test_help(cli_runner):
Expand Down

0 comments on commit d89676b

Please sign in to comment.