本文整理汇总了Python中black.main方法的典型用法代码示例。如果您正苦于以下问题:Python black.main方法的具体用法?Python black.main怎么用?Python black.main使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类black
的用法示例。
在下文中一共展示了black.main方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: run_black
# 需要导入模块: import black [as 别名]
# 或者: from black import main [as 别名]
def run_black(paths):
if black_main is None:
return 0
print("Running black check")
exit_code = 0
try:
black_main(["--check"] + paths)
except SystemExit as exc:
exit_code = exc.code
if exit_code:
print("black failed")
else:
print("black passed")
return exit_code
示例2: invokeBlack
# 需要导入模块: import black [as 别名]
# 或者: from black import main [as 别名]
def invokeBlack(
self, args: List[str], exit_code: int = 0, ignore_config: bool = True
) -> None:
runner = BlackRunner()
if ignore_config:
args = ["--verbose", "--config", str(THIS_DIR / "empty.toml"), *args]
result = runner.invoke(black.main, args)
self.assertEqual(
result.exit_code,
exit_code,
msg=(
f"Failed with args: {args}\n"
f"stdout: {runner.stdout_bytes.decode()!r}\n"
f"stderr: {runner.stderr_bytes.decode()!r}\n"
f"exception: {result.exception}"
),
)
示例3: test_piping_diff
# 需要导入模块: import black [as 别名]
# 或者: from black import main [as 别名]
def test_piping_diff(self) -> None:
diff_header = re.compile(
r"(STDIN|STDOUT)\t\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d\.\d\d\d\d\d\d "
r"\+\d\d\d\d"
)
source, _ = read_data("expression.py")
expected, _ = read_data("expression.diff")
config = THIS_DIR / "data" / "empty_pyproject.toml"
args = [
"-",
"--fast",
f"--line-length={black.DEFAULT_LINE_LENGTH}",
"--diff",
f"--config={config}",
]
result = BlackRunner().invoke(
black.main, args, input=BytesIO(source.encode("utf8"))
)
self.assertEqual(result.exit_code, 0)
actual = diff_header.sub(DETERMINISTIC_HEADER, result.output)
actual = actual.rstrip() + "\n" # the diff output has a trailing space
self.assertEqual(expected, actual)
示例4: test_piping_diff_with_color
# 需要导入模块: import black [as 别名]
# 或者: from black import main [as 别名]
def test_piping_diff_with_color(self) -> None:
source, _ = read_data("expression.py")
config = THIS_DIR / "data" / "empty_pyproject.toml"
args = [
"-",
"--fast",
f"--line-length={black.DEFAULT_LINE_LENGTH}",
"--diff",
"--color",
f"--config={config}",
]
result = BlackRunner().invoke(
black.main, args, input=BytesIO(source.encode("utf8"))
)
actual = result.output
# Again, the contents are checked in a different test, so only look for colors.
self.assertIn("\033[1;37m", actual)
self.assertIn("\033[36m", actual)
self.assertIn("\033[32m", actual)
self.assertIn("\033[31m", actual)
self.assertIn("\033[0m", actual)
示例5: test_expression_diff
# 需要导入模块: import black [as 别名]
# 或者: from black import main [as 别名]
def test_expression_diff(self) -> None:
source, _ = read_data("expression.py")
expected, _ = read_data("expression.diff")
tmp_file = Path(black.dump_to_file(source))
diff_header = re.compile(
rf"{re.escape(str(tmp_file))}\t\d\d\d\d-\d\d-\d\d "
r"\d\d:\d\d:\d\d\.\d\d\d\d\d\d \+\d\d\d\d"
)
try:
result = BlackRunner().invoke(black.main, ["--diff", str(tmp_file)])
self.assertEqual(result.exit_code, 0)
finally:
os.unlink(tmp_file)
actual = result.output
actual = diff_header.sub(DETERMINISTIC_HEADER, actual)
actual = actual.rstrip() + "\n" # the diff output has a trailing space
if expected != actual:
dump = black.dump_to_file(actual)
msg = (
"Expected diff isn't equal to the actual. If you made changes to"
" expression.py and this is an anticipated difference, overwrite"
f" tests/data/expression.diff with {dump}"
)
self.assertEqual(expected, actual, msg)
示例6: test_black
# 需要导入模块: import black [as 别名]
# 或者: from black import main [as 别名]
def test_black():
runner = CliRunner()
result = runner.invoke(black.main, [str(code_root), "--check"])
assert result.exit_code == 0, result.output
示例7: black_wrapper
# 需要导入模块: import black [as 别名]
# 或者: from black import main [as 别名]
def black_wrapper(writeback):
""" Helper function to invoke black programatically.
"""
check = [] if writeback else ["--check"]
exclude = "|".join(["cangivefilenameshere"])
sys.argv[1:] = check + ["--exclude", exclude, ROOT_DIR]
import black
black.main()
示例8: test_black
# 需要导入模块: import black [as 别名]
# 或者: from black import main [as 别名]
def test_black():
runner = CliRunner()
result = runner.invoke(
black.main,
[str(code_root / "tests"), str(code_root / "sqlite_utils"), "--check"],
)
assert result.exit_code == 0, result.output
示例9: test_piping
# 需要导入模块: import black [as 别名]
# 或者: from black import main [as 别名]
def test_piping(self) -> None:
source, expected = read_data("src/black/__init__", data=False)
result = BlackRunner().invoke(
black.main,
["-", "--fast", f"--line-length={black.DEFAULT_LINE_LENGTH}"],
input=BytesIO(source.encode("utf8")),
)
self.assertEqual(result.exit_code, 0)
self.assertFormatEqual(expected, result.output)
black.assert_equivalent(source, result.output)
black.assert_stable(source, result.output, black.FileMode())
示例10: test_no_cache_when_stdin
# 需要导入模块: import black [as 别名]
# 或者: from black import main [as 别名]
def test_no_cache_when_stdin(self) -> None:
mode = black.FileMode()
with cache_dir():
result = CliRunner().invoke(
black.main, ["-"], input=BytesIO(b"print('hello')")
)
self.assertEqual(result.exit_code, 0)
cache_file = black.get_cache_file(mode)
self.assertFalse(cache_file.exists())
示例11: test_pipe_force_pyi
# 需要导入模块: import black [as 别名]
# 或者: from black import main [as 别名]
def test_pipe_force_pyi(self) -> None:
source, expected = read_data("force_pyi")
result = CliRunner().invoke(
black.main, ["-", "-q", "--pyi"], input=BytesIO(source.encode("utf8"))
)
self.assertEqual(result.exit_code, 0)
actual = result.output
self.assertFormatEqual(actual, expected)
示例12: test_pipe_force_py36
# 需要导入模块: import black [as 别名]
# 或者: from black import main [as 别名]
def test_pipe_force_py36(self) -> None:
source, expected = read_data("force_py36")
result = CliRunner().invoke(
black.main,
["-", "-q", "--target-version=py36"],
input=BytesIO(source.encode("utf8")),
)
self.assertEqual(result.exit_code, 0)
actual = result.output
self.assertFormatEqual(actual, expected)
示例13: test_preserves_line_endings_via_stdin
# 需要导入模块: import black [as 别名]
# 或者: from black import main [as 别名]
def test_preserves_line_endings_via_stdin(self) -> None:
for nl in ["\n", "\r\n"]:
contents = nl.join(["def f( ):", " pass"])
runner = BlackRunner()
result = runner.invoke(
black.main, ["-", "--fast"], input=BytesIO(contents.encode("utf8"))
)
self.assertEqual(result.exit_code, 0)
output = runner.stdout_bytes
self.assertIn(nl.encode("utf8"), output)
if nl == "\n":
self.assertNotIn(b"\r\n", output)
示例14: test_blackd_main
# 需要导入模块: import black [as 别名]
# 或者: from black import main [as 别名]
def test_blackd_main(self) -> None:
with patch("blackd.web.run_app"):
result = CliRunner().invoke(blackd.main, [])
if result.exception is not None:
raise result.exception
self.assertEqual(result.exit_code, 0)