U
    jg]+                     @   s  d Z ddlmZ ddlmZ ddlZddlZddlmZ ddl	m
Z
mZmZ dd	lmZmZmZmZ e Ze Zejjejjd
ddddddgddddddgddd Zejjdd Zejjejjdddgddgdejjd d!d"gfd#d$d%gfd$d&gfd$d'gfd$d%d'gfgd(d)d*d+d,d-gdejjd.e
d/fed0fgd1d2gdd3d4 Zejjejd.e
d/fed0fgd5d6 Zejjejd.e
d/fed0fgd7d8 Zd9d: Zd;d< Zejjejd.e
d/fed0fgd=d> Zejjejd.e
d/fed0fgd?d@ ZdS )Az
Test the processor hooks
    )Path)TemporaryDirectoryN   )Pooch)UnzipUntar
Decompress   )pooch_test_urlpooch_test_registrycheck_tiny_datacapture_logzmethod,ext,name)autoxzN)lzmar   N)r   r   N)bzip2bz2N)gzipgzN)r   r   zdifferent-name.txtr   r   r   r   r   name)idsc              
   C   sF  t | |d}t (}t|}|dkr@t|dd|dg }nt|| }t|ttd}t r}|j	d| |d}	|
 }
|
 }t|d	kst|d
  d
 dkst|d dst| |d kstW 5 Q R X |	|kstt|	 t *}|j	d| |d}	|
 dkstW 5 Q R X |	|ks0tt|	 W 5 Q R X dS )z=Check that decompression after download works for all formats)methodr   N.tiny-data.txtdecomppathbase_urlregistryztiny-data.txt.	processorr   r   DownloadingZDecompressing )r   r   r   strjoinr   BASEURLREGISTRYr   fetchgetvalue
splitlineslenAssertionErrorsplit
startswithr   )r   extr   r    local_storer   	true_pathpuplog_filefnameZlogslines r6   ?/tmp/pip-unpacked-wheel-vga3p65l/pooch/tests/test_processors.pytest_decompress   s,    
r8   c                  C   s  t  } t| }t|ttd}tt.}t	  |j
dtddd W 5 Q R X W 5 Q R X |jjd dsttd|jjd ksttt.}t	  |j
dtd	dd W 5 Q R X W 5 Q R X |jjd d
std|jjd ksttt.}t	  |j
dtddd W 5 Q R X W 5 Q R X |jjd dsFtd|jjd ks\ttt.}t	  |j
dtddd W 5 Q R X W 5 Q R X |jjd dstd|jjd kstW 5 Q R X dS )zGShould fail if method='auto' and no extension is given in the file namer   r   r   )r   r   r   z"Unrecognized file extension '.txt'zpooch.Unzip/UntarZblaz Invalid compression method 'bla'zipz Invalid compression method 'zip'z	store.zipz"Unrecognized file extension '.zip'N)r   r   r   r&   r'   pytestZraises
ValueErrorwarningscatch_warningsr(   r   valueargsr.   r,   )r0   r   r2   	exceptionr6   r6   r7   test_decompress_failsE   s.    

(
(
(
(rA   target_pathZsome_custom_pathZdefault_pathZcustom_pathzarchive,members	tiny-datar   )storeNrD   store/tiny-data.txtstore/subdir/tiny-data.txtzstore/subdirZsingle_fileZarchive_allZarchive_fileZarchive_subdir_fileZarchive_subdirZarchive_multiplezprocessor_class,extensionz.zipz.tar.gzr   r   c              
   C   s  | ||d}|dkr"|| |j  }t }t|}t|||| | j\}}t|ttd}	t 2}
|	j	|| |d}t
||kstt|
| W 5 Q R X |D ]}t| qt 2}
|	j	|| |d}t
||kstt|
g  W 5 Q R X |D ]}t| qW 5 Q R X dS )zGTests the behaviour of processors for unpacking archives (Untar, Unzip))membersextract_dirNr   r   )suffixr   r   "_unpacking_expected_paths_and_logs__name__r   r&   r'   r   r(   setr,   _check_logsr   )processor_class	extensionrB   archiverG   r    r   
true_pathsZexpected_logr2   r3   fnamesr4   r6   r6   r7   test_unpackingd   s0       
rS   c           	   	   C   s   t  }tt|ttd}| dgd}|jd| |d}t|dksHtt|d  | ddgd}|jd| |d}t|d	kstt|d  t|d  |jd| |d}t|dkstt|d  W 5 Q R X d
S )zHTest that multiple subsequent calls to a processor yield correct resultsr   rE   rG   rD   r   r	   r   rF   r   N	r   r   r   r&   r'   r(   r+   r,   r   )	rN   rO   r0   r2   
processor1
filenames1
processor2
filenames2Z
filenames3r6   r6   r7   test_multiple_unpacking   s     rZ   c              	   C   sb   t  R}tt|ttd}| dgd}|jd| |d}t|dksHtt|d  W 5 Q R X dS )	zDTest that unpack members can also be specifed both with a leading ./r   z./store/tiny-data.txtrT   rD   r   r	   r   NrU   )rN   rO   r0   r2   rV   rW   r6   r6   r7   $test_unpack_members_with_leading_dot   s    r[   c                 C   sF   |    }t|t|ks tt||D ]\}}||s*tq*dS )zC
    Assert that the lines in the log match the expected ones.
    N)r)   r*   r+   r,   r9   r.   )r3   Zexpected_linesr5   lineZexpected_liner6   r6   r7   rM      s    rM   c                 C   s   dg}| dkr(t |d h}|d n| dkrv|dkrvt |d d t |d d d h}|| |d  d	 nn| dkr|dk	rg }|D ]L}|t|d
  }t |ds|d }|t | |d| d qt|}||fS )zo
    Generate the appropriate expected paths and log message depending on the
    parameters for the test.
    r!   rC   r   zExtracting 'tiny-data.txt'rD   Nsubdirr"   zing contents/zExtracting '')r$   appendr   r-   endswithrL   )rP   rG   r   r   Z	log_linesrQ   memberr1   r6   r6   r7   rJ      s&    rJ   c              	   C   s~   t  n}tt|ttd}| dgd}|jd| |d}t|dksHt|  }|jd| |d}t|dksptW 5 Q R X dS )zx
    Test that calling with valid members then without them works.
    https://github.com/fatiando/pooch/issues/364
    r   rE   rT   rD   r   r	   Nr   r   r   r&   r'   r(   r+   r,   rN   rO   r0   r2   rV   rW   rX   rY   r6   r6   r7   &test_unpacking_members_then_no_members   s    
re   c              	   C   s~   t  n}tt|ttd}| dgd}|jd| |d}t|dksHt|  }|jd| |d}t|dksptW 5 Q R X dS )zz
    Test that calling with invalid members then without them works.
    https://github.com/fatiando/pooch/issues/364
    r   znot-a-valid-file.csvrT   rD   r   r   Nrc   rd   r6   r6   r7   ,test_unpacking_wrong_members_then_no_members  s    
rf   ) __doc__pathlibr   tempfiler   r<   r:   r#   r   Z
processorsr   r   r   utilsr
   r   r   r   r'   r&   marknetworkZparametrizer8   rA   rS   rZ   r[   rM   rJ   re   rf   r6   r6   r6   r7   <module>   s   
  
	
