Skip to content

Commit

Permalink
tests: remove usage of FakeApt fixtures in lifecycle (canonical#3024)
Browse files Browse the repository at this point in the history
Use a couple targetted mock.patches to provide equivalent
functionality.  Some related cleanup included.

Signed-off-by: Chris Patterson <chris.patterson@canonical.com>
  • Loading branch information
Chris Patterson committed Apr 9, 2020
1 parent bb268fa commit ab892ee
Showing 1 changed file with 51 additions and 52 deletions.
103 changes: 51 additions & 52 deletions tests/unit/lifecycle/test_lifecycle.py
Original file line number Diff line number Diff line change
Expand Up @@ -609,21 +609,11 @@ def fake_uname(cmd, *args, **kwargs):

self.useFixture(
fixtures.MockPatch(
"snapcraft.internal.repo._deb.Ubuntu._extract_deb_name_version",
return_value="test-1.0",
"snapcraft.internal.repo.Repo.get_installed_packages",
return_value=["patchelf=0.9"],
)
)

self.useFixture(
fixtures.MockPatch("snapcraft.internal.repo._deb.Ubuntu._extract_deb")
)

self.fake_apt_cache = fixture_setup.FakeAptCache()
self.useFixture(self.fake_apt_cache)
self.fake_apt_cache.add_package(
fixture_setup.FakeAptCachePackage("patchelf", "0.9", installed=True)
)

self.fake_snapd.snaps_result = [
dict(name="core18", channel="stable", revision="10")
]
Expand Down Expand Up @@ -744,15 +734,16 @@ def test_prime_with_installed_snaps(self):
FileContains(expected),
)

def test_prime_with_installed_packages(self):
@mock.patch(
"snapcraft.internal.repo.Repo.get_installed_packages",
return_value=[
"patchelf=0.9",
"test-package1=test-version1",
"test-package2=test-version2",
],
)
def test_prime_with_installed_packages(self, mock_installed_packages):
self.useFixture(fixtures.EnvironmentVariable("SNAPCRAFT_BUILD_INFO", "1"))
for name, version in [
("test-package1", "test-version1"),
("test-package2", "test-version2"),
]:
self.fake_apt_cache.add_package(
fixture_setup.FakeAptCachePackage(name, version, installed=True)
)

project_config = self.make_snapcraft_project(
textwrap.dedent(
Expand Down Expand Up @@ -806,15 +797,12 @@ def test_prime_with_installed_packages(self):
FileContains(expected),
)

def test_prime_with_stage_packages(self):
@mock.patch(
"snapcraft.repo.Repo.get",
return_value=["test-package1=test-version1", "test-package2=test-version2"],
)
def test_prime_with_stage_packages(self, mock_stage_packages):
self.useFixture(fixtures.EnvironmentVariable("SNAPCRAFT_BUILD_INFO", "1"))
for name, version in [
("test-package1", "test-version1"),
("test-package2", "test-version2"),
]:
self.fake_apt_cache.add_package(
fixture_setup.FakeAptCachePackage(name, version)
)

project_config = self.make_snapcraft_project(
textwrap.dedent(
Expand Down Expand Up @@ -865,21 +853,21 @@ def test_prime_with_stage_packages(self):
project_config.project.deb_arch
)
)

self.assertThat(
os.path.join(steps.PRIME.name, "snap", "manifest.yaml"),
FileContains(expected),
)

@mock.patch("subprocess.check_call")
def test_prime_with_global_build_packages(self, _):
@mock.patch(
"snapcraft.repo.Repo.install_build_packages",
return_value=["test-package1=test-version1", "test-package2=test-version2"],
)
def test_prime_with_global_build_packages(
self, mock_build_packages, mock_check_call
):
self.useFixture(fixtures.EnvironmentVariable("SNAPCRAFT_BUILD_INFO", "1"))
for name, version in [
("test-package1", "test-version1"),
("test-package2", "test-version2"),
]:
self.fake_apt_cache.add_package(
fixture_setup.FakeAptCachePackage(name, version)
)

project_config = self.make_snapcraft_project(
textwrap.dedent(
Expand Down Expand Up @@ -934,13 +922,17 @@ def test_prime_with_global_build_packages(self, _):
os.path.join(steps.PRIME.name, "snap", "manifest.yaml"),
FileContains(expected),
)
self.assertThat(
mock_build_packages.mock_calls,
Equals([mock.call({"test-package1=test-version1", "test-package2"})]),
)

@mock.patch("subprocess.check_call")
def test_prime_with_source_details(self, _):
@mock.patch(
"snapcraft.repo.Repo.install_build_packages", return_value=["git=testversion"]
)
def test_prime_with_source_details(self, mock_build_packages, mock_check_call):
self.useFixture(fixtures.EnvironmentVariable("SNAPCRAFT_BUILD_INFO", "1"))
self.fake_apt_cache.add_package(
fixture_setup.FakeAptCachePackage("git", "testversion")
)

project_config = self.make_snapcraft_project(
textwrap.dedent(
Expand Down Expand Up @@ -1004,11 +996,14 @@ def test_prime_with_source_details(self, _):
)

@mock.patch("subprocess.check_call")
def test_prime_with_build_package_with_any_architecture(self, _):
@mock.patch(
"snapcraft.repo.Repo.install_build_packages",
return_value=["test-package=test-version"],
)
def test_prime_with_build_package_with_any_architecture(
self, mock_build_packages, mock_check_call
):
self.useFixture(fixtures.EnvironmentVariable("SNAPCRAFT_BUILD_INFO", "1"))
self.fake_apt_cache.add_package(
fixture_setup.FakeAptCachePackage("test-package", "test-version")
)

project_config = self.make_snapcraft_project(
textwrap.dedent(
Expand Down Expand Up @@ -1065,15 +1060,14 @@ def test_prime_with_build_package_with_any_architecture(self, _):
)

@mock.patch("subprocess.check_call")
def test_prime_with_virtual_build_package(self, _):
@mock.patch(
"snapcraft.repo.Repo.install_build_packages",
return_value=["test-provider-package=test-version"],
)
def test_prime_with_virtual_build_package(
self, mock_build_packages, mock_check_call
):
self.useFixture(fixtures.EnvironmentVariable("SNAPCRAFT_BUILD_INFO", "1"))
self.fake_apt_cache.add_package(
fixture_setup.FakeAptCachePackage(
"test-provider-package",
"test-version",
provides=["test-virtual-package"],
)
)

project_config = self.make_snapcraft_project(
textwrap.dedent(
Expand Down Expand Up @@ -1129,6 +1123,11 @@ def test_prime_with_virtual_build_package(self, _):
FileContains(expected),
)

self.assertThat(
mock_build_packages.mock_calls,
Equals([mock.call({"test-virtual-package"})]),
)

@mock.patch("snapcraft.plugins.nil.NilPlugin.get_manifest")
def test_prime_with_plugin_manifest(self, fake_plugin_manifest):
fake_plugin_manifest.return_value = {"test-plugin-manifest": "test-value"}
Expand Down

0 comments on commit ab892ee

Please sign in to comment.