
    x                         S r SSKJr  SSKJr  SSKJr  SSKrSSKrSSKrSSKrSSK	r	SSK
Jr   " S S\5      r " S	 S
\5      r " S S\	R                   5      rg)z8Unit test support library for GAE Externalized Runtimes.    )absolute_import)division)print_functionN)ext_runtimec                       \ rS rSrSrSrg)InvalidRuntime   z?Raised when the runtime directory is doesn't match the runtime. N)__name__
__module____qualname____firstlineno____doc____static_attributes__r
       +lib/third_party/gae_ext_runtime/testutil.pyr   r      s    Ir   r   c                       \ rS rSrSrS rSrg)AppInfoFake"   z'Serves as a fake for an AppInfo object.c                     U $ Nr
   selfs    r   ToDictAppInfoFake.ToDict%   s    r   r
   N)r   r   r   r   r   r   r   r
   r   r   r   r   "   s
    1r   r   c                   p    \ rS rSrSrS rS rS rSS jrSS jr	SS	 jr
SS
 jrS rS rS rS rS rSrg)TestBase)   zUnit 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                     [         R                  " 5       U l        [        R                  " 5       U l        U R                  [        U S5      S5        g )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   setUpTestBase.setUp0   s=    #??A!))+&89#	$r   c                 D    [         R                  " U R                  5        g r   )shutilrmtreer%   r   s    r   tearDownTestBase.tearDown8   s    dnn%r   c                     Xl         g)a	  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_environment"TestBase.set_execution_environment;   s	     !r   Nc                    [         R                  R                  U R                  U R                  5      nU=(       d    [         R
                  " S0 UD6n[        UR                  5       5        UR                  U R                  U5      nU$ )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_configuratorTestBase.maybe_get_configuratorG   sj     ,,11$2G2G26--A7;--77fmmoyy8r   c                 x    U R                   " U40 UD6nU(       d  gUR                  5         UR                  5       $ )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:
    (bool) Returns True if files are generated, False if not, None
    if configurator didn't match
N)r=   PrebuildGenerateConfigsr   r9   r:   r<   s       r   generate_configsTestBase.generate_configs\   s:     226DVD++--r   c                     U R                   " U40 UD6nU(       d/  [        SR                  U R                  U R                  5      5      eUR                  5         UR                  5       $ )a1  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 {})r=   r   formatr    r%   r@   GenerateConfigDatarB   s       r   generate_config_dataTestBase.generate_config_datas   sg    " 226DVD  "..4fT5J5J59^^/EF F 	..00r   c                     [         R                  R                  U R                  U R                  5      nU=(       d    [         R
                  " S0 UD6nUR                  U R                  U5      nU$ )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.Configurator or None) the identified runtime if found,
    None if not.
r
   )r   r3   r4   r    r"   r5   r7   r%   r8   s        r   detectTestBase.detect   s\     ,,11$2G2G26--A7;--77yy8r   c                 X    [         R                  R                  " U R                  /UQ76 $ )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_pathTestBase.full_path   s      ww||DNN=_==r   c                     [        [        R                  R                  U R                  U5      S5       nUR                  U5        S S S 5        g ! , (       d  f       g = f)Nw)openrN   rO   rP   r%   write)r   filenamecontentsfps       r   
write_fileTestBase.write_file   s9    "'',,t~~x8#>"HHX ?>>s   A
Ac                     [        [        R                  R                  " U R                  /UQ76 5       nUR                  5       sSSS5        $ ! , (       d  f       g= f)aX  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)rV   rN   rO   rP   r    read)r   argsrZ   s      r   read_runtime_def_fileTestBase.read_runtime_def_file   s;     "'',,t44<t<=779 >==s   A
Ac                    U R                  U5      nU R                  [        R                  R	                  U5      5        [        U5       nUR                  5       nSSS5        U R                  UW5        g! , (       d  f       N!= f)zAssert that the specified file exists with the given contents.

Args:
    filename: (str) New file name.
    contents: (str) File contents.
N)rR   r&   rN   rO   existsrV   r^   assertEqual)r   rX   rY   	full_namerZ   actual_contentss         r    assert_file_exists_with_contents)TestBase.assert_file_exists_with_contents   s\     NN8,	y12)_ ggiO ?3 _s   A77
Bc                     U H1  nUR                   U:X  d  M  U R                  UR                  U5          g    U R                  SR	                  X!5      5        g )Nz+filename {} not found in generated files {})rX   rd   rY   failrF   )r   	gen_filesrX   rY   gen_files        r   #assert_genfile_exists_with_contents,TestBase.assert_genfile_exists_with_contents   sR    !H(*x00(; "
 ))AHH# $r   )r"   r%   r   )r   r   r   r   r   r(   r-   r0   r=   rC   rH   rK   rR   r[   r`   rg   rm   r   r
   r   r   r   r   )   sC    $&
!*..16,>4$r   r   )r   
__future__r   r   r   loggingrN   r+   r#   unittestgae_ext_runtimer   	Exceptionr   dictr   TestCaser   r
   r   r   <module>rv      sS    ? &  %  	    'JY J$ p$x   p$r   