python/mach/mach/test/test_entry_point.py
author Andrew Halberstadt <ahalberstadt@mozilla.com>
Tue, 27 Sep 2016 17:11:00 -0400
changeset 420147 9e5f2ed402c67185b97b6870ea655fc761cc7696
parent 162966 656e14ae93635886e0ac22cd889d73a175ca3d12
permissions -rw-r--r--
Bug 1304593 - Fix broken test_entry_point.py in mach, r?gps Fix and enable python/mach/mach/test/test_entry_poiny.py. MozReview-Commit-ID: FtMpt8Nmq3g

# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
from __future__ import unicode_literals

import imp
import os
import sys

from mach.base import MachError
from mach.test.common import TestBase
from mock import patch

from mozunit import main


here = os.path.abspath(os.path.dirname(__file__))


class Entry():
    """Stub replacement for pkg_resources.EntryPoint"""
    def __init__(self, providers):
        self.providers = providers

    def load(self):
        def _providers():
            return self.providers
        return _providers


class TestEntryPoints(TestBase):
    """Test integrating with setuptools entry points"""
    provider_dir = os.path.join(here, 'providers')

    def _run_mach(self):
        return TestBase._run_mach(self, ['help'], entry_point='mach.providers')

    @patch('pkg_resources.iter_entry_points')
    def test_load_entry_point_from_directory(self, mock):
        # Ensure parent module is present otherwise we'll (likely) get
        # an error due to unknown parent.
        if b'mach.commands' not in sys.modules:
            mod = imp.new_module(b'mach.commands')
            sys.modules[b'mach.commands'] = mod

        mock.return_value = [Entry([self.provider_dir])]
        # Mach error raised due to conditions_invalid.py
        with self.assertRaises(MachError):
            self._run_mach()

    @patch('pkg_resources.iter_entry_points')
    def test_load_entry_point_from_file(self, mock):
        mock.return_value = [Entry([os.path.join(self.provider_dir, 'basic.py')])]

        result, stdout, stderr = self._run_mach()
        self.assertIsNone(result)
        self.assertIn('cmd_foo', stdout)


if __name__ == '__main__':
    main()