# Path: tests/paths.py
# TEST_ENTRY_POINT_PATH = os.path.join(TEST_RESOURCES_PATH, "test_entry_point")
# 
# Path: hoplite/api/root.py
# def reload_site_packages():
#     # TODO: Can probably be replaced with reload(pkg_resources)
#     opsys = platform.system().lower()
#     python_exe_path = sys.executable
#     bin_path = os.path.split(python_exe_path)[0]
#     venv_path = os.path.split(bin_path)[0]
#     site_path = ""
#     if opsys == 'windows':
#         # If running in virtual environment, we must account for different
#         # folder structure
#         if hasattr(sys, 'real_prefix'):
#             site_path = os.path.join(venv_path, "..", "Lib", "site-packages")
#         else:
#             site_path = os.path.join(venv_path, "Lib", "site-packages")
#     elif opsys == 'linux' or opsys == 'darwin':
#         # If running in virtual environment, we must account for different
#         # folder structure
#         if hasattr(sys, 'real_prefix'):
#             site_path = os.path.join(
#                 venv_path, "..", "lib", "python{0}.{1}".format(
#                     sys.version_info[0], sys.version_info[1]), "site-packages")
#         else:
#             site_path = os.path.join(
#                 venv_path, "lib", "python{0}.{1}".format(
#                     sys.version_info[0], sys.version_info[1]), "site-packages")
#     site.addsitedir(site_path)
#     for path in sys.path:
#         pkg_resources.working_set.add_entry(path)
# 
# Path: hoplite/plugin_manager.py
# class EntryPointManager(object):
#     """
#     Interface to the hoplite.jobs set of entry points
#     """
#     def __init__(self, group_name='hoplite.jobs'):
#         self.entry_point_group_name = group_name
# 
#     def get_plugin_names(self):
#         names = []
#         for entry_point in self._entry_points():
#             names.append(entry_point.name)
#         return names
# 
#     def get_plugin_module_by_name(self, name):
#         # TODO: Should we validate the module implements the correct methods
#         # before returning it to the caller?
#         for entry_point in self._entry_points():
#             if entry_point.name == name:
#                 return entry_point.load()
# 
#     def _entry_points(self):
#         # TODO: If it takes too long to reload pkg_resources, it might be worth
#         # looking into only loading if a package
#         # TODO: isn't found and then try again.
#         reload(pkg_resources)  # makes sure entry points are up-to-date
#         for entry_point in pkg_resources.iter_entry_points(group=self.entry_point_group_name):
#             logger.debug("Found entry point: {0}".format(entry_point.name))
#             yield entry_point


import unittest2
import pip
import types
import os
import sys
import pkg_resources
from tests.paths import TEST_ENTRY_POINT_PATH
from hoplite.api.root import reload_site_packages
from hoplite.plugin_manager import EntryPointManager

class TestEntryPointManager(unittest2.TestCase):
    @classmethod
    def setUpClass(cls):
        # Install hoplite so that its entry points are added
        pip_args = ["install", os.path.join(os.path.dirname(os.path.abspath(__file__)), '..')]
        run_pip(pip_args)
        reload_site_packages()

    @classmethod
    def tearDownClass(cls):
        pip_args = ["uninstall", "hoplite", "-y"]
        run_pip(pip_args)

    def setUp(self):
        self.manager = EntryPointManager()

    def test_get_names_with_default_group_name(self):
        names = self.manager.get_plugin_names()
        self.assertIn('hoplite.plugins.download_folder_from_ftp_job', names)
        self.assertIn('hoplite.plugins.download_network_folder_job', names)
        self.assertIn('hoplite.plugins.upload_to_network_folder_job', names)

    def test_get_names_of_hot_installed_package(self):
        pip_args = ["install", TEST_ENTRY_POINT_PATH]
        run_pip(pip_args)
        reload_site_packages()
        for syspath in sys.path:
            pkg_resources.working_set.add_entry(syspath)
        try:

# Path: tests/paths.py
# TEST_ENTRY_POINT_PATH = os.path.join(TEST_RESOURCES_PATH, "test_entry_point")
# 
# Path: hoplite/api/root.py
# def reload_site_packages():
#     # TODO: Can probably be replaced with reload(pkg_resources)
#     opsys = platform.system().lower()
#     python_exe_path = sys.executable
#     bin_path = os.path.split(python_exe_path)[0]
#     venv_path = os.path.split(bin_path)[0]
#     site_path = ""
#     if opsys == 'windows':
#         # If running in virtual environment, we must account for different
#         # folder structure
#         if hasattr(sys, 'real_prefix'):
#             site_path = os.path.join(venv_path, "..", "Lib", "site-packages")
#         else:
#             site_path = os.path.join(venv_path, "Lib", "site-packages")
#     elif opsys == 'linux' or opsys == 'darwin':
#         # If running in virtual environment, we must account for different
#         # folder structure
#         if hasattr(sys, 'real_prefix'):
#             site_path = os.path.join(
#                 venv_path, "..", "lib", "python{0}.{1}".format(
#                     sys.version_info[0], sys.version_info[1]), "site-packages")
#         else:
#             site_path = os.path.join(
#                 venv_path, "lib", "python{0}.{1}".format(
#                     sys.version_info[0], sys.version_info[1]), "site-packages")
#     site.addsitedir(site_path)
#     for path in sys.path:
#         pkg_resources.working_set.add_entry(path)
# 
# Path: hoplite/plugin_manager.py
# class EntryPointManager(object):
#     """
#     Interface to the hoplite.jobs set of entry points
#     """
#     def __init__(self, group_name='hoplite.jobs'):
#         self.entry_point_group_name = group_name
# 
#     def get_plugin_names(self):
#         names = []
#         for entry_point in self._entry_points():
#             names.append(entry_point.name)
#         return names
# 
#     def get_plugin_module_by_name(self, name):
#         # TODO: Should we validate the module implements the correct methods
#         # before returning it to the caller?
#         for entry_point in self._entry_points():
#             if entry_point.name == name:
#                 return entry_point.load()
# 
#     def _entry_points(self):
#         # TODO: If it takes too long to reload pkg_resources, it might be worth
#         # looking into only loading if a package
#         # TODO: isn't found and then try again.
#         reload(pkg_resources)  # makes sure entry points are up-to-date
#         for entry_point in pkg_resources.iter_entry_points(group=self.entry_point_group_name):
#             logger.debug("Found entry point: {0}".format(entry_point.name))
#             yield entry_point


# Path: tests/test_plugin_manager.py
import unittest2
import pip
import types
import os
import sys
import pkg_resources
from tests.paths import TEST_ENTRY_POINT_PATH
from hoplite.api.root import reload_site_packages
from hoplite.plugin_manager import EntryPointManager


class TestEntryPointManager(unittest2.TestCase):
    @classmethod
    def setUpClass(cls):
        # Install hoplite so that its entry points are added
        pip_args = ["install", os.path.join(os.path.dirname(os.path.abspath(__file__)), '..')]
        run_pip(pip_args)
        reload_site_packages()

    @classmethod
    def tearDownClass(cls):
        pip_args = ["uninstall", "hoplite", "-y"]
        run_pip(pip_args)

    def setUp(self):
        self.manager = EntryPointManager()

    def test_get_names_with_default_group_name(self):
        names = self.manager.get_plugin_names()
        self.assertIn('hoplite.plugins.download_folder_from_ftp_job', names)
        self.assertIn('hoplite.plugins.download_network_folder_job', names)
        self.assertIn('hoplite.plugins.upload_to_network_folder_job', names)

    def test_get_names_of_hot_installed_package(self):
        pip_args = ["install", TEST_ENTRY_POINT_PATH]
        run_pip(pip_args)
        reload_site_packages()
        for syspath in sys.path:
            pkg_resources.working_set.add_entry(syspath)
        try:

            self.manager = EntryPointManager('entry.point.test')