Exemplo n.º 1
0
def test_diff_has_diff(exit_code, isatty, expect_reproducible_diff,
                       expected_return_value, capfd, mocker, tmpdir):
    mocker.patch.object(sys.stdout, "isatty", return_value=isatty)

    project_dir = cruft.create("https://github.com/cruft/cookiecutter-test",
                               Path(tmpdir),
                               directory="dir",
                               checkout="diff")
    (project_dir / "file0").write_text("new content 0\n")
    (project_dir / "dir0/file1").write_text("new content 1\n")
    (project_dir / "dir0/file2").unlink()

    assert cruft.diff(project_dir,
                      exit_code=exit_code) == expected_return_value

    captured = capfd.readouterr()
    stdout = captured.out
    stderr = captured.err

    assert stderr == ""

    expected_output = """diff --git a{tmpdir}/dir0/file1 b{tmpdir}/dir0/file1
index ac3e272..eaae237 100644
--- a{tmpdir}/dir0/file1
+++ b{tmpdir}/dir0/file1
@@ -1 +1 @@
-content1
+new content 1
diff --git a{tmpdir}/file0 b{tmpdir}/file0
index 1fc03a9..be6a56b 100644
--- a{tmpdir}/file0
+++ b{tmpdir}/file0
@@ -1 +1 @@
-content0
+new content 0
"""
    expected_output_regex = re.escape(expected_output)
    expected_output_regex = expected_output_regex.replace(
        r"\{tmpdir\}", r"([^\n]*)")
    expected_output_regex = fr"^{expected_output_regex}$"

    match = re.search(expected_output_regex, stdout, re.MULTILINE)
    assert match is not None

    if expect_reproducible_diff:
        # If the output is not displayed to the user (for example when piping the result
        # of the "cruft diff" command) or if the user requested an exit code, we must make
        # sure the absolute path to the temporary directory does not appear in the diff
        # because the user might want to process the output.
        # Conversely, when the output is suposed to be displayed to the user directly (e.g.
        # when running "cruft diff" command directly in a terminal), absolute path to the
        # actual files on disk may be displayed because git diff command is called directly
        # without reprocessing by cruft. This delegates diff coloring and paging to git which
        # improves user experience. As far as I know, there is no way to ask git diff to not
        # display this path.
        assert set(match.groups()) == {""}
Exemplo n.º 2
0
def test_diff_no_diff(exit_code, capfd, mocker, tmpdir):
    project_dir = cruft.create(
        "https://github.com/cruft/cookiecutter-test", Path(tmpdir), directory="dir", checkout="diff"
    )

    assert cruft.diff(project_dir, exit_code=exit_code) is True

    captured = capfd.readouterr()
    stdout = captured.out
    stderr = captured.err

    assert stdout == ""
    assert stderr == ""
Exemplo n.º 3
0
def test_diff_checkout(capfd, tmpdir):
    project_dir = cruft.create(
        "https://github.com/samj1912/cookiecutter-test",
        Path(tmpdir),
        directory="dir",
        checkout="master",
    )

    assert cruft.diff(project_dir, exit_code=True, checkout="updated") is False

    captured = capfd.readouterr()
    stdout = captured.out
    stderr = captured.err

    assert stderr == ""
    assert "--- a/README.md" in stdout
    assert "+++ b/README.md" in stdout
    assert "+Updated again" in stdout
    assert "-Updated" in stdout