Ejemplo n.º 1
0
def test_package_config_equal(job_config_simple):
    assert PackageConfig(
        specfile_path="fedora/package.spec",
        synced_files=SyncFilesConfig(files_to_sync=[
            SyncFilesItem(src="packit.yaml", dest="packit.yaml")
        ]),
        jobs=[job_config_simple],
    ) == PackageConfig(
        specfile_path="fedora/package.spec",
        synced_files=SyncFilesConfig(files_to_sync=[
            SyncFilesItem(src="packit.yaml", dest="packit.yaml")
        ]),
        jobs=[job_config_simple],
    )
Ejemplo n.º 2
0
def test_dist_git_package_url():
    di = {
        "dist_git_base_url": "https://packit.dev/",
        "downstream_package_name": "packit",
        "dist_git_namespace": "awesome",
        "synced_files": ["fedora/foobar.spec"],
        "specfile_path": "fedora/package.spec",
        "create_pr": False,
    }
    new_pc = PackageConfig.get_from_dict(di)
    pc = PackageConfig(
        dist_git_base_url="https://packit.dev/",
        downstream_package_name="packit",
        dist_git_namespace="awesome",
        synced_files=SyncFilesConfig(files_to_sync=[
            SyncFilesItem(src="fedora/foobar.spec", dest="fedora/foobar.spec")
        ]),
        specfile_path="fedora/package.spec",
        create_pr=False,
        jobs=get_default_job_config(),
    )
    assert new_pc.specfile_path.endswith("fedora/package.spec")
    assert pc.specfile_path.endswith("fedora/package.spec")
    assert pc == new_pc
    assert pc.dist_git_package_url == "https://packit.dev/awesome/packit.git"
    assert new_pc.dist_git_package_url == "https://packit.dev/awesome/packit.git"
    assert not pc.create_pr
Ejemplo n.º 3
0
def test_get_package_config_from_repo_alternative_config_name():
    gp = flexmock(GitProject)
    gp.should_receive("full_repo_name").and_return("a/b")
    gp.should_receive("get_file_content").with_args(path=".packit.yaml",
                                                    ref=None).and_raise(
                                                        FileNotFoundError,
                                                        "not found")
    gp.should_receive("get_file_content").with_args(
        path=".packit.yml",
        ref=None).and_return("---\nspecfile_path: packit.spec\n"
                             "synced_files:\n"
                             "  - packit.spec\n"
                             "  - src: .packit.yaml\n"
                             "    dest: .packit2.yaml")
    config = PackageConfigGetter.get_package_config_from_repo(
        project=GitProject(repo="", service=GitService(), namespace=""),
        reference=None,
        spec_file_path="packit.spec",
    )
    assert isinstance(config, PackageConfig)
    assert config.specfile_path == "packit.spec"
    assert config.synced_files == SyncFilesConfig(files_to_sync=[
        SyncFilesItem(src="packit.spec", dest="packit.spec"),
        SyncFilesItem(src=".packit.yaml", dest=".packit2.yaml"),
    ])
    assert config.create_pr
Ejemplo n.º 4
0
def test_get_package_config_from_repo(
    content,
    project: GitProject,
    mock_spec_search: bool,
    spec_path: Optional[str],
    spec_path_option: Optional[str],
    reference: str,
):
    gp = flexmock(GitProject)
    gp.should_receive("full_repo_name").and_return("a/b")
    gp.should_receive("get_file_content").with_args(
        path=".packit.yaml", ref=reference).and_return(content)
    if mock_spec_search:
        gp.should_receive("get_files").and_return(["packit.spec"]).once()
    config = PackageConfigGetter.get_package_config_from_repo(
        project=project, reference=reference, spec_file_path=spec_path_option)
    assert isinstance(config, PackageConfig)
    assert config.specfile_path == spec_path
    assert set(config.get_all_files_to_sync().files_to_sync) == set(
        SyncFilesConfig(files_to_sync=[
            SyncFilesItem(src="packit.spec", dest="packit.spec"),
            SyncFilesItem(src=".packit.yaml", dest=".packit2.yaml"),
        ]).files_to_sync)
    assert config.create_pr
    for j in config.jobs:
        assert j.specfile_path == spec_path
        assert j.downstream_package_name == config.downstream_package_name
        assert j.upstream_package_name == config.upstream_package_name
Ejemplo n.º 5
0
def test_get_packit_config_from_repo(content):
    flexmock(GitProject).should_receive("get_file_content").and_return(content)
    git_project = GitProject(repo="", service=GitService(), namespace="")
    config = get_packit_config_from_repo(sourcegit_project=git_project, ref="")
    assert isinstance(config, PackageConfig)
    assert Path(config.specfile_path).name == "packit.spec"
    assert config.synced_files == SyncFilesConfig(files_to_sync=[
        SyncFilesItem(src="packit.spec", dest="packit.spec"),
        SyncFilesItem(src=".packit.yaml", dest=".packit2.yaml"),
    ])
Ejemplo n.º 6
0
def test_package_config_not_equal(not_equal_package_config):
    config = PackageConfig(
        specfile_path="fedora/package.spec",
        synced_files=SyncFilesConfig(files_to_sync=[
            SyncFilesItem(src="c", dest="c"),
            SyncFilesItem(src="d", dest="d"),
        ]),
        jobs=[get_job_config_full()],
        create_pr=True,
    )
    assert config != not_equal_package_config
Ejemplo n.º 7
0
def test_package_config_not_equal(not_equal_package_config):
    j = get_job_config_full()
    j.metadata["b"] = "c"
    assert (not PackageConfig(
        specfile_path="fedora/package.spec",
        synced_files=SyncFilesConfig(files_to_sync=[
            SyncFilesItem(src="c", dest="c"),
            SyncFilesItem(src="d", dest="d"),
        ]),
        jobs=[j],
    ) == not_equal_package_config)
Ejemplo n.º 8
0
def test_get_package_config_from_repo(
    content,
    project: GitProject,
    mock_spec_search: bool,
    spec_path: Optional[str],
    spec_path_option: Optional[str],
):
    gp = flexmock(GitProject)
    gp.should_receive("full_repo_name").and_return("a/b")
    gp.should_receive("get_file_content").and_return(content)
    if mock_spec_search:
        gp.should_receive("get_files").and_return(["packit.spec"]).once()
    config = get_package_config_from_repo(project=project,
                                          spec_file_path=spec_path_option)
    assert isinstance(config, PackageConfig)
    assert config.specfile_path == spec_path
    assert config.synced_files == SyncFilesConfig(files_to_sync=[
        SyncFilesItem(src="packit.spec", dest="packit.spec"),
        SyncFilesItem(src=".packit.yaml", dest=".packit2.yaml"),
    ])
    assert config.create_pr
Ejemplo n.º 9
0
        synced_files=SyncFilesConfig(files_to_sync=[
            SyncFilesItem(src="packit.yaml", dest="packit.yaml")
        ]),
        jobs=[job_config_simple],
        downstream_package_name="package",
        create_pr=True,
    )


@pytest.mark.parametrize(
    "not_equal_package_config",
    [
        PackageConfig(
            specfile_path="fedora/other-package.spec",
            synced_files=SyncFilesConfig(files_to_sync=[
                SyncFilesItem(src="a", dest="a"),
                SyncFilesItem(src="b", dest="b"),
            ]),
            jobs=[get_job_config_simple()],
        ),
        PackageConfig(
            specfile_path="fedora/package.spec",
            synced_files=SyncFilesConfig(
                files_to_sync=[SyncFilesItem(src="c", dest="c")]),
            jobs=[get_job_config_simple()],
        ),
        PackageConfig(
            specfile_path="fedora/package.spec",
            synced_files=SyncFilesConfig(files_to_sync=[
                SyncFilesItem(src="a", dest="a"),
                SyncFilesItem(src="b", dest="b"),
            ]),
Ejemplo n.º 10
0
 def make_instance(self, data, **kwargs):
     return SyncFilesConfig(**data)
Ejemplo n.º 11
0
from pathlib import Path

import pytest

from packit.config import PackageConfig, SyncFilesItem, SyncFilesConfig
from packit.sync import RawSyncFilesItem
from tests.spellbook import TESTS_DIR
from packit.utils import cwd


@pytest.mark.parametrize(
    "packit_files,expected",
    [
        (
            SyncFilesConfig(files_to_sync=[
                SyncFilesItem(src="conftest.py", dest="conftest.py")
            ]),
            [
                RawSyncFilesItem(Path("conftest.py"), Path("conftest.py"),
                                 False)
            ],
        ),
        (
            SyncFilesConfig(files_to_sync=[
                SyncFilesItem(src="__init__.py", dest="__init__.py"),
                SyncFilesItem(src="conftest.py", dest="conftest.py"),
                SyncFilesItem(src="spellbook.py", dest="spellbook.py"),
            ]),
            [
                RawSyncFilesItem(Path("__init__.py"), Path("__init__.py"),
                                 False),
Ejemplo n.º 12
0
def test_raw_files_to_sync(files, result):
    files_to_sync = SyncFilesConfig(files_to_sync=files).raw_files_to_sync
    assert files_to_sync == result
Ejemplo n.º 13
0
from pathlib import Path

import pytest

from packit.config import PackageConfig, SyncFilesItem, SyncFilesConfig
from packit.sync import RawSyncFilesItem
from packit.utils.commands import cwd
from tests.spellbook import DATA_DIR


@pytest.mark.parametrize(
    "packit_files,expected",
    [
        (
            SyncFilesConfig(
                files_to_sync=[SyncFilesItem(src="a.md", dest="a.md")]),
            [RawSyncFilesItem(Path("a.md"), Path("a.md"), False)],
        ),
        (
            SyncFilesConfig(files_to_sync=[
                SyncFilesItem(src="a.md", dest="a.md"),
                SyncFilesItem(src="b.md", dest="b.md"),
                SyncFilesItem(src="c.txt", dest="c.txt"),
            ]),
            [
                RawSyncFilesItem(Path("a.md"), Path("a.md"), False),
                RawSyncFilesItem(Path("b.md"), Path("b.md"), False),
                RawSyncFilesItem(Path("c.txt"), Path("c.txt"), False),
            ],
        ),
        (
Ejemplo n.º 14
0
    def sync_from_downstream(
        self,
        dist_git_branch: str = None,
        upstream_branch: str = None,
        no_pr: bool = False,
        fork: bool = True,
        remote_name: str = None,
        exclude_files: Iterable[str] = None,
        force: bool = False,
        sync_only_specfile: bool = False,
    ):
        """
        Sync content of Fedora dist-git repo back to upstream

        :param dist_git_branch: branch in dist-git, defaults to repo's default branch
        :param upstream_branch: upstream branch, defaults to repo's default branch
        :param no_pr: won't create a pull request if set to True
        :param fork: forks the project if set to True
        :param remote_name: name of remote where we should push; if None, try to find a ssh_url
        :param exclude_files: files that will be excluded from the sync
        :param force: ignore changes in the git index
        :param sync_only_specfile: whether to sync only content of specfile
        """
        exclude_files = exclude_files or []
        if not dist_git_branch:
            dist_git_branch = self.dg.local_project.git_project.default_branch
            logger.info(
                f"Dist-git branch not set, defaulting to {dist_git_branch!r}.")
        if not upstream_branch:
            upstream_branch = self.up.local_project.git_project.default_branch
            logger.info(
                f"Upstream branch not set, defaulting to {upstream_branch!r}.")
        logger.info(f"Upstream active branch: {self.up.active_branch}")

        if not force and self.up.is_dirty():
            raise PackitException(
                "The repository is dirty, will not discard the changes. Use --force to bypass."
            )
        self.dg.update_branch(dist_git_branch)
        self.dg.checkout_branch(dist_git_branch)

        logger.info(f"Using {dist_git_branch!r} dist-git branch.")

        if no_pr:
            self.up.checkout_branch(upstream_branch)
        else:
            local_pr_branch = f"{dist_git_branch}-downstream-sync"
            self.up.create_branch(local_pr_branch)
            self.up.checkout_branch(local_pr_branch)

        files = (SyncFilesConfig([
            self.package_config.get_specfile_sync_files_item()
        ]) if sync_only_specfile else self.package_config.synced_files)

        raw_sync_files = files.get_raw_files_to_sync(
            dest_dir=self.dg.local_project.working_dir,
            src_dir=self.up.local_project.working_dir,
        )

        reverse_raw_sync_files = [
            raw_file.reversed() for raw_file in raw_sync_files
            if Path(raw_file.dest).name not in exclude_files
        ]
        sync_files(reverse_raw_sync_files, fail_on_missing=False)

        if not no_pr:
            description = f"Downstream commit: {self.dg.local_project.commit_hexsha}\n"

            commit_msg = f"Sync from downstream branch {dist_git_branch!r}"
            pr_title = f"Update from downstream branch {dist_git_branch!r}"

            self.up.commit(title=commit_msg, msg=description)

            # the branch may already be up, let's push forcefully
            source_branch, fork_username = self.up.push_to_fork(
                self.up.local_project.ref,
                fork=fork,
                force=True,
                remote_name=remote_name,
            )
            self.up.create_pull(
                pr_title,
                description,
                source_branch=source_branch,
                target_branch=upstream_branch,
                fork_username=fork_username,
            )