- @unittest.skipUnless(has_blackd_deps, "blackd's dependencies are not installed")
- @async_test
- async def test_blackd_unsupported_version(self) -> None:
- app = blackd.make_app()
- async with TestClient(TestServer(app)) as client:
- response = await client.post(
- "/", data=b"what", headers={blackd.VERSION_HEADER: "2"}
- )
- self.assertEqual(response.status, 501)
-
- @unittest.skipUnless(has_blackd_deps, "blackd's dependencies are not installed")
- @async_test
- async def test_blackd_supported_version(self) -> None:
- app = blackd.make_app()
- async with TestClient(TestServer(app)) as client:
- response = await client.post(
- "/", data=b"what", headers={blackd.VERSION_HEADER: "1"}
- )
- self.assertEqual(response.status, 200)
-
- @unittest.skipUnless(has_blackd_deps, "blackd's dependencies are not installed")
- @async_test
- async def test_blackd_invalid_python_variant(self) -> None:
- app = blackd.make_app()
- async with TestClient(TestServer(app)) as client:
- response = await client.post(
- "/", data=b"what", headers={blackd.PYTHON_VARIANT_HEADER: "lol"}
- )
- self.assertEqual(response.status, 400)
-
- @unittest.skipUnless(has_blackd_deps, "blackd's dependencies are not installed")
- @async_test
- async def test_blackd_pyi(self) -> None:
- app = blackd.make_app()
- async with TestClient(TestServer(app)) as client:
- source, expected = read_data("stub.pyi")
- response = await client.post(
- "/", data=source, headers={blackd.PYTHON_VARIANT_HEADER: "pyi"}
- )
- self.assertEqual(response.status, 200)
- self.assertEqual(await response.text(), expected)
-
- @unittest.skipUnless(has_blackd_deps, "blackd's dependencies are not installed")
- @async_test
- async def test_blackd_py36(self) -> None:
- app = blackd.make_app()
- async with TestClient(TestServer(app)) as client:
- response = await client.post(
- "/",
- data=(
- "def f(\n"
- " and_has_a_bunch_of,\n"
- " very_long_arguments_too,\n"
- " and_lots_of_them_as_well_lol,\n"
- " **and_very_long_keyword_arguments\n"
- "):\n"
- " pass\n"
- ),
- headers={blackd.PYTHON_VARIANT_HEADER: "3.6"},
- )
- self.assertEqual(response.status, 200)
- response = await client.post(
- "/",
- data=(
- "def f(\n"
- " and_has_a_bunch_of,\n"
- " very_long_arguments_too,\n"
- " and_lots_of_them_as_well_lol,\n"
- " **and_very_long_keyword_arguments\n"
- "):\n"
- " pass\n"
- ),
- headers={blackd.PYTHON_VARIANT_HEADER: "3.5"},
- )
- self.assertEqual(response.status, 204)
- response = await client.post(
- "/",
- data=(
- "def f(\n"
- " and_has_a_bunch_of,\n"
- " very_long_arguments_too,\n"
- " and_lots_of_them_as_well_lol,\n"
- " **and_very_long_keyword_arguments\n"
- "):\n"
- " pass\n"
- ),
- headers={blackd.PYTHON_VARIANT_HEADER: "2"},
- )
- self.assertEqual(response.status, 204)
-
- @unittest.skipUnless(has_blackd_deps, "blackd's dependencies are not installed")
- @async_test
- async def test_blackd_fast(self) -> None:
- app = blackd.make_app()
- async with TestClient(TestServer(app)) as client:
- response = await client.post("/", data=b"ur'hello'")
- self.assertEqual(response.status, 500)
- self.assertIn("failed to parse source file", await response.text())
- response = await client.post(
- "/", data=b"ur'hello'", headers={blackd.FAST_OR_SAFE_HEADER: "fast"}
- )
- self.assertEqual(response.status, 200)
-
- @unittest.skipUnless(has_blackd_deps, "blackd's dependencies are not installed")
- @async_test
- async def test_blackd_line_length(self) -> None:
- app = blackd.make_app()
- async with TestClient(TestServer(app)) as client:
- response = await client.post(
- "/", data=b'print("hello")\n', headers={blackd.LINE_LENGTH_HEADER: "7"}
- )
- self.assertEqual(response.status, 200)
-
- @unittest.skipUnless(has_blackd_deps, "blackd's dependencies are not installed")
- @async_test
- async def test_blackd_invalid_line_length(self) -> None:
- app = blackd.make_app()
- async with TestClient(TestServer(app)) as client:
- response = await client.post(
- "/",
- data=b'print("hello")\n',
- headers={blackd.LINE_LENGTH_HEADER: "NaN"},
+ 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))
+ 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["exclude"], r"\.pyi?$")
+ self.assertEqual(config["include"], r"\.py?$")
+
+ def test_read_pyproject_toml(self) -> None:
+ test_toml_file = THIS_DIR / "test.toml"
+ fake_ctx = FakeContext()
+ black.read_pyproject_toml(fake_ctx, FakeParameter(), str(test_toml_file))
+ config = fake_ctx.default_map
+ 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["exclude"], r"\.pyi?$")
+ self.assertEqual(config["include"], r"\.py?$")
+
+ def test_find_project_root(self) -> None:
+ with TemporaryDirectory() as workspace:
+ root = Path(workspace)
+ test_dir = root / "test"
+ test_dir.mkdir()
+
+ src_dir = root / "src"
+ src_dir.mkdir()
+
+ root_pyproject = root / "pyproject.toml"
+ root_pyproject.touch()
+ src_pyproject = src_dir / "pyproject.toml"
+ src_pyproject.touch()
+ src_python = src_dir / "foo.py"
+ src_python.touch()
+
+ self.assertEqual(
+ black.find_project_root((src_dir, test_dir)), root.resolve()