def test_create_stores_checkout_value(value, tmpdir): tmpdir.chdir() cruft.create("https://github.com/timothycrosley/cookiecutter-python", Path(tmpdir), checkout=value) assert (json.load((tmpdir / "python_project_name" / ".cruft.json").open("r"))["checkout"] == value)
def test_update_stores_checkout_value(value, tmpdir): tmpdir.chdir() cruft.create( "https://github.com/timothycrosley/cookiecutter-python", Path(tmpdir), checkout="ea8f733f85e7089df338d41ace199d3f4d397e29", ) project_dir = tmpdir / "python_project_name" cruft.update(Path(project_dir), checkout=value) assert json.load((project_dir / ".cruft.json").open("r"))["checkout"] == value
def test_directory_and_checkout(tmpdir): output_path = cruft.create( "https://github.com/samj1912/cookiecutter-test", output_dir=Path(tmpdir), directory="dir", checkout="initial", ) cruft_file = get_cruft_file(output_path) assert cruft_file.exists() assert cruft.check(output_path, checkout="initial") assert not cruft.check(output_path, checkout="updated") assert cruft.update(output_path, checkout="updated") assert cruft.check(output_path, checkout="updated") cruft_file.unlink() assert not cruft_file.exists() assert cruft.link( "https://github.com/samj1912/cookiecutter-test", project_dir=output_path, directory="dir", checkout="updated", ) assert cruft.check(output_path, checkout="updated") # Add checks for strictness where master is an older # version than updated assert not cruft.check(output_path, strict=True) assert cruft.check(output_path, strict=False)
def test_diff_git_subdir(capfd, tmpdir): tmpdir.chdir() temp_dir = Path(tmpdir) Repo.clone_from("https://github.com/cruft/cookiecutter-test", temp_dir) # Create something deeper in the git tree project_dir = cruft.create( "https://github.com/cruft/cookiecutter-test", Path("tmpdir/foo/bar"), directory="dir", checkout="master", ) # not added & committed assert not cruft.update(project_dir) # Add & commit the changes so that the repo is clean run(["git", "add", "."], cwd=temp_dir) run( [ "git", "-c", "user.name='test'", "-c", "user.email='*****@*****.**'", "commit", "-am", "test", ], cwd=temp_dir, ) assert cruft.update(project_dir, checkout="updated")
def cookiecutter_dir_updated(tmpdir): yield Path( cruft.create( "https://github.com/samj1912/cookiecutter-test", Path(tmpdir), directory="dir", checkout="updated", ))
def test_relative_repo_check(tmpdir): tmpdir.chdir() temp_dir = Path(tmpdir) Repo.clone_from("https://github.com/samj1912/cookiecutter-test", str(temp_dir / "cc")) project_dir = cruft.create("./cc", output_dir=str(temp_dir / "output"), directory="dir") assert cruft.check(project_dir)
def cookiecutter_dir_hooked_git(tmpdir): yield Path( cruft.create( # See pull request! "https://github.com/juhuebner/cookiecutter-test", Path(tmpdir), directory="dir", checkout="with-git-from-hook", ))
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()) == {""}
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 == ""
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
def test_no_cookiecutter_dir(tmpdir): with pytest.raises(exceptions.UnableToFindCookiecutterTemplate): cruft.create("https://github.com/samj1912/cookiecutter-test", Path(tmpdir))
def test_invalid_cookiecutter_reference(tmpdir): with pytest.raises(exceptions.InvalidCookiecutterRepository): cruft.create("https://github.com/samj1912/cookiecutter-test", Path(tmpdir), checkout="DNE")
def test_invalid_cookiecutter_repo(tmpdir): with pytest.raises(exceptions.InvalidCookiecutterRepository): cruft.create("DNE", Path(tmpdir))
def test_create_with_skips(tmpdir): tmpdir.chdir() skips = ["setup.cfg"] cruft.create("https://github.com/timothycrosley/cookiecutter-python", Path(tmpdir), skip=skips) assert json.load((tmpdir / "python_project_name" / ".cruft.json").open("r"))["skip"] == skips
def cookiecutter_dir(tmpdir): yield Path( cruft.create("https://github.com/samj1912/cookiecutter-test", Path(tmpdir), directory="dir"))