Example #1
0
    def setUp(self):
        super().setUp()
        temp_cwd_fixture = fixture_setup.TempCWD()
        self.useFixture(temp_cwd_fixture)
        self.path = temp_cwd_fixture.path

        # Use a separate path for XDG dirs, or changes there may be detected as
        # source changes.
        self.xdg_path = self.useFixture(fixtures.TempDir()).path
        self.useFixture(fixture_setup.TempXDG(self.xdg_path))
        self.fake_terminal = fixture_setup.FakeTerminal()
        self.useFixture(self.fake_terminal)
        self.useFixture(fixture_setup.SilentSnapProgress())
        # Some tests will directly or indirectly change the plugindir, which
        # is a module variable. Make sure that it is returned to the original
        # value when a test ends.
        self.addCleanup(common.set_plugindir, common.get_plugindir())
        self.addCleanup(common.set_schemadir, common.get_schemadir())
        self.addCleanup(common.set_librariesdir, common.get_librariesdir())
        self.addCleanup(common.reset_env)
        common.set_schemadir(os.path.join(get_snapcraft_path(), "schema"))
        self.fake_logger = fixtures.FakeLogger(level=logging.ERROR)
        self.useFixture(self.fake_logger)

        patcher = mock.patch("multiprocessing.cpu_count")
        self.cpu_count = patcher.start()
        self.cpu_count.return_value = 2
        self.addCleanup(patcher.stop)

        patcher = mock.patch(
            "snapcraft.internal.indicators.ProgressBar", new=SilentProgressBar
        )
        patcher.start()
        self.addCleanup(patcher.stop)

        # These are what we expect by default
        self.snap_dir = os.path.join(os.getcwd(), "snap")
        self.prime_dir = os.path.join(os.getcwd(), "prime")
        self.stage_dir = os.path.join(os.getcwd(), "stage")
        self.parts_dir = os.path.join(os.getcwd(), "parts")
        self.local_plugins_dir = os.path.join(self.snap_dir, "plugins")

        # Avoid installing patchelf in the tests
        self.useFixture(fixtures.EnvironmentVariable("SNAPCRAFT_NO_PATCHELF", "1"))

        # Disable Sentry reporting for tests, otherwise they'll hang waiting
        # for input
        self.useFixture(
            fixtures.EnvironmentVariable("SNAPCRAFT_ENABLE_SENTRY", "false")
        )

        machine = os.environ.get("SNAPCRAFT_TEST_MOCK_MACHINE", None)
        self.base_environment = fixture_setup.FakeBaseEnvironment(machine=machine)
        self.useFixture(self.base_environment)

        # Make sure "SNAPCRAFT_ENABLE_DEVELOPER_DEBUG" is reset between tests
        self.useFixture(
            fixtures.EnvironmentVariable("SNAPCRAFT_ENABLE_DEVELOPER_DEBUG")
        )
        self.useFixture(fixture_setup.FakeSnapcraftctl())
Example #2
0
 def setUp(self):
     temp_cwd_fixture = fixture_setup.TempCWD()
     self.useFixture(temp_cwd_fixture)
     super().setUp()
     self.useFixture(
         fixtures.EnvironmentVariable("XDG_CACHE_HOME",
                                      os.path.join(self.path, ".cache")))
Example #3
0
    def test_with_TEMPDIR_env_var(self):
        with tempfile.TemporaryDirectory() as test_tmp_dir:
            with fixtures.EnvironmentVariable('TMPDIR', test_tmp_dir):
                temp_cwd_fixture = fixture_setup.TempCWD()
                self.useFixture(temp_cwd_fixture)

        self.assertThat(os.path.dirname(temp_cwd_fixture.path),
                        Equals(test_tmp_dir))
Example #4
0
    def setUp(self):
        super().setUp()
        temp_cwd_fixture = fixture_setup.TempCWD()
        self.useFixture(temp_cwd_fixture)
        self.path = temp_cwd_fixture.path
        self.useFixture(fixture_setup.TempXDG(self.path))
        self.fake_terminal = fixture_setup.FakeTerminal()
        self.useFixture(self.fake_terminal)
        self.useFixture(fixture_setup.SilentSnapProgress())
        # Some tests will directly or indirectly change the plugindir, which
        # is a module variable. Make sure that it is returned to the original
        # value when a test ends.
        self.addCleanup(common.set_plugindir, common.get_plugindir())
        self.addCleanup(common.set_schemadir, common.get_schemadir())
        self.addCleanup(common.set_librariesdir, common.get_librariesdir())
        self.addCleanup(common.reset_env)
        common.set_schemadir(os.path.join(get_snapcraft_path(), 'schema'))
        self.fake_logger = fixtures.FakeLogger(level=logging.ERROR)
        self.useFixture(self.fake_logger)

        patcher = mock.patch('multiprocessing.cpu_count')
        self.cpu_count = patcher.start()
        self.cpu_count.return_value = 2
        self.addCleanup(patcher.stop)

        patcher = mock.patch('snapcraft.internal.indicators.ProgressBar',
                             new=SilentProgressBar)
        patcher.start()
        self.addCleanup(patcher.stop)

        # These are what we expect by default
        self.snap_dir = os.path.join(os.getcwd(), 'snap')
        self.prime_dir = os.path.join(os.getcwd(), 'prime')
        self.stage_dir = os.path.join(os.getcwd(), 'stage')
        self.parts_dir = os.path.join(os.getcwd(), 'parts')
        self.local_plugins_dir = os.path.join(self.snap_dir, 'plugins')

        # Avoid installing patchelf in the tests
        self.useFixture(
            fixtures.EnvironmentVariable('SNAPCRAFT_NO_PATCHELF', '1'))

        # Disable Sentry reporting for tests, otherwise they'll hang waiting
        # for input
        self.useFixture(
            fixtures.EnvironmentVariable('SNAPCRAFT_ENABLE_SENTRY', 'false'))

        machine = os.environ.get('SNAPCRAFT_TEST_MOCK_MACHINE', None)
        self.base_environment = fixture_setup.FakeBaseEnvironment(
            machine=machine)
        self.useFixture(self.base_environment)

        # Make sure SNAPCRAFT_DEBUG is reset between tests
        self.useFixture(fixtures.EnvironmentVariable('SNAPCRAFT_DEBUG'))
        self.useFixture(fixture_setup.FakeSnapcraftctl())
Example #5
0
    def setUp(self):
        temp_cwd_fixture = fixture_setup.TempCWD()
        self.useFixture(temp_cwd_fixture)
        super().setUp()

        # Since we're running in a temporary directory use
        # the original source tree version of snapcraft-parser
        if not os.getenv('SNAPCRAFT_FROM_DEB', False):
            self.snapcraft_parser_command = os.path.join(
                os.path.dirname(__file__), '..', '..', '..', '..',
                'bin', 'snapcraft-parser')
Example #6
0
    def setUp(self):
        super().setUp()
        temp_cwd_fixture = fixture_setup.TempCWD()
        self.useFixture(temp_cwd_fixture)
        self.path = temp_cwd_fixture.path

        # Use a separate path for XDG dirs, or changes there may be detected as
        # source changes.
        self.xdg_path = self.useFixture(fixtures.TempDir()).path
        self.useFixture(fixture_setup.TempXDG(self.xdg_path))
        self.fake_terminal = fixture_setup.FakeTerminal()
        self.useFixture(self.fake_terminal)
        # Some tests will directly or indirectly change the plugindir, which
        # is a module variable. Make sure that it is returned to the original
        # value when a test ends.
        self.addCleanup(common.set_plugindir, common.get_plugindir())
        self.addCleanup(common.set_schemadir, common.get_schemadir())
        self.addCleanup(common.set_extensionsdir, common.get_extensionsdir())
        self.addCleanup(common.set_keyringsdir, common.get_keyringsdir())
        self.addCleanup(common.reset_env)
        common.set_schemadir(os.path.join(get_snapcraft_path(), "schema"))
        self.fake_logger = fixtures.FakeLogger(level=logging.ERROR)
        self.useFixture(self.fake_logger)

        # Some tests will change the apt Dir::Etc::Trusted and
        # Dir::Etc::TrustedParts directories. Make sure they're properly reset.
        self.addCleanup(
            apt.apt_pkg.config.set,
            "Dir::Etc::Trusted",
            apt.apt_pkg.config.find_file("Dir::Etc::Trusted"),
        )
        self.addCleanup(
            apt.apt_pkg.config.set,
            "Dir::Etc::TrustedParts",
            apt.apt_pkg.config.find_file("Dir::Etc::TrustedParts"),
        )

        patcher = mock.patch("os.sched_getaffinity")
        self.cpu_count = patcher.start()
        self.cpu_count.return_value = {1, 2}
        self.addCleanup(patcher.stop)

        # We do not want the paths to affect every test we have.
        patcher = mock.patch(
            "snapcraft.file_utils.get_snap_tool_path", side_effect=lambda x: x
        )
        patcher.start()
        self.addCleanup(patcher.stop)

        patcher = mock.patch(
            "snapcraft.internal.indicators.ProgressBar", new=SilentProgressBar
        )
        patcher.start()
        self.addCleanup(patcher.stop)

        # These are what we expect by default
        self.snap_dir = os.path.join(os.getcwd(), "snap")
        self.prime_dir = os.path.join(os.getcwd(), "prime")
        self.stage_dir = os.path.join(os.getcwd(), "stage")
        self.parts_dir = os.path.join(os.getcwd(), "parts")
        self.local_plugins_dir = os.path.join(self.snap_dir, "plugins")

        # Use this host to run through the lifecycle tests
        self.useFixture(
            fixtures.EnvironmentVariable("SNAPCRAFT_BUILD_ENVIRONMENT", "host")
        )

        # Make sure snap installation does the right thing
        self.fake_snapd.installed_snaps = [
            dict(name="core16", channel="stable", revision="10"),
            dict(name="core18", channel="stable", revision="10"),
        ]
        self.fake_snapd.snaps_result = [
            dict(name="core16", channel="stable", revision="10"),
            dict(name="core18", channel="stable", revision="10"),
        ]
        self.fake_snapd.find_result = [
            dict(
                core16=dict(
                    channel="stable",
                    channels={"latest/stable": dict(confinement="strict")},
                )
            ),
            dict(
                core18=dict(
                    channel="stable",
                    channels={"latest/stable": dict(confinement="strict")},
                )
            ),
        ]
        self.fake_snapd.snap_details_func = None

        self.fake_snap_command = fixture_setup.FakeSnapCommand()
        self.useFixture(self.fake_snap_command)

        # Avoid installing patchelf in the tests
        self.useFixture(fixtures.EnvironmentVariable("SNAPCRAFT_NO_PATCHELF", "1"))

        # Disable Sentry reporting for tests, otherwise they'll hang waiting
        # for input
        self.useFixture(
            fixtures.EnvironmentVariable("SNAPCRAFT_ENABLE_ERROR_REPORTING", "false")
        )

        # Don't let the managed host variable leak into tests
        self.useFixture(fixtures.EnvironmentVariable("SNAPCRAFT_MANAGED_HOST"))

        machine = os.environ.get("SNAPCRAFT_TEST_MOCK_MACHINE", None)
        self.base_environment = fixture_setup.FakeBaseEnvironment(machine=machine)
        self.useFixture(self.base_environment)

        # Make sure "SNAPCRAFT_ENABLE_DEVELOPER_DEBUG" is reset between tests
        self.useFixture(
            fixtures.EnvironmentVariable("SNAPCRAFT_ENABLE_DEVELOPER_DEBUG")
        )
        self.useFixture(fixture_setup.FakeSnapcraftctl())

        # Don't let host SNAPCRAFT_BUILD_INFO variable leak into tests
        self.useFixture(fixtures.EnvironmentVariable("SNAPCRAFT_BUILD_INFO"))
Example #7
0
    def setUp(self):
        super().setUp()

        self.patchelf_command = "patchelf"
        self.execstack_command = "execstack"

        package_type = os.getenv("SNAPCRAFT_PACKAGE_TYPE")
        if package_type == "snap":
            self.snapcraft_command = "/snap/bin/snapcraft"
            self.patchelf_command = "/snap/snapcraft/current/usr/bin/patchelf"
            self.execstack_command = "/snap/snapcraft/current/usr/sbin/execstack"
        elif package_type == "deb":
            self.snapcraft_command = "/usr/bin/snapcraft"
            self.snapcraft_parser_command = "/usr/bin/snapcraft-parser"
        elif os.getenv("VIRTUAL_ENV") and sys.platform == "win32":
            self.snapcraft_command = ["python", "-m", "snapcraft.cli.__main__"]
            self.snapcraft_parser_command = os.path.join(
                os.getenv("VIRTUAL_ENV"), "bin", "snapcraft-parser")
        elif os.getenv("VIRTUAL_ENV"):
            self.snapcraft_command = os.path.join(os.getenv("VIRTUAL_ENV"),
                                                  "bin", "snapcraft")
            self.snapcraft_parser_command = os.path.join(
                os.getenv("VIRTUAL_ENV"), "bin", "snapcraft-parser")
        elif package_type == "brew":
            self.snapcraft_command = "/usr/local/bin/snapcraft"
        else:
            raise EnvironmentError(
                "snapcraft is not setup correctly for testing. Either set "
                "SNAPCRAFT_PACKAGE_TYPE to 'snap', 'deb' or 'brew', to run from "
                "either the snap, deb or homebrew or make sure your venv is properly "
                "setup as described in HACKING.md.")

        self.snaps_dir = os.path.join(os.path.dirname(__file__), "snaps")
        temp_cwd_fixture = fixture_setup.TempCWD()
        self.useFixture(temp_cwd_fixture)
        self.path = temp_cwd_fixture.path

        # Use a separate path for XDG dirs, or changes there may be detected as
        # source changes.
        self.xdg_path = self.useFixture(fixtures.TempDir()).path
        self.useFixture(fixture_setup.TempXDG(self.xdg_path))

        # Use this host to run through the lifecycle tests
        self.useFixture(
            fixtures.EnvironmentVariable("SNAPCRAFT_BUILD_ENVIRONMENT",
                                         "host"))

        # Use a dumb terminal for tests
        self.useFixture(fixtures.EnvironmentVariable("TERM", "dumb"))

        # Disable Sentry reporting for tests, otherwise they'll hang waiting
        # for input
        self.useFixture(
            fixtures.EnvironmentVariable("SNAPCRAFT_ENABLE_ERROR_REPORTING",
                                         "false"))

        # Don't let the managed host variable leak into tests
        self.useFixture(fixtures.EnvironmentVariable("SNAPCRAFT_MANAGED_HOST"))

        # Note that these directories won't exist when the test starts,
        # they might be created after calling the snapcraft command on the
        # project dir.
        self.parts_dir = "parts"
        self.stage_dir = "stage"
        self.prime_dir = "prime"

        self.deb_arch = platform.get_deb_arch()
        self.arch_triplet = platform.get_arch_triplet()

        self.distro_series = os_release.get_version_codename()
Example #8
0
    def setUp(self):
        super().setUp()
        if os.getenv("SNAPCRAFT_FROM_SNAP", False):
            self.snapcraft_command = "/snap/bin/snapcraft"
        elif os.getenv("SNAPCRAFT_FROM_DEB", False):
            self.snapcraft_command = "/usr/bin/snapcraft"
            self.snapcraft_parser_command = "/usr/bin/snapcraft-parser"
        elif os.getenv("VIRTUAL_ENV") and sys.platform == "win32":
            self.snapcraft_command = ["python", "-m", "snapcraft.cli.__main__"]
            self.snapcraft_parser_command = os.path.join(
                os.getenv("VIRTUAL_ENV"), "bin", "snapcraft-parser")
        elif os.getenv("VIRTUAL_ENV"):
            self.snapcraft_command = os.path.join(os.getenv("VIRTUAL_ENV"),
                                                  "bin", "snapcraft")
            self.snapcraft_parser_command = os.path.join(
                os.getenv("VIRTUAL_ENV"), "bin", "snapcraft-parser")
        elif os.getenv("SNAPCRAFT_FROM_BREW", False):
            self.snapcraft_command = "/usr/local/bin/snapcraft"
        else:
            raise EnvironmentError(
                "snapcraft is not setup correctly for testing. Either set "
                "SNAPCRAFT_FROM_SNAP, SNAPCRAFT_FROM_DEB or "
                "SNAPCRAFT_FROM_BREW to run from either the snap, deb or "
                "brew, or make sure your venv is properly setup as described "
                "in HACKING.md.")

        if os.getenv("SNAPCRAFT_FROM_SNAP", False):
            self.patchelf_command = "/snap/snapcraft/current/usr/bin/patchelf"
            self.execstack_command = "/snap/snapcraft/current/usr/sbin/execstack"
        else:
            self.patchelf_command = "patchelf"
            self.execstack_command = "execstack"

        self.snaps_dir = os.path.join(os.path.dirname(__file__), "snaps")
        temp_cwd_fixture = fixture_setup.TempCWD()
        self.useFixture(temp_cwd_fixture)
        self.path = temp_cwd_fixture.path

        # Use a separate path for XDG dirs, or changes there may be detected as
        # source changes.
        self.xdg_path = self.useFixture(fixtures.TempDir()).path
        self.useFixture(fixture_setup.TempXDG(self.xdg_path))

        # Use a dumb terminal for tests
        self.useFixture(fixtures.EnvironmentVariable("TERM", "dumb"))

        # Disable Sentry reporting for tests, otherwise they'll hang waiting
        # for input
        self.useFixture(
            fixtures.EnvironmentVariable("SNAPCRAFT_ENABLE_SENTRY", "false"))

        # Note that these directories won't exist when the test starts,
        # they might be created after calling the snapcraft command on the
        # project dir.
        self.parts_dir = "parts"
        self.stage_dir = "stage"
        self.prime_dir = "prime"

        self.deb_arch = platform.get_deb_arch()
        self.arch_triplet = platform.get_arch_triplet()

        self.distro_series = os_release.get_version_codename()
Example #9
0
    def setUp(self):
        super().setUp()
        if os.getenv('SNAPCRAFT_FROM_SNAP', False):
            self.snapcraft_command = '/snap/bin/snapcraft'
        elif os.getenv('SNAPCRAFT_FROM_DEB', False):
            self.snapcraft_command = '/usr/bin/snapcraft'
            self.snapcraft_parser_command = '/usr/bin/snapcraft-parser'
        elif os.getenv('VIRTUAL_ENV') and sys.platform == 'win32':
            self.snapcraft_command = ['python', '-m', 'snapcraft.cli.__main__']
            self.snapcraft_parser_command = os.path.join(
                os.getenv('VIRTUAL_ENV'), 'bin', 'snapcraft-parser')
        elif os.getenv('VIRTUAL_ENV'):
            self.snapcraft_command = os.path.join(os.getenv('VIRTUAL_ENV'),
                                                  'bin', 'snapcraft')
            self.snapcraft_parser_command = os.path.join(
                os.getenv('VIRTUAL_ENV'), 'bin', 'snapcraft-parser')
        elif os.getenv('SNAPCRAFT_FROM_BREW', False):
            self.snapcraft_command = '/usr/local/bin/snapcraft'
        else:
            raise EnvironmentError(
                'snapcraft is not setup correctly for testing. Either set '
                'SNAPCRAFT_FROM_SNAP, SNAPCRAFT_FROM_DEB or '
                'SNAPCRAFT_FROM_BREW to run from either the snap, deb or '
                'brew, or make sure your venv is properly setup as described '
                'in HACKING.md.')

        if os.getenv('SNAPCRAFT_FROM_SNAP', False):
            self.patchelf_command = '/snap/snapcraft/current/usr/bin/patchelf'
            self.execstack_command = (
                '/snap/snapcraft/current/usr/sbin/execstack')
        else:
            self.patchelf_command = 'patchelf'
            self.execstack_command = 'execstack'

        self.snaps_dir = os.path.join(os.path.dirname(__file__), 'snaps')
        temp_cwd_fixture = fixture_setup.TempCWD()
        self.useFixture(temp_cwd_fixture)
        self.path = temp_cwd_fixture.path

        self.useFixture(
            fixtures.EnvironmentVariable('XDG_CONFIG_HOME',
                                         os.path.join(self.path, '.config')))
        self.useFixture(
            fixtures.EnvironmentVariable('XDG_DATA_HOME',
                                         os.path.join(self.path, 'data')))
        self.useFixture(fixtures.EnvironmentVariable('TERM', 'dumb'))

        # Disable Sentry reporting for tests, otherwise they'll hang waiting
        # for input
        self.useFixture(
            fixtures.EnvironmentVariable('SNAPCRAFT_ENABLE_SENTRY', 'false'))

        patcher = mock.patch('xdg.BaseDirectory.xdg_config_home',
                             new=os.path.join(self.path, '.config'))
        patcher.start()
        self.addCleanup(patcher.stop)
        patcher = mock.patch('xdg.BaseDirectory.xdg_data_home',
                             new=os.path.join(self.path, 'data'))
        patcher.start()
        self.addCleanup(patcher.stop)

        patcher_dirs = mock.patch('xdg.BaseDirectory.xdg_config_dirs',
                                  new=[xdg.BaseDirectory.xdg_config_home])
        patcher_dirs.start()
        self.addCleanup(patcher_dirs.stop)

        patcher_dirs = mock.patch('xdg.BaseDirectory.xdg_data_dirs',
                                  new=[xdg.BaseDirectory.xdg_data_home])
        patcher_dirs.start()
        self.addCleanup(patcher_dirs.stop)

        # Note that these directories won't exist when the test starts,
        # they might be created after calling the snapcraft command on the
        # project dir.
        self.parts_dir = 'parts'
        self.stage_dir = 'stage'
        self.prime_dir = 'prime'

        self.deb_arch = platform.get_deb_arch()
        self.arch_triplet = platform.get_arch_triplet()

        self.distro_series = os_release.get_version_codename()
Example #10
0
    def setUp(self):
        super().setUp()

        self.useFixture(fixtures.FakeLogger(level=logging.ERROR))

        temp_cwd = fixture_setup.TempCWD()
        self.useFixture(temp_cwd)

        snapcraft_yaml = fixture_setup.SnapcraftYaml(
            temp_cwd.path,
            base="core18",
            parts={"test-part": {
                "plugin": "nil"
            }})
        self.useFixture(snapcraft_yaml)

        project = Project(
            snapcraft_yaml_file_path=snapcraft_yaml.snapcraft_yaml_file_path)

        self.global_state_filepath = project._get_global_state_file_path()

        self.project_config = project_loader.load_config(project)

        self.useFixture(
            fixtures.MockPatchObject(self.project_config,
                                     "get_build_snaps",
                                     return_value={"core18"}))

        self.useFixture(
            fixtures.MockPatch(
                "snapcraft.internal.lifecycle._runner._Executor.run"))

        self.useFixture(
            fixtures.MockPatch("snapcraft.internal.repo.snaps.install_snaps"))

        # Avoid unnecessary calls to info.
        channel_map = []
        for arch in ("amd64", "i386", "s390x", "arm64", "armhf", "ppc64el"):
            channel_map.append({
                "channel": {
                    "architecture": arch,
                    "name": "stable",
                    "released-at": "2019-10-15T13:54:06.800280+00:00",
                    "risk": "stable",
                    "track": "latest",
                },
                "confinement": "strict",
                "download": {
                    "deltas": [],
                    "sha3-384":
                    "64d232d6bfa65be14d7f8d84e952d4e372e12021e2c3dbaf70cf2af5e78bf51c4baf9c9107dd6db815064636b781bda6",
                    "size":
                    57151488,
                    "url":
                    "https://api.snapcraft.io/api/v1/snaps/download/CSO04Jhav2yK0uz97cr0ipQRyqg0qQL6_1223.snap",
                },
                "revision": 1223,
            })
        info = {
            "channel-map": channel_map,
            "default-track": None,
            "name": "core18",
            "snap": {
                "name": "core18",
                "publisher": {
                    "display-name": "Canonical",
                    "id": "canonical",
                    "username": "******",
                    "validation": "verified",
                },
                "snap-id": "CSO04Jhav2yK0uz97cr0ipQRyqg0qQL6",
            },
            "snap-id": "CSO04Jhav2yK0uz97cr0ipQRyqg0qQL6",
        }
        self.fake_storeapi_get_info = fixtures.MockPatch(
            "snapcraft.storeapi._snap_api.SnapAPI.get_info",
            return_value=SnapInfo(info),
        )
        self.useFixture(self.fake_storeapi_get_info)
    def setUp(self):
        super().setUp()
        if os.getenv('SNAPCRAFT_FROM_SNAP', False):
            self.snapcraft_command = '/snap/bin/snapcraft'
        elif os.getenv('SNAPCRAFT_FROM_DEB', False):
            self.snapcraft_command = '/usr/bin/snapcraft'
            self.snapcraft_parser_command = '/usr/bin/snapcraft-parser'
        elif os.getenv('VIRTUAL_ENV'):
            self.snapcraft_command = os.path.join(
                os.getenv('VIRTUAL_ENV'), 'bin', 'snapcraft')
            self.snapcraft_parser_command = os.path.join(
                os.getenv('VIRTUAL_ENV'), 'bin', 'snapcraft-parser')
        else:
            raise EnvironmentError(
                'snapcraft is not setup correctly for testing. Either set '
                'SNAPCRAFT_FROM_SNAP or SNAPCRAFT_FROM_DEB to run from either '
                'the snap or deb, or make sure your venv is properly setup '
                'as described in HACKING.md.')

        if os.getenv('SNAPCRAFT_FROM_SNAP', False):
            self.patchelf_command = '/snap/snapcraft/current/bin/patchelf'
            self.execstack_command = (
                '/snap/snapcraft/current/usr/sbin/execstack')
        else:
            self.patchelf_command = 'patchelf'
            self.execstack_command = 'execstack'

        self.snaps_dir = os.path.join(os.path.dirname(__file__), 'snaps')
        temp_cwd_fixture = fixture_setup.TempCWD()
        self.useFixture(temp_cwd_fixture)
        self.path = temp_cwd_fixture.path

        self.useFixture(fixtures.EnvironmentVariable(
            'XDG_CONFIG_HOME', os.path.join(self.path, '.config')))
        self.useFixture(fixtures.EnvironmentVariable(
            'XDG_CACHE_HOME', os.path.join(self.path, '.cache')))
        self.useFixture(fixtures.EnvironmentVariable(
            'XDG_DATA_HOME', os.path.join(self.path, 'data')))
        self.useFixture(fixtures.EnvironmentVariable('TERM', 'dumb'))

        patcher = mock.patch(
            'xdg.BaseDirectory.xdg_config_home',
            new=os.path.join(self.path, '.config'))
        patcher.start()
        self.addCleanup(patcher.stop)
        patcher = mock.patch(
            'xdg.BaseDirectory.xdg_data_home',
            new=os.path.join(self.path, 'data'))
        patcher.start()
        self.addCleanup(patcher.stop)
        patcher = mock.patch(
            'xdg.BaseDirectory.xdg_cache_home',
            new=os.path.join(self.path, '.cache'))
        patcher.start()
        self.addCleanup(patcher.stop)

        patcher_dirs = mock.patch(
            'xdg.BaseDirectory.xdg_config_dirs',
            new=[xdg.BaseDirectory.xdg_config_home])
        patcher_dirs.start()
        self.addCleanup(patcher_dirs.stop)

        patcher_dirs = mock.patch(
            'xdg.BaseDirectory.xdg_data_dirs',
            new=[xdg.BaseDirectory.xdg_data_home])
        patcher_dirs.start()
        self.addCleanup(patcher_dirs.stop)

        # Note that these directories won't exist when the test starts,
        # they might be created after calling the snapcraft command on the
        # project dir.
        self.parts_dir = 'parts'
        self.stage_dir = 'stage'
        self.prime_dir = 'prime'

        self.deb_arch = platform.get_deb_arch()
        self.arch_triplet = platform.get_arch_triplet()

        release = OsRelease()
        self.distro_series = release.version_codename()
Example #12
0
 def setUp(self):
     super().setUp()
     temp_cwd_fixture = fixture_setup.TempCWD()
     self.useFixture(temp_cwd_fixture)
     self.path = temp_cwd_fixture.path