HEX
Server: Apache
System: Linux 185.122.168.184.host.secureserver.net 5.14.0-570.60.1.el9_6.x86_64 #1 SMP PREEMPT_DYNAMIC Wed Nov 5 05:00:59 EST 2025 x86_64
User: barbeatleanalyti (1024)
PHP: 8.1.33
Disabled: NONE
Upload Files
File: //var/opt/nydus/ops/jsonschema/tests/__pycache__/test_cli.cpython-39.pyc
a

�,�h�o�@sFddlmZmZddlmZddlmZddlmZddl	m
Z
ddlmZddl
mZddlZddlZddlZddlZddlZddlZdd	lmZmZdd
lmZmZmZddlmZmZe���&e�d�dd
lm Z Wd�n1s�0Ydd�Z!dd�Z"dd�Z#Gdd�de�Z$Gdd�de�Z%Gdd�de�Z&dS)�)�redirect_stderr�redirect_stdout)�metadata)�StringIO)�JSONDecodeError)�Path)�dedent)�TestCaseN)�Draft4Validator�Draft202012Validator)�SchemaError�ValidationError�_RefResolutionError)�_LATEST_VERSION�validate�ignore)�clics"tt����G�fdd�d�}|S)Ncs,eZdZdd�Z�fdd�Zedd��ZdS)z%fake_validator.<locals>.FakeValidatorc_sdS�N�)�self�args�kwargsrr�J/opt/nydus/tmp/pip-target-wkfpz8uv/lib/python/jsonschema/tests/test_cli.py�__init__ sz.fake_validator.<locals>.FakeValidator.__init__cs�r���SgSr)�pop�r�instance��errorsrr�iter_errors#sz1fake_validator.<locals>.FakeValidator.iter_errorscSsdSrr�r�schemarrr�check_schema(sz2fake_validator.<locals>.FakeValidator.check_schemaN)�__name__�
__module__�__qualname__rr�classmethodr"rrrr�
FakeValidatorsr')�list�reversed)rr'rrr�fake_validators
r*cs�fdd�}|S)Ncs"��|�}|durt|��t|�Sr)�get�FileNotFoundErrorr)�path�contents��all_contentsrr�open0s
zfake_open.<locals>.openr)r0r1rr/r�	fake_open/sr2c
CsHzt�|�Wn,ty:}zt|�WYd}~Sd}~00td��dS)Nz.Tried and failed to capture a JSON dump error.)�json�loadsr�str�RuntimeError)Znon_json�errorrrr�_message_for8s
r8c@s�eZdZde�dfdd�Zd^dd�Zdd	�Zd
d�Zdd
�Zdd�Z	dd�Z
dd�Zdd�Zdd�Z
dd�Zdd�Zdd�Zdd�Zd d!�Zd"d#�Zd$d%�Zd&d'�Zd(d)�Zd*d+�Zd,d-�Zd.d/�Zd0d1�Zd2d3�Zd4d5�Zd6d7�Zd8d9�Zd:d;�Zd<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]�Z0dS)_�TestCLINrc

Ks�t�|�}|�|�|�ttd��t|p,i�t_z(t�t�}}tj||||d�}	Wt`nt`0|j	|	|t
d|�d|	�d|���d|���d�	�d�|��|��fS)	Nr1)�stdin�stdout�stderrz.
                    Expected an exit code of z != z.

                    stdout: z

                    stderr: z
                )�msg)r�
parse_args�update�assertFalse�hasattrr2r1r�run�assertEqualr�getvalue)
r�argv�filesr:�	exit_code�override�	argumentsr;r<Zactual_exit_coderrr�run_cliBs6

�������zTestCLI.run_cli�cKs(|�|jfi|��t|�t|�f�dSr)rCrJr)rr;r<rrrr�
assertOutputsbs�zTestCLI.assertOutputscCs>tddd�}|jtdt�|j�d�t|g�gd�ddd	�dS)
N�I am an error!��r�*{"does not": "matter since it is stubbed"}��some_schema�
some_instance��-irSrR��12: I am an error!
�rF�	validatorrErGr<�r
rL�dictr3�dumpsrr*�rr7rrr�test_invalid_instancehs
��zTestCLI.test_invalid_instancecCs>tddd�}|jtdt�|j�d�t|g�gd�ddd	�dS)
NrMrNrOrPrQ�rUrS�--output�prettyrRrVz�                ===[ValidationError]===(some_instance)===

                I am an error!
                -----------------------------
            rXrZr]rrr�#test_invalid_instance_pretty_outputws
��z+TestCLI.test_invalid_instance_pretty_outputcCs>tddd�}|jtdt�|j�d�t|g�gd�ddd	�dS)
NrMrNrOrPrQ)r`�plainrUrSrRrVrWrXrZr]rrr�+test_invalid_instance_explicit_plain_output�s
��z3TestCLI.test_invalid_instance_explicit_plain_outputcCsNd}td|d�}td|d�}|jtdt�|�d�t||g�gd�dd	d
�dS)NrN�First errorrO�Second errorrPrQrTrVzM                12: First error
                12: Second error
            rX�r
rLr[r3r\r*�rr�first�secondrrr�%test_invalid_instance_multiple_errors�s�
�z-TestCLI.test_invalid_instance_multiple_errorscCsNd}td|d�}td|d�}|jtdt�|�d�t||g�gd�dd	d
�dS)NrNrerOrfrPrQr_rVa                ===[ValidationError]===(some_instance)===

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

                Second error
                -----------------------------
            rXrgrhrrr�3test_invalid_instance_multiple_errors_pretty_output�s�
�z;TestCLI.test_invalid_instance_multiple_errors_pretty_outputcCsfd}td|d�td|d�g}d}td|d�g}|jtdt�|�t�|�d�t||�gd	�d
dd�dS)
NrN�An errorrO�
Another error�foo�BOOMrP�rR�some_first_instance�some_second_instance)rUrrrUrsrRrVze                12: An error
                12: Another error
                foo: BOOM
            rXrg�r�first_instanceZfirst_errors�second_instanceZ
second_errorsrrr�test_multiple_invalid_instances�s"

���z'TestCLI.test_multiple_invalid_instancescCsfd}td|d�td|d�g}d}td|d�g}|jtdt�|�t�|�d�t||�gd	�d
dd�dS)
NrNrmrOrnrorprPrq)r`rarUrrrUrsrRrVa�                ===[ValidationError]===(some_first_instance)===

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

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

                BOOM
                -----------------------------
            rXrgrtrrr�-test_multiple_invalid_instances_pretty_output�s"

���z5TestCLI.test_multiple_invalid_instances_pretty_outputcCsfd}td|d�td|d�g}d}td|d�g}|jtdt�|�t�|�d�t||�gd	�d
dd�dS)
NrNrmrOrnrorprPrq)�--error-format�&:{error.message}._-_.{error.instance}:rUrrrUrsrRrVz5:An error._-_.12::Another error._-_.12::BOOM._-_.foo:rXrgrtrrr�test_custom_error_formats"

���z TestCLI.test_custom_error_formatcCs|jtdd�dgddd�dS)Nz{"type": 12}�rRrRrV�O                12: 12 is not valid under any of the given schemas
            �rFrErGr<�rLr[�rrrr�test_invalid_schema.s�zTestCLI.test_invalid_schemacCszddi}|�t��}t|dd�Wd�n1s40Yt|j�}|jtt�|�d�gd�ddt|�d	d
�dS)N�typerNrK�r!rr|�r`rarRrV�%===[SchemaError]===(some_schema)===

�
-----------------------------
r~�	�assertRaisesrrr5�	exceptionrLr[r3r\�rr!�er7rrr�!test_invalid_schema_pretty_output9s*
���z)TestCLI.test_invalid_schema_pretty_outputcCs|jtdd�dgddd�dS)Nz{"type": 12, "items": 57}r|rRrVzF                57: 57 is not of type 'object', 'boolean'
            r~rr�rrr�#test_invalid_schema_multiple_errorsLs�z+TestCLI.test_invalid_schema_multiple_errorscCs|ddd�}|�t��}t|dd�Wd�n1s60Yt|j�}|jtt�|�d�gd�dd	t|�d
d�dS)NrN�9)r��itemsrKr�r|r�rVr�r�r~r�r�rrr�1test_invalid_schema_multiple_errors_pretty_outputWs
*
���z9TestCLI.test_invalid_schema_multiple_errors_pretty_outputcCs"|jtddd�gd�ddd�dS)	zv
        "Validating" an instance that's invalid under an invalid schema
        just shows the schema error.
        z{"type": 12, "minimum": 30}Z13rQrTrVr}r~Nrr�rrr�)test_invalid_schema_with_invalid_instancejs��z1TestCLI.test_invalid_schema_with_invalid_instancecCs�dddd�}}|�t��}t||d�Wd�n1s<0Yt|j�}|jtt�|�t�|�d�gd�dd	t|�d
d�dS)N�
rN��r�Zminimumr�rQ�r`rarUrSrRrVr�r�r~r�)rrr!r�r7rrr�7test_invalid_schema_with_invalid_instance_pretty_output|s"*
����z?TestCLI.test_invalid_schema_with_invalid_instance_pretty_outputcCs.|jtdddd�gd�dd�td��d�dS)	Nz{"minimum": 30}�not valid JSON!Z12)rRrurv)rUrurUrvrRrVzw                Failed to parse 'first_instance': {}
                12: 12 is less than the minimum of 30
            r~)rLr[�formatr8r�rrr�-test_invalid_instance_continues_with_the_rest�s���z5TestCLI.test_invalid_instance_continues_with_the_restcCsldddd�}}|�t��t||d�Wd�n1s<0Y|jtt�|�d�gd�dd	d
�dS)Nr�rNr�r�r�r|)ryrzrRrVz7:12 is not valid under any of the given schemas._-_.12:r~)r�rrrLr[r3r\)rrr!rrr�1test_custom_error_format_applies_to_schema_errors�s*�z9TestCLI.test_custom_error_format_applies_to_schema_errorscCs2d}|jtd|d�gd�ddt|��d�d�dS)	Nr��{}rQrTrVz1                Failed to parse 'some_instance': �
            r~�rLr[r8rrrr�test_instance_is_invalid_JSON�s
��z%TestCLI.test_instance_is_invalid_JSONcCsF|jtddd�gd�dd�\}}|�|�|�d|�|�d|�dS)	Nr�r�rQr�rV�rFrErGz7(some_instance)===

Traceback (most recent call last):
rR�rJr[r@�assertIn�assertNotIn�rr;r<rrr�+test_instance_is_invalid_JSON_pretty_output�s��


�z3TestCLI.test_instance_is_invalid_JSON_pretty_outputcCs4d}|jtdd�t|�dgddt|��d�d�dS)	Nr�r�r|rRrVz)                Failed to parse <stdin>: r�)rFr:rErGr<)rLr[rr8rrrr�&test_instance_is_invalid_JSON_on_stdin�s��z.TestCLI.test_instance_is_invalid_JSON_on_stdincCsJ|jtdd�td�gd�dd�\}}|�|�|�d|�|�d|�dS)	Nr�r|r�r�rV)rFr:rErGz1(<stdin>)===

Traceback (most recent call last):
rR)rJr[rr@r�r�r�rrr�4test_instance_is_invalid_JSON_on_stdin_pretty_output�s�

�z<TestCLI.test_instance_is_invalid_JSON_on_stdin_pretty_outputcCs.d}|jt|d�dgddt|��d�d�dS)Nr�r|rRrV�/                Failed to parse 'some_schema': r�r~r�r rrr�test_schema_is_invalid_JSON�s��z#TestCLI.test_schema_is_invalid_JSONcCs8|jtdd�gd�dd�\}}|�|�|�d|�dS)Nr�r|r�rVr��5(some_schema)===

Traceback (most recent call last):
)rJr[r@r�r�rrr�)test_schema_is_invalid_JSON_pretty_outputs�

�z1TestCLI.test_schema_is_invalid_JSON_pretty_outputcCs4d\}}|jt||d�dgddt|��d�d�dS)	�M
        Only the schema error is reported, as we abort immediately.
        )r��also not valid JSON!rQrRrVr�r�r~Nr��rr!rrrr�.test_schema_and_instance_are_both_invalid_JSONs
��z6TestCLI.test_schema_and_instance_are_both_invalid_JSONcCsF|jtddd�gd�dd�\}}|�|�|�d|�|�d|�d	S)
r�r�r�rQr�rVr�r�rSNr�r�rrr�<test_schema_and_instance_are_both_invalid_JSON_pretty_output"s��


�zDTestCLI.test_schema_and_instance_are_both_invalid_JSON_pretty_outputcCs |jtdd�gd�ddd�dS)Nr�r|)rU�nonexisting_instancerRrVzC                'nonexisting_instance' does not exist.
            r~rr�rrr�test_instance_does_not_exist7s�z$TestCLI.test_instance_does_not_existcCs |jtdd�gd�ddd�dS)Nr�r|)r`rarUr�rRrVz�                ===[FileNotFoundError]===(nonexisting_instance)===

                'nonexisting_instance' does not exist.
                -----------------------------
            r~rr�rrr�*test_instance_does_not_exist_pretty_outputBs�z2TestCLI.test_instance_does_not_exist_pretty_outputcCs|jdgddd�dS)N�nonexisting_schemarV�%'nonexisting_schema' does not exist.
�rErGr<�rLr�rrr�test_schema_does_not_existTs
�z"TestCLI.test_schema_does_not_existcCs|jgd�ddd�dS)N)r`rar�rV��                ===[FileNotFoundError]===(nonexisting_schema)===

                'nonexisting_schema' does not exist.
                -----------------------------
            r�r�r�rrr�(test_schema_does_not_exist_pretty_output\s
�z0TestCLI.test_schema_does_not_exist_pretty_outputcCs|jgd�ddd�dS)N)rUr�r�rVr�r�r�r�rrr�&test_neither_instance_nor_schema_existis
�z.TestCLI.test_neither_instance_nor_schema_existcCs|jgd�ddd�dS)N)r`rarUr�r�rVr�r�r�r�rrr�4test_neither_instance_nor_schema_exist_pretty_outputqs
�z<TestCLI.test_neither_instance_nor_schema_exist_pretty_outputcCs"|jtddd�gd�ddd�dS�Nr�rQrTrK�rFrEr;r<rr�rrr�test_successful_validation�s
�z"TestCLI.test_successful_validationcCs"|jtddd�gd�ddd�dS�Nr�rQr�z"===[SUCCESS]===(some_instance)===
rKr�rr�rrr�(test_successful_validation_pretty_output�s
�z0TestCLI.test_successful_validation_pretty_outputcCs$|jtdd�td�dgddd�dS)Nr�r|rRrK�rFr:rEr;r<�rLr[rr�rrr�#test_successful_validation_of_stdin�s�z+TestCLI.test_successful_validation_of_stdincCs&|jtdd�td�gd�ddd�dS)Nr�r|r�z===[SUCCESS]===(<stdin>)===
rKr�r�r�rrr�1test_successful_validation_of_stdin_pretty_output�s�z9TestCLI.test_successful_validation_of_stdin_pretty_outputcCs"|jtddd�gd�ddd�dSr�rr�rrr�-test_successful_validation_of_just_the_schema�s
�z5TestCLI.test_successful_validation_of_just_the_schemacCs"|jtddd�gd�ddd�dSr�rr�rrr�;test_successful_validation_of_just_the_schema_pretty_output�s
�zCTestCLI.test_successful_validation_of_just_the_schema_pretty_outputcCsxtjdd�}|��|�tj|j�t|j�}|�d�d|j�d�}|j	t
|dd�dd	d
|j��ddgd
d
d�dS)NF��delete�-{"definitions": {"num": {"type": "integer"}}}�
{"$ref": "�#/definitions/num"}�1rQrUrS�
--base-uri�/rRrKr��
�tempfile�NamedTemporaryFile�closeZ
addCleanup�os�remove�namer�
write_textrLr[�parent�as_uri�rZref_schema_fileZref_pathr!rrr�0test_successful_validation_via_explicit_base_uri�s


��z8TestCLI.test_successful_validation_via_explicit_base_uricCsztjdd�}|��|�tj|j�t|j�}|�d�d|j�d�}|j	t
|dd�dd	d
|j��ddgd
ddd�dS)NFr�r�r�r�z"1"rQrUrSr�r�rRrVrKz 1: '1' is not of type 'integer'
�rFrErGr;r<r�r�rrr�2test_unsuccessful_validation_via_explicit_base_uri�s 


��z:TestCLI.test_unsuccessful_validation_via_explicit_base_uricCs|d}d}|�t��8}|jt||d�dddt����dgd�Wd�n1sP0Yt|j�}|�	t
j�d	�|�dS)
Nz4{"$ref": "someNonexistentFile.json#definitions/num"}r�rQrUrSr�rR�rFrEzsomeNonexistentFile.json')r�rrLr[r�cwdr�r5r�r�r��sep�rr!rr�r7rrr�,test_nonexistent_file_with_explicit_base_uri�s���$
z4TestCLI.test_nonexistent_file_with_explicit_base_uricCsfd}d}|�t��*}|jt||d�gd�d�Wd�n1sB0Yt|j�}|�|d�dS)Nz${"$ref": "foo.json#definitions/num"}r�rQ)rUrSr�znot@UR1rRr�zunknown url type: 'foo.json')r�rrLr[r5r�rCr�rrr�test_invalid_explicit_base_uri�s��$
�z&TestCLI.test_invalid_explicit_base_uricCs0|�tt�|jtddd�gd�dddd�dS)	Nz{"const": "check"}z"a"rQrTrVrKza: 'check' was expected
r�)�assertIsrrrLr[r�rrr�=test_it_validates_using_the_latest_validator_when_unspecifieds
�zETestCLI.test_it_validates_using_the_latest_validator_when_unspecifiedcCs,d}d}|jt||d�gd�dddd�d	S)
zG
        Specifically, `const` validation applies for Draft 7.
        z�
            {
                "$schema": "http://json-schema.org/draft-07/schema#",
                "const": "check"
            }
        �"foo"rQrTrVrKzfoo: 'check' was expected
r�Nrr�rrr�-test_it_validates_using_draft7_when_specifieds
�z5TestCLI.test_it_validates_using_draft7_when_specifiedcCs*d}d}|jt||d�gd�ddd�dS)zP
        Specifically, `const` validation *does not* apply for Draft 4.
        z�
            {
                "$schema": "http://json-schema.org/draft-04/schema#",
                "const": "check"
            }
            r�rQrTrKr�Nrr�rrr�-test_it_validates_using_draft4_when_specified*s
�z5TestCLI.test_it_validates_using_draft4_when_specified)rKrK)1r#r$r%rrJrLr^rbrdrkrlrwrxr{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�rrrrr9As\�
 
)

		r9c@s:eZdZe�Zdd�Zdd�Zdd�Zdd�Zd	d
�Z	dS)�
TestParsercCs$t�gd��}|�|d|j�dS)N)�--validatorz2jsonschema.tests.test_cli.TestParser.FakeValidator�
--instance�mem://some/instance�mem://some/schemarY)rr>r�r'�rrIrrr�2test_find_validator_by_fully_qualified_object_nameAs�z=TestParser.test_find_validator_by_fully_qualified_object_namecCs"t�gd��}|�|dt�dS)N)r�r
r�r�r�rY)rr>r�r
r�rrr�!test_find_validator_in_jsonschemaLs�z,TestParser.test_find_validator_in_jsonschemac
Gs�t�t�}}t|��lt|��D|�t��t�|�Wd�n1sL0YWd�n1sj0YWd�n1s�0Y|��|��fSr)rrrr��
SystemExitrr>rD)rrEr;r<rrr�cli_output_forVs
dzTestParser.cli_output_forcCs,|�ddd�\}}|�d|�|�|�dS)Nr`ror�zinvalid choice: 'foo'�r�r�r@r�rrr�test_unknown_output]s�zTestParser.test_unknown_outputcCs0|�ddddd�\}}|�d|�|�|�dS)Nr`raryror�z3--error-format can only be used with --output plainr�r�rrr�test_useless_error_formates��z$TestParser.test_useless_error_formatN)
r#r$r%r*r'r�r�r�r�r�rrrrr�=s
r�c@s$eZdZdd�Zdd�Zdd�ZdS)�TestCLIIntegrationcCs t�d�}|�|�d�d�dS)N�
jsonschemazLicense-Expression�MIT)rrCr+)rZour_metadatarrr�test_licensess
zTestCLIIntegration.test_licensecCsBtjtjdddddgtjd�}|�d���}|�|t�	d��dS)Nz-Wr�-mr�z	--version�r<zutf-8)
�
subprocess�check_output�sys�
executable�STDOUT�decode�striprCr�version)rrrrr�test_versionws�zTestCLIIntegration.test_versioncCsBtjtjddgtjd�}tjtjdddgtjd�}|�||�dS)Nr�r�r�z--help)r�r�r�r�r�rC)r�outputZoutput_for_helprrr�#test_no_arguments_shows_usage_notess
��z6TestCLIIntegration.test_no_arguments_shows_usage_notesN)r#r$r%r�rrrrrrr�rsr�)'�
contextlibrr�	importlibr�iorr3r�pathlibr�textwrapr�unittestr	r�r�r�r��warningsr�r
rZjsonschema.exceptionsrr
rZjsonschema.validatorsrr�catch_warnings�simplefilterrr*r2r8r9r�r�rrrr�<module>s<

*		5