live fixes

also rename branches to has_remotes method and change return type
This commit is contained in:
Evgenii Alekseev 2021-10-12 21:11:48 +03:00
parent 8d95ac5e9f
commit ea9c031e0c
4 changed files with 40 additions and 39 deletions

View File

@ -117,9 +117,10 @@ class Application:
def add_local(path: Path) -> Path:
package = Package.load(path, self.repository.pacman, aur_url)
Sources.init(path)
shutil.copytree(path, self.repository.paths.manual_for(package.base)) # copy package for the build
shutil.copytree(path, self.repository.paths.cache_for(package.base)) # copy package to store in caches
cache_dir = self.repository.paths.cache_for(package.base)
shutil.copytree(path, cache_dir) # copy package to store in caches
Sources.init(cache_dir) # we need to run init command in directory where we do have permissions
shutil.copytree(cache_dir, self.repository.paths.manual_for(package.base)) # copy package for the build
return self.repository.paths.manual_for(package.base)
def add_remote(src: str) -> Path:
@ -233,7 +234,7 @@ class Application:
def has_local(package_base: str) -> bool:
cache_dir = self.repository.paths.cache_for(package_base)
return cache_dir.is_dir() and not Sources.branches(cache_dir)
return cache_dir.is_dir() and not Sources.has_remotes(cache_dir)
return [
package

View File

@ -53,16 +53,6 @@ class Sources:
*[str(fn.relative_to(sources_dir)) for fn in found_files],
exception=None, cwd=sources_dir, logger=Sources.logger)
@staticmethod
def branches(sources_dir: Path) -> List[str]:
"""
list current branches. Currently this method is used to define if there is initialized git repository
:param sources_dir: local path to git repository
:return: sorted list of available branches
"""
branches = Sources._check_output("git", "branch", exception=None, cwd=sources_dir, logger=Sources.logger)
return sorted(branches.splitlines())
@staticmethod
def diff(sources_dir: Path, patch_path: Path) -> None:
"""
@ -82,7 +72,7 @@ class Sources:
"""
# local directory exists and there is .git directory
is_initialized_git = (sources_dir / ".git").is_dir()
if is_initialized_git and not Sources.branches(sources_dir):
if is_initialized_git and not Sources.has_remotes(sources_dir):
# there is git repository, but no remote configured so far
Sources.logger.info("skip update at %s because there are no branches configured", sources_dir)
return
@ -100,6 +90,16 @@ class Sources:
Sources._check_output("git", "reset", "--hard", f"origin/{Sources._branch}",
exception=None, cwd=sources_dir, logger=Sources.logger)
@staticmethod
def has_remotes(sources_dir: Path) -> bool:
"""
check if there are remotes for the repository
:param sources_dir: local path to git repository
:return: True in case if there is any remote and false otherwise
"""
remotes = Sources._check_output("git", "remote", exception=None, cwd=sources_dir, logger=Sources.logger)
return bool(remotes)
@staticmethod
def init(sources_dir: Path) -> None:
"""

View File

@ -168,8 +168,9 @@ def test_add_local(application: Application, package_ahriman: Package, mocker: M
application.add([package_ahriman.base], PackageSource.Local, True)
init_mock.assert_called_once()
copytree_mock.assert_has_calls([
mock.call(Path(package_ahriman.base), application.repository.paths.manual_for(package_ahriman.base)),
mock.call(Path(package_ahriman.base), application.repository.paths.cache_for(package_ahriman.base)),
mock.call(application.repository.paths.cache_for(package_ahriman.base),
application.repository.paths.manual_for(package_ahriman.base)),
])
@ -321,7 +322,7 @@ def test_unknown_no_aur(application: Application, package_ahriman: Package, mock
mocker.patch("ahriman.core.repository.repository.Repository.packages", return_value=[package_ahriman])
mocker.patch("ahriman.models.package.Package.from_aur", side_effect=Exception())
mocker.patch("pathlib.Path.is_dir", return_value=True)
mocker.patch("ahriman.core.build_tools.sources.Sources.branches", return_value=[])
mocker.patch("ahriman.core.build_tools.sources.Sources.has_remotes", return_value=False)
assert not application.unknown()

View File

@ -22,26 +22,6 @@ def test_add(mocker: MockerFixture) -> None:
exception=None, cwd=local, logger=pytest.helpers.anyvar(int))
def test_branches(mocker: MockerFixture) -> None:
"""
must ask for available branches
"""
check_output_mock = mocker.patch("ahriman.core.build_tools.sources.Sources._check_output", return_value="b\na")
local = Path("local")
branches = Sources.branches(local)
check_output_mock.assert_called_with("git", "branch", exception=None, cwd=local, logger=pytest.helpers.anyvar(int))
assert branches == ["a", "b"]
def test_branches_empty(mocker: MockerFixture) -> None:
"""
must ask for available branches and do not fail if no branches found
"""
mocker.patch("ahriman.core.build_tools.sources.Sources._check_output", return_value="")
assert Sources.branches(Path("local")) == []
def test_diff(mocker: MockerFixture) -> None:
"""
must calculate diff
@ -60,7 +40,7 @@ def test_fetch_empty(mocker: MockerFixture) -> None:
must do nothing in case if no branches available
"""
mocker.patch("pathlib.Path.is_dir", return_value=True)
mocker.patch("ahriman.core.build_tools.sources.Sources.branches", return_value=[])
mocker.patch("ahriman.core.build_tools.sources.Sources.has_remotes", return_value=False)
check_output_mock = mocker.patch("ahriman.core.build_tools.sources.Sources._check_output")
Sources.fetch(Path("local"), "remote")
@ -72,7 +52,7 @@ def test_fetch_existing(mocker: MockerFixture) -> None:
must fetch new package via fetch command
"""
mocker.patch("pathlib.Path.is_dir", return_value=True)
mocker.patch("ahriman.core.build_tools.sources.Sources.branches", return_value=["master"])
mocker.patch("ahriman.core.build_tools.sources.Sources.has_remotes", return_value=True)
check_output_mock = mocker.patch("ahriman.core.build_tools.sources.Sources._check_output")
local = Path("local")
@ -105,6 +85,25 @@ def test_fetch_new(mocker: MockerFixture) -> None:
])
def test_has_remotes(mocker: MockerFixture) -> None:
"""
must ask for remotes
"""
check_output_mock = mocker.patch("ahriman.core.build_tools.sources.Sources._check_output", return_value="origin")
local = Path("local")
assert Sources.has_remotes(local)
check_output_mock.assert_called_with("git", "remote", exception=None, cwd=local, logger=pytest.helpers.anyvar(int))
def test_has_remotes_empty(mocker: MockerFixture) -> None:
"""
must ask for remotes and return false in case if no remotes found
"""
mocker.patch("ahriman.core.build_tools.sources.Sources._check_output", return_value="")
assert not Sources.has_remotes(Path("local"))
def test_init(mocker: MockerFixture) -> None:
"""
must create empty repository at the specified path