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 5 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
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -42,6 +42,8 @@ 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.
-n, --dry-run Do not change the file in-place. Useful when running
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why -n? I am using the first letter as a short-hand for each flag; -d would be more consistent (and intuitive imo)

as a linting process.
-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
5 changes: 3 additions & 2 deletions src/flynt/api.py
Expand Up @@ -77,8 +77,9 @@ def default_result():
)
return default_result()

with open(filename, "w", encoding="utf-8") as f:
f.write(new_code)
if not state.dry_run:
with open(filename, "w", encoding="utf-8") as f:
f.write(new_code)

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

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

parser.add_argument(
"-d",
"--dry-run",
action="store_true",
default=False,
help="Do not change the file in-place. Note that this must be used in "
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When suggesting this change, I didn't consider we will end up implementing printing diff. Now it's more relevant to state that proposed diff will be printed to stdout, and --dry-run because useful on it's own without --fail-on-change.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be fixed now.

"conjunction with '--fail-on-change' when used for linting purposes."
)

parser.add_argument(
"-tc",
"--transform-concats",
Expand Down Expand Up @@ -91,6 +100,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 +113,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