def test_good_cli_run(tmp_path): run_result = CopierApp.run( ["--quiet", "-a", "altered-answers.yml", str(SIMPLE_DEMO_PATH), str(tmp_path)], exit=False, ) a_txt = tmp_path / "a.txt" assert run_result[1] == 0 assert a_txt.exists() assert a_txt.is_file() assert a_txt.read_text().strip() == "EXAMPLE_CONTENT" answers = yaml.safe_load((tmp_path / "altered-answers.yml").read_text()) assert answers["_src_path"] == str(SIMPLE_DEMO_PATH)
def test_updatediff(tmp_path_factory): src, dst = map(tmp_path_factory.mktemp, ("src", "dst")) # Prepare repo bundle repo = src / "repo" bundle = src / "demo_updatediff_repo.bundle" last_commit = "" build_file_tree({ repo / ".copier-answers.yml.jinja": """\ # Changes here will be overwritten by Copier {{ _copier_answers|to_nice_yaml }} """, repo / "copier.yml": """\ _envops: "keep_trailing_newline": True project_name: to become a pirate author_name: Guybrush """, repo / "README.txt.jinja": """ Let me introduce myself. My name is {{author_name}}, and my project is {{project_name}}. Thanks for your attention. """, }) with local.cwd(repo): git("init") git("add", ".") git("commit", "-m", "Guybrush wants to be a pirate") git("tag", "v0.0.1") build_file_tree({ repo / "copier.yml": """\ _envops: "keep_trailing_newline": True project_name: to become a pirate author_name: Guybrush _migrations: - version: v0.0.1 before: - touch before-v0.0.1 after: - touch after-v0.0.1 - version: v0.0.2 before: - touch before-v0.0.2 after: - touch after-v0.0.2 - version: v1.0.0 before: - touch before-v1.0.0 after: - touch after-v1.0.0 """, }) with local.cwd(repo): git("init") git("add", ".") git("commit", "-m", "Add migrations") git("tag", "v0.0.2") build_file_tree({ repo / "copier.yml": """\ _envops: "keep_trailing_newline": True project_name: to rule author_name: Elaine _migrations: - version: v0.0.1 before: - touch before-v0.0.1 after: - touch after-v0.0.1 - version: v0.0.2 before: - touch before-v0.0.2 after: - touch after-v0.0.2 - version: v1.0.0 before: - touch before-v1.0.0 after: - touch after-v1.0.0 """, repo / "README.txt.jinja": """ Let me introduce myself. My name is {{author_name}}. My project is {{project_name}}. Thanks for your attention. """, }) with local.cwd(repo): git("init") git("add", ".") git("commit", "-m", "Elaine wants to rule") git("bundle", "create", bundle, "--all") last_commit = git("describe", "--tags").strip() # Generate repo bundle target = dst / "target" readme = target / "README.txt" answers = target / ".copier-answers.yml" commit = git["commit", "--all"] # Run copier 1st time, with specific tag CopierApp.invoke("copy", str(bundle), str(target), force=True, vcs_ref="v0.0.1") # Check it's copied OK assert answers.read_text() == dedent(f"""\ # Changes here will be overwritten by Copier _commit: v0.0.1 _src_path: {bundle} author_name: Guybrush project_name: to become a pirate\n """) assert readme.read_text() == dedent(""" Let me introduce myself. My name is Guybrush, and my project is to become a pirate. Thanks for your attention. """) # Init destination as a new independent git repo with local.cwd(target): git("init") # Configure git in case you're running in CI git("config", "user.name", "Copier Test") git("config", "user.email", "test@copier") # Commit changes git("add", ".") commit("-m", "hello world") # Emulate the user modifying the README by hand with open(readme, "w") as readme_fd: readme_fd.write( dedent(""" Let me introduce myself. My name is Guybrush, and my project is to become a pirate. Thanks for your grog. """)) commit("-m", "I prefer grog") # Update target to latest tag and check it's updated in answers file CopierApp.invoke(force=True) assert answers.read_text() == dedent(f"""\ # Changes here will be overwritten by Copier _commit: v0.0.2 _src_path: {bundle} author_name: Guybrush project_name: to become a pirate\n """) # Check migrations were executed properly assert not (target / "before-v0.0.1").is_file() assert not (target / "after-v0.0.1").is_file() assert (target / "before-v0.0.2").is_file() assert (target / "after-v0.0.2").is_file() (target / "before-v0.0.2").unlink() (target / "after-v0.0.2").unlink() assert not (target / "before-v1.0.0").is_file() assert not (target / "after-v1.0.0").is_file() commit("-m", "Update template to v0.0.2") # Update target to latest commit, which is still untagged CopierApp.invoke(force=True, vcs_ref="HEAD") # Check no new migrations were executed assert not (target / "before-v0.0.1").is_file() assert not (target / "after-v0.0.1").is_file() assert not (target / "before-v0.0.2").is_file() assert not (target / "after-v0.0.2").is_file() assert not (target / "before-v1.0.0").is_file() assert not (target / "after-v1.0.0").is_file() # Check it's updated OK assert answers.read_text() == dedent(f"""\ # Changes here will be overwritten by Copier _commit: {last_commit} _src_path: {bundle} author_name: Guybrush project_name: to become a pirate\n """) assert readme.read_text() == dedent(""" Let me introduce myself. My name is Guybrush. My project is to become a pirate. Thanks for your grog. """) commit("-m", f"Update template to {last_commit}") assert not git("status", "--porcelain") # No more updates exist, so updating again should change nothing CopierApp.invoke(force=True, vcs_ref="HEAD") assert not git("status", "--porcelain") # If I change an option, it updates properly copy( data={ "author_name": "Largo LaGrande", "project_name": "to steal a lot" }, force=True, vcs_ref="HEAD", ) assert readme.read_text() == dedent(""" Let me introduce myself. My name is Largo LaGrande. My project is to steal a lot. Thanks for your grog. """) commit("-m", "Subproject evolved") # Reapply template ignoring subproject evolution Worker( data={ "author_name": "Largo LaGrande", "project_name": "to steal a lot" }, force=True, vcs_ref="HEAD", ).run_copy() assert readme.read_text() == dedent(""" Let me introduce myself. My name is Largo LaGrande. My project is to steal a lot. Thanks for your attention. """)
def test_updatediff(tmpdir): tmp_path = Path(tmpdir) target = tmp_path / "target" readme = target / "README.txt" answers = target / ".copier-answers.yml" commit = git["commit", "--all"] # Run copier 1st time, with specific tag CopierApp.invoke("copy", str(REPO_BUNDLE_PATH), str(target), force=True, vcs_ref="v0.0.1") # Check it's copied OK assert answers.read_text() == dedent(f""" # Changes here will be overwritten by Copier _commit: v0.0.1 _src_path: {REPO_BUNDLE_PATH} author_name: Guybrush project_name: to become a pirate """) assert readme.read_text() == dedent(""" Let me introduce myself. My name is Guybrush, and my project is to become a pirate. Thanks for your attention. """) # Init destination as a new independent git repo with local.cwd(target): git("init") # Configure git in case you're running in CI git("config", "user.name", "Copier Test") git("config", "user.email", "test@copier") # Commit changes git("add", ".") commit("-m", "hello world") # Emulate the user modifying the README by hand with open(readme, "w") as readme_fd: readme_fd.write( dedent(""" Let me introduce myself. My name is Guybrush, and my project is to become a pirate. Thanks for your grog. """)) commit("-m", "I prefer grog") # Update target to latest tag and check it's updated in answers file CopierApp.invoke(force=True) assert answers.read_text() == dedent(f""" # Changes here will be overwritten by Copier _commit: v0.0.2 _src_path: {REPO_BUNDLE_PATH} author_name: Guybrush project_name: to become a pirate """) # Check migrations were executed properly assert not (target / "before-v0.0.1").is_file() assert not (target / "after-v0.0.1").is_file() assert (target / "before-v0.0.2").is_file() assert (target / "after-v0.0.2").is_file() (target / "before-v0.0.2").unlink() (target / "after-v0.0.2").unlink() assert not (target / "before-v1.0.0").is_file() assert not (target / "after-v1.0.0").is_file() commit("-m", "Update template to v0.0.2") # Update target to latest commit, which is still untagged CopierApp.invoke(force=True, vcs_ref="HEAD") # Check no new migrations were executed assert not (target / "before-v0.0.1").is_file() assert not (target / "after-v0.0.1").is_file() assert not (target / "before-v0.0.2").is_file() assert not (target / "after-v0.0.2").is_file() assert not (target / "before-v1.0.0").is_file() assert not (target / "after-v1.0.0").is_file() # Check it's updated OK assert answers.read_text() == dedent(f""" # Changes here will be overwritten by Copier _commit: v0.0.2-1-g81c335d _src_path: {REPO_BUNDLE_PATH} author_name: Guybrush project_name: to become a pirate """) assert readme.read_text() == dedent(""" Let me introduce myself. My name is Guybrush. My project is to become a pirate. Thanks for your grog. """) commit("-m", "Update template to v0.0.2-1-g81c335d") assert not git("status", "--porcelain") # No more updates exist, so updating again should change nothing CopierApp.invoke(force=True, vcs_ref="HEAD") assert not git("status", "--porcelain") # If I change an option, it updates properly copy( data={ "author_name": "Largo LaGrande", "project_name": "to steal a lot" }, force=True, vcs_ref="HEAD", ) assert readme.read_text() == dedent(""" Let me introduce myself. My name is Largo LaGrande. My project is to steal a lot. Thanks for your grog. """)
from copier.cli import CopierApp if __name__ == "__main__": CopierApp.run()