def test_get_env_shell_file_paths_from_setup_environment_elem():
    xml = """<action name="setup_r_environment">
        <repository name="package_r_3_0_1" owner="bgruening" toolshed="toolshed.g2.bx.psu.edu" changeset_revision="1234567">
            <package name="R" version="3.0.1" />
        </repository>
    </action>
    """
    mock_app = MockApp()
    action_elem = parse_xml_string( xml )
    required_for_install_env_sh = '/path/to/existing.sh'
    all_env_paths = [ required_for_install_env_sh ]
    action_dict = {}
    env_manager = EnvManager( mock_app )

    r_env_sh = '/path/to/go/env.sh'

    def mock_get_env_shell_file_paths( elem ):
        assert elem.get( 'name' ) == "package_r_3_0_1"
        return [ r_env_sh ]

    with __mock_common_util_method( env_manager, "get_env_shell_file_paths", mock_get_env_shell_file_paths ):
        env_manager.get_env_shell_file_paths_from_setup_environment_elem( all_env_paths, action_elem, action_dict )
        ## Verify old env files weren't deleted.
        assert required_for_install_env_sh in all_env_paths
        ## Verify new ones added.
        assert r_env_sh in all_env_paths
        ## env_shell_file_paths includes everything
        assert all( [ env in action_dict[ 'env_shell_file_paths' ] for env in all_env_paths ] )

        ## action_shell_file_paths includes only env files defined in
        ## inside the setup_ action element.
        assert required_for_install_env_sh not in action_dict[ 'action_shell_file_paths' ]
        assert r_env_sh in action_dict[ 'action_shell_file_paths' ]
def test_get_env_shell_file_paths_from_setup_environment_elem():
    xml = """<action name="setup_r_environment">
        <repository name="package_r_3_0_1" owner="bgruening" toolshed="toolshed.g2.bx.psu.edu" changeset_revision="1234567">
            <package name="R" version="3.0.1" />
        </repository>
        <repository name="package_zlib_1_2_8" owner="iuc" toolshed="toolshed.g2.bx.psu.edu" changeset_revision="7654321">
            <package name="zlib" version="1.2.8" />
        </repository>
    </action>
    """
    mock_app = MockApp()
    action_elem = parse_xml_string(xml)
    required_for_install_env_sh = '/path/to/existing.sh'
    all_env_paths = [required_for_install_env_sh]
    action_dict = {}
    env_manager = EnvManager(mock_app)

    r_env_sh = '/path/to/go/env.sh'

    def mock_get_env_shell_file_paths(elem):
        assert elem.get('name') in ["package_r_3_0_1", "package_zlib_1_2_8"]
        return [r_env_sh]

    with __mock_common_util_method(env_manager, "get_env_shell_file_paths",
                                   mock_get_env_shell_file_paths):
        env_manager.get_env_shell_file_paths_from_setup_environment_elem(
            all_env_paths, action_elem, action_dict)
        # Verify old env files weren't deleted.
        assert required_for_install_env_sh in all_env_paths
        # Verify new ones added.
        assert r_env_sh in all_env_paths
        # env_shell_file_paths includes everything
        assert all([
            env in action_dict['env_shell_file_paths'] for env in all_env_paths
        ])
        # for every given repository there should be one env
        # file + the required_for_install_env_sh file
        assert len(action_dict['env_shell_file_paths']) == 3

        # action_shell_file_paths includes only env files defined
        # inside the setup_ action element.
        assert required_for_install_env_sh in action_dict[
            'action_shell_file_paths']
        assert r_env_sh in action_dict['action_shell_file_paths']
    def set_environment( self, elem, tool_shed_repository, attr_tups_of_dependencies_for_install ):
        """
        Create a ToolDependency to set an environment variable.  This is different from the process used to
        set an environment variable that is associated with a package.  An example entry in a tool_dependencies.xml
        file is::

            <set_environment version="1.0">
                <environment_variable name="R_SCRIPT_PATH" action="set_to">$REPOSITORY_INSTALL_DIR</environment_variable>
            </set_environment>

        This method must also handle the sub-element tag::
            <environment_variable name="R_SCRIPT_PATH" action="set_to">$REPOSITORY_INSTALL_DIR</environment_variable>
        """
        # TODO: Add support for a repository dependency definition within this tool dependency type's tag set.  This should look something like
        # the following.  See the implementation of support for this in the tool dependency package type's method above.
        # This function is only called for set environment actions as defined below, not within an <install version="1.0"> tool
        # dependency type. Here is an example of the tag set this function does handle:
        # <action type="set_environment">
        #     <environment_variable name="PATH" action="prepend_to">$INSTALL_DIR</environment_variable>
        # </action>
        # Here is an example of the tag set this function does not handle:
        # <set_environment version="1.0">
        #    <repository toolshed="<tool shed>" name="<repository name>" owner="<repository owner>" changeset_revision="<changeset revision>" />
        # </set_environment>
        env_manager = EnvManager( self.app )
        tool_dependencies = []
        env_var_version = elem.get( 'version', '1.0' )
        tool_shed_repository_install_dir = os.path.abspath( tool_shed_repository.repo_files_directory( self.app ) )
        if elem.tag == 'environment_variable':
            # <environment_variable name="R_SCRIPT_PATH" action="set_to">$REPOSITORY_INSTALL_DIR</environment_variable>
            elems = [ elem ]
        else:
            # <set_environment version="1.0">
            #    <environment_variable name="R_SCRIPT_PATH" action="set_to">$REPOSITORY_INSTALL_DIR</environment_variable>
            # </set_environment>
            elems = [ env_var_elem for env_var_elem in elem ]
        for env_var_elem in elems:
            env_var_name = env_var_elem.get( 'name', None )
            # The value of env_var_name must match the text value of at least 1 <requirement> tag in the
            # tool config's <requirements> tag set whose "type" attribute is "set_environment" (e.g.,
            # <requirement type="set_environment">R_SCRIPT_PATH</requirement>).
            env_var_action = env_var_elem.get( 'action', None )
            if env_var_name and env_var_action:
                # Tool dependencies of type "set_environment" always have the version attribute set to None.
                attr_tup = ( env_var_name, None, 'set_environment' )
                if attr_tup in attr_tups_of_dependencies_for_install:
                    install_dir = \
                        tool_dependency_util.get_tool_dependency_install_dir( app=self.app,
                                                                              repository_name=tool_shed_repository.name,
                                                                              repository_owner=tool_shed_repository.owner,
                                                                              repository_changeset_revision=tool_shed_repository.installed_changeset_revision,
                                                                              tool_dependency_type='set_environment',
                                                                              tool_dependency_name=env_var_name,
                                                                              tool_dependency_version=None )
                    install_environment = InstallEnvironment( app=self.app,
                                                              tool_shed_repository_install_dir=tool_shed_repository_install_dir,
                                                              install_dir=install_dir )
                    env_var_dict = env_manager.create_env_var_dict( elem=env_var_elem,
                                                                    install_environment=install_environment )
                    if env_var_dict:
                        if not os.path.exists( install_dir ):
                            os.makedirs( install_dir )
                        status = self.app.install_model.ToolDependency.installation_status.INSTALLING
                        tool_dependency = \
                            tool_dependency_util.create_or_update_tool_dependency( app=self.app,
                                                                                   tool_shed_repository=tool_shed_repository,
                                                                                   name=env_var_name,
                                                                                   version=None,
                                                                                   type='set_environment',
                                                                                   status=status,
                                                                                   set_status=True )
                        if env_var_version == '1.0':
                            # Create this tool dependency's env.sh file.
                            env_file_builder = EnvFileBuilder( install_dir )
                            return_code = env_file_builder.append_line( make_executable=True, **env_var_dict )
                            if return_code:
                                error_message = 'Error creating env.sh file for tool dependency %s, return_code: %s' % \
                                    ( str( tool_dependency.name ), str( return_code ) )
                                log.debug( error_message )
                                status = self.app.install_model.ToolDependency.installation_status.ERROR
                                tool_dependency = \
                                    tool_dependency_util.set_tool_dependency_attributes( self.app,
                                                                                         tool_dependency=tool_dependency,
                                                                                         status=status,
                                                                                         error_message=error_message )
                            else:
                                if tool_dependency.status not in [ self.app.install_model.ToolDependency.installation_status.ERROR,
                                                                   self.app.install_model.ToolDependency.installation_status.INSTALLED ]:
                                    status = self.app.install_model.ToolDependency.installation_status.INSTALLED
                                    tool_dependency = \
                                        tool_dependency_util.set_tool_dependency_attributes( self.app,
                                                                                             tool_dependency=tool_dependency,
                                                                                             status=status )
                                    log.debug( 'Environment variable %s set in %s for tool dependency %s.' %
                                        ( str( env_var_name ), str( install_dir ), str( tool_dependency.name ) ) )
                        else:
                            error_message = 'Only set_environment version 1.0 is currently supported (i.e., change your tag to be <set_environment version="1.0">).'
                            status = self.app.install_model.ToolDependency.installation_status.ERROR
                            tool_dependency = \
                                tool_dependency_util.set_tool_dependency_attributes( self.app,
                                                                                     tool_dependency=tool_dependency,
                                                                                     status=status,
                                                                                     error_message=error_message )
            tool_dependencies.append( tool_dependency )
        return tool_dependencies