From c5ddb8ab1c292a183818d342996951bdc5a2bb40 Mon Sep 17 00:00:00 2001 From: M Bussonnier Date: Wed, 17 Jul 2024 14:47:51 +0200 Subject: [PATCH] add test --- tests/test_cmdline.py | 62 +++++++++++++++++++++++++++++++++---------- 1 file changed, 48 insertions(+), 14 deletions(-) diff --git a/tests/test_cmdline.py b/tests/test_cmdline.py index a01b766d..01e26e42 100644 --- a/tests/test_cmdline.py +++ b/tests/test_cmdline.py @@ -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 @@ -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(''' @@ -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(