Skip to content

Commit

Permalink
Fix state reading from args, support state with --string (#163)
Browse files Browse the repository at this point in the history
* Correctly read State from args

* Support concat and join with `--string` too

Fixes #162
  • Loading branch information
akx committed Jan 8, 2023
1 parent 5b07158 commit b247c6b
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 10 deletions.
32 changes: 22 additions & 10 deletions src/flynt/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from typing import List, Optional

from flynt import __version__
from flynt.api import fstringify, fstringify_code_by_line, fstringify_content
from flynt.api import fstringify, fstringify_content
from flynt.pyproject_finder import find_pyproject_toml, parse_pyproject_toml
from flynt.state import State

Expand Down Expand Up @@ -179,20 +179,17 @@ def run_flynt_cli(arglist: Optional[List[str]] = None) -> int:
level=(logging.DEBUG if args.verbose else logging.CRITICAL),
)

state = State(
aggressive=args.aggressive,
quiet=args.quiet,
dry_run=args.dry_run,
)
state = state_from_args(args)
if args.verbose:
logging.getLogger("flynt").setLevel(logging.DEBUG)

if args.string:
converted, _ = fstringify_code_by_line(
" ".join(args.src),
state=state,
content = " ".join(args.src)
result = fstringify_content(
content,
state,
)
print(converted)
print(result.content if result else content)
return 0
if "-" in args.src:
if len(args.src) > 1:
Expand Down Expand Up @@ -222,6 +219,7 @@ def run_flynt_cli(arglist: Optional[List[str]] = None) -> int:
)
parser.set_defaults(**cfg)
args = parser.parse_args(arglist)
state = state_from_args(args)
if not args.quiet:
print(salutation)
if args.verbose:
Expand All @@ -234,3 +232,17 @@ def run_flynt_cli(arglist: Optional[List[str]] = None) -> int:
fail_on_changes=args.fail_on_change,
state=state,
)


def state_from_args(args) -> State:
return State(
aggressive=args.aggressive,
dry_run=args.dry_run,
len_limit=args.line_length,
multiline=(not args.no_multiline),
quiet=args.quiet,
transform_concat=args.transform_concats,
transform_format=args.transform_format,
transform_join=args.transform_joins,
transform_percent=args.transform_percent,
)
21 changes: 21 additions & 0 deletions test/integration/test_cli.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import io
import os
import sys

import pytest

Expand Down Expand Up @@ -84,6 +85,26 @@ def test_cli_string_unquoted(capsys, code_in, code_out):
assert err == ""


@pytest.mark.skipif(sys.version_info < (3, 8), reason="requires python3.8 or higher")
def test_cli_string_supports_flags(capsys):
"""
Tests a --string invocation also does additional transformations.
See https://github.com/ikamensh/flynt/issues/162
"""
return_code = run_flynt_cli(
[
"-tc",
"--string",
"test_string = 'This' + ' ' + 'may' + ' ' + 'not' + ' ' + 'work'",
]
)
assert return_code == 0
out, err = capsys.readouterr()
assert out.strip() == 'test_string = f"This may not work"'
assert err == ""


@pytest.mark.parametrize("code_in, code_out", valid_snippets)
def test_cli_stdin(monkeypatch, capsys, code_in, code_out):
"""
Expand Down

0 comments on commit b247c6b

Please sign in to comment.