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_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_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_find_config(self): config = Config.find(Path(__file__)) expected_config = Path(__file__).parent.parent.joinpath( IMPORTANIZE_SETUP_CONFIG ) self.assertEqual(config.path, expected_config)
def test_plugin(self) -> None: activate_plugin("separate_libs") try: config = Config.default() config.add_imports = [ ImportStatement( "__future__", leafs=[ ImportLeaf("absolute_import"), ImportLeaf("print_function"), ImportLeaf("unicode_literals"), ], ) ] result = next( run_importanize_on_text( self.input_text.read_text(), self.input_text, config, RuntimeConfig(_config=config), )) assert result.organized == self.output_grouped_separate_libs.read_text( ) finally: deactivate_all_plugins()
def test_find_config_current_dir(self) -> None: # If path is a file, and we have a config for the project and no # subconfig, and we dont find the config for the file, return the # passed config. config = Config.find(Path("tests/test_main.py")) # Instead of thekabsolute path, assume, user is running importanize # from the current directory. expected_config = Path( __file__).parent.parent / IMPORTANIZE_SETUP_CONFIG assert config.path == expected_config
def test_merged_config(self) -> None: r = RuntimeConfig( path_names=["-"], formatter_name="lines", length=100, _config=Config(add_imports=[ImportStatement("foo")]), ) assert r.merged_config.length == 100 assert r.merged_config.formatter is LinesFormatter assert not r.merged_config.add_imports
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_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( self, stem: str, leafs: typing.List[ImportLeaf], expected: typing.List[str], sep: str = "\n", inline_comments: typing.List[str] = None, standalone_comments: typing.List[str] = None, ) -> None: """Facilitate the output tests of formatters""" statement = ImportStatement( stem, leafs=leafs, inline_comments=inline_comments, standalone_comments=standalone_comments, ) actual = self.formatter(statement, config=Config.default(), artifacts=Artifacts(sep=sep)).format() assert actual == sep.join(expected)
def test_from_path_no_path(self) -> None: assert not Config.from_path(None)
def test_from_path_strict(self) -> None: with pytest.raises(NoImportanizeConfig): Config.from_path(__file__, strict=True)
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_group_name(self) -> None: with pytest.raises(InvalidConfig): Config.from_ini( StdPath("invalid.ini"), "\n".join(["[importanize]", "groups=", " foo"]), )
def test_ini_invalid_require_packages(self) -> None: with pytest.raises(InvalidConfig): Config.from_ini( StdPath("invalid.ini"), "\n".join(["[importanize]", "groups=", " packages"]), )
def test_merge(self) -> None: c = Config.default().merge(Config(length=100, formatter=LinesFormatter)) assert c.length == 100 assert c.formatter is LinesFormatter
def test_ini_no_section(self) -> None: with pytest.raises(NoImportanizeConfig): Config.from_ini(StdPath("invalid.ini"), "")
def test_default(self) -> None: assert "*/.tox/*" in Config.default().exclude
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(self) -> None: config = Config.find(Path(__file__)) expected_config = Path( __file__).parent.parent / IMPORTANIZE_SETUP_CONFIG assert config.path == expected_config
def test_bool(self) -> None: assert not Config.default() assert Config(path=Path("setup.py"))
def test_find_config_current_dir_invalid(self) -> None: assert not Config.find(StdPath("tests/test_main.py"))
def test_as_json(self) -> None: c = Config.default() assert json.loads(c.as_json()) == c.as_dict()
def test_as_dict(self) -> None: assert Config.default().as_dict()["formatter"] == "grouped" assert Config.default().as_dict()["groups"][0] == {"type": "stdlib"}
def test_find_config_nonfound(self): config = Config.find(Path(Path(__file__).root)) self.assertIsNone(config.path)
def test_find_invalid(self) -> None: path = Path(__file__).parent / "test_data" / "invalid" assert not Config.find(path, root=path.parent)
def test_str(self) -> None: assert str(Config.default()) == "<default pep8>" assert str(Config(path=Path("setup.py"))) == "setup.py"
def test_find_config_nonfound(self) -> None: assert not Config.find(Path(Path(__file__).root))
def test_from_config(self) -> None: groups = ImportGroups.from_config(config=Config( groups=[GroupConfig(type="stdlib")])) assert len(groups.groups) == 1 assert isinstance(groups.groups[0], StdLibGroup)
def test_repr(self) -> None: assert repr(Config.default()) == Config.default().as_ini()