
X+#                 @   s  d  d l  Z d  d l j j 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 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 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! d- d.   Z" d/ d0   Z# d1 d2   Z$ d3 d4   Z% d5 d6   Z& d7 d8   Z' d9 d:   Z( d; d<   Z) d= d>   Z* d? d@   Z+ d S)A    N)
exceptions)fstree)	get_rules)parse_rules)startupc               C   s#   i t  j d 6d d 6d d 6d d 6S)z9Helper function: return a config (dict) with a rules_pathrules_sourcepath
rules_pathbeginbegin_marker   	max_depth)r   FILE_SOURCE r   r   V/home/kop/biz/barnett/enforcer_project/BRIEF-Script/Enforcer/tests/test_parse_rules.pysample_config    s    r   c               C   s   d d d d d d d g S)z+Helper function: Produce a basic rule file.zremark1
zremark2
zbegin
zone
z,two
z,,three
zfour
r   r   r   r   r   sample_rules(   s    r   c             C   s   t  j t   |   S)z.Helper function: Tokenize the basic rule file.)r   lex_rules_fs_structr   )rulesr   r   r   sample_tokens3   s    r   c               C   s*   y t  j j   Wn t k
 r% Yn Xd S)z Clear out the global error stateN)r   emexit_if_errored
SystemExitr   r   r   r   clear_error_state8   s    r   c              C   sF  d }  t  j } i t j d 6|  d 6} | |  } | |  k } | s0t j d | f d | |  f  i d t j   k s t j |   r t j	 |   n d d 6t j	 |  d 6d	 t j   k s t j t   r t j	 t   n d	 d
 6t j	 |  d 6t j	 |  d 6} d i | d 6} t
 t j |    n  d } } } } d S)z,Does a file source produce expected results?z	some pathr   r	   ==Q%(py6)s
{%(py6)s = %(py2)s
{%(py2)s = %(py0)s.get_file_id
}(%(py4)s)
} == %(py8)sr   py8py6r   py0py2py4 assert %(py10)spy10N)r   )r   r"   )r   get_file_idr   r   
@pytest_ar_call_reprcompare@py_builtinslocals_should_repr_global_name	_safereprAssertionError_format_explanation)r   @py_assert1@py_assert3@py_assert5@py_assert7@py_format9@py_format11r   r   r   test_get_file_id_fileF   s    	        r3   c              C   sF  d }  t  j } i t j d 6|  d 6} | |  } | |  k } | s0t j d | f d | |  f  i d t j   k s t j |   r t j	 |   n d d 6t j	 |  d 6d	 t j   k s t j t   r t j	 t   n d	 d
 6t j	 |  d 6t j	 |  d 6} d i | d 6} t
 t j |    n  d } } } } d S)z.Does a google source produce expected results?z	some namer   spreadsheet_namer   Q%(py6)s
{%(py6)s = %(py2)s
{%(py2)s = %(py0)s.get_file_id
}(%(py4)s)
} == %(py8)snamer   r   r   r   r   r    r!   assert %(py10)sr#   N)r   )r5   r7   )r   r$   r   GOOGLE_SOURCEr%   r&   r'   r(   r)   r*   r+   r,   )r6   r-   r.   r/   r0   r1   r2   r   r   r   test_get_file_id_googleN   s    	        r9   c              C   sd   t  j t   d d g d  d }  |  sZ d	 i t j |   d 6} t t j |    n  d }  d S)
z+A non-empty row fails to raise an exception   oner   Tr!   assert %(py1)spy1Nr<   )r   check_empty_rowr   r%   r*   r+   r,   )@py_assert0@py_format2r   r   r   test_raise_empty_row_fullX   s        rA   c               C   s9   t  j t j  ! t j t   d d g d  Wd QXd S)z An empty row raises an exceptionr:   r!      N)pytestraisesexEmptyRowErrorr   r>   r   r   r   r   r   test_check_empty_row_empty^   s    rG   c              C   sa   t  j t   d d	 d  d }  |  sW d
 i t j |   d 6} t t j |    n  d }  d S)z!First rule can start at top levelr:   rB   r   Tr!   assert %(py1)sr=   NrH   )r   check_skipped_sub_dirr   r%   r*   r+   r,   )r?   r@   r   r   r   test_check_depth_delta_initialf   s        rK   c               C   s6   t  j t j   t j t   d d d  Wd QXd S)z"First rule must start at top levelr:   rB   NrI   )rC   rD   rE   SkippedSubDirErrorr   rJ   r   r   r   r   r   &test_check_skipped_sub_dir_initial_badl   s    rM   c              C   sa   t  j t   d d d  d }  |  sW d	 i t j |   d 6} t t j |    n  d }  d S)
z1Rules can go downward one level in directory tree   r   rB   Tr!   assert %(py1)sr=   NrO   )r   rJ   r   r%   r*   r+   r,   )r?   r@   r   r   r   test_check_skipped_sub_dirr   s        rP   c              C   sa   t  j t   d d d  d }  |  sW d	 i t j |   d 6} t t j |    n  d }  d S)
zBCheck that the rules can skip directries going upwards in the tree
   rN   r   Tr!   assert %(py1)sr=   NrR   )r   rJ   r   r%   r*   r+   r,   )r?   r@   r   r   r   "test_check_skipped_sub_dir_upwardsx   s        rS   c               C   s?   t  j t j  ' t j t   d d d d g d  Wd QXd S)z0A row with 2 non-empty cells raises an exceptionrN   r!   twothreerB   N)rC   rD   rE   ParentWithChildErrorr   check_parent_with_childr   r   r   r   r   !test_check_parent_with_child_fail   s    rX   c               C   s&   t  j t   d d d d g d  d S)z>A row with only one non-empty cell does not raise an exceptionrN   r!   rT   rB   N)r   rW   r   r   r   r   r   test_check_parent_with_child   s    rY   c              C   s  t  j }  d d d g } |  |  } d } | | k } | st j d | f d | | f  i t j |  d 6d t j   k s t j t   r t j t   n d d 6t j |   d 6t j |  d	 6t j |  d
 6} d i | d 6} t t j	 |    n  d }  } } } } d S)z'An empty row has a depth of N-cells + 1r!   rN   r   O%(py6)s
{%(py6)s = %(py2)s
{%(py2)s = %(py0)s.get_depth
}(%(py4)s)
} == %(py9)sr   r   r   r   r    py9assert %(py11)spy11N)r   )rZ   r\   )
r   	get_depthr%   r&   r*   r'   r(   r)   r+   r,   )r-   r.   r/   @py_assert8r0   @py_format10@py_format12r   r   r   test_get_depth_emptystrings   s    	         rb   c              C   s  t  j }  d d d g } |  |  } d } | | k } | st j d | f d | | f  i t j |  d 6d t j   k s t j t   r t j t   n d d 6t j |   d	 6t j |  d
 6t j |  d 6} d i | d 6} t t j	 |    n  d }  } } } } d S)z?A non-empty row can have the non-empty cell's column identifiedr!   r;   rB   r   O%(py6)s
{%(py6)s = %(py2)s
{%(py2)s = %(py0)s.get_depth
}(%(py4)s)
} == %(py9)sr   r   r   r   r    r[   assert %(py11)sr]   N)r   )rc   rd   )
r   r^   r%   r&   r*   r'   r(   r)   r+   r,   )r-   r.   r/   r_   r0   r`   ra   r   r   r   test_get_depth   s    	         re   c            
   C   s0   t  j t j   t j t   g   Wd QXd S)z-An empty rules file should raise an exceptionN)rC   rD   rE   NoMarkerErrorr   r   r   r   r   r   r   test_lex_rules_fs_struct_empty   s    rg   c               C   s9   t  j t j  ! t j t   d d d g  Wd QXd S)z5A rules file with no marker should raise an exceptionzone
ztwo
zthree
N)rC   rD   rE   rf   r   r   r   r   r   r   r   "test_lex_rules_fs_struct_no_marker   s    rh   c              C   sG  d d d d d d g }  d d d	 g f d
 d d d d d g f d d d d d g f g } t  j t   |   } | | k } | s=t j d | f d | | f  i d t j   k s t j |  r t j |  n d d 6d t j   k s t j |  r	t j |  n d d 6} d i | d 6} t	 t j
 |    n  d } d S)z-max_depth is 4, ignore all subsequent columnszremark1
zremark2
zbegin
zone
z,two,,,comment
z,,three
r   r   r;      rB   r!   rT      r:   rU   r   %(py0)s == %(py2)stokensr   expected_resultsr   assert %(py4)sr    N)r   )rk   rn   )r   r   r   r%   r&   r'   r(   r)   r*   r+   r,   )r   rm   rl   r-   @py_format3@py_format5r   r   r   "test_lex_rules_fs_struct_max_depth   s    	     rq   c              C   sA  t    }  d d d g f d d d d g f d d	 d d d
 g f d d d g f g } t j t   |   } | | k } | s7t j d | f d | | f  i d t j   k s t j |  r t j	 |  n d d 6d t j   k s t j |  rt j	 |  n d d 6} d i | d 6} t
 t j |    n  d } d S)z!Produce the right token structurer   r   r;   ri   rB   r!   rT   rj   r:   rU      fourr   %(py0)s == %(py2)srl   r   rm   r   assert %(py4)sr    N)r   )rt   ru   )r   r   r   r   r%   r&   r'   r(   r)   r*   r+   r,   )r   rm   rl   r-   ro   rp   r   r   r   test_lex_rules_fs_struct   s    	     rv   c               C   sN   t  j t j  6 t j t   t j t j	 d  d d g  f d  Wd QXd S)z?An bad row should return a BadRow exception.  Use an empty row.patternrB   r   N)
rC   rD   rE   BadRowErrorr   raise_bad_rowr   r   FSDirDecisionTreer   r   r   r   test_raise_bad_row_exception   s
    r|   c              C   s   t  j t j t   t j t j d  d d g  f d  }  d } |  | k } | s t j	 d | f d |  | f  i t j
 |  d 6d t j   k s t j |   r t j
 |   n d d	 6} d i | d 6} t t j |    n  d } } t   d S)z<A bad row where the exception is caught should return false.rw   rB   r   Fis%(py0)s is %(py3)spy3resultr   r!   assert %(py5)spy5N)r}   )r~   r   )r   validate_rowr   r   r   r   rz   r{   r%   r&   r*   r'   r(   r)   r+   r,   r   )r   @py_assert2r-   @py_format4@py_format6r   r   r   test_validate_row_false   s       l   
r   c              C   s   t  j t j t   t j t j d  d d d g f d  }  d } |  | k } | s t j	 d | f d |  | f  i t j
 |  d 6d	 t j   k s t j |   r t j
 |   n d	 d
 6} d i | d 6} t t j |    n  d } } d S)zA good row should return true.rw   rB   r   r;   Tr}   %(py0)s is %(py3)sr   r   r   r!   assert %(py5)sr   N)r}   )r   r   )r   r   r   r   r   r   rz   r{   r%   r&   r*   r'   r(   r)   r+   r,   )r   r   r-   r   r   r   r   r   test_validate_row_true   s       l   r   c               C   s<   t  j t j  $ t j t j t j	 t
   g   Wd  QXd  S)N)rC   rD   rE   NoRulesErrorr   parse_rules_fs_structr   r{   r   r   r   r   r   r   r   'test_parse_rules_fs_struct_no_fs_struct   s    r   c              C   s%  t  j t j t j t   t t     }  |  j	 } | j
 } d d g } | | k } | st j d | f d | | f  i t j |  d 6d t j   k s t j |   r t j |   n d d	 6t j |  d
 6t j |  d 6} d i | d 6} t t j |    n  d } } } } d S)z#Is the root node's content correct?r   r;   rr   rs   r   O%(py4)s
{%(py4)s = %(py2)s
{%(py2)s = %(py0)s.decision_tree
}.tree
} == %(py7)spy7rootr   r   r    r!   assert %(py9)sr[   N)r   zone)rr   zfour)r   )r   r   )r   r   libtestMockTreer   r   r   r   r   decision_treetreer%   r&   r*   r'   r(   r)   r+   r,   )r   r-   r.   @py_assert6r/   @py_format8r`   r   r   r   &test_parse_rules_fs_struct_rootcontent   s    	 	       r   c              C   s  t  j t j t j t   t t     }  |  j	 d } t
 j } t | |  } | s@d d i t j |  d 6d t j   k s t j t  r t j t  n d d 6d t j   k s t j |  r t j |  n d d 6t j |  d	 6d
 t j   k st j t
  rt j t
  n d
 d 6} t t j |    n  d } } | j } d } | | k } | st j d | f d | | f  i d t j   k st j |  rt j |  n d d 6t j |  d 6t j |  d 6} d i | d 6} t t j |    n  d } } } | j } d } | | k } | st j d | f d | | f  i d t j   k st j |  rt j |  n d d 6t j |  d 6t j |  d 6} d i | d 6} t t j |    n  d } } } | j } | j } d g }	 | |	 k } | st j d  | f d! | |	 f  i t j |	  d 6d t j   k sqt j |  rt j |  n d d 6t j |  d 6t j |  d	 6} d" i | d 6}
 t t j |
    n  d } } } }	 d S)#zIs the first dir correct?r   r!   zPassert %(py6)s
{%(py6)s = %(py0)s(%(py1)s, %(py4)s
{%(py4)s = %(py2)s.FSDir
})
}r   
isinstancer   first_childr=   r    r   r   Nr;   r   4%(py2)s
{%(py2)s = %(py0)s.name_pattern
} == %(py5)sr   assert %(py7)sr   .%(py2)s
{%(py2)s = %(py0)s.rownum
} == %(py5)sri   rT   O%(py4)s
{%(py4)s = %(py2)s
{%(py2)s = %(py0)s.decision_tree
}.tree
} == %(py7)sassert %(py9)sr[   )r   )r   r   )r   )r   r   )ri   ztwo)r   )r   r   )r   r   r   r   r   r   r   r   r   childrenr   rz   r   r%   r*   r'   r(   r)   r+   r,   name_patternr&   rownumr   r   )r   r   r.   r/   @py_format7r-   @py_assert4r   r   r   r`   r   r   r   %test_parse_rules_fs_struct_firstchild   sF    	     
	    |   	    |   	 	 	      r   c              C   s  t  j t j t j t   t t     }  |  j	 d } | j	 d } t
 j } t | |  } | sMd d i t j |  d 6d t j   k s t j t  r t j t  n d d 6d t j   k s t j |  r t j |  n d d	 6t j |  d
 6d t j   k st j t
  r*t j t
  n d d 6} t t j |    n  d } } | j } d } | | k } | st j d | f d | | f  i d t j   k st j |  rt j |  n d d 6t j |  d 6t j |  d 6} d i | d 6}	 t t j |	    n  d } } } | j } d } | | k } | st j d | f d | | f  i d t j   k st j |  rt j |  n d d 6t j |  d 6t j |  d 6} d i | d 6}	 t t j |	    n  d } } } | j } | j } d  g }
 | |
 k } | st j d! | f d" | |
 f  i t j |
  d 6d t j   k s~t j |  rt j |  n d d 6t j |  d 6t j |  d
 6}	 d# i |	 d 6} t t j |    n  d } } } }
 d S)$zIs the grandchild dir correct?r   ri   r!   zPassert %(py6)s
{%(py6)s = %(py0)s(%(py1)s, %(py4)s
{%(py4)s = %(py2)s.FSDir
})
}r   r   r   
grandchildr=   r    r   r   NrT   r   4%(py2)s
{%(py2)s = %(py0)s.name_pattern
} == %(py5)sr   assert %(py7)sr   .%(py2)s
{%(py2)s = %(py0)s.rownum
} == %(py5)srj   rU   O%(py4)s
{%(py4)s = %(py2)s
{%(py2)s = %(py0)s.decision_tree
}.tree
} == %(py7)sassert %(py9)sr[   )r   )r   r   )r   )r   r   )rj   zthree)r   )r   r   )r   r   r   r   r   r   r   r   r   r   r   rz   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   %test_parse_rules_fs_struct_grandchild  sH    	     
	    |   	    |   	 	 	      r   c               C   s[   t  j t j t j t t d d d d d d g   t j	 t
   t j j   Wd QXd S)zA bad rule generates an errorzremark1
zbegin
zone
z,two - (bad)
z,,three - (bad)
zfour
N)r   r   r   r{   r   r   r   r   rC   rD   r   r   r   r   r   r    test_parse_rules_fs_struct_error  s    r   c             C   s-  t  j t j t j t   d d g  f d d g  f g  |  j   \ } } t j } | |  } | sd d i d t	 j
   k s t j |  r t j |  n d d 6d t	 j
   k s t j t  r t j t  n d d	 6t j |  d
 6t j |  d 6} t t j |    n  d } } t   d S)z;Do we get multiple errors when there are multiple problems?rB   r   r:   r!   zQassert %(py5)s
{%(py5)s = %(py2)s
{%(py2)s = %(py0)s.multiple_errors
}(%(py3)s)
}errr   r   r   r   r   N)r   r   r   r   r   r   r   
readouterrmultiple_errorsr'   r(   r%   r)   r*   r+   r,   r   )capsysoutr   r-   r   r   r   r   r   !test_parse_rules_fs_struct_errors$  s    %	     
r   ),builtinsr'   _pytest.assertion.rewrite	assertionrewriter%   rC   enforcerr   rE   r   r   r   r   r   r   r   r   r   r3   r9   rA   rG   rK   rM   rP   rS   rX   rY   rb   re   rg   rh   rq   rv   r|   r   r   r   r   r   r   r   r   r   r   r   r   <module>   sJ     


