Skip to content

Commit

Permalink
Update system package manager tests to mock with_apt().
Browse files Browse the repository at this point in the history
  • Loading branch information
seanballais committed Jul 4, 2020
1 parent 2426839 commit cba4503
Showing 1 changed file with 28 additions and 3 deletions.
31 changes: 28 additions & 3 deletions conans/test/unittests/client/tools/system_pm_test.py
Expand Up @@ -56,20 +56,25 @@ def verify_update_test(self):
self.assertIn('Not updating system_requirements. CONAN_SYSREQUIRES_MODE=verify',
self.out)

def add_repositories_exception_cases_test(self):
# We gotta mock the with_apt property, since it checks for the existence of apt.
@mock.patch('conans.client.tools.oss.OSInfo.with_apt', new_callable=mock.PropertyMock)
def add_repositories_exception_cases_test(self, patched_with_apt):
os_info = OSInfo()
os_info.is_macos = False
os_info.is_linux = True
os_info.is_windows = False
os_info.linux_distro = "fedora" # Will instantiate DnfTool

patched_with_apt.return_value = False

with six.assertRaisesRegex(self, ConanException, "add_repository not implemented"):
new_out = StringIO()
spt = SystemPackageTool(os_info=os_info, output=ConanOutput(new_out))
spt.add_repository(repository="deb http://repo/url/ saucy universe multiverse",
repo_key=None)

def add_repository_test(self):
@mock.patch('conans.client.tools.oss.OSInfo.with_apt', new_callable=mock.PropertyMock)
def add_repository_test(self, patched_with_apt):
class RunnerOrderedMock:
commands = [] # Command + return value

Expand Down Expand Up @@ -99,6 +104,8 @@ def _run_add_repository_test(repository, gpg_key, sudo, isatty, update):
os_info.is_linux = True
os_info.is_windows = False
os_info.linux_distro = "debian"
patched_with_apt.return_value = True

new_out = StringIO()
spt = SystemPackageTool(runner=runner, os_info=os_info, output=ConanOutput(new_out))

Expand All @@ -118,7 +125,9 @@ def _run_add_repository_test(repository, gpg_key, sudo, isatty, update):
with mock.patch("sys.stdout.isatty", return_value=True):
_run_add_repository_test(repository, gpg_key, sudo=True, isatty=True, update=True)

def system_package_tool_test(self):
# We gotta mock the with_apt property, since it checks for the existence of apt.
@mock.patch('conans.client.tools.oss.OSInfo.with_apt', new_callable=mock.PropertyMock)
def system_package_tool_test(self, patched_with_apt):

with tools.environment_append({"CONAN_SYSREQUIRES_SUDO": "True"}):
runner = RunnerMock()
Expand All @@ -127,6 +136,8 @@ def system_package_tool_test(self):
os_info.is_macos = False
os_info.is_linux = True
os_info.is_windows = False
patched_with_apt.return_value = True

os_info.linux_distro = "debian"

spt = SystemPackageTool(runner=runner, os_info=os_info, output=self.out)
Expand All @@ -148,6 +159,9 @@ def system_package_tool_test(self):
spt.update()
self.assertEqual(runner.command_called, "sudo -A apt-get update")

# We'll be testing non-Ubuntu and non-Debian-based distros.
patched_with_apt.return_value = False

with mock.patch("conans.client.tools.oss.which", return_value=True):
os_info.linux_distro = "fedora"
spt = SystemPackageTool(runner=runner, os_info=os_info, output=self.out)
Expand Down Expand Up @@ -183,6 +197,7 @@ def system_package_tool_test(self):
self.assertEqual(runner.command_called, "sudo -A yum install -y a_package.i?86")

os_info.linux_distro = "debian"
patched_with_apt.return_value = True
spt = SystemPackageTool(runner=runner, os_info=os_info, output=self.out)
with self.assertRaises(ConanException):
runner.return_ok = False
Expand All @@ -198,6 +213,7 @@ def system_package_tool_test(self):
os_info.is_macos = True
os_info.is_linux = False
os_info.is_windows = False
patched_with_apt.return_value = False

spt = SystemPackageTool(runner=runner, os_info=os_info, output=self.out)
spt.update()
Expand All @@ -207,6 +223,7 @@ def system_package_tool_test(self):

os_info.is_freebsd = True
os_info.is_macos = False
patched_with_apt.return_value = False

spt = SystemPackageTool(runner=runner, os_info=os_info, output=self.out)
spt.update()
Expand All @@ -220,6 +237,7 @@ def system_package_tool_test(self):
if platform.system() == "Windows" and which("choco.exe"):
os_info.is_freebsd = False
os_info.is_windows = True
patched_with_apt.return_value = False
spt = SystemPackageTool(runner=runner, os_info=os_info, output=self.out,
tool=ChocolateyTool(output=self.out))
spt.update()
Expand All @@ -236,13 +254,16 @@ def system_package_tool_test(self):
os_info = OSInfo()
os_info.is_linux = True
os_info.linux_distro = "redhat"
patched_with_apt.return_value = False

spt = SystemPackageTool(runner=runner, os_info=os_info, output=self.out)
spt.install("a_package", force=True)
self.assertEqual(runner.command_called, "yum install -y a_package")
spt.update()
self.assertEqual(runner.command_called, "yum check-update -y")

os_info.linux_distro = "ubuntu"
patched_with_apt.return_value = True
spt = SystemPackageTool(runner=runner, os_info=os_info, output=self.out)
spt.install("a_package", force=True)
self.assertEqual(runner.command_called,
Expand Down Expand Up @@ -280,6 +301,7 @@ def system_package_tool_test(self):
os_info.is_macos = True
os_info.is_linux = False
os_info.is_windows = False
patched_with_apt.return_value = False
spt = SystemPackageTool(runner=runner, os_info=os_info, output=self.out)

spt.update()
Expand All @@ -290,6 +312,7 @@ def system_package_tool_test(self):
os_info.is_freebsd = True
os_info.is_macos = False
os_info.is_windows = False
patched_with_apt.return_value = False

spt = SystemPackageTool(runner=runner, os_info=os_info, output=self.out)
spt.update()
Expand All @@ -302,6 +325,7 @@ def system_package_tool_test(self):
os_info.is_solaris = True
os_info.is_freebsd = False
os_info.is_windows = False
patched_with_apt.return_value = False

spt = SystemPackageTool(runner=runner, os_info=os_info, output=self.out)
spt.update()
Expand All @@ -315,6 +339,7 @@ def system_package_tool_test(self):
if platform.system() == "Windows" and which("choco.exe"):
os_info.is_solaris = False
os_info.is_windows = True
patched_with_apt.return_value = False

spt = SystemPackageTool(runner=runner, os_info=os_info, output=self.out,
tool=ChocolateyTool(output=self.out))
Expand Down

0 comments on commit cba4503

Please sign in to comment.