o
    mio                     @   sF  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 d dlZd dlZd dlZd dlZd dlZd dl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 sxw   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)metadata)StringIO)JSONDecodeError)Path)dedent)TestCaseN)Draft4ValidatorDraft202012Validator)SchemaErrorValidationError_RefResolutionError)_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   S/home/kim/smarthome/.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   open0   s   
zfake_open.<locals>.openr   )r1   r2   r   r0   r   	fake_open/   s   r3   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)Znon_jsonerrorr   r   r   _message_for8   s   r9   c                   @   s  e Zd Zd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d0d1 Zd2d3 Zd4d5 Zd6d7 Zd8d9 Zd:d; Zd<d= Z d>d? Z!d@dA Z"dBdC Z#dDdE Z$dFdG Z%dHdI Z&dJdK Z'dLdM Z(dNdO Z)dPdQ Z*dRdS Z+dTdU Z,dVdW Z-dXdY Z.dZd[ Z/d\d] Z0dS )_TestCLINr   c           
      K   s   t |}|| | tt d t|pi t _zt t }}t j||||d}	W t `nt `w | j	|	|t
d| d|	 d|  d|  d	d | | fS )	Nr2   )stdinstdoutstderrz.
                    Expected an exit code of z != z.

                    stdout: z

                    stderr: z
                )msg)r   
parse_argsupdateassertFalsehasattrr3   r2   r   runassertEqualr   getvalue)
r   argvfilesr;   	exit_codeoverride	argumentsr<   r=   Zactual_exit_coder   r   r   run_cliB   s6   

zTestCLI.run_cli c                 K   s(   |  | jdi |t|t|f d S )Nr   )rD   rK   r   )r   r<   r=   r   r   r   r   assertOutputsb   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-irU   rT      12: I am an error!
rG   	validatorrF   rH   r=   r   rM   dictr4   dumpsr   r+   r   r8   r   r   r   test_invalid_instanceh      

zTestCLI.test_invalid_instancec                 C   rN   )
NrO   rP   rQ   rR   rS   rW   rU   --outputprettyrT   rX   z                ===[ValidationError]===(some_instance)===

                I am an error!
                -----------------------------
            rZ   r\   r_   r   r   r   #test_invalid_instance_pretty_outputw   ra   z+TestCLI.test_invalid_instance_pretty_outputc                 C   rN   )
NrO   rP   rQ   rR   rS   )rc   plainrW   rU   rT   rX   rY   rZ   r\   r_   r   r   r   +test_invalid_instance_explicit_plain_output   ra   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 )NrP   First errorrQ   Second errorrR   rS   rV   rX   zM                12: First error
                12: Second error
            rZ   r   rM   r]   r4   r^   r+   r   r   firstsecondr   r   r   %test_invalid_instance_multiple_errors      

z-TestCLI.test_invalid_instance_multiple_errorsc                 C   rh   )NrP   ri   rQ   rj   rR   rS   rb   rX   a                  ===[ValidationError]===(some_instance)===

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

                Second error
                -----------------------------
            rZ   rk   rl   r   r   r   3test_invalid_instance_multiple_errors_pretty_output   rp   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 )NrP   An errorrQ   Another errorfooBOOMrR   rT   some_first_instancesome_second_instance)rW   rx   rW   ry   rT   rX   ze                12: An error
                12: Another error
                foo: BOOM
            rZ   rk   r   first_instanceZfirst_errorssecond_instanceZsecond_errorsr   r   r   test_multiple_invalid_instances   s"   


z'TestCLI.test_multiple_invalid_instancesc                 C   rr   )NrP   rs   rQ   rt   ru   rv   rR   rw   )rc   rd   rW   rx   rW   ry   rT   rX   a                  ===[ValidationError]===(some_first_instance)===

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

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

                BOOM
                -----------------------------
            rZ   rk   rz   r   r   r   -test_multiple_invalid_instances_pretty_output   "   


z5TestCLI.test_multiple_invalid_instances_pretty_outputc                 C   rr   )NrP   rs   rQ   rt   ru   rv   rR   rw   )--error-format&:{error.message}._-_.{error.instance}:rW   rx   rW   ry   rT   rX   z5:An error._-_.12::Another error._-_.12::BOOM._-_.foo:rZ   rk   rz   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}rT   rT   rX   O                12: 12 is not valid under any of the given schemas
            rG   rF   rH   r=   rM   r]   r   r   r   r   test_invalid_schema.     
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typerP   rL   r"   r   r   rc   rd   rT   rX   %===[SchemaError]===(some_schema)===


-----------------------------
r   	assertRaisesr   r   r6   	exceptionrM   r]   r4   r^   r   r"   er8   r   r   r   !test_invalid_schema_pretty_output9  s   

z)TestCLI.test_invalid_schema_pretty_outputc                 C   r   )Nz{"type": 12, "items": 57}r   rT   rX   zF                57: 57 is not of type 'object', 'boolean'
            r   r   r   r   r   r   #test_invalid_schema_multiple_errorsL  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 )NrP   9   )r   itemsrL   r   r   r   rX   r   r   r   r   r   r   r   r   1test_invalid_schema_multiple_errors_pretty_outputW  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}Z13rS   rV   rX   r   r   Nr   r   r   r   r   )test_invalid_schema_with_invalid_instancej  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   rP      r   minimumr   rS   rc   rd   rW   rU   rT   rX   r   r   r   r   )r   r   r"   r   r8   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!Z12)rT   r{   r|   )rW   r{   rW   r|   rT   rX   zw                Failed to parse 'first_instance': {}
                12: 12 is less than the minimum of 30
            r   )rM   r]   formatr9   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   rP   r   r   r   r   )r   r   rT   rX   z7:12 is not valid under any of the given schemas._-_.12:r   )r   r   r   rM   r]   r4   r^   )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   s2   d}| j td|dg dddt| dd d S )	Nr   {}rS   rV   rX   z1                Failed to parse 'some_instance': 
            r   rM   r]   r9   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   rS   r   rX   rG   rF   rH   z7(some_instance)===

Traceback (most recent call last):
rT   rK   r]   rA   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   s4   d}| j tddt|dgddt| dd d S )	Nr   r   r   rT   rX   z)                Failed to parse <stdin>: r   )rG   r;   rF   rH   r=   )rM   r]   r   r9   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   rX   )rG   r;   rF   rH   z1(<stdin>)===

Traceback (most recent call last):
rT   )rK   r]   r   rA   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 d S )Nr   r   rT   rX   /                Failed to parse 'some_schema': r   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   rX   r   5(some_schema)===

Traceback (most recent call last):
)rK   r]   rA   r   r   r   r   r   )test_schema_is_invalid_JSON_pretty_output  s   

z1TestCLI.test_schema_is_invalid_JSON_pretty_outputc                 C   s4   d\}}| j t||ddgddt| dd dS )	M
        Only the schema error is reported, as we abort immediately.
        )r   also not valid JSON!rS   rT   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   rS   r   rX   r   r   rU   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   )rW   nonexisting_instancerT   rX   zC                'nonexisting_instance' does not exist.
            r   r   r   r   r   r   test_instance_does_not_exist7  s   
z$TestCLI.test_instance_does_not_existc                 C   r   )Nr   r   )rc   rd   rW   r   rT   rX   z                ===[FileNotFoundError]===(nonexisting_instance)===

                'nonexisting_instance' does not exist.
                -----------------------------
            r   r   r   r   r   r   *test_instance_does_not_exist_pretty_outputB  s   
z2TestCLI.test_instance_does_not_exist_pretty_outputc                 C   s   | j dgddd d S )Nnonexisting_schemarX   %'nonexisting_schema' does not exist.
rF   rH   r=   rM   r   r   r   r   test_schema_does_not_existT  s
   
z"TestCLI.test_schema_does_not_existc                 C      | j g dddd d S )N)rc   rd   r   rX                   ===[FileNotFoundError]===(nonexisting_schema)===

                'nonexisting_schema' does not exist.
                -----------------------------
            r   r   r   r   r   r   (test_schema_does_not_exist_pretty_output\  
   
z0TestCLI.test_schema_does_not_exist_pretty_outputc                 C   r   )N)rW   r   r   rX   r   r   r   r   r   r   r   &test_neither_instance_nor_schema_existi  r   z.TestCLI.test_neither_instance_nor_schema_existc                 C   r   )N)rc   rd   rW   r   r   rX   r   r   r   r   r   r   r   4test_neither_instance_nor_schema_exist_pretty_outputq  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   rS   rV   rL   rG   rF   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   rS   r   z"===[SUCCESS]===(some_instance)===
rL   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   rT   rL   rG   r;   rF   r<   r=   rM   r]   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>)===
rL   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"}1rS   rW   rU   
--base-uri/rT   rL   r   tempfileNamedTemporaryFilecloseZ
addCleanuposremovenamer   
write_textrM   r]   parentas_urir   Zref_schema_fileZ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"rS   rW   rU   r   r   rT   rX   rL   z 1: '1' is not of type 'integer'
rG   rF   rH   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   rS   rW   rU   r   rT   rG   rF   zsomeNonexistentFile.json')r   r   rM   r]   r   cwdr   r6   r   r   r   sepr   r"   r   r   r8   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   rS   )rW   rU   r   znot@UR1rT   r   zunknown url type: 'foo.json')r   r   rM   r]   r6   r   rD   r   r   r   r   test_invalid_explicit_base_uri  s   
z&TestCLI.test_invalid_explicit_base_uric                 C   s0   |  tt | jtdddg ddddd d S )	Nz{"const": "check"}z"a"rS   rV   rX   rL   za: 'check' was expected
r   )assertIsr   r   rM   r]   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"rS   rV   rX   rL   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   rS   rV   rL   r   Nr   r   r   r   r   -test_it_validates_using_draft4_when_specified*  s   

z5TestCLI.test_it_validates_using_draft4_when_specified)rL   rL   )1r$   r%   r&   r   rK   rM   r`   re   rg   ro   rq   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:   A   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   r?   r   r(   r   rJ   r   r   r   2test_find_validator_by_fully_qualified_object_nameA  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   r?   r   r
   r   r   r   r   !test_find_validator_in_jsonschemaL  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   r?   rE   )r   rF   r<   r=   r   r   r   cli_output_forV  s    zTestParser.cli_output_forc                 C   s,   |  ddd\}}| d| | | d S )Nrc   ru   r   zinvalid choice: 'foo'r  r   rA   r   r   r   r   test_unknown_output]  s   zTestParser.test_unknown_outputc                 C   s0   |  ddddd\}}| d| | | d S )Nrc   rd   r   ru   r   z3--error-format can only be used with --output plainr  r   r   r   r   test_useless_error_formate  s   z$TestParser.test_useless_error_formatN)
r$   r%   r&   r+   r(   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 )TestCLIIntegrationc                 C   s    t  d}| |dd d S )N
jsonschemazLicense-ExpressionMIT)r   rD   r,   )r   Zour_metadatar   r   r   test_licenses  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   -mr  z	--versionr=   zutf-8)

subprocesscheck_outputsys
executableSTDOUTdecodestriprD   r   version)r   r  r   r   r   test_versionw  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  rD   )r   outputZ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  r  s    r  )'
contextlibr   r   	importlibr   ior   r4   r   pathlibr   textwrapr   Zunittestr	   r   r  r  r   warningsr  r
   r   Zjsonschema.exceptionsr   r   r   Zjsonschema.validatorsr   r   catch_warningssimplefilterr   r+   r3   r9   r:   r   r  r   r   r   r   <module>   s@    

		      5