def test_ini_invalid_length(self) -> None: with pytest.raises(InvalidConfig): Config.from_ini(StdPath("invalid.ini"), "\n".join(["[importanize]", "length=a"])) with pytest.raises(InvalidConfig): Config.from_ini(StdPath("invalid.ini"), "\n".join(["[importanize]", "length=1"]))
def test_ini(self) -> None: Config.from_ini(StdPath("config.ini"), "\n".join(["[importanize]" ])) == Config(StdPath("config.ini")) Config.from_ini( StdPath("config.ini"), "\n".join([ "[importanize]", "after_imports_new_lines=5", "length=100", "formatter=lines", "groups=", " stdlib", " packages:mypackage", "exclude=exclude", "add_imports=", " import foo", "allow_plugins=false", ]), ) == Config( path=StdPath("config.json"), after_imports_new_lines=5, length=100, formatter=LinesFormatter, groups=[ GroupConfig(type="stdlib"), GroupConfig(type="packages", packages=["foo"]), ], exclude=["exclude"], add_imports=[ImportStatement("foo")], are_plugins_allowed=False, )
def test_file(self) -> None: p = StdPath("test").with_streams( filein=OpenBytesIO(b"hello world"), fileout=OpenBytesIO() ) assert p.read_text() == "hello world" p.write_text("hello mars") assert p.fileout.read() == b"hello mars"
def test_pep263(self) -> None: p = StdPath("-").with_streams( stdin=io.BytesIO("# -*- coding: ascii -*-\nпривет".encode("utf-8")), stdout=io.BytesIO(), ) assert p.is_file() with pytest.raises(UnicodeDecodeError): assert p.read_text()
def test_ini_invalid_new_lines(self) -> None: with pytest.raises(InvalidConfig): Config.from_ini( StdPath("invalid.ini"), "\n".join(["[importanize]", "after_imports_new_lines=a"]), ) with pytest.raises(InvalidConfig): Config.from_ini( StdPath("invalid.ini"), "\n".join(["[importanize]", "after_imports_new_lines=10"]), )
def test_ini_invalid_add_imports(self) -> None: with pytest.raises(InvalidConfig): Config.from_ini( StdPath("invalid.ini"), "\n".join( ["[importanize]", "add_imports=", " from foo from bar"]), )
def test_aggregator_invalid(self) -> None: stdin = OpenBytesIO((TEST_DATA / "invalid.py").read_bytes()) stdout = OpenBytesIO() result = Aggregator( RuntimeConfig( _config=CONFIG, _paths=[StdPath("-").with_streams(stdin=stdin, stdout=stdout)], show_header=True, ))() assert result == 1 assert stdout.read().decode("utf-8") == ""
def test_invalid_config(self) -> None: stdin = OpenBytesIO((TEST_DATA / "output_grouped.py").read_bytes()) stdout = OpenBytesIO() result = Aggregator( RuntimeConfig( config_path=__file__, _paths=[StdPath("-").with_streams(stdin=stdin, stdout=stdout)], show_header=True, ))() assert result == 1 assert stdout.read().decode("utf-8") == ""
def test_aggregator_no_changes(self) -> None: stdin = OpenBytesIO((TEST_DATA / "output_grouped.py").read_bytes()) stdout = OpenBytesIO() result = Aggregator( RuntimeConfig( _config=CONFIG, _paths=[StdPath("-").with_streams(stdin=stdin, stdout=stdout)], show_header=True, is_subconfig_allowed=False, ))() assert result == 0 assert stdout.read().decode("utf-8") == ""
def test_json(self) -> None: assert Config.from_json(StdPath("config.json"), "{}") == Config(StdPath("config.json")) assert Config.from_json( StdPath("config.json"), json.dumps({ "after_imports_new_lines": "5", "length": "100", "formatter": "lines", "groups": [{ "type": "remainder" }], "exclude": ["exclude"], "add_imports": ["import foo"], }), ) == Config( path=StdPath("config.json"), after_imports_new_lines=5, length=100, formatter=LinesFormatter, groups=[GroupConfig(type="remainder")], exclude=["exclude"], add_imports=(ImportStatement("foo"), ), )
def test_aggregator_subconfig(self) -> None: stdout = OpenBytesIO() result = Aggregator( RuntimeConfig( _config=CONFIG, _paths=[ StdPath( TEST_DATA / "subconfig" / "input_few_imports.py").with_streams(fileout=stdout) ], show_header=True, ))() assert result == 0 assert (TEST_DATA / "subconfig" / "output_grouped_few_imports.py" ).read_text() in stdout.read().decode("utf-8")
def test_print_aggregator_piped(self) -> None: stdin = OpenBytesIO((TEST_DATA / "input.py").read_bytes()) stdout = OpenBytesIO() out = OpenStringIO() result = PrintAggregator( RuntimeConfig( _config=CONFIG, _paths=[StdPath("-").with_streams(stdin=stdin, stdout=stdout)], show_header=True, stdout=out, is_subconfig_allowed=False, ))() assert result == 0 assert str(TEST_DATA / "input.py") not in out.read() assert ( TEST_DATA / "output_grouped.py").read_text() in stdout.read().decode("utf-8")
def test_stdin(self) -> None: p = StdPath("-").with_streams( stdin=io.BytesIO(b" hello\n world\n"), stdout=io.BytesIO() ) assert p.is_file() assert p.read_text() == "hello\nworld\n" p.write_text("hello\nmars\n") p.stdout.seek(0) assert p.stdout.read() == b" hello\n mars\n"
def test_ini_invalid_formatter(self) -> None: with pytest.raises(InvalidConfig): Config.from_ini( StdPath("invalid.ini"), "\n".join(["[importanize]", "formatter=foo"]), )
def test_ini_invalid_require_packages(self) -> None: with pytest.raises(InvalidConfig): Config.from_ini( StdPath("invalid.ini"), "\n".join(["[importanize]", "groups=", " packages"]), )
from importanize.config import IMPORTANIZE_SETUP_CONFIG, Config, GroupConfig from importanize.formatters import GroupedFormatter, LinesFormatter from importanize.importanize import ( Aggregator, CIAggregator, ListAggregator, PrintAggregator, Result, RuntimeConfig, run_importanize_on_source, run_importanize_on_text, ) from importanize.statements import ImportLeaf, ImportStatement from importanize.utils import OpenBytesIO, OpenStringIO, StdPath TEST_DATA = StdPath(__file__).parent / "test_data" CONFIG = Config( # need path so that subconfigs are not found within test_data subfolder path=TEST_DATA / "config", add_imports=[ ImportStatement( "__future__", leafs=[ ImportLeaf("absolute_import"), ImportLeaf("print_function"), ImportLeaf("unicode_literals"), ], ) ], are_plugins_allowed=False, plugins=[],
def test_ini_invalid_group_name(self) -> None: with pytest.raises(InvalidConfig): Config.from_ini( StdPath("invalid.ini"), "\n".join(["[importanize]", "groups=", " foo"]), )
def test_ini_no_section(self) -> None: with pytest.raises(NoImportanizeConfig): Config.from_ini(StdPath("invalid.ini"), "")
def test_ini_invalid_plugins(self) -> None: with pytest.raises(InvalidConfig): Config.from_ini( StdPath("invalid.ini"), "\n".join(["[importanize]", "plugins=\n foo"]), )
def test_json_invalid_json(self) -> None: with pytest.raises(NoImportanizeConfig): Config.from_json(StdPath("invalid.json"), "invalid data") with pytest.raises(InvalidConfig): Config.from_json(StdPath("invalid.json"), "{'length': 'a'}")
def test_find_config_current_dir_invalid(self) -> None: assert not Config.find(StdPath("tests/test_main.py"))