
    x                         d Z ddlmZ ddlmZ ddlmZ ddlZddlZddlZddlZddl	Z	ddl
mZ  G d de      Z G d	 d
e      Z G d de	j                         Zy)z8Unit test support library for GAE Externalized Runtimes.    )absolute_import)division)print_functionN)ext_runtimec                       e Zd ZdZy)InvalidRuntimez?Raised when the runtime directory is doesn't match the runtime.N)__name__
__module____qualname____doc__     +lib/third_party/gae_ext_runtime/testutil.pyr   r      s    Ir   r   c                       e Zd ZdZd Zy)AppInfoFakez'Serves as a fake for an AppInfo object.c                     | S Nr   selfs    r   ToDictzAppInfoFake.ToDict%   s    r   N)r	   r
   r   r   r   r   r   r   r   r   "   s
    1r   r   c                   `    e Zd ZdZd Zd Zd ZddZddZddZ	dd	Z
d
 Zd Zd Zd Zd Zy)TestBasezUnit testing base class.

    Derived classes must define a setUp() method that sets a runtime_def_root
    attribute containing the path to the root directory of the runtime.
    c                     t        j                         | _        t        j                         | _        | j                  t        | d      d       y )Nruntime_def_rootzoYour test suite must define a setUp() method that sets a runtime_def_root attribute to the root of the runtime.)r   DefaultExecutionEnvironmentexec_envtempfilemkdtemp	temp_path
assertTruehasattrr   s    r   setUpzTestBase.setUp0   s=    #??A!))+&89#	$r   c                 B    t        j                  | j                         y r   )shutilrmtreer   r   s    r   tearDownzTestBase.tearDown8   s    dnn%r   c                     || _         y)a9  Set the execution environment used by generate_configs.

        If this is not set, an instance of
        ext_runtime.DefaultExecutionEnvironment is used.

        Args:
            exec_env: (ext_runtime.ExecutionEnvironment) The execution
                environment to be used for config generation.
        N)r   )r   r   s     r   set_execution_environmentz"TestBase.set_execution_environment;   s     !r   Nc                 
   t         j                  j                  | j                  | j                        }|xs t        j
                  di |}t        |j                                |j                  | j                  |      }|S )a  Load the runtime definition.

        Args:
            params: (ext_runtime.Params) Runtime parameters.  DEPRECATED.
                Use the keyword args, instead.
            **kwargs: ({str: object, ...}) If specified, these are the
                arguments to the ext_runtime.Params() constructor
                (valid args are at this time are: appinfo, custom and deploy,
                check ext_runtime.Params() for full details)

        Returns:
            configurator or None if configurator didn't match
        r   )
r   ExternalizedRuntimeLoadr   r   Paramsprintr   Detectr   r   paramskwargsrtconfigurators        r   maybe_get_configuratorzTestBase.maybe_get_configuratorG   sj     ,,11$2G2G26--A7;--77fmmoyy8r   c                 n     | j                   |fi |}|sy|j                          |j                         S )ad  Load the runtime definition and generate configs from it.

        Args:
            params: (ext_runtime.Params) Runtime parameters.  DEPRECATED.
                Use the keyword args, instead.
            **kwargs: ({str: object, ...}) If specified, these are the
                arguments to the ext_runtime.Params() constructor
                (valid args are at this time are: appinfo, custom and deploy,
                check ext_runtime.Params() for full details)

        Returns:
            (bool) Returns True if files are generated, False if not, None
            if configurator didn't match
        N)r4   PrebuildGenerateConfigsr   r0   r1   r3   s       r   generate_configszTestBase.generate_configs\   s<     3t226DVD++--r   c                      | j                   |fi |}|s/t        dj                  | j                  | j                              |j                          |j                         S )a  Load the runtime definition and generate configs from it.

        Args:
            params: (ext_runtime.Params) Runtime parameters.  DEPRECATED.
                Use the keyword args, instead.
            **kwargs: ({str: object, ...}) If specified, these are the
                arguments to the ext_runtime.Params() constructor
                (valid args are at this time are: appinfo, custom and deploy,
                check ext_runtime.Params() for full details)

        Returns:
            ([ext_runtime.GeneratedFile, ...]) Returns list of generated files.

        Raises:
            InvalidRuntime: Couldn't detect a matching runtime.
        z/Runtime defined in {} did not detect code in {})r4   r   formatr   r   r6   GenerateConfigDatar8   s       r   generate_config_datazTestBase.generate_config_datas   si    " 3t226DVD  "..4fT5J5J59^^/EF F 	..00r   c                     t         j                  j                  | j                  | j                        }|xs t        j
                  di |}|j                  | j                  |      }|S )aY  Load the runtime definition and generate configs from it.

        Args:
            params: (ext_runtime.Params) Runtime parameters.  DEPRECATED.
                Use the keyword args, instead.
            **kwargs: ({str: object, ...}) If specified, these are the
                arguments to the ext_runtime.Params() constructor
                (valid args are at this time are: appinfo, custom and deploy,
                check ext_runtime.Params() for full details)

        Returns:
            (ext_runtime.Configurator or None) the identified runtime if found,
            None if not.
        r   )r   r*   r+   r   r   r,   r.   r   r/   s        r   detectzTestBase.detect   s\     ,,11$2G2G26--A7;--77yy8r   c                 V    t        j                  j                  | j                  g| S )zReturns the fully qualified path for a relative filename.

        e.g. self.full_path('foo', 'bar', 'baz') -> '/temp/path/foo/bar/baz'

        Args:
            *path_components: ([str, ...]) Path components.

        Returns:
            (str)
        )ospathjoinr   )r   path_componentss     r   	full_pathzTestBase.full_path   s      ww||DNN=_==r   c                     t        t        j                  j                  | j                  |      d      5 }|j                  |       d d d        y # 1 sw Y   y xY w)Nw)openrA   rB   rC   r   write)r   filenamecontentsfps       r   
write_filezTestBase.write_file   s9    "'',,t~~x8#>"HHX ?>>s   AAc                     t        t        j                  j                  | j                  g|       5 }|j                         cddd       S # 1 sw Y   yxY w)a  Read the entire contents of the file.

        Returns the entire contents of the file identified by a set of
        arguments forming a path relative to the root of the runtime
        definition.

        Args:
            *args: A set of path components (see full_path()).  Note that
                these are relative to the runtime definition root, not the
                temporary directory.
        N)rH   rA   rB   rC   r   read)r   argsrL   s      r   read_runtime_def_filezTestBase.read_runtime_def_file   s;     "'',,t44<t<=779 >==s   AAc                    | j                  |      }| j                  t        j                  j	                  |             t        |      5 }|j                         }ddd       | j                  |       y# 1 sw Y   xY w)zAssert that the specified file exists with the given contents.

        Args:
            filename: (str) New file name.
            contents: (str) File contents.
        N)rE   r    rA   rB   existsrH   rO   assertEqual)r   rJ   rK   	full_namerL   actual_contentss         r    assert_file_exists_with_contentsz)TestBase.assert_file_exists_with_contents   s\     NN8,	y12)_ ggiO ?3 _s   A77B c                     |D ]/  }|j                   |k(  s| j                  |j                  |        y  | j                  dj	                  ||             y )Nz+filename {} not found in generated files {})rJ   rT   rK   failr;   )r   	gen_filesrJ   rK   gen_files        r   #assert_genfile_exists_with_contentsz,TestBase.assert_genfile_exists_with_contents   sT    !H(*x00(; "
 ))AHH	# $r   r   )r	   r
   r   r   r"   r&   r(   r4   r9   r=   r?   rE   rM   rQ   rW   r\   r   r   r   r   r   )   sC    $&
!*..16,>4$r   r   )r   
__future__r   r   r   loggingrA   r$   r   unittestgae_ext_runtimer   	Exceptionr   dictr   TestCaser   r   r   r   <module>rd      sS    ? &  %  	    'JY J$ p$x   p$r   