o
    a"<f5M                     @   s  d dl Z d dlZd dlZd dl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l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mZ d dl m!Z! d dl"m#Z# dd Z$dd Z%G dd dZ&G dd dZ'e j()e j(*e j(+e,Z-G dd deZ.eddddG dd deZ/G d d! d!eZ0edd"G d#d$ d$e/Z1G d%d& d&e/Z2G d'd( d(eZ3G d)d* d*eZ4G d+d, d,eZ5G d-d. d.eZ6dS )/    N)mkdtemp)rmtreecopytreeBeautifulSoup)locmem)SimpleTestCase)override_settingscache)SOURCE_FILESOURCE_HUNK)get_cachekeyget_precompiler_cachekey)settings)CssCompressor)FilterDoesNotExistFilterError)JsCompressor)DefaultStoragec                 C   s
   t | dS )Nzhtml.parserr   )markup r   Q/var/www/html/kck/venv/lib/python3.10/site-packages/compressor/tests/test_base.py	make_soup      
r   c                 K   s(   d dd | D }d}|| |f S )N c                 S   s   g | ]
\}}d ||f qS )z %s="%s"r   ).0kvr   r   r   
<listcomp>   s    zcss_tag.<locals>.<listcomp>z3<link rel="stylesheet" href="%s" type="text/css"%s>)joinitems)hrefkwargsrendered_attrstemplater   r   r   css_tag   s   r&   c                   @   &   e Zd ZdZ		dddZdd ZdS )TestPrecompilerz4A filter whose output is always the string 'OUTPUT' Nc                 C   s   d S Nr   selfcontentattrsfilter_typefilenamecharsetr   r   r   __init__#   s   zTestPrecompiler.__init__c                 K   s   dS )NOUTPUTr   r+   r#   r   r   r   input'   s   zTestPrecompiler.inputNNN__name__
__module____qualname____doc__r1   r4   r   r   r   r   r(   !       
r(   c                   @   r'   )PassthroughPrecompilerz,A filter whose outputs the input unmodified Nc                 C   s
   || _ d S r)   r,   r*   r   r   r   r1   -   s   
zPassthroughPrecompiler.__init__c                 K   s   | j S r)   r=   r3   r   r   r   r4   1   s   zPassthroughPrecompiler.inputr5   r6   r   r   r   r   r<   +   r;   r<   c                   @   s.   e Zd Zdd Zdd Zedddd Zd	S )
$PrecompilerAndAbsoluteFilterTestCasec                 C   s(   d| _ d| _d| _d| _d| _d| _d S )NzM<link rel="stylesheet" href="/static/css/relative_url.css" type="text/css" />zK<link rel="stylesheet" href="/static/css/relative_url.css" type="text/css">z^<link rel="stylesheet" href="/static/CACHE/css/relative_url.e8602322bfa6.css" type="text/css">z^<link rel="stylesheet" href="/static/CACHE/css/relative_url.376db5682982.css" type="text/css">z+p { background: url('../img/python.png'); }z=p { background: url('/static/img/python.png?ccb38978f900'); })	html_orightml_auto_close_removedhtml_link_to_precompiled_csshtml_link_to_absolutized_csscss_origcss_absolutizedr+   r   r   r   setUp:   s   
z*PrecompilerAndAbsoluteFilterTestCase.setUpc           	      C   sz   |rdnd}|r
dnd}| j ||d|id td| j}t| d }| || W d    d S 1 s6w   Y  d S )N))ztext/cssz1compressor.tests.test_base.PassthroughPrecompilerr   )z0compressor.filters.css_default.CssAbsoluteFiltercss)COMPRESS_ENABLEDCOMPRESS_PRECOMPILERSCOMPRESS_FILTERSr   )r   r   r?   listhunksassertEqual)	r+   enableduse_precompileruse_absolute_filterexpected_outputprecompilerfilterscss_nodeoutputr   r   r   helperB   s   "z+PrecompilerAndAbsoluteFilterTestCase.helperr,   )COMPRESS_CSS_HASHING_METHODc                 C   s   | j ddd| jd | j ddd| jd | j ddd| jd | j ddd| jd | j ddd| jd | j ddd| jd | j ddd| jd | j ddd| jd dS )a3  
        Tests whether specifying a precompiler also runs the CssAbsoluteFilter even if
        compression is disabled, but only if the CssAbsoluteFilter is actually contained
        in the filters setting.
        While at it, ensure that everything runs as expected when compression is enabled.
        F)rN   rO   rP   rQ   TN)rV   r@   rA   rB   rC   rD   rE   r   r   r   !test_precompiler_enables_absoluteM   s   zFPrecompilerAndAbsoluteFilterTestCase.test_precompiler_enables_absoluteN)r7   r8   r9   rF   rV   r	   rX   r   r   r   r   r>   8   s
    r>   Tr   
nocompress)rH   rI   COMPRESS_DEBUG_TOGGLEc                   @   sH  e Z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
ed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%d& Zed'dd(d)d* Zd+d, Zed-d.d/d0 Zed1d.d2d3 Zed4d.d5d6 Zed7d8d(d9d: Zed;d8d(d<d= Zed>d8d(d?d@ ZedAd8d(dBdC ZdDS )ECompressorTestCasec                 C   s,   d| _ td| j | _d| _td| j| _d S )Nz<link rel="stylesheet" href="/static/css/one.css" type="text/css">
<style type="text/css">p { border:5px solid green;}</style>
<link rel="stylesheet" href="/static/css/two.css" type="text/css">rG   z}<script src="/static/js/one.js" type="text/javascript"></script>
<script type="text/javascript">obj.value = "value";</script>js)rG   r   rT   r\   r   js_noderE   r   r   r   rF   f   s   zCompressorTestCase.setUpc                 C       dd }|  |||| dS )zr
        assertEqual with internal newlines collapsed to single, and
        trailing whitespace removed.
        c                 S   s   t dd|  S )Nz\n+
)resubrstrip)sr   r   r   collapsew   s   z9CompressorTestCase.assertEqualCollapsed.<locals>.collapseNrM   )r+   abrd   r   r   r   assertEqualCollapsedr      z'CompressorTestCase.assertEqualCollapsedc                 C   r^   )zz
        assertEqual for splits, particularly ignoring the presence of
        a trailing newline on the content.
        c                 S   s   dd | D S )Nc                 S   s,   g | ]}|d  |d |d |d   fqS r            )rb   r   xr   r   r   r      s   , zHCompressorTestCase.assertEqualSplits.<locals>.mangle.<locals>.<listcomp>r   )splitr   r   r   mangle   s   z4CompressorTestCase.assertEqualSplits.<locals>.mangleNre   )r+   rf   rg   rq   r   r   r   assertEqualSplits{   ri   z$CompressorTestCase.assertEqualSplitsc                    sj   t tjtjddddftdd dft tjtjdddd	fg} j } fd
d|D } 	|| d S )NrG   one.csszcss/one.csszB<link rel="stylesheet" href="/static/css/one.css" type="text/css">p { border:5px solid green;}z;<style type="text/css">p { border:5px solid green;}</style>ztwo.csszcss/two.csszB<link rel="stylesheet" href="/static/css/two.css" type="text/css">c              	      2   g | ]}|d  |d |d  j j|d fqS rj   )rT   parserelem_strrn   rE   r   r   r         2 z5CompressorTestCase.test_css_split.<locals>.<listcomp>)
r   ospathr    r   COMPRESS_ROOTr   rT   split_contentsrr   r+   outrp   r   rE   r   test_css_split   s$   
z!CompressorTestCase.test_css_splitc                 C   s"   g d}|  |t| j  d S )N)zbody { background:#990; }rt   zbody { color:#fff; })rM   rK   rT   rL   r+   r~   r   r   r   test_css_hunks      z!CompressorTestCase.test_css_hunksc                 C   s.   d}d dd | j D }| || d S )NzKbody { background:#990; }
p { border:5px solid green;}
body { color:#fff; }r_   c                 S      g | ]}|qS r   r   r   hr   r   r   r          z6CompressorTestCase.test_css_output.<locals>.<listcomp>)r    rT   rL   rM   )r+   r~   rL   r   r   r   test_css_output   s   z"CompressorTestCase.test_css_outputc                 C   s:   d}d}t d|}ddd | D }| || d S )Nz6body { background:#990; }
.compress-test {color: red;}z<link rel="stylesheet" href="/static/css/one.css" type="text/css" />
        <link rel="stylesheet" href="/static/css/utf-8_with-BOM.css" type="text/css" />rG   r_   c                 S   r   r   r   r   r   r   r   r      r   zECompressorTestCase.test_css_output_with_bom_input.<locals>.<listcomp>)r   r    rL   rM   )r+   r~   rG   css_node_with_bomrL   r   r   r   test_css_output_with_bom_input   s
   
z1CompressorTestCase.test_css_output_with_bom_inputc                 C   s:   t d}| jjD ]}| |tt|d|  q	d S )Nz^\d{10}[\.\d]+$z?mtimes is returning something that doesn't look like a date: %s)r`   compilerT   mtimes
assertTruematchstrfloat)r+   is_datedater   r   r   test_css_mtimes   s   
z"CompressorTestCase.test_css_mtimesF)rH   c                 C      |  | j| j  d S r)   )rh   rG   rT   rU   rE   r   r   r   test_css_return_if_off      z)CompressorTestCase.test_css_return_if_offc                 C   s$   t d}| || jjd d S )Nz\w{12}z@cachekey is returning something that doesn't look like r'\w{12}')r`   r   r   r   rT   cachekey)r+   is_cachekeyr   r   r   test_cachekey   s   
z CompressorTestCase.test_cachekeyc                 C   s"   t d}| || j   d S )N"/static/CACHE/css/600674ea1d3d.css)r&   rM   rT   rU   stripr+   rU   r   r   r   test_css_return_if_on   r   z(CompressorTestCase.test_css_return_if_onc                 C      d}|  || jjdd d S )NzK<link rel="preload" href="/static/CACHE/css/600674ea1d3d.css" as="style" />preloadmode)rM   rT   rU   r   r   r   r   test_css_preload_output      z*CompressorTestCase.test_css_preload_outputc                    sR   t tjtjddddftdd dfg} j } fdd|D } 	|| d S )	Nr\   zone.jsz	js/one.jsz@<script src="/static/js/one.js" type="text/javascript"></script>obj.value = "value";z<<script type="text/javascript">obj.value = "value";</script>c              	      ru   rj   )r]   rv   rw   rn   rE   r   r   r      rx   z4CompressorTestCase.test_js_split.<locals>.<listcomp>)
r   ry   rz   r    r   r{   r   r]   r|   rr   r}   r   rE   r   test_js_split   s   
z CompressorTestCase.test_js_splitc                 C   s"   ddg}|  |t| j  d S )Nz	obj = {};r   )rM   rK   r]   rL   r   r   r   r   test_js_hunks   r   z CompressorTestCase.test_js_hunksc                 C      d}|  || j  d S N8<script src="/static/CACHE/js/8a0fed36c317.js"></script>rM   r]   rU   r   r   r   r   test_js_output      z!CompressorTestCase.test_js_outputc                 C   r   )NzJ<link rel="preload" href="/static/CACHE/js/8a0fed36c317.js" as="script" />r   r   r   r   r   r   r   test_js_preload_output   r   z)CompressorTestCase.test_js_preload_outputc                 C   s,   | j jddi d}| || j   d S )NurlThis is not a url, just a textr   )r]   contextupdaterM   rU   r   r   r   r   test_js_override_url   s   z'CompressorTestCase.test_js_override_urlc                 C   s4   | j jddi td}| || j    d S )Nr   r   r   )rT   r   r   r&   rM   rU   r   r   r   r   r   test_css_override_url   s   z(CompressorTestCase.test_css_override_urlr   rI   rH   c                 C   r   r)   )rh   r\   r]   rU   rE   r   r   r   test_js_return_if_off   r   z(CompressorTestCase.test_js_return_if_offc                 C   r   r   r   r   r   r   r   test_js_return_if_on   r   z'CompressorTestCase.test_js_return_if_oncustom)COMPRESS_OUTPUT_DIRc                 C       d}|  |td| j  d S )Nz9<script src="/static/custom/js/8a0fed36c317.js"></script>r\   rM   r   r\   rU   r   r   r   r   test_custom_output_dir1      z*CompressorTestCase.test_custom_output_dir1r   c                 C   r   )Nz2<script src="/static/js/8a0fed36c317.js"></script>r\   r   r   r   r   r   test_custom_output_dir2   r   z*CompressorTestCase.test_custom_output_dir2z/custom/nested/c                 C   r   )Nz@<script src="/static/custom/nested/js/8a0fed36c317.js"></script>r\   r   r   r   r   r   test_custom_output_dir3  r   z*CompressorTestCase.test_custom_output_dir3))text/foobarz*compressor.tests.test_base.TestPrecompilerTc                 C   4   d}t d|}t|d}| |jjd d d S )N=<style type="text/foobar">p { border:10px solid red;}</style>rG   inliner   r2   r   r   rU   rM   stylecontentsr+   rG   rT   rU   r   r   r   test_precompiler_class_used     
z.CompressorTestCase.test_precompiler_class_used))r   z,compressor.tests.test_base.NonexistentFilterc                 C   "   d}t d|}| t|jd d S Nr   rG   r   )r   assertRaisesr   rU   r+   rG   rT   r   r   r   (test_nonexistent_precompiler_class_error     
z;CompressorTestCase.test_nonexistent_precompiler_class_error))r   z./foo -I ./bar/bazc                 C   r   r   )r   r   r   rU   r   r   r   r   !test_command_with_dot_precompiler  r   z4CompressorTestCase.test_command_with_dot_precompiler))ztext/djangoz*compressor.filters.template.TemplateFilterc                 C   r   )Nza<style type="text/django">p { border:10px solid {% if 1 %}green{% else %}red{% endif %};}</style>rG   r   r   zp{border:10px solid green}r   r   r   r   r   test_template_precompiler  r   z,CompressorTestCase.test_template_precompilerN)r7   r8   r9   rF   rh   rr   r   r   r   r   r   r	   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r[   _   sZ    		








r[   c                   @   sR   e Zd Zdd Zdd Zdd Zeddeje	j
ed	f ffd
ddd ZdS )CssMediaTestCasec                 C   
   d| _ d S )Na'  <link rel="stylesheet" href="/static/css/one.css" type="text/css" media="screen">
<style type="text/css" media="print">p { border:5px solid green;}</style>
<link rel="stylesheet" href="/static/css/two.css" type="text/css" media="all">
<style type="text/css">h1 { border:5px solid green;}</style>)rG   rE   r   r   r   rF   *  r   zCssMediaTestCase.setUpc                 C   sP   t d| j}t| d}g d}| t|d | |dd |D  d S )NrG   link)screenprintallN   c                 S      g | ]}| d dqS mediaNgetr   r   r   r   r   r   6      z4CssMediaTestCase.test_css_output.<locals>.<listcomp>)r   rG   r   rU   find_allrM   len)r+   rT   linksr   r   r   r   r   1  s
   z CssMediaTestCase.test_css_outputc                 C   sH   | j d }td|}g d}t| d}| |dd |D  d S )NzH<style type="text/css" media="print">p { border:10px solid red;}</style>rG   )r   r   r   Nr   r   c                 S   r   r   r   r   r   r   r   r   =  r   z>CssMediaTestCase.test_avoid_reordering_css.<locals>.<listcomp>)rG   r   r   rU   r   rM   )r+   rG   rT   r   r   r   r   r   test_avoid_reordering_css8  s
   

z*CssMediaTestCase.test_avoid_reordering_cssr   z%s %s {infile} {outfile}zprecompiler.pyFr   c                 C   s\   d}t d|}t| ddg}| g ddd |D  | g dd	d |D  d S )
Nz<link rel="stylesheet" href="/static/css/one.css" type="text/css" media="screen">
<link rel="stylesheet" href="/static/css/two.css" type="text/css" media="screen">
<style type="text/foobar" media="screen">h1 { border:5px solid green;}</style>rG   r   r   )z/static/css/one.cssz/static/css/two.cssNc                 S   r   )r"   Nr   r   r   r   r   r   J  r   zKCssMediaTestCase.test_passthough_when_compress_disabled.<locals>.<listcomp>)r   r   r   c                 S   r   r   r   r   r   r   r   r   L  r   )r   r   rU   r   rM   r   r   r   r   &test_passthough_when_compress_disabled?  s   


z7CssMediaTestCase.test_passthough_when_compress_disabledN)r7   r8   r9   rF   r   r   r	   sys
executablery   rz   r    test_dirr   r   r   r   r   r   )  s    r   )COMPRESS_VERBOSEc                   @   s   e Zd ZdS )VerboseTestCaseN)r7   r8   r9   r   r   r   r   r   O  s    r   c                   @   s   e Zd Zdd ZdS )CacheBackendTestCasec                 C   s    ddl m} | |jtj d S )Nr   r
   )compressor.cacher   rM   	__class__r   LocMemCache)r+   r   r   r   r   test_correct_backendV  s   z)CacheBackendTestCase.test_correct_backendN)r7   r8   r9   r   r   r   r   r   r   T  s    r   c                   @      e Zd Zdd Zdd ZdS )JsAsyncDeferTestCasec                 C   r   )Na2              <script src="/static/js/one.js" type="text/javascript"></script>
            <script src="/static/js/two.js" type="text/javascript" async></script>
            <script src="/static/js/three.js" type="text/javascript" defer></script>
            <script type="text/javascript">obj.value = "value";</script>
            <script src="/static/js/one.js" type="text/javascript" async></script>
            <script src="/static/js/two.js" type="text/javascript" async></script>
            <script src="/static/js/three.js" type="text/javascript"></script>r\   rE   r   r   r   rF   \  r   zJsAsyncDeferTestCase.setUpc                    sP   dd  t d| j}g d}t| d} fdd|D }| || d S )Nc                 S   s    |  drdS |  drdS d S )Nasyncdefer)has_attr)tagr   r   r   extract_attrg  s
   

z9JsAsyncDeferTestCase.test_js_output.<locals>.extract_attrr\   )Nr   r   Nr   Nscriptc                    s   g | ]} |qS r   r   )r   rc   r   r   r   r   o  s    z7JsAsyncDeferTestCase.test_js_output.<locals>.<listcomp>)r   r\   r   rU   r   rM   )r+   r]   rU   scriptsr-   r   r   r   r   f  s   z#JsAsyncDeferTestCase.test_js_outputN)r7   r8   r9   rF   r   r   r   r   r   r   [  s    
r   c                   @   r   )JSWithParensTestCasec                 C   r   )Nzm
        <script src="/static/js/one.js"></script>
        <script src="/static/js/two.js"></script>
        r   rE   r   r   r   rF   t  r   zJSWithParensTestCase.setUpc                 C   s8   t d| j}| }| |d d | |d d d S )Nr\   r   z
obj = {};;rk   zpollos = {};)r   r\   filter_inputrM   )r+   r]   r,   r   r   r   test_js_contentz  s   z$JSWithParensTestCase.test_js_contentN)r7   r8   r9   rF   r   r   r   r   r   r   s  s    r   c                   @   sH   e Zd Zdd Zdd Zedddd Zed	dd
d Zdd ZdS )CacheTestCasec                 C   s
   d t _d S r)   )cachemod_cachekey_funcrE   r   r   r   rF     r   zCacheTestCase.setUpc                 C   s   |  tdd d S )Nfoozdjango_compressor.foo)rM   r   rE   r   r   r   test_get_cachekey_basic  s   z%CacheTestCase.test_get_cachekey_basicz.leading.dot)COMPRESS_CACHE_KEY_FUNCTIONc                 C      |  tdd  d S )Nc                   S      t dS Nr   r   r   r   r   r   <lambda>      z=CacheTestCase.test_get_cachekey_leading_dot.<locals>.<lambda>r   ImportErrorrE   r   r   r   test_get_cachekey_leading_dot     z+CacheTestCase.test_get_cachekey_leading_dotzinvalid.modulec                 C   r  )Nc                   S   r  r  r  r   r   r   r   r    r  z=CacheTestCase.test_get_cachekey_invalid_mod.<locals>.<lambda>r  rE   r   r   r   test_get_cachekey_invalid_mod  r
  z+CacheTestCase.test_get_cachekey_invalid_modc                 C   s0   zt dd W d S  ty   | d Y d S w )Nasdfz6get_precompiler_cachekey raised TypeError unexpectedly)r   	TypeErrorfailrE   r   r   r   test_get_precompiler_cachekey  s
   z+CacheTestCase.test_get_precompiler_cachekeyN)	r7   r8   r9   rF   r   r	   r	  r  r  r   r   r   r   r     s    

r   c                   @   s$   e Zd Zdd Zdd Zdd ZdS )CompressorInDebugModeTestCasec              	   C   s\   d| _ d| _t | _tj| jd}ttj	| | jdddd||tj
gd| _| j  d S )NzD<link rel="stylesheet" href="/static/css/one.css" type="text/css" />5c6a60375256staticTr   rY   )rH   rI   rZ   DEBUGSTATIC_ROOTr{   STATICFILES_DIRS)rG   expected_css_hashr   tmpdirry   rz   r    r   r   r  r{   r	   	__enter__)r+   new_static_rootr   r   r   rF     s   	z#CompressorInDebugModeTestCase.setUpc                 C   s   t | j | jd d d  d S r)   )r   r  r	   __exit__rE   r   r   r   tearDown  s   
z&CompressorInDebugModeTestCase.tearDownc                    s    fdd}|  t jtjdd}d}t|d}|d || W d    n1 s.w   Y  |  t jtjddd	 j }t|d
}| }W d    n1 sWw   Y   	||v d S )Nc                     s6   d j  } td j}t |_| } | | d S )NzG<link rel="stylesheet" href="/static/CACHE/css/%s.css" type="text/css">rG   )r  r   rG   r   storagerU   rM   )expected
compressorrU   rE   r   r   compare  s
   
zJCompressorInDebugModeTestCase.test_filename_in_debug_mode.<locals>.comparerG   rs   zp { font-family: 'test' }rf   r_   CACHEz%s.cssr)
ry   rz   r    r   r{   openwriter  readr   )r+   r  r/   test_css_contentrG   result_filenamefresultr   rE   r   test_filename_in_debug_mode  s"   

z9CompressorInDebugModeTestCase.test_filename_in_debug_modeN)r7   r8   r9   rF   r  r)  r   r   r   r   r    s    r  )7ry   r`   r   tempfiler   shutilr   r   bs4r   django.core.cache.backendsr   django.testr   django.test.utilsr	   r  r   r   compressor.baser   r   r   r   r   compressor.confr   compressor.cssr   compressor.exceptionsr   r   compressor.jsr   compressor.storager   r   r&   r(   r<   rz   abspathr    dirname__file__r   r>   r[   r   r   r   r   r   r   r  r   r   r   r   <module>   sL    

' F&