def test_centos_conf(cronie, tmp_path: Path): """ make sure the centos-specific configuration is correct """ source_git_path = tmp_path.joinpath("cronie-sg") # create src-git source_git_path.mkdir() create_new_repo(source_git_path, []) sgg = SourceGitGenerator( LocalProject(working_dir=source_git_path), Config(), dist_git_path=cronie, upstream_ref="cronie-1.5.2", centos_package="cronie", ) dg = sgg.dist_git assert isinstance(dg, CentOS8DistGit) flexmock( DistGit, download_upstream_archive=lambda: cronie / "SOURCES" / "cronie-1.5.2.tar.gz", ) assert sgg.primary_archive == cronie / "SOURCES" / "cronie-1.5.2.tar.gz" assert dg.absolute_source_dir == cronie / "SOURCES" assert dg.absolute_specfile_dir == cronie / "SPECS" assert dg.absolute_specfile_path == cronie / "SPECS" / "cronie.spec"
def test_centos_cronie(dist_git_branch, upstream_ref, api_instance_source_git, tmp_path: Path): source_git_path = tmp_path.joinpath("cronie-sg") # create src-git source_git_path.mkdir() create_new_repo(source_git_path, []) sgg = SourceGitGenerator( LocalProject(working_dir=source_git_path), api_instance_source_git.config, "https://github.com/cronie-crond/cronie", upstream_ref=upstream_ref, centos_package="cronie", dist_git_branch=dist_git_branch, ) sgg.create_from_upstream() if dist_git_branch == "c8s": assert CENTOS_DOMAIN in sgg.dist_git.local_project.git_url else: assert CENTOS_STREAM_GITLAB in sgg.dist_git.local_project.git_url # verify it subprocess.check_call(["packit", "srpm"], cwd=source_git_path) srpm_path = list(source_git_path.glob("cronie-*.src.rpm"))[0] assert srpm_path.is_file()
def test_set_spec_ver_empty_changelog(tmp_path): u_remote_path = tmp_path / "upstream_remote" u_remote_path.mkdir(parents=True, exist_ok=True) create_new_repo(u_remote_path, ["--bare"]) u = tmp_path / "upstream_git" shutil.copytree(EMPTY_CHANGELOG, u) initiate_git_repo(u, tag="0.1.0") with cwd(tmp_path): c = get_test_config() pc = get_local_package_config(str(u)) pc.upstream_project_url = str(u) lp = LocalProject(working_dir=u) ups = Upstream(c, pc, lp) new_ver = "1.2.3" ups.specfile.version = new_ver ups.specfile.add_changelog_entry("- asdqwe") assert ups.get_specfile_version() == new_ver assert "%changelog" not in u.joinpath("beer.spec").read_text()
def test_set_spec_macro_source(tmp_path): u_remote_path = tmp_path / "upstream_remote" u_remote_path.mkdir(parents=True, exist_ok=True) create_new_repo(u_remote_path, ["--bare"]) u = tmp_path / "upstream_git" shutil.copytree(UPSTREAM_MACRO_IN_SOURCE, u) initiate_git_repo(u, tag="0.1.0") with cwd(tmp_path): c = get_test_config() pc = get_local_package_config(str(u)) pc.upstream_project_url = str(u) lp = LocalProject(working_dir=u) ups = Upstream(c, pc, lp) expected_sources = ups.specfile.sources new_ver = "1.2.3" ups.specfile.set_spec_version(version=new_ver, changelog_entry="- asdqwe") assert ups.get_specfile_version() == new_ver assert ups.specfile.sources == expected_sources expected_sources = ups.specfile.sources new_rel = "121" ups.specfile.set_spec_version(release=new_rel) assert ups.specfile.get_release() == new_rel assert ups.specfile.sources == expected_sources
def initiate_git_repo( directory, tag=None, upstream_remote="https://lol.wat", push=False, copy_from: Optional[Path] = None, remotes=None, empty_commits_count: int = 3, add_initial_content: bool = True, ): """ Initiate a git repo for testing. :param directory: path to the git repo :param tag: if set, tag the latest commit with this tag :param upstream_remote: name of the origin - upstream remote (not used when remotes are set) :param remotes: provide list of tuples (name, remote_url) :param push: push to the remote? :param copy_from: source tree to copy to the newly created git repo """ if remotes is None: remotes = [("origin", upstream_remote)] if copy_from: shutil.copytree(copy_from, directory) create_new_repo(directory, []) git_set_user_email(directory) for i in range(empty_commits_count): subprocess.check_call( ["git", "commit", "--allow-empty", "-m", f"empty commit #{i}"], cwd=directory, ) if add_initial_content: directory_path = Path(directory) directory_path.joinpath("README").write_text( "Best upstream project ever!") # this file is in the tarball directory_path.joinpath("hops").write_text("Cascade\n") subprocess.check_call(["git", "add", "."], cwd=directory) subprocess.check_call(["git", "commit", "-m", "commit with data"], cwd=directory) if tag: subprocess.check_call( ["git", "tag", "-a", "-m", f"tag {tag}, tests", tag], cwd=directory) for name, url in remotes: subprocess.check_call(["git", "remote", "add", name, url], cwd=directory) if push: subprocess.check_call(["git", "fetch", "origin"], cwd=directory) # tox strips some env vars so your user gitconfig is not picked up # hence we need to be very explicit with git commands here subprocess.check_call( ["git", "push", "--tags", "-u", "origin", "main:main"], cwd=directory)
def sourcegit_and_remote(tmp_path): sourcegit_remote = tmp_path / "source_git_remote" sourcegit_remote.mkdir() create_new_repo(sourcegit_remote, ["--bare"]) sourcegit_dir = tmp_path / "source_git" shutil.copytree(SOURCEGIT_UPSTREAM, sourcegit_dir) initiate_git_repo(sourcegit_dir, tag=SOURCE_GIT_RELEASE_TAG) subprocess.check_call(["cp", "-R", SOURCEGIT_SOURCEGIT, tmp_path], cwd=sourcegit_remote) git_add_and_commit(directory=sourcegit_dir, message="sourcegit content") return sourcegit_dir, sourcegit_remote
def test_pr_id_and_ref(tmp_path: Path, merge, hops_file_content): """p-s passes both ref and pr_id, we want to check out PR""" remote = tmp_path / "remote" remote.mkdir() create_new_repo(remote, ["--bare"]) upstream_git = tmp_path / "upstream_git" upstream_git.mkdir() initiate_git_repo(upstream_git, push=True, upstream_remote=str(remote)) # mimic github PR pr_id = "123" ref = ( subprocess.check_output(["git", "rev-parse", "HEAD^"], cwd=upstream_git) .strip() .decode() ) local_tmp_branch = "asdqwe" subprocess.check_call(["git", "branch", local_tmp_branch, ref], cwd=upstream_git) subprocess.check_call( ["git", "push", "origin", f"{local_tmp_branch}:refs/pull/{pr_id}/head"], cwd=upstream_git, ) subprocess.check_call(["git", "branch", "-D", local_tmp_branch], cwd=upstream_git) git_project = flexmock(repo="random_name", namespace="random_namespace") git_project.should_receive("get_pr").and_return(flexmock(target_branch="main")) LocalProject( working_dir=upstream_git, offline=True, pr_id=pr_id, ref=ref, git_service=GithubService(), git_project=git_project, merge_pr=merge, ) assert ( subprocess.check_output( ["git", "rev-parse", "--abbrev-ref", "HEAD"], cwd=upstream_git ) .strip() .decode() == f"pr/{pr_id}" ) if hops_file_content: # the changes are merged into main so the file should be there assert (upstream_git / "hops").read_text() == hops_file_content else: # we haven't merged into main, so there should be no "hops" file assert not (upstream_git / "hops").is_file()
def downstream_n_distgit(tmp_path): d_remote = tmp_path / "downstream_remote" d_remote.mkdir() create_new_repo(d_remote, ["--bare"]) d = tmp_path / "dist_git" shutil.copytree(DISTGIT, d) initiate_git_repo(d, tag="0.0.0") u = tmp_path / "upstream_git" shutil.copytree(UPSTREAM, u) initiate_git_repo(u, push=False, upstream_remote=str(d_remote)) return u, d
def test_pr_id_and_ref_gitlab(tmp_path: Path): """p-s passes both ref and pr_id, we want to check out PR""" remote = tmp_path / "remote" remote.mkdir() create_new_repo(remote, ["--bare"]) upstream_git = tmp_path / "upstream_git" upstream_git.mkdir() initiate_git_repo(upstream_git, push=True, upstream_remote=str(remote)) # mimic gitlab MR pr_id = "123" ref = ( subprocess.check_output(["git", "rev-parse", "HEAD^"], cwd=upstream_git) .strip() .decode() ) local_tmp_branch = "asdqwe" subprocess.check_call(["git", "branch", local_tmp_branch, ref], cwd=upstream_git) subprocess.check_call( [ "git", "push", "origin", f"{local_tmp_branch}:refs/merge-requests/{pr_id}/head", ], cwd=upstream_git, ) subprocess.check_call(["git", "branch", "-D", local_tmp_branch], cwd=upstream_git) git_project = flexmock(repo="random_name", namespace="random_namespace") git_project.should_receive("get_pr").and_return(flexmock(target_branch="main")) LocalProject( working_dir=upstream_git, offline=True, pr_id=pr_id, ref=ref, git_service=GitlabService(token="12345"), git_project=git_project, ) assert ( subprocess.check_output( ["git", "rev-parse", "--abbrev-ref", "HEAD"], cwd=upstream_git ) .strip() .decode() == f"pr/{pr_id}" )
def test_create_srcgit_requre_clean(api_instance_source_git, tmp_path: Path): """ use requre to create a source-git out of it in an empty git repo - packit will pull upstream git history """ # clone dist-git pkg = "python-requre" dist_git_ref = "6b27ffacda06289ca2d546e15b3c96845243005f" dist_git_path = tmp_path.joinpath(pkg) source_git_path = tmp_path.joinpath("requre-sg") FedPKG().clone(pkg, str(dist_git_path), anonymous=True) dg_lp = LocalProject(working_dir=dist_git_path) # check out specific ref subprocess.check_call(["git", "reset", "--hard", dist_git_ref], cwd=dist_git_path) # add a patch in there spec = Specfile(dist_git_path / f"{pkg}.spec", sources_dir=dist_git_path) patch_name = "hello.patch" patch_path = dist_git_path.joinpath(patch_name) patch_path.write_text(REQURE_PATCH) patch = PatchMetadata(name=patch_name, path=patch_path, present_in_specfile=False) spec.add_patch(patch) dg_lp.stage() dg_lp.commit("add the hello patch") subprocess.check_call(["fedpkg", "prep"], cwd=dist_git_path) # create src-git source_git_path.mkdir() create_new_repo(Path(source_git_path), []) sgg = SourceGitGenerator( LocalProject(working_dir=source_git_path), api_instance_source_git.config, "https://github.com/packit/requre", upstream_ref="0.4.0", dist_git_path=dist_git_path, ) sgg.create_from_upstream() # verify it subprocess.check_call(["packit", "srpm"], cwd=source_git_path) srpm_path = list( source_git_path.glob("python-requre-0.4.0-2.*.src.rpm"))[0] assert srpm_path.is_file()
def ogr_distgit_and_remote(tmp_path) -> Tuple[Path, Path]: d_remote_path = tmp_path / "ogr_dist_git_remote" d_remote_path.mkdir(parents=True, exist_ok=True) create_new_repo(d_remote_path, ["--bare"]) d = tmp_path / "ogr_dist_git" shutil.copytree(DG_OGR, d) initiate_git_repo( d, push=True, remotes=[ ("origin", str(d_remote_path)), ("i_am_distgit", "https://src.fedoraproject.org/rpms/python-ogr"), ], ) prepare_dist_git_repo(d) return d, d_remote_path
def distgit_with_autochangelog_and_remote(tmp_path) -> Tuple[Path, Path]: d_remote_path = tmp_path / "autochangelog_dist_git_remote" d_remote_path.mkdir(parents=True, exist_ok=True) create_new_repo(d_remote_path, ["--bare"]) d = tmp_path / "autochangelog_dist_git" shutil.copytree(DISTGIT_WITH_AUTOCHANGELOG, d) initiate_git_repo( d, push=True, remotes=[ ("origin", str(d_remote_path)), ("i_am_distgit", "https://src.fedoraproject.org/rpms/python-ogr"), ], ) prepare_dist_git_repo(d) return d, d_remote_path
def get_git_repo_and_remote(target_dir: Path, repo_template_path: Path) -> Tuple[Path, Path]: """ :param target_dir: tmpdir from pytest - we'll work here :param repo_template_path: git repo template from tests/data/ """ u_remote_path = target_dir / f"upstream_remote-{repo_template_path.name}" u_remote_path.mkdir(parents=True, exist_ok=True) create_new_repo(u_remote_path, ["--bare"]) u = target_dir / f"local_clone-{repo_template_path.name}" shutil.copytree(repo_template_path, u) initiate_git_repo(u, tag="0.1.0", push=True, upstream_remote=str(u_remote_path)) return u, u_remote_path
def test_centos_cronie(api_instance_source_git, tmp_path: Path): source_git_path = tmp_path.joinpath("cronie-sg") # create src-git source_git_path.mkdir() create_new_repo(source_git_path, []) sgg = SourceGitGenerator( LocalProject(working_dir=source_git_path), api_instance_source_git.config, "https://github.com/cronie-crond/cronie", upstream_ref="cronie-1.5.2", centos_package="cronie", ) sgg.create_from_upstream() # verify it subprocess.check_call(["packit", "srpm"], cwd=source_git_path) srpm_path = list(source_git_path.glob("cronie-1.5.2-*.src.rpm"))[0] assert srpm_path.is_file()
def test_fetch_upstream_ref(api_instance_source_git, tmp_path: Path): tag = "1.0.0" s = tmp_path.joinpath("s") u = tmp_path.joinpath("u") u.mkdir() create_new_repo(Path(s), []) initiate_git_repo(u, tag=tag) sgg = SourceGitGenerator( LocalProject(working_dir=s), api_instance_source_git.config, str(u), upstream_ref=tag, centos_package="x", ) sgg._pull_upstream_ref() assert s.joinpath(".git").is_dir() assert sgg.local_project.ref == "main" assert sgg.local_project.working_dir.joinpath("hops").read_text() == "Cascade\n" assert sgg.local_project.git_repo.head.commit.message == "commit with data\n"
def test_pr_id_and_ref(tmp_path: Path): """ p-s passes both ref and pr_id, we want to check out PR """ remote = tmp_path / "remote" remote.mkdir() create_new_repo(remote, ["--bare"]) upstream_git = tmp_path / "upstream_git" upstream_git.mkdir() initiate_git_repo(upstream_git, push=True, upstream_remote=str(remote)) # mimic github PR pr_id = "123" ref = (subprocess.check_output(["git", "rev-parse", "HEAD^"], cwd=upstream_git).strip().decode()) local_tmp_branch = "asdqwe" subprocess.check_call(["git", "branch", local_tmp_branch, ref], cwd=upstream_git) subprocess.check_call( [ "git", "push", "origin", f"{local_tmp_branch}:refs/pull/{pr_id}/head" ], cwd=upstream_git, ) subprocess.check_call(["git", "branch", "-D", local_tmp_branch], cwd=upstream_git) LocalProject( working_dir=upstream_git, offline=True, pr_id=pr_id, ref=ref, git_service=GithubService(), ) assert (subprocess.check_output( ["git", "rev-parse", "--abbrev-ref", "HEAD"], cwd=upstream_git).strip().decode() == f"pr/{pr_id}")
def upstream_without_config(tmp_path): u_remote = tmp_path / "upstream_remote" u_remote.mkdir() create_new_repo(u_remote, ["--bare"]) return u_remote