def test_get_specfile_sync_files_item(): pc = PackageConfig(specfile_path="fedora/python-ogr.spec", downstream_package_name="python-ogr") upstream_specfile_path = "fedora/python-ogr.spec" downstream_specfile_path = "python-ogr.spec" assert pc.get_specfile_sync_files_item() == SyncFilesItem( src=[upstream_specfile_path], dest=downstream_specfile_path) assert pc.get_specfile_sync_files_item( from_downstream=True) == SyncFilesItem(src=[downstream_specfile_path], dest=upstream_specfile_path)
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
def _deserialize( self, value: Any, attr: Optional[str], data: Optional[Mapping[str, Any]], **kwargs, ) -> SyncFilesItem: if isinstance(value, dict): return SyncFilesItem(**SyncFilesItemSchema().load(value)) elif isinstance(value, str): return SyncFilesItem(src=[value], dest=value) else: raise ValidationError(f"Expected 'dict' or 'str', got {type(value)!r}.")
def test_package_config_equal(job_config_simple): assert PackageConfig( specfile_path="fedora/package.spec", synced_files=[SyncFilesItem(src=["packit.yaml"], dest="packit.yaml")], jobs=[job_config_simple], downstream_package_name="package", create_pr=True, ) == PackageConfig( specfile_path="fedora/package.spec", synced_files=[SyncFilesItem(src=["packit.yaml"], dest="packit.yaml")], jobs=[job_config_simple], downstream_package_name="package", create_pr=True, )
def test_get_package_config_from_repo(content): gp = flexmock(GitProject) gp.should_receive("full_repo_name").and_return("a/b") gp.should_receive("get_file_content").and_return(content) gp.should_receive("get_files").and_return(["packit.spec"]) git_project = GitProject(repo="", service=GitService(), namespace="") config = get_package_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"), ]) assert config.create_pr
def get_all_files_to_sync(self): """ Adds the default files (config file, spec file) to synced files if the new files_to_sync option is not yet used otherwise do not performer any addition to the list of files to be synced. When the old option will be removed this method could be removed as well. :return: Files to be synced """ files = self.files_to_sync if not self._files_to_sync_used: if self.specfile_path not in iter_srcs(files): files.append(self.get_specfile_sync_files_item()) if self.config_file_path and self.config_file_path not in iter_srcs(files): # this relative because of glob: "Non-relative patterns are unsupported" files.append( SyncFilesItem( src=[self.config_file_path], dest=self.config_file_path ) ) return files
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
def get_from_dict(cls, raw_dict: dict, validate=True) -> "SyncFilesConfig": if validate: cls.validate(raw_dict) files_to_sync = [] if isinstance(raw_dict, list): for f in raw_dict: if isinstance(f, dict): files_to_sync.append(SyncFilesItem(src=f["src"], dest=f["dest"])) else: files_to_sync.append(SyncFilesItem(src=f, dest=f)) if isinstance(raw_dict, dict): for f in raw_dict: files_to_sync.append(SyncFilesItem(src=f["src"], dest=f["dest"])) return SyncFilesConfig(files_to_sync=files_to_sync)
def get_all_files_to_sync(self): """ Adds the default files (config file, spec file) to synced files when doing propose-update. :return: SyncFilesConfig with default files """ files = self.synced_files.files_to_sync if self.specfile_path not in (item.src for item in files): files.append( SyncFilesItem(src=self.specfile_path, dest=self.specfile_path)) if self.config_file_path and self.config_file_path not in ( item.src for item in files): files.append( SyncFilesItem(src=self.config_file_path, dest=self.config_file_path)) return SyncFilesConfig(files)
def test_command_globs(tmp_path): src_dir = tmp_path / "src" src_dir.mkdir() (src_dir / "file1").touch() (src_dir / "file2").touch() item = SyncFilesItem(["src/*"], "dest") item.resolve(src_base=tmp_path, dest_base=tmp_path) command = item.command() # The return value of glob.glob() is unordered command = command[:-3] + sorted(command[-3:-1]) + command[-1:] assert [ "rsync", "--archive", "--ignore-missing-args", f"{tmp_path}/src/file1", f"{tmp_path}/src/file2", f"{tmp_path}/dest", ] == command
def get_specfile_sync_files_item(self): """ Get SyncFilesItem object for the specfile. :return: SyncFilesItem """ return SyncFilesItem( src=self.specfile_path, dest=f"{self.downstream_package_name}.spec" if self.downstream_package_name else self.specfile_path, )
def get_all_files_to_sync(self): """ Adds the default files (config file, spec file) to synced files when doing propose-update. :return: SyncFilesConfig with default files """ files = self.synced_files.files_to_sync if self.specfile_path not in (item.src for item in files): files.append(SyncFilesItem(src=self.specfile_path, dest=self.specfile_path)) if self.config_file_path and self.config_file_path not in ( item.src for item in files ): # this relative because of glob: "Non-relative patterns are unsupported" files.append( SyncFilesItem(src=self.config_file_path, dest=self.config_file_path) ) return SyncFilesConfig(files)
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.files_to_sync == [ SyncFilesItem(src=["packit.spec"], dest="packit.spec"), SyncFilesItem(src=[".packit.yaml"], dest=".packit2.yaml"), ] assert config.create_pr
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 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
def _deserialize( self, value: Any, attr: Optional[str], data: Optional[Mapping[str, Any]], **kwargs, ) -> SyncFilesItem: if isinstance(value, dict): try: if not isinstance(value["src"], str): raise ValidationError("Field `src` should have type str.") if not isinstance(value["dest"], str): raise ValidationError("Field `dest` should have type str.") file_to_sync = SyncFilesItem(src=value["src"], dest=value["dest"]) except KeyError as e: raise ValidationError(e.__repr__()) elif isinstance(value, str): file_to_sync = SyncFilesItem(src=value, dest=value) else: raise ValidationError(f"'dict' or 'str' required, got {type(value)!r}.") return file_to_sync
def get_specfile_sync_files_item(self, from_downstream: bool = False): """ Get SyncFilesItem object for the specfile. :param from_downstream: True when syncing from downstream :return: SyncFilesItem """ upstream_specfile_path = self.specfile_path downstream_specfile_path = (f"{self.downstream_package_name}.spec" if self.downstream_package_name else basename(upstream_specfile_path)) return SyncFilesItem( src=[ downstream_specfile_path if from_downstream else upstream_specfile_path ], dest=upstream_specfile_path if from_downstream else downstream_specfile_path, )
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"),
) == PackageConfig( specfile_path="fedora/package.spec", synced_files=[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=[ SyncFilesItem(src=["a"], dest="a"), SyncFilesItem(src=["b"], dest="b"), ], jobs=[get_job_config_simple()], ), PackageConfig( specfile_path="fedora/package.spec", synced_files=[SyncFilesItem(src=["c"], dest="c")], jobs=[get_job_config_simple()], ), PackageConfig( specfile_path="fedora/package.spec", synced_files=[ SyncFilesItem(src=["a"], dest="a"), SyncFilesItem(src=["b"], dest="b"), ],
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. import glob import pytest from flexmock import flexmock from packit.config import SyncFilesConfig from packit.sync import get_files_from_wildcard, get_raw_files, SyncFilesItem @pytest.mark.parametrize( "file,glob_files,result", [ ("file", "file", [SyncFilesItem(src="file", dest="dest")]), ( "file/", ["file/a", "file/b"], [ SyncFilesItem(src="file/a", dest="dest"), SyncFilesItem(src="file/b", dest="dest"), ], ), ( "*.md", ["a.md", "b.md"], [ SyncFilesItem(src="a.md", dest="dest"), SyncFilesItem(src="b.md", dest="dest"), ],
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. from pathlib import Path import pytest from flexmock import flexmock from packit.sync import get_raw_files, SyncFilesItem, RawSyncFilesItem @pytest.mark.parametrize( "file,glob_files,result", [ ( SyncFilesItem(src="file/*", dest="dest"), [Path("file/a"), Path("file/b")], [ RawSyncFilesItem(Path("file/a"), Path("dest"), False), RawSyncFilesItem(Path("file/b"), Path("dest"), False), ], ), ( SyncFilesItem(src="file/*", dest="dest/"), [Path("file/a"), Path("file/b")], [ RawSyncFilesItem(Path("file/a"), Path("dest"), True), RawSyncFilesItem(Path("file/b"), Path("dest"), True), ], ), (
), (Path("../test/this"), Path("."), False, pytest.raises(PackitException)), (Path("test/../../this"), Path("."), False, pytest.raises(PackitException)), ], ) def test_check_subpath(subpath, path, trailing_slash, result): with result as r: assert check_subpath(subpath, path, trailing_slash) == r @pytest.mark.parametrize( "item,drop,result", [ (SyncFilesItem(["a", "b"], "dest"), { "src": "b" }, SyncFilesItem(["a"], "dest")), (SyncFilesItem(["a"], "dest"), { "src": Path("a") }, None), ( SyncFilesItem(["a", "b"], "dest"), { "src": "c" }, SyncFilesItem(["a", "b"], "dest"), ), ( SyncFilesItem(["src/a", "src/b"], "dest"), {