@@ -2482,6 +2482,41 @@ def test_get_sources_with_stdin_filename_and_force_exclude(self) -> None:
24822482 )
24832483
24842484
2485+ class TestDeFactoAPI :
2486+ """Test that certain symbols that are commonly used externally keep working.
2487+
2488+ We don't (yet) formally expose an API (see issue #779), but we should endeavor to
2489+ keep certain functions that external users commonly rely on working.
2490+
2491+ """
2492+
2493+ def test_format_str (self ) -> None :
2494+ # format_str and Mode should keep working
2495+ assert (
2496+ black .format_str ("print('hello')" , mode = black .Mode ()) == 'print("hello")\n '
2497+ )
2498+
2499+ # you can pass line length
2500+ assert (
2501+ black .format_str ("print('hello')" , mode = black .Mode (line_length = 42 ))
2502+ == 'print("hello")\n '
2503+ )
2504+
2505+ # invalid input raises InvalidInput
2506+ with pytest .raises (black .InvalidInput ):
2507+ black .format_str ("syntax error" , mode = black .Mode ())
2508+
2509+ def test_format_file_contents (self ) -> None :
2510+ # You probably should be using format_str() instead, but let's keep
2511+ # this one around since people do use it
2512+ assert (
2513+ black .format_file_contents ("x=1" , fast = True , mode = black .Mode ()) == "x = 1\n "
2514+ )
2515+
2516+ with pytest .raises (black .NothingChanged ):
2517+ black .format_file_contents ("x = 1\n " , fast = True , mode = black .Mode ())
2518+
2519+
24852520try :
24862521 with open (black .__file__ , "r" , encoding = "utf-8" ) as _bf :
24872522 black_source_lines = _bf .readlines ()
0 commit comments