Repository: psf/black
File: tests/test_black.py (Python)

Code before:

    def test_parse_pyproject_toml(self) -> None:
        test_toml_file = THIS_DIR / "test.toml"
        config = black.parse_pyproject_toml(str(test_toml_file))
        self.assertEqual(config["verbose"], 1)
        self.assertEqual(config["check"], "no")
        self.assertEqual(config["diff"], "y")
        self.assertEqual(config["color"], True)
        self.assertEqual(config["line_length"], 79)
        self.assertEqual(config["target_version"], ["py36", "py37", "py38"])
        self.assertEqual(config["python_cell_magics"], ["custom1", "custom2"])
        self.assertEqual(config["exclude"], r"\.pyi?$")
        self.assertEqual(config["include"], r"\.py?$")

    def test_parse_pyproject_toml_project_metadata(self) -> None:
        for test_toml, expected in [
            ("only_black_pyproject.toml", ["py310"]),
            ("only_metadata_pyproject.toml", ["py37", "py38", "py39", "py310"]),
            ("neither_pyproject.toml", None),
            ("both_pyproject.toml", ["py310"]),
        ]:
            test_toml_file = THIS_DIR / "data" / "project_metadata" / test_toml
            config = black.parse_pyproject_toml(str(test_toml_file))
            self.assertEqual(config.get("target_version"), expected)

    @pytest.mark.filterwarnings("ignore::DeprecationWarning")
    def test_infer_target_version(self) -> None:
        for version, expected in [
            ("3.6", [TargetVersion.PY36]),
            ("3.11.0rc1", [TargetVersion.PY311]),
            (">=3.10", [TargetVersion.PY310, TargetVersion.PY311]),
            (">=3.10.6", [TargetVersion.PY310, TargetVersion.PY311]),
            ("<3.6", [TargetVersion.PY33, TargetVersion.PY34, TargetVersion.PY35]),
            (">3.7,<3.10", [TargetVersion.PY38, TargetVersion.PY39]),
            (">3.7,!=3.8,!=3.9", [TargetVersion.PY310, TargetVersion.PY311]),
            (
                "> 3.9.4, != 3.10.3",
                [TargetVersion.PY39, TargetVersion.PY310, TargetVersion.PY311],
            ),
            (
                "!=3.3,!=3.4",
                [
                    TargetVersion.PY35,
                    TargetVersion.PY36,
                    TargetVersion.PY37,
                    TargetVersion.PY38,
                    TargetVersion.PY39,
                    TargetVersion.PY310,
                    TargetVersion.PY311,
                ],
            ),

Review comment (suggestion):
We create invalid specifiers to test the code, which causes `packaging` to throw a DeprecationWarning. So in order to test that these invalid specifiers return `None` as expected, we need to ignore these warnings.

Code after:
        try:
            from click import _unicodefun  # type: ignore
        except ImportError:
            self.skipTest("Incompatible Click version")

        if not hasattr(_unicodefun, "_verify_python_env"):
            self.skipTest("Incompatible Click version")

        # First, let's see if Click is crashing with a preferred ASCII charset.
        with patch("locale.getpreferredencoding") as gpe:
            gpe.return_value = "ASCII"
            with self.assertRaises(RuntimeError):
                _unicodefun._verify_python_env()
        # Now, let's silence Click...
        black.patch_click()
        # ...and confirm it's silent.
        with patch("locale.getpreferredencoding") as gpe:
            gpe.return_value = "ASCII"
            try:
                _unicodefun._verify_python_env()
            except RuntimeError as re:
                self.fail(f"`patch_click()` failed, exception still raised: {re}")

    def test_root_logger_not_used_directly(self) -> None:
        def fail(*args: Any, **kwargs: Any) -> None:
            self.fail("Record created with root logger")

        with patch.multiple(
            logging.root,
            debug=fail,
            info=fail,
            warning=fail,
            error=fail,
            critical=fail,
            log=fail,
        ):
            ff(THIS_DIR / "util.py")

    def test_invalid_config_return_code(self) -> None:
        tmp_file = Path(black.dump_to_file())
        try:
            tmp_config = Path(black.dump_to_file())
            tmp_config.unlink()
            args = ["--config", str(tmp_config), str(tmp_file)]
            self.invokeBlack(args, exit_code=2, ignore_config=False)
        finally:
            tmp_file.unlink()

    def test_parse_pyproject_toml(self) -> None:
        test_toml_file = THIS_DIR / "test.toml"
        config = black.parse_pyproject_toml(str(test_toml_file))