Esempio n. 1
0
def test_initial_run_without_change_on_template_just_initializes_branch(
        cookiecutter_template_directory: Path, cookies: Cookies):
    result: Result = cookies.bake(
        extra_context=SAMPLE_CONTEXT,
        template=str(cookiecutter_template_directory))
    if result.exception is not None:
        raise result.exception
    project_directory = Path(result.project)
    _initialize_git_repo(project_directory)
    subprocess.run(["git", "add", "-A"],
                   cwd=str(project_directory),
                   check=True)
    subprocess.run(["git", "commit", "-m", "initial"],
                   cwd=str(project_directory),
                   check=True)

    context = json.loads(
        project_directory.joinpath(
            "docs", "cookiecutter_input.json").read_text(encoding="utf-8"))
    update_project_template_branch(context,
                                   str(project_directory),
                                   "cookiecutter-template",
                                   merge_now=None,
                                   push_template_branch_changes=False,
                                   exclude_pathspecs=None)
    subprocess.run(["git", "rev-parse", "cookiecutter-template"],
                   cwd=str(project_directory),
                   check=True)
Esempio n. 2
0
def test_exclude_paths(cookiecutter_template_directory: Path,
                       cookies: Cookies):
    result: Result = cookies.bake(
        extra_context=SAMPLE_CONTEXT,
        template=str(cookiecutter_template_directory))
    if result.exception is not None:
        raise result.exception
    project_directory = Path(result.project)
    _initialize_git_repo(project_directory)
    subprocess.run(["git", "add", "-A"],
                   cwd=str(project_directory),
                   check=True)
    subprocess.run(["git", "commit", "-m", "initial"],
                   cwd=str(project_directory),
                   check=True)

    context = json.loads(
        project_directory.joinpath(
            "docs", "cookiecutter_input.json").read_text(encoding="utf-8"))

    context['_template'] = str(cookiecutter_template_directory)
    context['project_slug'] = 'a_new_name'

    update_project_template_branch(context,
                                   str(project_directory),
                                   "cookiecutter-template",
                                   merge_now=True,
                                   push_template_branch_changes=False,
                                   exclude_pathspecs='README.rst')

    readme = project_directory.joinpath("README.rst").read_text(
        encoding="utf-8")
    assert 'a_new_name' not in readme
Esempio n. 3
0
def test_first_run_creates_branch_on_first_commit_and_updates_based_on_template(
        cookiecutter_template_directory: Path, cookies: Cookies):
    result: Result = cookies.bake(
        extra_context=SAMPLE_CONTEXT,
        template=str(cookiecutter_template_directory))
    if result.exception is not None:
        raise result.exception
    project_directory = Path(result.project)
    _initialize_git_repo(project_directory)
    subprocess.run(["git", "add", "-A"],
                   cwd=str(project_directory),
                   check=True)
    subprocess.run(["git", "commit", "-m", "initial"],
                   cwd=str(project_directory),
                   check=True)

    context = json.loads(
        project_directory.joinpath(
            "docs", "cookiecutter_input.json").read_text(encoding="utf-8"))

    cookiecutter_template_directory.joinpath(
        "{{cookiecutter.project_slug}}",
        "README.rst").write_text("updated readme")
    cookiecutter_template_directory.joinpath("{{cookiecutter.project_slug}}", ".file_with_dot.txt") \
        .write_text("updated content")
    new_template_file_with_dot_at_start = cookiecutter_template_directory.joinpath(
        "{{cookiecutter.project_slug}}", ".travis.yml")
    new_template_file_with_dot_at_start.touch()
    new_template_file_with_dot_at_start.write_text("new file text")
    subprocess.run(["git", "add", "-A"],
                   cwd=str(cookiecutter_template_directory),
                   check=True)
    subprocess.run(["git", "commit", "-m", "updated readme"],
                   cwd=str(cookiecutter_template_directory),
                   check=True)

    context['_template'] = str(cookiecutter_template_directory)
    update_project_template_branch(context,
                                   str(project_directory),
                                   "cookiecutter-template",
                                   "master",
                                   merge_now=None,
                                   push_template_branch_changes=False,
                                   exclude_pathspecs=(),
                                   interactive=False)

    subprocess.run(["git", "merge", "cookiecutter-template"],
                   cwd=str(project_directory),
                   check=True)
    readme = project_directory.joinpath("README.rst").read_text(
        encoding="utf-8")
    assert readme == "updated readme"

    assert project_directory.joinpath(".file_with_dot.txt").read_text(
        encoding="utf-8") == "updated content"

    project_new_file = project_directory.joinpath(".travis.yml")
    assert project_new_file.exists()
    assert project_new_file.read_text(encoding="utf-8") == "new file text"
def main(context_file: str, branch: str,
         merge_now: Optional[bool],
         push_template_branch_changes: Optional[bool]):
    """Upgrade projects created from a Cookiecutter template"""
    context = _load_context(context_file)
    project_directory = os.getcwd()
    update_project_template_branch(context,
                                   project_directory,
                                   branch,
                                   merge_now,
                                   push_template_branch_changes)
def main(context_file: str, branch: str, upgrade_branch: Optional[str],
         zip_file: Optional[str], interactive: bool, merge_now: Optional[bool],
         push_template_branch_changes: Optional[bool], exclude: Tuple[str,
                                                                      ...]):
    """Upgrade projects created from a Cookiecutter template"""
    context = _load_context(context_file)
    if zip_file is not None and _is_valid_file(zip_file):
        click.echo(f"Using zip-file: {zip_file} for upgrade.")
        context['_template'] = zip_file
    project_directory = os.getcwd()
    update_project_template_branch(context, project_directory, branch,
                                   upgrade_branch, merge_now,
                                   push_template_branch_changes, exclude,
                                   interactive)
Esempio n. 6
0
def test_change_project_slug(cookiecutter_template_directory: Path,
                             cookies: Cookies):
    result: Result = cookies.bake(
        extra_context=SAMPLE_CONTEXT,
        template=str(cookiecutter_template_directory))
    if result.exception is not None:
        raise result.exception
    project_directory = Path(result.project)
    _initialize_git_repo(project_directory)
    subprocess.run(["git", "add", "-A"],
                   cwd=str(project_directory),
                   check=True)
    subprocess.run(["git", "commit", "-m", "initial"],
                   cwd=str(project_directory),
                   check=True)

    context = json.loads(
        project_directory.joinpath(
            "docs", "cookiecutter_input.json").read_text(encoding="utf-8"))

    cookiecutter_template_directory.joinpath(
        "{{cookiecutter.project_slug}}",
        "README.rst").write_text("updated readme")
    subprocess.run(["git", "add", "-A"],
                   cwd=str(cookiecutter_template_directory),
                   check=True)
    subprocess.run(["git", "commit", "-m", "updated readme"],
                   cwd=str(cookiecutter_template_directory),
                   check=True)

    context['_template'] = str(cookiecutter_template_directory)
    context['project_name'] = "My New Name For Project"
    old_project_slug = context['project_slug']
    new_project_slug = "my_new_name_for_project"
    context['project_slug'] = new_project_slug
    update_project_template_branch(context,
                                   str(project_directory),
                                   "cookiecutter-template",
                                   "master",
                                   merge_now=True,
                                   push_template_branch_changes=False,
                                   exclude_pathspecs=(),
                                   interactive=False)

    readme = project_directory.joinpath("README.rst").read_text(
        encoding="utf-8")
    assert readme == "updated readme"
    assert project_directory.joinpath(new_project_slug).is_dir()
    assert not project_directory.joinpath(old_project_slug).exists()
Esempio n. 7
0
def test_changes_from_zip_file(cookiecutter_template_directory: Path,
                               cookies: Cookies):
    result: Result = cookies.bake(
        extra_context=SAMPLE_CONTEXT,
        template=str(cookiecutter_template_directory))
    if result.exception is not None:
        raise result.exception
    project_directory = Path(result.project)
    _initialize_git_repo(project_directory)
    subprocess.run(["git", "add", "-A"],
                   cwd=str(project_directory),
                   check=True)
    subprocess.run(["git", "commit", "-m", "initial"],
                   cwd=str(project_directory),
                   check=True)

    context = json.loads(
        project_directory.joinpath(
            "docs", "cookiecutter_input.json").read_text(encoding="utf-8"))

    cookiecutter_template_directory.joinpath(
        "{{cookiecutter.project_slug}}",
        "README.rst").write_text("updated readme for zip test")
    subprocess.run(["git", "add", "-A"],
                   cwd=str(cookiecutter_template_directory),
                   check=True)
    subprocess.run(["git", "commit", "-m", "updated readme for zip test"],
                   cwd=str(cookiecutter_template_directory),
                   check=True)

    # Pack dummy template to zip file
    zipfile_path = ''
    with CreateTempDirectory("testzip") as testdir:
        zipfile_path = str(
            testdir.joinpath(
                basename(cookiecutter_template_directory) + ".zip"))

        with ZipFile(str(zipfile_path), 'w', ZIP_DEFLATED) as zip:
            # this line is needed for cookiecutter zip verification
            zip.write(cookiecutter_template_directory,
                      str(basename(cookiecutter_template_directory)))
            for folderName, subfolders, filenames in os.walk(
                    cookiecutter_template_directory):
                for filename in filenames:
                    filePath = os.path.join(folderName, filename)
                    zip.write(
                        filePath,
                        str(filePath).replace(
                            str(cookiecutter_template_directory),
                            basename(str(cookiecutter_template_directory))))

        context['_template'] = str(zipfile_path)
        update_project_template_branch(context,
                                       str(project_directory),
                                       "cookiecutter-template",
                                       "master",
                                       merge_now=True,
                                       push_template_branch_changes=False,
                                       exclude_pathspecs=(),
                                       interactive=False)

    readme = project_directory.joinpath("README.rst").read_text(
        encoding="utf-8")
    assert readme == "updated readme for zip test"