o
    a"<f                     @   sJ  d dl Z d dlZd dlZd dlmZ d dlmZ d dlmZ d dl	m
Z
 d dlmZ d dlmZmZ d dlmZmZmZ d d	lmZmZ d d
lmZmZ d dlmZmZ d dlmZ d dlm Z! d dl"m#Z# d dl$m%Z% dd Z&dd Z'G dd de(Z)edd Z*G dd dZ+G dd de+eZ,G dd de+eZ-G dd dZ.G d d! d!e.e+eZ/G d"d# d#e.e+eZ0G d$d% d%e.e+eZ1G d&d' d'e.e+eZ2G d(d) d)e+eZ3G d*d+ d+e+eZ4G d,d- d-e+eZ5G d.d/ d/e+eZ6G d0d1 d1e+eZ7G d2d3 d3e.e+eZ8G d4d5 d5e+eZ9G d6d7 d7e.e9Z:G d8d9 d9e+eZ;G d:d; d;e.e;Z<G d<d= d=e;Z=G d>d? d?e+eZ>G d@dA dAe.e+eZ?G dBdC dCe+eZ@G dDdE dEe+eZAG dFdG dGe+eZBG dHdI dIe+eZCG dJdK dKe+eZDG dLdM dMe+eZEG dNdO dOe+eZFG dPdQ dQe+eZGG dRdS dSe.e+eZHG dTdU dUe+eZIG dVdW dWe;ZJdS )X    N)contextmanager)import_moduleSkipTest)patch)settings)call_commandCommandError)ContextOriginTemplate)override_settingsTestCaseget_script_prefixset_script_prefix)flush_offline_manifestget_offline_manifest)OfflineGenerationError)Command) default_offline_manifest_storage)get_mod_funcc                  c   s$    t ddD ]	} dd|  iV  qd S )N      contentzOK %d!)range)i r   T/var/www/html/kck/venv/lib/python3.10/site-packages/compressor/tests/test_offline.pyoffline_context_generator   s   r   c                   c   s    dt jiV  d S )N
STATIC_URL)r   r    r   r   r   r   static_url_context_generator   s   r!   c                   @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )LazyScriptNamePrefixedUrla  
    Lazy URL with ``SCRIPT_NAME`` WSGI param as path prefix.

    .. code-block :: python

        settings.STATIC_URL = LazyScriptNamePrefixedUrl('/static/')

        # HTTP request to '/some/page/' without SCRIPT_NAME
        str(settings.STATIC_URL) == '/static/'

        # HTTP request to '/app/prefix/some/page/` with SCRIPT_NAME = '/app/prefix/'
        str(settings.STATIC_URL) == '/app/prefix/static/'

        # HTTP request to '/another/prefix/some/page/` with SCRIPT_NAME = '/another/prefix/'
        str(settings.STATIC_URL) == '/another/prefix/static/'

    The implementation is incomplete, all ``str`` methods must be overridden
    in order to work correctly with the rest of Django core.
    c                 C   s    |  drt | dd   S | S )N/r   )
startswithr   selfr   r   r   __str__3   s    z!LazyScriptNamePrefixedUrl.__str__c                 C   s   t | S N)strr%   r   r   r   __unicode__6      z%LazyScriptNamePrefixedUrl.__unicode__c                 O      t | j|i |S )zU
        Override ``.split()`` method to make it work with ``{% static %}``.
        )r)   splitr&   argskwargsr   r   r   r-   9   s   zLazyScriptNamePrefixedUrl.splitc                 O   r,   )a<   Override ``.replace()`` to make it work with ``{% static %}``.

        In ``django.core.files.storage``, ``FileSystemStorage.url()`` passes
        this object to ``urllib.parse.urljoin``.

        In ``urrlib.parse``, the function that calls ``replace()`` is
        ``_remove_unsafe_bytes_from_url()``.

        )r)   replacer.   r   r   r   r1   ?   s   
z!LazyScriptNamePrefixedUrl.replaceN)__name__
__module____qualname____doc__r'   r*   r-   r1   r   r   r   r   r"      s    r"   c                 c   s"    t  }t|  dV  t| dS )z
    Override ``SCRIPT_NAME`` WSGI param, yield, then restore its original value.

    :param new_prefix: New ``SCRIPT_NAME`` value.
    Nr   )
new_prefix
old_prefixr   r   r   script_prefixL   s
   r8   c                   @   s   e Zd ZdZdZdZdZdZdZdZ	dd Z
d	d
 Zdd Zdd Zdd Zdd Zd)ddZd*ddZdd Zdd Zdd Zdd  Zd!d" Zd#d$ Zd%d& Zd'd( ZdS )+OfflineTestCaseMixinzutf-8test_compressor_offline.html output)djangojinja2Nc                    s  t tj}tj|d d d  j}tj|d d d  j}|g|d d< |g|d d< |ddd}d jv rC fdd|d	<  j	d urN|
 j	  jdi | _ j  d
 jv rtj| j _t j jd}tj j jd}t| |d _W d    n1 sw   Y  d jv rtj| j _|d	  }tj j jd}||  _W d    d S 1 sw   Y  d S d S )Nr   DIRSr   T)	TEMPLATESCOMPRESS_ENABLEDCOMPRESS_OFFLINEr>   c                      s      S r(   )_get_jinja2_envr   r%   r   r   <lambda>   s    z,OfflineTestCaseMixin.setUp.<locals>.<lambda>COMPRESS_JINJA2_GET_ENVIRONMENTr=   )nametemplate_nameencoding)originr   )copydeepcopyr   r@   ospathjointemplates_direnginesadditional_test_settingsupdater   	__enter__rG   template_pathr   ioopenCHARSETr   readtemplatetemplate_path_jinja2from_stringtemplate_jinja2)r&   r@   django_template_dirjinja2_template_dirr   rJ   file_
jinja2_envr   r%   r   setUpd   sZ   	






"zOfflineTestCaseMixin.setUpc                 C   s0   | j d d d  d}t|rt| d S d S )Nmanifest.json)r   __exit__r   existsdelete)r&   manifest_filenamer   r   r   tearDown   s
   
zOfflineTestCaseMixin.tearDownc                 C   s@   t j}t|ttfs|g}|dkrdd |D S |dkr|S d S )Nr=   c                 S      g | ]}t |qS r   r
   .0cr   r   r   
<listcomp>       z:OfflineTestCaseMixin._prepare_contexts.<locals>.<listcomp>r>   )r   COMPRESS_OFFLINE_CONTEXT
isinstancelisttupler&   enginecontextsr   r   r   _prepare_contexts   s   z&OfflineTestCaseMixin._prepare_contextsc                    sR     |}|dkrd fdd|D S |dkr'd fdd|D d S d S )Nr=   r;   c                 3       | ]	} j |V  qd S r(   )rZ   renderrk   r%   r   r   	<genexpr>   s    z8OfflineTestCaseMixin._render_template.<locals>.<genexpr>r>   
c                 3   rx   r(   )r]   ry   rk   r%   r   r   rz      s    
)rw   rO   rt   r   r%   r   _render_template   s   
z%OfflineTestCaseMixin._render_templatec                 C      d tj| j|S )Nz+<script src="{}CACHE/js/{}.{}.js"></script>formatr   COMPRESS_URL_PLACEHOLDERexpected_basenamer&   hashr   r   r   _render_script   
   
z#OfflineTestCaseMixin._render_scriptc                 C   r}   )NzD<link rel="stylesheet" href="{}CACHE/css/{}.{}.css" type="text/css">r~   r   r   r   r   _render_link   r   z!OfflineTestCaseMixin._render_linkr{   c                 C   s   | |d tjttjS Nr{   )rO   r1   r   r   r)   COMPRESS_URLr&   result	separatorr   r   r   _render_result   s   z#OfflineTestCaseMixin._render_resultr   c                    sz    j }t|ttfs|g}t j|g|d\}} t||   fdd|D |  |} | 	| d S )NrQ   	verbosityc                    s   g | ]}  |qS r   r   )rl   hr%   r   r   rn      s    z6OfflineTestCaseMixin._test_offline.<locals>.<listcomp>)
expected_hashrq   rr   rs   CompressCommandhandle_innerassertEquallenr|   r   )r&   ru   r   hashescountr   rendered_templater   r%   r   _test_offline   s   

z"OfflineTestCaseMixin._test_offlinec                 C   "   d| j vr	td| jdd d S )Nr=   /This test class does not support django engine.ru   rQ   r   r   r%   r   r   r   test_offline_django      
z(OfflineTestCaseMixin.test_offline_djangoc                 C   r   )Nr>   /This test class does not support jinja2 engine.r   r   r%   r   r   r   test_offline_jinja2   r   z(OfflineTestCaseMixin.test_offline_jinja2c                 C   $   d| j vr	td| jddd d S )Nr=   r   r   ru   r   r   r%   r   r   r   test_offline_django_verbosity_1      
z4OfflineTestCaseMixin.test_offline_django_verbosity_1c                 C   r   )Nr>   r   r   r   r   r%   r   r   r   test_offline_jinja2_verbosity_1   r   z4OfflineTestCaseMixin.test_offline_jinja2_verbosity_1c                 C   r   )Nr=   r      r   r   r%   r   r   r   test_offline_django_verbosity_2   r   z4OfflineTestCaseMixin.test_offline_django_verbosity_2c                 C   r   )Nr>   r   r   r   r   r%   r   r   r   test_offline_jinja2_verbosity_2   r   z4OfflineTestCaseMixin.test_offline_jinja2_verbosity_2c                 C   s\   dd l }ddlm}m} ddlm} |||jj|jjg}| 	 }|j
||d}||jd< |S )Nr   )url_forSpacelessExtension)CompressorExtension)
extensionsloaderr   )
jinja2.extcompressor.offline.jinja2r   r   compressor.contrib.jinja2extr   extwith_do_get_jinja2_loaderEnvironmentglobals)r&   r>   r   r   r   r   r   envr   r   r   rC      s   
z$OfflineTestCaseMixin._get_jinja2_envc                 C   s&   dd l }|jtjd d | jd}|S )Nr   r   r?   rH   )r>   FileSystemLoaderr   r@   rX   )r&   r>   r   r   r   r   r     s
   z'OfflineTestCaseMixin._get_jinja2_loaderr{   r   )r2   r3   r4   rX   rG   rP   r   r   rQ   rR   rb   rh   rw   r|   r   r   r   r   r   r   r   r   r   r   rC   r   r   r   r   r   r9   Y   s0    6
	

r9   c                   @   s   e Zd ZdZdZeeddd Zeeddd Z	eeddd	 Z
eedd
d Zdd Zdd Zdd Zdd Zdd Zededddd ZdS )OfflineCompressBasicTestCasebasic822ac7501287compressc                 C   s(   i dg f|_ t   | |jd d S )Nr   )return_valuer   handler   
call_countr&   compress_mockr   r   r   test_handle_no_args  s   
z0OfflineCompressBasicTestCase.test_handle_no_argsc              	   C   r   | j dd" | t t   W d    n1 sw   Y  W d    n1 s+w   Y  | |jd d S )NF)rA   r   r   assertRaisesr	   r   r   r   r   r   r   r   r   test_handle_compress_disabled     z:OfflineCompressBasicTestCase.test_handle_compress_disabledc              	   C   r   )NFrB   r   r   r   r   r   r   %test_handle_compress_offline_disabled  r   zBOfflineCompressBasicTestCase.test_handle_compress_offline_disabledc                 C   sX   i dg f|_ | jdd t jdd W d    n1 sw   Y  | |jd d S )Nr   Fr   T)force)r   r   r   r   r   r   r   r   r   r   +test_handle_compress_offline_disabled_force$  s
   zHOfflineCompressBasicTestCase.test_handle_compress_offline_disabled_forcec                 C   s    t   | t| jjti  d S r(   )r   r   r   rZ   ry   r
   r%   r   r   r   0test_rendering_without_manifest_raises_exception+  s   zMOfflineCompressBasicTestCase.test_rendering_without_manifest_raises_exceptionc                 C   s   t   | t| jji  d S r(   )r   r   r   r]   ry   r%   r   r   r   7test_rendering_without_manifest_raises_exception_jinja21  s   zTOfflineCompressBasicTestCase.test_rendering_without_manifest_raises_exception_jinja2c                 C   sv   t  j|gdd\}}t  d}t|rt| | d| | | | jg| | 	|}| || 
| d S )Nr   r   rc   r   )r   r   r   r   re   rf   r   r   r   r|   r   )r&   ru   r   r   rg   r   r   r   r   1_test_deleting_manifest_does_not_affect_rendering7  s   


zNOfflineCompressBasicTestCase._test_deleting_manifest_does_not_affect_renderingc                 C   s   | j D ]}| | qd S r(   )rQ   r   r&   ru   r   r   r   0test_deleting_manifest_does_not_affect_renderingB  s   
zMOfflineCompressBasicTestCase.test_deleting_manifest_does_not_affect_renderingc                 C   s   d}| j |d. ddlm} ddlm} t  }| t|d | | t|d | W d    d S 1 s9w   Y  d S )N)z%django.template.loaders.cached.Loader)z)django.template.loaders.filesystem.Loaderz.django.template.loaders.app_directories.Loader)TEMPLATE_LOADERSr   )Loaderr   )r   "django.template.loaders.filesystemr   'django.template.loaders.app_directoriesr   get_loaders
assertTruerq   )r&   r   r   AppDirectoriesLoaderloadersr   r   r   test_get_loadersF  s   
"z-OfflineCompressBasicTestCase.test_get_loadersz2compressor.offline.django.DjangoParser.render_nodes   non-ascii character here:ä)side_effectc                 C   s>   |  t t jdd W d    d S 1 sw   Y  d S )Nr   )r   )r   r	   r   r   )r&   mockr   r   r   !test_non_ascii_exception_messagesV  s   "z>OfflineCompressBasicTestCase.test_non_ascii_exception_messagesN)r2   r3   r4   rP   r   r   objectr   r   r   r   r   r   r   r   r   r   	Exceptionr   r   r   r   r   r     s(    







r   c                   @      e Zd ZdZdddZdS )%OfflineCompressSkipDuplicatesTestCasetest_duplicater   c                 C   s\   t  j|g|d\}}| d| | | dg| | |}| || |d d d S )Nr   r   r   r   r;   r   r   r   r   r|   r   r&   ru   r   r   r   r   r   r   r   r   `  s   

z3OfflineCompressSkipDuplicatesTestCase._test_offlineNr   r2   r3   r4   rP   r   r   r   r   r   r   ]      r   c                   @   s   e Zd ZdZdS )
SuperMixinr=   N)r2   r3   r4   rQ   r   r   r   r   r   m  s    r   c                   @      e Zd ZdZdZdS )!OfflineCompressBlockSuperTestCasetest_block_super817b5defb197Nr2   r3   r4   rP   r   r   r   r   r   r   r      r   c                   @   r   ))OfflineCompressBlockSuperMultipleTestCasetest_block_super_multipled3f749e83c81Nr   r   r   r   r   r   x  r   r   c                   @   s   e Zd ZdZdZddiZdS )5OfflineCompressBlockSuperMultipleCachedLoaderTestCase test_block_super_multiple_cached055f88f4751fr   r   Nr2   r3   r4   rP   r   rR   r   r   r   r   r   ~  s
    r   c                   @   r   )1OfflineCompressBlockSuperTestCaseWithExtraContenttest_block_super_extrar   c                 C   s`   t  j|g|d\}}| d| | | d| dg| | |}| || |d d S )Nr   r   bfcec76e0f28r   r;   r   r   r   r   r   r     s   

z?OfflineCompressBlockSuperTestCaseWithExtraContent._test_offlineNr   r   r   r   r   r   r         r   c                   @       e Zd ZdZdZdddiiZdS ) OfflineCompressConditionTestCasetest_conditiona3275743dc69rp   	conditionredNr   r   r   r   r   r         r   c                   @   r   )"OfflineCompressTemplateTagTestCasetest_templatetag2bb88185b4f5Nr   r   r   r   r   r        r  c                   @   r   )(OfflineCompressStaticTemplateTagTestCasetest_static_templatetagbe0b1eade28bNr   r   r   r   r   r    r  r  c                   @      e Zd ZdZdZdZdS )'OfflineCompressTemplateTagNamedTestCasetest_templatetag_namedoutput_namer   N)r2   r3   r4   rP   r   r   r   r   r   r   r    s    r  c                   @   r   )"OfflineCompressTestCaseWithContexttest_with_contextc6bf81bca7adrp   r   OK!Nr   r   r   r   r   r    r  r  c                   @   r   )'OfflineCompressTestCaseWithContextSupertest_with_context_superdd79e1bd1527rp   r   r  Nr   r   r   r   r   r    s    r  c                   @   s.   e Zd ZdZg dZdee iZdd ZdS )&OfflineCompressTestCaseWithContextListr  8b4a7452e1c555b3123e884cbfc63829cc58rp   c                 C   s*   |dkrdd t jD S |dkrt jS d S )Nr=   c                 S   ri   r   rj   rk   r   r   r   rn     ro   zLOfflineCompressTestCaseWithContextList._prepare_contexts.<locals>.<listcomp>r>   )r   rp   r   r   r   r   rw     s
   z8OfflineCompressTestCaseWithContextList._prepare_contextsN)	r2   r3   r4   rP   r   rr   r   rR   rw   r   r   r   r   r    s    
r  c                   @   s&   e Zd ZdZg dZdee iZdS )+OfflineCompressTestCaseWithContextListSuperr  b39975a8f6eaed565a1d262f6ac9e4b29febrp   N)r2   r3   r4   rP   r   rr   r   rR   r   r   r   r   r    s
    
r  c                   @   s(   e Zd ZdZg dZddiZdd ZdS )+OfflineCompressTestCaseWithContextGeneratorr  r  rp   7compressor.tests.test_offline.offline_context_generatorc                 C   sD   t tj\}}tt|| }|dkrdd |D S |dkr |S d S )Nr=   c                 s   s    | ]}t |V  qd S r(   rj   rk   r   r   r   rz     s    zPOfflineCompressTestCaseWithContextGenerator._prepare_contexts.<locals>.<genexpr>r>   )r   r   rp   getattrr   )r&   ru   modulefunctionrv   r   r   r   rw     s   z=OfflineCompressTestCaseWithContextGenerator._prepare_contextsN)r2   r3   r4   rP   r   rR   rw   r   r   r   r   r     s    r   c                   @   s    e Zd ZdZg dZddiZdS )0OfflineCompressTestCaseWithContextGeneratorSuperr  r  rp   r!  Nr   r   r   r   r   r%    s
    r%  c                   @   s,   e Zd ZdZdZdZdddZddd	Zd
S ),OfflineCompressStaticUrlIndependenceTestCasez
    Test that the offline manifest is independent of STATIC_URL.
    I.e. users can use the manifest with any other STATIC_URL in the future.
    test_static_url_independenceb0bfc3754fd4z/custom/static/url/:compressor.tests.test_offline.static_url_context_generator)r    rp   r   c                 C   s   t  j|g|d\}}| d| | | | jg| | | || | | jdd | | || | W d    d S 1 sFw   Y  d S )Nr   r   z/another/static/url/)r    )r   r   r   r   r   r|   r   r   r&   ru   r   r   r   r   r   r   r     s   
"z:OfflineCompressStaticUrlIndependenceTestCase._test_offlineNr   )r2   r3   r4   r5   rP   r   rR   r   r   r   r   r   r&     s    	r&  c                       s2   e Zd ZdZdZdddiiZd	 fdd	Z  ZS )
5OfflineCompressTestCaseWithContextVariableInheritance&test_with_context_variable_inheritanceb8376aad1357rp   parent_template	base.htmlr{   c                    s   dt  || S r   )superr   r   	__class__r   r   r   *  s   zDOfflineCompressTestCaseWithContextVariableInheritance._render_resultr   )r2   r3   r4   rP   r   rR   r   __classcell__r   r   r1  r   r+     s    r+  c                   @   s,   e Zd ZdZdddiddigiZddgZdS )	:OfflineCompressTestCaseWithContextVariableInheritanceSuper,test_with_context_variable_inheritance_superrp   r.  z
base1.html
base2.htmlcee48db7cedcc877c436363aN)r2   r3   r4   rP   rR   r   r   r   r   r   r4  .  s    r4  c                   @   r   )6OfflineCompressTestCaseWithContextGeneratorImportErrorr  r   c              	   C   s  | j dd | jtt j|gd W d    n1 sw   Y  | j dd | jtt j|gd W d    n1 s=w   Y  | j dd | jtt j|gd W d    n1 s^w   Y  | j dd | jtt j|gd W d    n1 sw   Y  d}| j |d+ zt j|g|d W n ty   | d	 Y n	w W d    d S W d    d S 1 sw   Y  d S )
Nzinvalid_mod.invalid_func)rp   )rQ   
compressorz!compressor.tests.invalid_functionzcompressor.tests.test_offliner!  r   zCValid path to offline context generator must not raise ImportError.)r   r   ImportErrorr   r   fail)r&   ru   r   
valid_pathr   r   r   r   ?  sN   
"zDOfflineCompressTestCaseWithContextGeneratorImportError._test_offlineNr   r   r   r   r   r   r9  ;  r   r9  c                   @   r   )OfflineCompressTestCaseErrorstest_error_handlingr   c                 C   s   t  j|g|d\}}|dkr| d| n| d| | |d | | d| | | d| | | d| | | d	| d S )
Nr   r=   r   r   r>   187e2ce75808fffafcdf428eeeabdac292329a7f06880ce3)r   r   r   assertInr   r   r*  r   r   r   r   j  s   
z+OfflineCompressTestCaseErrors._test_offlineNr   r   r   r   r   r   r>  g  r   r>  c                   @   s"   e Zd ZdZddiZdddZdS )	 OfflineCompressTestCaseWithErrorr?  COMPRESS_PRECOMPILERS))ztext/coffeescriptznonexisting-binaryr   c                 C   s   | j dd | jtt j|g|d W d   n1 sw   Y  | j dd | jtt j|g|d W d   dS 1 s@w   Y  dS )z
        Test that a CommandError is raised with DEBUG being False as well as
        True, as otherwise errors in configuration will never show in
        production.
        T)DEBUGr   NF)r   r   r	   r   r   r&   ru   r   r   r   r   r     s   "z.OfflineCompressTestCaseWithError._test_offlineNr   r2   r3   r4   rP   rR   r   r   r   r   r   rE  ~  s
    rE  c                   @   s"   e Zd ZdZdZdZdddZdS )	OfflineCompressEmptyTaga  
        In case of a compress template tag with no content, an entry
        will be added to the manifest with an empty string as value.
        This test makes sure there is no recompression happening when
        compressor encounters such an emptystring in the manifest.
    r   r   r   c                 C   s>   t  j|g|d t }d|t|d < | | |d d S )Nr   r;   r   r{   )r   r   r   rr   r   r|   )r&   ru   r   manifestr   r   r   r     s   z%OfflineCompressEmptyTag._test_offlineNr   )r2   r3   r4   r5   rP   r   r   r   r   r   r   rJ    s
    rJ  c                       sN   e Zd Zg dZdZg dZg dZdZ fddZdd	 Z	dddZ
  ZS )'OfflineCompressBlockSuperBaseCompressed)r/  r6  r:    test_block_super_base_compressed)e4e9263fa4c09cecd41a505fr   )028c3fc422322e9d3f5545a6r   r   c              	      s   t    g | _g | _| jD ]:}tjtj	d d d |}| j
| tj|| jd}t| }W d    n1 s=w   Y  | j
| qd S )Nr   r?   rH   )r0  rb   template_paths	templatestemplate_namesrM   rN   rO   r   r@   appendrV   rW   rX   r   rY   )r&   rG   rU   r`   rZ   r1  r   r   rb     s    

z-OfflineCompressBlockSuperBaseCompressed.setUpc                 C   s4   |dkr| ttjS |dkr| tjd S d S )Nr=   r>   r{   )ry   r
   r   rp   )r&   rZ   ru   r   r   r   r|     s
   z8OfflineCompressBlockSuperBaseCompressed._render_templater   c           	      C   sz   t  j|g|d\}}| t| j| t| j| jD ]\}}| |}| 	|| | 
||}| || |g qd S )Nr   )r   r   r   r   r   zipexpected_hash_offlinerS  r   rD  r|   r   )	r&   ru   r   r   r   r   rZ   expectedr   r   r   r   r     s   

z5OfflineCompressBlockSuperBaseCompressed._test_offliner   )r2   r3   r4   rT  rP   rW  r   rQ   rb   r|   r   r3  r   r   r1  r   rL    s    rL  c                   @   s&   e Zd ZdZdddiiZd	ddZdS )
%OfflineCompressInlineNonAsciiTestCasetest_inline_non_asciirp   test_non_ascii_valueu   —r   c                 C   s:   t  j|g|d\}}| |}| |d|d  d S )Nr   r;   r{   )r   r   r|   r   rO   )r&   ru   r   _r   r   r   r   r   r     s
   

z3OfflineCompressInlineNonAsciiTestCase._test_offlineNr   rI  r   r   r   r   rY    s    rY  c                   @   s(   e Zd ZdZddddiZd
ddZd	S )OfflineCompressComplexTestCasetest_complexrp   r  )z	js/one.jszjs/nonasc.js)r  my_namesr   c                 C   st   t  j|g|d\}}| d| | | d| d| dg| | |}| || |d |d gd d S )	Nr      76a82cfab9ab7219642b8ab4567bb77b13dbr   r   r;   r   r   r   r   r   r     s   

z,OfflineCompressComplexTestCase._test_offlineNr   rI  r   r   r   r   r]    s    
r]  c                   @   s0   e Zd ZdZdZg dZeedd	ddZdS )
'OfflineCompressExtendsRecursionTestCasez
    Test that templates extending templates with the same name
    (e.g. admin/index.html) don't cause an infinite test_extends_recursion
    test_extends_recursion)zdjango.contrib.adminzdjango.contrib.authzdjango.contrib.contenttypeszdjango.contrib.staticfilesr:  )INSTALLED_APPSr   c                 C   s&   t  j|g|d\}}| |d d S )Nr   r   )r   r   r   )r&   ru   r   r   r\  r   r   r   r   	  s   
z5OfflineCompressExtendsRecursionTestCase._test_offlineNr   )r2   r3   r4   r5   rP   rf  r   r   r   r   r   r   rd    s    rd  c                   @   r  )&OfflineCompressExtendsRelativeTestCasezu
    Test that templates extending templates using relative paths
    (e.g. ./base.html) are evaluated correctly
    test_extends_relativer   N)r2   r3   r4   r5   rP   r   r   r   r   r   rg    s    rg  c                   @   s*   e Zd ZdZd
ddZdd Zdd Zd	S )TestCompressCommandtest_compress_commandr   c                 C   s   t d)NzNot utilized for this test caser   rH  r   r   r   r     r+   z!TestCompressCommand._test_offlinec                    s    fdd|  D S )Nc                    s   i | ]
\}}|  |qS r   r   )rl   kvr%   r   r   
<dictcomp>   s    z@TestCompressCommand._build_expected_manifest.<locals>.<dictcomp>)items)r&   rX  r   r%   r   _build_expected_manifest  s   
z,TestCompressCommand._build_expected_manifestc                 C   s   ddd}t dddgi| t }| ddi}| || t ddd	gi| t }| d
di}| || t dddd	gi| t }| ddd}| || d S )NTr   )r   r   r   rQ   r=   @0fed9c02607acba22316a328075a81a74e0983ae79470daa9d3707a337623dc30241107e9a9ar>   @077408d23d4a829b8f88db2eadcf902b29d71b14f94018d900f38a3f8ed24c945694ca83dd14)rp  rr  )r   )r   r   ro  r   )r&   optsmanifest_djangomanifest_django_expectedmanifest_jinja2manifest_jinja2_expectedmanifest_bothmanifest_both_expectedr   r   r   test_multiple_engines$  s.   z)TestCompressCommand.test_multiple_enginesNr   )r2   r3   r4   rP   r   ro  r{  r   r   r   r   ri    s
    
ri  c                   @   s6   e Zd ZdZdZededddZdZddd	Zd
S ).OfflineCompressTestCaseWithLazyStringAlikeUrlsad  
    Test offline compressing with ``STATIC_URL`` and ``COMPRESS_URL`` as instances of
    *lazy string-alike objects* instead of strings.

    In particular, lazy string-alike objects that add ``SCRIPT_NAME`` WSGI param
    as URL path prefix.

    For example:

    - We've generated offline assets and deployed them with our Django project.
    - We've configured HTTP server (e.g. Nginx) to serve our app at two different URLs:
      ``http://example.com/my/app/`` and ``http://app.example.com/``.
    - Both URLs are leading to the same app, but in the first case we pass
      ``SCRIPT_NAME = /my/app/`` to WSGI app server (e.g. to uWSGI, which is *behind* Nginx).
    - Django (1.11.7, as of today) *ignores* ``SCRIPT_NAME`` when generating
      static URLs, while it uses ``SCRIPT_NAME`` when generating Django views URLs -
      see https://code.djangoproject.com/ticket/25598.
    - As a solution - we can use a lazy string-alike object instead of ``str`` for ``STATIC_URL``
      so it will know about ``SCRIPT_NAME`` and add it as a prefix every time we do any
      string operation with ``STATIC_URL``.
    - However, there are some cases when we cannot force CPython to render our lazy string
      correctly - e.g. ``some_string.replace(STATIC_URL, '...')``. So we need to do explicit
      ``str`` type cast: ``some_string.replace(str(STATIC_URL), '...')``.
    r	  /static/r)  )r    r   rp   r
  r   c              	   C   s   t  j|g|d\}}| d| dD ]K}t|= | ttj|dd  | ttj|dd  | 	|}| 
|}| || | ttj| W d    n1 sYw   Y  qd S )Nr   r   )r;   z/app/prefix/z/another/prefix/r#   r}  )r   r   r   r8   r)   r   r    rstripr   r   r|   rD  )r&   ru   r   r   r   script_nameexpected_resultactual_resultr   r   r   r   c  s*   



z<OfflineCompressTestCaseWithLazyStringAlikeUrls._test_offlineNr   )	r2   r3   r4   r5   rP   r"   rR   r   r   r   r   r   r   r|  >  s    	r|  )KrK   rV   rM   
contextlibr   	importlibr   unittestr   unittest.mockr   django.confr   django.core.managementr   r	   django.templater
   r   r   django.testr   r   django.urlsr   r   compressor.cacher   r   compressor.exceptionsr   'compressor.management.commands.compressr   r   compressor.storager   compressor.utilsr   r   r!   r)   r"   r8   r9   r   r   r   r   r   r   r   r   r  r  r  r  r  r  r  r   r%  r&  r+  r4  r9  r>  rE  rJ  rL  rY  r]  rd  rg  ri  r|  r   r   r   r   <module>   s    -
 4Q








	



 

,+	%