Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dry run mode #72

Merged
merged 9 commits into from Aug 9, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Expand Up @@ -42,6 +42,10 @@ optional arguments:
--no-multiline convert only single line expressions
-ll LINE_LENGTH, --line-length LINE_LENGTH
for expressions spanning multiple lines, convert only if the resulting single line will fit into the line length limit. Default value is 88 characters.
-d, --dry-run Do not change the files in-place and print the diff
instead. Note that this must be used in conjunction
with '--fail-on-change' when used for linting
purposes.
-tc, --transform-concats
Replace string concatenations (defined as + operations involving string literals) with f-strings. Available only if flynt is installed with 3.8+
interpreter.
Expand Down
9 changes: 7 additions & 2 deletions src/flynt/api.py
Expand Up @@ -4,6 +4,7 @@
import time
import traceback
from typing import Tuple, List
from difflib import unified_diff

import astor

Expand Down Expand Up @@ -77,8 +78,12 @@ def default_result():
)
return default_result()

with open(filename, "w", encoding="utf-8") as f:
f.write(new_code)
if state.dry_run:
for l in unified_diff(contents.split('\n'), new_code.split('\n'), fromfile=filename):
print(l)
else:
with open(filename, "w", encoding="utf-8") as f:
f.write(new_code)

return True, changes, len(contents), len(new_code)

Expand Down
14 changes: 14 additions & 0 deletions src/flynt/cli.py
Expand Up @@ -52,6 +52,16 @@ def main():
default=88,
)

parser.add_argument(
"-d",
"--dry-run",
action="store_true",
default=False,
help="Do not change the files in-place and print the diff instead. "
"Note that this must be used in conjunction with '--fail-on-change' when "
"used for linting purposes."
)

parser.add_argument(
"-tc",
"--transform-concats",
Expand Down Expand Up @@ -91,6 +101,9 @@ def main():

args = parser.parse_args()

if args.dry_run:
print("Running flynt in dry-run mode. No files will be changed.")

if args.transform_concats:
if sys.version_info < (3, 8):
raise Exception(
Expand All @@ -101,6 +114,7 @@ def main():
state.aggressive = args.aggressive
state.verbose = args.verbose
state.quiet = args.quiet
state.dry_run = args.dry_run

return fstringify(
args.src,
Expand Down
1 change: 1 addition & 0 deletions src/flynt/state.py
Expand Up @@ -3,6 +3,7 @@
verbose = False
quiet = False
aggressive = False
dry_run = False

percent_candidates = 0
percent_transforms = 0
Expand Down
15 changes: 15 additions & 0 deletions test/integration/test_api.py
Expand Up @@ -4,6 +4,7 @@
import pytest

from flynt import api
from flynt import state
from flynt.api import _fstringify_file


Expand Down Expand Up @@ -91,3 +92,17 @@ def broken_fstringify_by_line(*args, **kwargs):

assert not modified
assert content_after == content_before


def test_dry_run(formattable_file, monkeypatch):
monkeypatch.setattr(state, "dry_run", True)
with open(formattable_file) as f:
content_before = f.read()

modified, _, _, _ = _fstringify_file(formattable_file, True, 1000)

with open(formattable_file) as f:
content_after = f.read()

assert modified
assert content_after == content_before