Skip to content

Commit

Permalink
Update just in mode install and don't fail if mode check for tools.sy…
Browse files Browse the repository at this point in the history
…stem.package_manager (#11712)

* update just if install

* review

* unused import
  • Loading branch information
czoido committed Jul 27, 2022
1 parent 0fb3d62 commit b2ddd7a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 37 deletions.
15 changes: 5 additions & 10 deletions conan/tools/system/package_manager.py
Expand Up @@ -132,16 +132,11 @@ def _install(self, packages, update=False, check=True, **kwargs):
"installed".format(" ".join(packages)))

def _update(self):
if self._mode == self.mode_check:
raise ConanException("Can't update because tools.system.package_manager:mode is '{0}'."
"Please update packages manually or set "
"'tools.system.package_manager:mode' "
"to '{1}' in the [conf] section of the profile, "
"or in the command line using "
"'-c tools.system.package_manager:mode={1}'".format(self.mode_check,
self.mode_install))
command = self.update_command.format(sudo=self.sudo_str, tool=self.tool_name)
return self._conanfile_run(command, self.accepted_update_codes)
# we just update the package manager database in case we are in 'install mode'
# in case we are in check mode warn about that but don't fail
if self._mode == self.mode_install:
command = self.update_command.format(sudo=self.sudo_str, tool=self.tool_name)
return self._conanfile_run(command, self.accepted_update_codes)

def _check(self, packages):
missing = [pkg for pkg in packages if self.check_package(self.get_package_name(pkg)) != 0]
Expand Down
40 changes: 13 additions & 27 deletions conans/test/integration/tools/system/package_manager_test.py
Expand Up @@ -130,27 +130,8 @@ def fake_check(*args, **kwargs):
"'-c tools.system.package_manager:mode=install'"


@pytest.mark.parametrize("tool_class",
[Apt, Yum, Dnf, Brew, Pkg, PkgUtil, Chocolatey, PacMan, Zypper])
def test_tools_update_mode_check(tool_class):
conanfile = ConanFileMock()
conanfile.conf = Conf()
conanfile.settings = Settings()
conanfile.conf["tools.system.package_manager:tool"] = tool_class.tool_name
conanfile.conf["tools.system.package_manager:mode"] = "check"
with mock.patch('conans.ConanFile.context', new_callable=PropertyMock) as context_mock:
context_mock.return_value = "host"
tool = tool_class(conanfile)
with pytest.raises(ConanException) as exc_info:
tool.update()
assert exc_info.value.args[0] == "Can't update because tools.system.package_manager:mode is " \
"'check'.Please update packages manually or set " \
"'tools.system.package_manager:mode' to 'install' in the [conf] " \
"section of the profile, or in the command line using " \
"'-c tools.system.package_manager:mode=install'"


@pytest.mark.parametrize("tool_class, result", [
@pytest.mark.parametrize("tool_class, result",
[
(Apt, "apt-get update"),
(Yum, "yum check-update -y"),
(Dnf, "dnf check-update -y"),
Expand All @@ -166,12 +147,17 @@ def test_tools_update_mode_install(tool_class, result):
conanfile.conf = Conf()
conanfile.settings = Settings()
conanfile.conf["tools.system.package_manager:tool"] = tool_class.tool_name
conanfile.conf["tools.system.package_manager:mode"] = "install"
with mock.patch('conans.ConanFile.context', new_callable=PropertyMock) as context_mock:
context_mock.return_value = "host"
tool = tool_class(conanfile)
tool.update()
assert tool._conanfile.command == result
for mode in ["check", "install"]:
conanfile.conf["tools.system.package_manager:mode"] = mode
with mock.patch('conans.ConanFile.context', new_callable=PropertyMock) as context_mock:
context_mock.return_value = "host"
tool = tool_class(conanfile)
tool.update()
if mode == "install":
assert tool._conanfile.command == result
else:
# does not run the update when mode check
assert tool._conanfile.command == None


@pytest.mark.parametrize("tool_class, result", [
Expand Down

0 comments on commit b2ddd7a

Please sign in to comment.