示例#1
0
def test_black_options_skip_string_normalization(git_repo, config, options, expect):
    """Black string normalization config and cmdline option are combined correctly"""
    added_files = git_repo.add(
        {"main.py": "foo", "pyproject.toml": joinlines(["[tool.black]"] + config)},
        commit="Initial commit",
    )
    added_files["main.py"].write_bytes(b"bar")
    mode_class_mock = Mock(wraps=black_diff.Mode)
    # Speed up tests by mocking `format_str` to skip running Black
    format_str = Mock(return_value="bar")
    with patch.multiple(black_diff, Mode=mode_class_mock, format_str=format_str):

        main(options + [str(path) for path in added_files.values()])

    assert mode_class_mock.call_args_list == [expect]
示例#2
0
def test_revision(git_repo, monkeypatch, capsys, revision, worktree_content,
                  expect):
    monkeypatch.chdir(git_repo.root)
    # 2: HEAD~2:
    paths = git_repo.add(
        {
            "+2.py": "ORIGINAL=1\n",
            "+2M1.py": "ORIGINAL=1\n",
            "+2-1.py": "ORIGINAL=1\n",
            "+2M1-0.py": "ORIGINAL=1\n",
        },
        commit="First commit",
    )
    # 1: HEAD~1 i.e. HEAD^
    paths.update(
        git_repo.add(
            {
                "+2M1.py": "MODIFIED=1\n",
                "+1.py": "ORIGINAL=1\n",
                "+1M0.py": "ORIGINAL=1\n",
                "+2-1.py": None,
                "+2M1-0.py": "MODIFIED=1\n",
            },
            commit="Second commit",
        ))
    # 0: HEAD~0 i.e. HEAD:
    git_repo.add(
        {
            "+1M0.py": "MODIFIED=1\n",
            "+2M1-0.py": None
        },
        commit="Third commit",
    )
    # Working tree:
    for path in paths.values():
        path.write_bytes(worktree_content)
    arguments = ["--diff", "--revision", revision, "."]

    with raises_if_exception(expect):
        main(arguments)

        modified_files = [
            line[4:-3] for line in capsys.readouterr().out.splitlines()
            if line.startswith("+++ ")
        ]
        assert modified_files == expect
示例#3
0
def test_black_options(monkeypatch, tmpdir, git_repo, options, expect):
    monkeypatch.chdir(tmpdir)
    (tmpdir / 'pyproject.toml').write("[tool.black]\n")
    (tmpdir / 'black.cfg').write(
        dedent("""
            [tool.black]
            line-length = 81
            skip-string-normalization = false
            """))
    added_files = git_repo.add({"main.py": 'print("Hello World!")\n'},
                               commit="Initial commit")
    added_files["main.py"].write_bytes(b'print ("Hello World!")\n')
    with patch.object(black_diff, 'Mode', wraps=black_diff.Mode) as Mode:

        main(options + [str(path) for path in added_files.values()])

    _, expect_args, expect_kwargs = expect
    Mode.assert_called_once_with(*expect_args, **expect_kwargs)
示例#4
0
def test_main_retval(check, changes, expect_retval):
    """main() return value is correct based on --check and the need to reformat files"""
    format_edited_parts = Mock()
    format_edited_parts.return_value = ([(Path('/dummy.py'), 'old\n', 'new\n',
                                          ['new'])] if changes else [])
    check_arg_maybe = ['--check'] if check else []
    with patch.multiple('darker.__main__',
                        format_edited_parts=format_edited_parts,
                        modify_file=DEFAULT):

        retval = main(check_arg_maybe + ['a.py'])

    assert retval == expect_retval
示例#5
0
def test_options(tmpdir, monkeypatch, options, expect):
    """The main engine is called with correct parameters based on the command line

    Executed in a clean directory so Darker's own ``pyproject.toml`` doesn't interfere.

    """
    monkeypatch.chdir(tmpdir)
    (tmpdir / "my.cfg").write("")
    with patch('darker.__main__.format_edited_parts') as format_edited_parts:

        retval = main(options)

    format_edited_parts.assert_called_once_with(*expect)
    assert retval == 0
示例#6
0
def test_main_retval(check, changes, expect_retval):
    """main() return value is correct based on --check and the need to reformat files"""
    format_edited_parts = Mock()
    format_edited_parts.return_value = ([(
        Path("/dummy.py"),
        TextDocument.from_lines(["old"]),
        TextDocument.from_lines(["new"]),
    )] if changes else [])
    check_arg_maybe = ['--check'] if check else []
    with patch.multiple('darker.__main__',
                        format_edited_parts=format_edited_parts,
                        modify_file=DEFAULT):

        retval = main(check_arg_maybe + ['a.py'])

    assert retval == expect_retval
示例#7
0
def test_options(git_repo, options, expect):
    """The main engine is called with correct parameters based on the command line

    Executed in a clean directory so Darker's own ``pyproject.toml`` doesn't interfere.

    """
    paths = git_repo.add(
        {"a.py": "1\n", "b.py": "2\n", "my.cfg": ""}, commit="Initial commit"
    )
    paths["a.py"].write_bytes(b"one\n")
    with patch('darker.__main__.format_edited_parts') as format_edited_parts:

        retval = main(options)

    expect = (Path(git_repo.root), expect[1]) + expect[2:]
    format_edited_parts.assert_called_once_with(*expect)
    assert retval == 0
示例#8
0
def test_options(options, expect):
    with patch('darker.__main__.format_edited_parts') as format_edited_parts:

        main(options)

    format_edited_parts.assert_called_once_with(*expect)