Ejemplo n.º 1
0
def test_version_0_0_0_ignored(template_path, tmp_path, monkeypatch):
    monkeypatch.setattr("copier.__version__", "0.0.0")
    # assert no error
    with warnings.catch_warnings():
        warnings.simplefilter("error")
        with pytest.raises(UnknownCopierVersionWarning):
            copier.run_copy(template_path, tmp_path)
Ejemplo n.º 2
0
def test_empty_dir(tmp_path_factory, generate):
    src, dst = map(tmp_path_factory.mktemp, ("src", "dst"))
    build_file_tree(
        {
            src / "copier.yaml":
            """
                _subdirectory: tpl
                _templates_suffix: .jinja
                do_it:
                    type: bool
            """,
            src / "tpl" / "[% if do_it %]one_dir[% endif %]" / "one.txt.jinja":
            "[[ do_it ]]",
            src / "tpl" / "two.txt":
            "[[ do_it ]]",
            src / "tpl" / "[% if do_it %]three.txt[% endif %].jinja":
            "[[ do_it ]]",
            src / "tpl" / "four" / "[% if do_it %]five.txt[% endif %].jinja":
            "[[ do_it ]]",
        }, )
    copier.run_copy(str(src), dst, {"do_it": generate}, force=True)
    assert (dst / "four").is_dir()
    assert (dst / "two.txt").read_text() == "[[ do_it ]]"
    assert (dst / "one_dir").exists() == generate
    assert (dst / "three.txt").exists() == generate
    assert (dst / "one_dir").is_dir() == generate
    assert (dst / "one_dir" / "one.txt").is_file() == generate
    if generate:
        assert (dst / "one_dir" / "one.txt").read_text() == repr(generate)
        assert (dst / "three.txt").read_text() == repr(generate)
        assert (dst / "four" / "five.txt").read_text() == repr(generate)
Ejemplo n.º 3
0
def test_preserved_permissions(tmp_path_factory: pytest.TempPathFactory,
                               permissions: int):
    src, dst = map(tmp_path_factory.mktemp, ("src", "dst"))
    src_file = src / "the_file"
    src_file.write_text("content")
    src_file.chmod(permissions)
    copier.run_copy(str(src), dst, defaults=True, overwrite=True)
    dst_file = dst / "the_file"
    assert (dst_file.stat().st_mode & permissions) == permissions
Ejemplo n.º 4
0
def test_jinja_defaults_v6_min_version(tmp_path_factory):
    src, dst = map(tmp_path_factory.mktemp, ("src", "dst"))
    build_file_tree({
        src / "copier.yml": "{_min_copier_version: '6.0.0a5.post1', q: a}",
        src / "result.jinja": "{{ q }}",
    })
    # Make sure the future warning is not raised
    with warnings.catch_warnings():
        warnings.simplefilter("error")
        copier.run_copy(str(src), dst, force=True)
    assert (dst / "result").read_text() == "a"
Ejemplo n.º 5
0
def test_future_jinja_defaults_warning(tmp_path_factory):
    src, dst = map(tmp_path_factory.mktemp, ("src", "dst"))
    build_file_tree({
        src / "copier.yml": """\
                _min_copier_version: "5"
                q: a
            """,
        src / "result.tmpl": "[[ q ]]",
    })
    # Make sure the future warning is raised
    with warnings.catch_warnings():
        warnings.simplefilter("error", FutureWarning)
        with pytest.raises(FutureWarning):
            copier.run_copy(str(src), dst, force=True)
    # Run again without capturing warning, to see how it renders properly
    copier.run_copy(str(src), dst, force=True)
    assert (dst / "result").read_text() == "a"
Ejemplo n.º 6
0
def test_version_bigger_major_warning(template_path, tmp_path, monkeypatch):
    monkeypatch.setattr("copier.__version__", "11.0.0a0")
    with warnings.catch_warnings():
        warnings.simplefilter("error")
        with pytest.raises(OldTemplateWarning):
            copier.run_copy(template_path, tmp_path)
Ejemplo n.º 7
0
def test_update_subdirectory_from_root_path(tmp_path_factory):
    src, dst = map(tmp_path_factory.mktemp, ("src", "dst"))
    with local.cwd(src):
        build_file_tree(
            {
                "copier.yaml": """\
                    q1:
                        type: str
                        default: a1
                """,
                "file1.jinja": """\
                    version 1
                    hello
                    {{ q1 }}
                    bye
                """,
                "{{ _copier_conf.answers_file }}.jinja": "{{ _copier_answers|to_nice_yaml }}",
            }
        )
        git("init")
        git("add", ".")
        git("commit", "-m1")
        git("tag", "1")
        build_file_tree(
            {
                "file1.jinja": """\
                    version 2
                    hello
                    {{ q1 }}
                    bye
                """,
            }
        )
        git("commit", "-am2")
        git("tag", "2")
    with local.cwd(dst):
        build_file_tree({"dst_top_file": "one"})
        git("init")
        git("add", ".")
        git("commit", "-m0")
    copier.run_copy(
        str(src),
        str(dst / "subfolder"),
        vcs_ref="1",
        defaults=True,
        overwrite=True,
        answers_file=".custom.copier-answers.yaml",
    )
    assert (dst / "subfolder" / "file1").read_text() == "version 1\nhello\na1\nbye\n"
    with local.cwd(dst):
        git("add", ".")
        git("commit", "-m1")
        copier.run_update(
            "subfolder",
            defaults=True,
            overwrite=True,
            answers_file=".custom.copier-answers.yaml",
        )
    answers = yaml.safe_load(
        (dst / "subfolder" / ".custom.copier-answers.yaml").read_bytes()
    )
    assert answers["_commit"] == "2"
    assert (dst / "subfolder" / "file1").read_text() == "version 2\nhello\na1\nbye\n"