示例#1
0
 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"]))
示例#2
0
 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"]),
         )
示例#3
0
 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"]),
         )
示例#4
0
    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)
示例#5
0
    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()
示例#6
0
    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
示例#7
0
    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
示例#8
0
 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,
     )
示例#9
0
 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"), ),
     )
示例#10
0
 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)
示例#11
0
 def test_from_path_no_path(self) -> None:
     assert not Config.from_path(None)
示例#12
0
 def test_from_path_strict(self) -> None:
     with pytest.raises(NoImportanizeConfig):
         Config.from_path(__file__, strict=True)
示例#13
0
 def test_ini_invalid_formatter(self) -> None:
     with pytest.raises(InvalidConfig):
         Config.from_ini(
             StdPath("invalid.ini"),
             "\n".join(["[importanize]", "formatter=foo"]),
         )
示例#14
0
 def test_ini_invalid_group_name(self) -> None:
     with pytest.raises(InvalidConfig):
         Config.from_ini(
             StdPath("invalid.ini"),
             "\n".join(["[importanize]", "groups=", "  foo"]),
         )
示例#15
0
 def test_ini_invalid_require_packages(self) -> None:
     with pytest.raises(InvalidConfig):
         Config.from_ini(
             StdPath("invalid.ini"),
             "\n".join(["[importanize]", "groups=", "  packages"]),
         )
示例#16
0
 def test_merge(self) -> None:
     c = Config.default().merge(Config(length=100,
                                       formatter=LinesFormatter))
     assert c.length == 100
     assert c.formatter is LinesFormatter
示例#17
0
 def test_ini_no_section(self) -> None:
     with pytest.raises(NoImportanizeConfig):
         Config.from_ini(StdPath("invalid.ini"), "")
示例#18
0
 def test_default(self) -> None:
     assert "*/.tox/*" in Config.default().exclude
示例#19
0
 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'}")
示例#20
0
    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
示例#21
0
 def test_bool(self) -> None:
     assert not Config.default()
     assert Config(path=Path("setup.py"))
示例#22
0
 def test_find_config_current_dir_invalid(self) -> None:
     assert not Config.find(StdPath("tests/test_main.py"))
示例#23
0
 def test_as_json(self) -> None:
     c = Config.default()
     assert json.loads(c.as_json()) == c.as_dict()
示例#24
0
 def test_as_dict(self) -> None:
     assert Config.default().as_dict()["formatter"] == "grouped"
     assert Config.default().as_dict()["groups"][0] == {"type": "stdlib"}
示例#25
0
    def test_find_config_nonfound(self):
        config = Config.find(Path(Path(__file__).root))

        self.assertIsNone(config.path)
示例#26
0
 def test_find_invalid(self) -> None:
     path = Path(__file__).parent / "test_data" / "invalid"
     assert not Config.find(path, root=path.parent)
示例#27
0
 def test_str(self) -> None:
     assert str(Config.default()) == "<default pep8>"
     assert str(Config(path=Path("setup.py"))) == "setup.py"
示例#28
0
 def test_find_config_nonfound(self) -> None:
     assert not Config.find(Path(Path(__file__).root))
示例#29
0
    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)
示例#30
0
 def test_repr(self) -> None:
     assert repr(Config.default()) == Config.default().as_ini()