<s> import abc <EOL> import copy <EOL> import os <EOL> import six <EOL> import sys <EOL> import unittest2 <EOL> import st2common . util . loader as plugin_loader <EOL> PLUGIN_FOLDER = '<STR_LIT>' <EOL> SRC_RELATIVE = os . path . join ( '<STR_LIT>' , PLUGIN_FOLDER ) <EOL> SRC_ROOT = os . path . join ( os . path . abspath ( os . path . dirname ( __file__ ) ) , SRC_RELATIVE ) <EOL> class LoaderTest ( unittest2 . TestCase ) : <EOL> sys_path = None <EOL> @ six . add_metaclass ( abc . ABCMeta ) <EOL> class DummyPlugin ( object ) : <EOL> """<STR_LIT>""" <EOL> @ abc . abstractmethod <EOL> def do_work ( self ) : <EOL> pass <EOL> def setUp ( self ) : <EOL> LoaderTest . sys_path = copy . copy ( sys . path ) <EOL> def tearDown ( self ) : <EOL> sys . path = LoaderTest . sys_path <EOL> def test_module_load_from_file ( self ) : <EOL> plugin_path = os . path . join ( SRC_ROOT , '<STR_LIT>' ) <EOL> plugin_classes = plugin_loader . register_plugin ( <EOL> LoaderTest . DummyPlugin , plugin_path ) <EOL> self . assertEqual ( <NUM_LIT:1> , len ( plugin_classes ) ) <EOL> self . assertTrue ( os . path . abspath ( os . path . join ( SRC_ROOT , '<STR_LIT>' ) ) in sys . path ) <EOL> for plugin_class in plugin_classes : <EOL> try : <EOL> plugin_instance = plugin_class ( ) <EOL> ret_val = plugin_instance . do_work ( ) <EOL> self . assertIsNotNone ( ret_val , '<STR_LIT>' ) <EOL> except : <EOL> pass <EOL> def test_module_load_from_file_fail ( self ) : <EOL> try : <EOL> plugin_path = os . path . join ( SRC_ROOT , '<STR_LIT>' ) <EOL> plugin_loader . register_plugin ( LoaderTest . DummyPlugin , plugin_path ) <EOL> self . assertTrue ( False , '<STR_LIT>' ) <EOL> except ImportError : <EOL> self . assertTrue ( True ) <EOL> def test_syspath_unchanged_load_multiple_plugins ( self ) : <EOL> plugin_1_path = os . path . join ( SRC_ROOT , '<STR_LIT>' ) <EOL> try : <EOL> plugin_loader . register_plugin ( <EOL> LoaderTest . DummyPlugin , plugin_1_path ) <EOL> except ImportError : <EOL> pass <EOL> old_sys_path = copy . copy ( sys . path ) <EOL> plugin_2_path = os . path . join ( SRC_ROOT , '<STR_LIT>' ) <EOL> try : <EOL> plugin_loader . register_plugin ( <EOL> LoaderTest . DummyPlugin , plugin_2_path ) <EOL> except ImportError : <EOL> pass <EOL> self . assertEqual ( old_sys_path , sys . path , '<STR_LIT>' ) <EOL> def test_register_plugin_class_class_doesnt_exist ( self ) : <EOL> file_path = os . path . join ( SRC_ROOT , '<STR_LIT>' ) <EOL> expected_msg = '<STR_LIT>' <EOL> self . assertRaisesRegexp ( Exception , expected_msg , <EOL> plugin_loader . register_plugin_class , <EOL> base_class = LoaderTest . DummyPlugin , <EOL> file_path = file_path , <EOL> class_name = '<STR_LIT>' ) <EOL> def test_register_plugin_class_abstract_method_not_implemented ( self ) : <EOL> file_path = os . path . join ( SRC_ROOT , '<STR_LIT>' ) <EOL> expected_msg = '<STR_LIT>' <EOL> self . assertRaisesRegexp ( plugin_loader . IncompatiblePluginException , expected_msg , <EOL> plugin_loader . register_plugin_class , <EOL> base_class = LoaderTest . DummyPlugin , <EOL> file_path = file_path , <EOL> class_name = '<STR_LIT>' ) </s>
