o
    ip                     @   sr  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Zd dlZd dlZd dlZd dlZd dlZzd dlmZ W n eyS   d dlZY nw 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! e"  e#d d dlm$Z$ W d   n1 sw   Y  dd Z%dd Z&dd Z'G dd deZ(G dd deZ)G dd deZ*dS )    )redirect_stderrredirect_stdout)StringIO)JSONDecodeError)Path)dedent)TestCaseN)metadata)m)Draft4ValidatorDraft202012Validator)RefResolutionErrorSchemaErrorValidationError)_LATEST_VERSIONvalidateignore)clic                     s"   t t  G  fddd}|S )Nc                       s,   e Zd Zdd Z fddZedd ZdS )z%fake_validator.<locals>.FakeValidatorc                 _      d S N )selfargskwargsr   r   T/var/www/edux/Edux_v2/venv/lib/python3.10/site-packages/jsonschema/tests/test_cli.py__init__&   s   z.fake_validator.<locals>.FakeValidator.__init__c                    s    r   S g S r   )popr   instanceerrorsr   r   iter_errors)   s   z1fake_validator.<locals>.FakeValidator.iter_errorsc                 S   r   r   r   r   schemar   r   r   check_schema.   s   z2fake_validator.<locals>.FakeValidator.check_schemaN)__name__
__module____qualname__r   r!   classmethodr$   r   r   r   r   FakeValidator%   s
    r)   )listreversed)r    r)   r   r   r   fake_validator"   s   r,   c                    s    fdd}|S )Nc                    s"     | }|d u rt| t|S r   )getFileNotFoundErrorr   )pathcontentsall_contentsr   r   open6   s   
zfake_open.<locals>.openr   )r2   r3   r   r1   r   	fake_open5   s   r4   c              
   C   sB   z
t |  W td ty  } z
t|W  Y d }~S d }~ww )Nz.Tried and failed to capture a JSON dump error.)jsonloadsr   strRuntimeError)non_jsonerrorr   r   r   _message_for>   s   r;   c                   @   s  e Zd Ze e df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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d1d2 Zd3d4 Zd5d6 Zd7d8 Zd9d: Z d;d< Z!d=d> Z"d?d@ Z#dAdB Z$dCdD Z%dEdF Z&dGdH Z'dIdJ Z(dKdL Z)dMdN Z*dOdP Z+dQdR Z,dSdT Z-dUdV Z.dWdX Z/dYdZ Z0d[d\ Z1d]S )_TestCLIr   c           
      K   s   t |}|| | tt d t|t _zt t }}t j||||d}	W t `nt `w | j	|	|t
d||	| | d | | fS )Nr3   )stdinstdoutstderrz
                    Expected an exit code of {} != {}.

                    stdout: {}

                    stderr: {}
                )msg)r   
parse_argsupdateassertFalsehasattrr4   r3   r   runassertEqualr   formatgetvalue)
r   argvfilesr=   	exit_codeoverride	argumentsr>   r?   actual_exit_coder   r   r   run_cliH   s2   


zTestCLI.run_cli c                 K   s(   |  | jdi |t|t|f d S )Nr   )rF   rO   r   )r   r>   r?   r   r   r   r   assertOutputsm   s   zTestCLI.assertOutputsc                 C   >   t ddd}| jtdt|jdt|gg dddd	 d S )
NI am an error!   r   *{"does not": "matter since it is stubbed"}some_schemasome_instance-irY   rX      12: I am an error!
rJ   	validatorrI   rK   r?   r   rQ   dictr5   dumpsr   r,   r   r:   r   r   r   test_invalid_instances      

zTestCLI.test_invalid_instancec                 C   rR   )
NrS   rT   rU   rV   rW   r[   rY   --outputprettyrX   r\   z                ===[ValidationError]===(some_instance)===

                I am an error!
                -----------------------------
            r^   r`   rc   r   r   r   #test_invalid_instance_pretty_output   re   z+TestCLI.test_invalid_instance_pretty_outputc                 C   rR   )
NrS   rT   rU   rV   rW   )rg   plainr[   rY   rX   r\   r]   r^   r`   rc   r   r   r   +test_invalid_instance_explicit_plain_output   re   z3TestCLI.test_invalid_instance_explicit_plain_outputc                 C   N   d}t d|d}t d|d}| jtdt|dt||gg ddd	d
 d S )NrT   First errorrU   Second errorrV   rW   rZ   r\   zM                12: First error
                12: Second error
            r^   r   rQ   ra   r5   rb   r,   r   r   firstsecondr   r   r   %test_invalid_instance_multiple_errors      

z-TestCLI.test_invalid_instance_multiple_errorsc                 C   rl   )NrT   rm   rU   rn   rV   rW   rf   r\   a                  ===[ValidationError]===(some_instance)===

                First error
                -----------------------------
                ===[ValidationError]===(some_instance)===

                Second error
                -----------------------------
            r^   ro   rp   r   r   r   3test_invalid_instance_multiple_errors_pretty_output   rt   z;TestCLI.test_invalid_instance_multiple_errors_pretty_outputc                 C   f   d}t d|dt d|dg}d}t d|dg}| jtdt|t|dt||g d	d
dd d S )NrT   An errorrU   Another errorfooBOOMrV   rX   some_first_instancesome_second_instance)r[   r|   r[   r}   rX   r\   ze                12: An error
                12: Another error
                foo: BOOM
            r^   ro   r   first_instancefirst_errorssecond_instancesecond_errorsr   r   r   test_multiple_invalid_instances   s"   


z'TestCLI.test_multiple_invalid_instancesc                 C   rv   )NrT   rw   rU   rx   ry   rz   rV   r{   )rg   rh   r[   r|   r[   r}   rX   r\   a                  ===[ValidationError]===(some_first_instance)===

                An error
                -----------------------------
                ===[ValidationError]===(some_first_instance)===

                Another error
                -----------------------------
                ===[ValidationError]===(some_second_instance)===

                BOOM
                -----------------------------
            r^   ro   r~   r   r   r   -test_multiple_invalid_instances_pretty_output   "   


z5TestCLI.test_multiple_invalid_instances_pretty_outputc                 C   rv   )NrT   rw   rU   rx   ry   rz   rV   r{   )--error-format&:{error.message}._-_.{error.instance}:r[   r|   r[   r}   rX   r\   z5:An error._-_.12::Another error._-_.12::BOOM._-_.foo:r^   ro   r~   r   r   r   test_custom_error_format  r   z TestCLI.test_custom_error_formatc                 C      | j tdddgddd d S )Nz{"type": 12}rX   rX   r\   O                12: 12 is not valid under any of the given schemas
            rJ   rI   rK   r?   rQ   ra   r   r   r   r   test_invalid_schema9     
zTestCLI.test_invalid_schemac                 C   sz   ddi}|  t}t|dd W d    n1 sw   Y  t|j}| jtt|dg dddt| d	 d
 d S )NtyperT   rP   r#   r   r   rg   rh   rX   r\   %===[SchemaError]===(some_schema)===


-----------------------------
r   	assertRaisesr   r   r7   	exceptionrQ   ra   r5   rb   r   r#   er:   r   r   r   !test_invalid_schema_pretty_outputD  s   

z)TestCLI.test_invalid_schema_pretty_outputc                 C   r   )Nz{"type": 12, "items": 57}r   rX   r\   zF                57: 57 is not of type 'object', 'boolean'
            r   r   r   r   r   r   #test_invalid_schema_multiple_errorsW  r   z+TestCLI.test_invalid_schema_multiple_errorsc                 C   s|   ddd}|  t}t|dd W d    n1 sw   Y  t|j}| jtt|dg ddd	t| d
 d d S )NrT   9   )r   itemsrP   r   r   r   r\   r   r   r   r   r   r   r   r   1test_invalid_schema_multiple_errors_pretty_outputb  s   


z9TestCLI.test_invalid_schema_multiple_errors_pretty_outputc                 C   s"   | j tdddg dddd dS )	zv
        "Validating" an instance that's invalid under an invalid schema
        just shows the schema error.
        z{"type": 12, "minimum": 30}13rW   rZ   r\   r   r   Nr   r   r   r   r   )test_invalid_schema_with_invalid_instanceu  s   
z1TestCLI.test_invalid_schema_with_invalid_instancec                 C   s   dddd}}|  t}t||d W d    n1 sw   Y  t|j}| jtt|t|dg ddd	t| d
 d d S )N   rT      r   minimumr   rW   rg   rh   r[   rY   rX   r\   r   r   r   r   )r   r   r#   r   r:   r   r   r   7test_invalid_schema_with_invalid_instance_pretty_output  s$   

z?TestCLI.test_invalid_schema_with_invalid_instance_pretty_outputc                 C   s.   | j tddddg dddtdd d S )	Nz{"minimum": 30}not valid JSON!12)rX   r   r   )r[   r   r[   r   rX   r\   zw                Failed to parse 'first_instance': {}
                12: 12 is less than the minimum of 30
            r   rQ   ra   rG   r;   r   r   r   r   -test_invalid_instance_continues_with_the_rest  s   

z5TestCLI.test_invalid_instance_continues_with_the_restc                 C   sl   dddd}}|  t t||d W d    n1 sw   Y  | jtt|dg ddd	d
 d S )Nr   rT   r   r   r   r   )r   r   rX   r\   z7:12 is not valid under any of the given schemas._-_.12:r   )r   r   r   rQ   ra   r5   rb   )r   r   r#   r   r   r   1test_custom_error_format_applies_to_schema_errors  s   
z9TestCLI.test_custom_error_format_applies_to_schema_errorsc                 C   s0   d}| j td|dg dddt|d d S )Nr   {}rW   rZ   r\   z@                Failed to parse 'some_instance': {}
            r   r   r   r   r   r   test_instance_is_invalid_JSON  s   


z%TestCLI.test_instance_is_invalid_JSONc                 C   sF   | j tdddg ddd\}}| | | d| | d| d S )	Nr   r   rW   r   r\   rJ   rI   rK   z7(some_instance)===

Traceback (most recent call last):
rX   rO   ra   rC   assertInassertNotInr   r>   r?   r   r   r   +test_instance_is_invalid_JSON_pretty_output  s   


z3TestCLI.test_instance_is_invalid_JSON_pretty_outputc              	   C   s2   d}| j tddt|dgddt|d d S )Nr   r   r   rX   r\   z8                Failed to parse <stdin>: {}
            )rJ   r=   rI   rK   r?   )rQ   ra   r   rG   r;   r   r   r   r   &test_instance_is_invalid_JSON_on_stdin  s   

z.TestCLI.test_instance_is_invalid_JSON_on_stdinc                 C   sJ   | j tddtdg ddd\}}| | | d| | d| d S )	Nr   r   r   r   r\   )rJ   r=   rI   rK   z1(<stdin>)===

Traceback (most recent call last):
rX   )rO   ra   r   rC   r   r   r   r   r   r   4test_instance_is_invalid_JSON_on_stdin_pretty_output  s   

z<TestCLI.test_instance_is_invalid_JSON_on_stdin_pretty_outputc                 C   s,   d}| j t|ddgddt|d d S )Nr   r   rX   r\   >                Failed to parse 'some_schema': {}
            r   r   r"   r   r   r   test_schema_is_invalid_JSON  s   

z#TestCLI.test_schema_is_invalid_JSONc                 C   s8   | j tddg ddd\}}| | | d| d S )Nr   r   r   r\   r   5(some_schema)===

Traceback (most recent call last):
)rO   ra   rC   r   r   r   r   r   )test_schema_is_invalid_JSON_pretty_output  s   

z1TestCLI.test_schema_is_invalid_JSON_pretty_outputc                 C   s2   d\}}| j t||ddgddt|d dS )M
        Only the schema error is reported, as we abort immediately.
        )r   also not valid JSON!rW   rX   r\   r   r   Nr   r   r#   r   r   r   r   .test_schema_and_instance_are_both_invalid_JSON  s   


z6TestCLI.test_schema_and_instance_are_both_invalid_JSONc                 C   sF   | j tdddg ddd\}}| | | d| | d| d	S )
r   r   r   rW   r   r\   r   r   rY   Nr   r   r   r   r   <test_schema_and_instance_are_both_invalid_JSON_pretty_output-  s   


zDTestCLI.test_schema_and_instance_are_both_invalid_JSON_pretty_outputc                 C       | j tddg dddd d S )Nr   r   )r[   nonexisting_instancerX   r\   zC                'nonexisting_instance' does not exist.
            r   r   r   r   r   r   test_instance_does_not_existB  s   
z$TestCLI.test_instance_does_not_existc                 C   r   )Nr   r   )rg   rh   r[   r   rX   r\   z                ===[FileNotFoundError]===(nonexisting_instance)===

                'nonexisting_instance' does not exist.
                -----------------------------
            r   r   r   r   r   r   *test_instance_does_not_exist_pretty_outputM  s   
z2TestCLI.test_instance_does_not_exist_pretty_outputc                 C   s   | j dgddd d S )Nnonexisting_schemar\   %'nonexisting_schema' does not exist.
rI   rK   r?   rQ   r   r   r   r   test_schema_does_not_exist_  s
   
z"TestCLI.test_schema_does_not_existc                 C      | j g dddd d S )N)rg   rh   r   r\                   ===[FileNotFoundError]===(nonexisting_schema)===

                'nonexisting_schema' does not exist.
                -----------------------------
            r   r   r   r   r   r   (test_schema_does_not_exist_pretty_outputg  
   
z0TestCLI.test_schema_does_not_exist_pretty_outputc                 C   r   )N)r[   r   r   r\   r   r   r   r   r   r   r   &test_neither_instance_nor_schema_existt  r   z.TestCLI.test_neither_instance_nor_schema_existc                 C   r   )N)rg   rh   r[   r   r   r\   r   r   r   r   r   r   r   4test_neither_instance_nor_schema_exist_pretty_output|  s
   
z<TestCLI.test_neither_instance_nor_schema_exist_pretty_outputc                 C   "   | j tdddg dddd d S Nr   rW   rZ   rP   rJ   rI   r>   r?   r   r   r   r   r   test_successful_validation     

z"TestCLI.test_successful_validationc                 C   "   | j tdddg dddd d S Nr   rW   r   z"===[SUCCESS]===(some_instance)===
rP   r   r   r   r   r   r   (test_successful_validation_pretty_output  r   z0TestCLI.test_successful_validation_pretty_outputc                 C   s$   | j tddtddgddd d S )Nr   r   rX   rP   rJ   r=   rI   r>   r?   rQ   ra   r   r   r   r   r   #test_successful_validation_of_stdin  s   
z+TestCLI.test_successful_validation_of_stdinc                 C   s&   | j tddtdg dddd d S )Nr   r   r   z===[SUCCESS]===(<stdin>)===
rP   r   r   r   r   r   r   1test_successful_validation_of_stdin_pretty_output  s   
z9TestCLI.test_successful_validation_of_stdin_pretty_outputc                 C   r   r   r   r   r   r   r   -test_successful_validation_of_just_the_schema  r   z5TestCLI.test_successful_validation_of_just_the_schemac                 C   r   r   r   r   r   r   r   ;test_successful_validation_of_just_the_schema_pretty_output  r   zCTestCLI.test_successful_validation_of_just_the_schema_pretty_outputc                 C   sx   t jdd}|  | tj|j t|j}|d d|j d}| j	t
|dddd	d
|j d dgddd d S )NFdelete-{"definitions": {"num": {"type": "integer"}}}
{"$ref": "#definitions/num"}1rW   r[   rY   
--base-uri/rX   rP   r   tempfileNamedTemporaryFileclose
addCleanuposremovenamer   
write_textrQ   ra   parentas_urir   ref_schema_fileref_pathr#   r   r   r   0test_successful_validation_via_explicit_base_uri  s   



z8TestCLI.test_successful_validation_via_explicit_base_uric                 C   sz   t jdd}|  | tj|j t|j}|d d|j d}| j	t
|dddd	d
|j d dgdddd d S )NFr   r   r   r   z"1"rW   r[   rY   r   r   rX   r\   rP   z 1: '1' is not of type 'integer'
rJ   rI   rK   r>   r?   r   r   r   r   r   2test_unsuccessful_validation_via_explicit_base_uri  s    



z:TestCLI.test_unsuccessful_validation_via_explicit_base_uric                 C   s|   d}d}|  t}| jt||ddddt  dgd W d    n1 s(w   Y  t|j}| 	t
j d	| d S )
Nz4{"$ref": "someNonexistentFile.json#definitions/num"}r   rW   r[   rY   r   rX   rJ   rI   zsomeNonexistentFile.json')r   r   rQ   ra   r   cwdr   r7   r   r   r   sepr   r#   r   r   r:   r   r   r   ,test_nonexistent_file_with_explicit_base_uri  s    
z4TestCLI.test_nonexistent_file_with_explicit_base_uric                 C   sf   d}d}|  t}| jt||dg dd W d    n1 s!w   Y  t|j}| |d d S )Nz${"$ref": "foo.json#definitions/num"}r   rW   )r[   rY   r   znot@UR1rX   r   zunknown url type: 'foo.json')r   r   rQ   ra   r7   r   rF   r   r   r   r   test_invalid_exlicit_base_uri  s   
z%TestCLI.test_invalid_exlicit_base_uric                 C   s0   |  tt | jtdddg ddddd d S )	Nz{"const": "check"}z"a"rW   rZ   r\   rP   za: 'check' was expected
r   )assertIsr   r   rQ   ra   r   r   r   r   =test_it_validates_using_the_latest_validator_when_unspecified  s   

zETestCLI.test_it_validates_using_the_latest_validator_when_unspecifiedc                 C   s,   d}d}| j t||dg ddddd d	S )
zG
        Specifically, `const` validation applies for Draft 7.
        z
            {
                "$schema": "http://json-schema.org/draft-07/schema#",
                "const": "check"
            }
        "foo"rW   rZ   r\   rP   zfoo: 'check' was expected
r   Nr   r   r   r   r   -test_it_validates_using_draft7_when_specified"  s   

z5TestCLI.test_it_validates_using_draft7_when_specifiedc                 C   s*   d}d}| j t||dg dddd dS )zP
        Specifically, `const` validation *does not* apply for Draft 4.
        z
            {
                "$schema": "http://json-schema.org/draft-04/schema#",
                "const": "check"
            }
            r   rW   rZ   rP   r   Nr   r   r   r   r   -test_it_validates_using_draft4_when_specified5  s   

z5TestCLI.test_it_validates_using_draft4_when_specifiedN)rP   rP   )2r%   r&   r'   r
   r   rO   rQ   rd   ri   rk   rs   ru   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   r   r   r   r   r   r   r   r   r   r   r   r   r<   G   s^    


%)		r<   c                   @   s:   e Zd Ze Zdd Zdd Zdd Zdd Zd	d
 Z	dS )
TestParserc                 C   s$   t g d}| |d | j d S )N)--validatorz2jsonschema.tests.test_cli.TestParser.FakeValidator
--instancemem://some/instancemem://some/schemar_   )r   rA   r   r)   r   rM   r   r   r   2test_find_validator_by_fully_qualified_object_nameL  s   z=TestParser.test_find_validator_by_fully_qualified_object_namec                 C   s"   t g d}| |d t d S )N)r  r   r  r  r  r_   )r   rA   r   r   r  r   r   r   !test_find_validator_in_jsonschemaW  s   z,TestParser.test_find_validator_in_jsonschemac              
   G   s   t  t  }}t|6 t|" | t t| W d    n1 s&w   Y  W d    n1 s5w   Y  W d    n1 sDw   Y  | | fS r   )r   r   r   r   
SystemExitr   rA   rH   )r   rI   r>   r?   r   r   r   cli_output_fora  s    zTestParser.cli_output_forc                 C   s,   |  ddd\}}| d| | | d S )Nrg   ry   r  zinvalid choice: 'foo'r
  r   rC   r   r   r   r   test_unknown_outputh  s   zTestParser.test_unknown_outputc                 C   s0   |  ddddd\}}| d| | | d S )Nrg   rh   r   ry   r  z3--error-format can only be used with --output plainr  r   r   r   r   test_useless_error_formatp  s   z$TestParser.test_useless_error_formatN)
r%   r&   r'   r,   r)   r  r  r
  r  r  r   r   r   r   r  H  s    
r  c                   @   s$   e Zd Zdd Zdd Zdd ZdS )TestCLIIntegrationc                 C   s,   t jtjddddgt jd}| d| d S )N-mpipshow
jsonschemar?   s   License: MIT)
subprocesscheck_outputsys
executableSTDOUTr   )r   outputr   r   r   test_license~  s
   zTestCLIIntegration.test_licensec                 C   sB   t jtjdddddgt jd}|d }| |t	d d S )Nz-Wr   r  r  z	--versionr  zutf-8)
r  r  r  r  r  decodestriprF   r	   version)r   r  r   r   r   test_version  s   zTestCLIIntegration.test_versionc                 C   sB   t jtjddgt jd}t jtjdddgt jd}| || d S )Nr  r  r  z--help)r  r  r  r  r  rF   )r   r  output_for_helpr   r   r   #test_no_arguments_shows_usage_notes  s   
z6TestCLIIntegration.test_no_arguments_shows_usage_notesN)r%   r&   r'   r  r  r   r   r   r   r   r  }  s    r  )+
contextlibr   r   ior   r5   r   pathlibr   textwrapr   unittestr   r   r  r  r   warnings	importlibr	   ImportErrorimportlib_metadata
pyrsistentr
   r  r   r   jsonschema.exceptionsr   r   r   jsonschema.validatorsr   r   catch_warningssimplefilterr   r,   r4   r;   r<   r  r  r   r   r   r   <module>   sJ    

		      5