Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
Carreau committed Jul 17, 2024
1 parent 7272bdb commit c5ddb8a
Showing 1 changed file with 48 additions and 14 deletions.
62 changes: 48 additions & 14 deletions tests/test_cmdline.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,26 +60,34 @@ def test_tidy_imports_quiet_1():

def test_tidy_imports_log_level_1():
with EnvVarCtx(PYFLYBY_LOG_LEVEL="WARNING"):
result = pipe([BIN_DIR+"/tidy-imports"], stdin="os, sys")
expected = dedent('''
result = pipe([BIN_DIR + "/tidy-imports"], stdin="os, sys")
expected = dedent(
"""
import os
import sys
os, sys
''').strip()
"""
).strip()
assert result == expected


def test_tidy_imports_filename_action_print_1():
with tempfile.NamedTemporaryFile(suffix=".py", mode='w+') as f:
f.write(dedent('''
with tempfile.NamedTemporaryFile(suffix=".py", mode="w+") as f:
f.write(
dedent(
"""
# hello
def foo():
foo() + os + sys
''').lstrip())
"""
).lstrip()
)
f.flush()
result = pipe([BIN_DIR+"/tidy-imports", f.name])
expected = dedent('''
result = pipe([BIN_DIR + "/tidy-imports", f.name])
expected = (
dedent(
"""
[PYFLYBY] {f.name}: added 'import os'
[PYFLYBY] {f.name}: added 'import sys'
# hello
Expand All @@ -88,10 +96,28 @@ def foo():
def foo():
foo() + os + sys
''').strip().format(f=f)
"""
)
.strip()
.format(f=f)
)
assert result == expected


def test_unsafe_cwd():
with tempfile.TemporaryDirectory() as d:
from pathlib import Path

p = Path(d)
unsafe = p / "foo#bar" / "foo#qux"
file = unsafe / "foo.py"
unsafe.mkdir(parents=True)
file.write_text("np.sin")
result = pipe([BIN_DIR + "/py"], cwd=unsafe, stdin="np")
assert "Unsafe" not in result
assert result == "[PYFLYBY] import numpy as np"


def test_tidy_imports_filename_action_replace_1():
with tempfile.NamedTemporaryFile(suffix=".py", delete=False, mode='w+') as f:
f.write(dedent('''
Expand Down Expand Up @@ -763,24 +789,32 @@ def test_tidy_imports_forward_references():
with tempfile.TemporaryDirectory() as temp_dir:
foo = os.path.join(temp_dir, "foo.py")
with open(foo, "w") as foo_fp:
foo_fp.write(dedent("""
foo_fp.write(
dedent(
"""
from __future__ import annotations
class A:
param1: str
param2: B
class B:
param1: str
""").lstrip())
"""
).lstrip()
)
foo_fp.flush()

dot_pyflyby = os.path.join(temp_dir, ".pyflyby")
with open(dot_pyflyby, "w") as dot_pyflyby_fp:
dot_pyflyby_fp.write(dedent("""
dot_pyflyby_fp.write(
dedent(
"""
from foo import A, B
""").lstrip())
"""
).lstrip()
)
dot_pyflyby_fp.flush()
with CwdCtx(temp_dir):
result = pipe(
Expand Down

0 comments on commit c5ddb8a

Please sign in to comment.