`dBp<ddlmZmZddlmZddlmZddlmZddl m Z ddl m Z ddlZddl Z ddlZddlZddlZ ddlmZdd lmZdd lmZmZmZdd lmZmZmZdd lm Z m!Z!d Z"dZ#dZ$Gdde Z%Gdde Z&Gdde Z'y#e$rddlZYXwxYw))redirect_stderrredirect_stdout)StringIO)JSONDecodeError)Path)dedent)TestCaseN)metadata)m)Draft4ValidatorDraft202012Validatorcli)RefResolutionError SchemaErrorValidationError)_LATEST_VERSIONvalidatecRttGfddt}|S)Nc.eZdZdZfdZedZy)%fake_validator..FakeValidatorcyN)selfargskwargss ;/usr/lib/python3/dist-packages/jsonschema/tests/test_cli.py__init__z.fake_validator..FakeValidator.__init__!s c,rjSgSr)pop)rinstanceerrorss r iter_errorsz1fake_validator..FakeValidator.iter_errors$szz|#Ircyrrrschemas r check_schemaz2fake_validator..FakeValidator.check_schema)s rN)__name__ __module__ __qualname__rr$ classmethodr()r#sr FakeValidatorr s!      rr-)listreversedobject)r#r-s` rfake_validatorr1s& (6" #F   rcfd}|S)NcVj|}| t|t|Sr)getFileNotFoundErrorr)pathcontents all_contentss ropenzfake_open..open1s/##D)  #D) )!!rr)r8r9s` r fake_openr:0s" Krc tj|td#t$r}t |cYd}~Sd}~wwxYw)Nz.Tried and failed to capture a JSON dump error.)jsonloads RuntimeErrorrstr)non_jsonerrors r _message_forrB9s>M 8KLL 5zs" A ;AAc8eZdZeedfdZd0dZdZdZdZ dZ dZ d Z d Z d Zd Zd ZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZ dZ!dZ"d Z#d!Z$d"Z%d#Z&d$Z'd%Z(d&Z)d'Z*d(Z+d)Z,d*Z-d+Z.d,Z/d-Z0d.Z1y/)1TestCLIrc tj|}|j||jt tdt |t_ tt}}tj||||} t`|j| |tdj|| |j|j|j|jfS#t`wxYw)Nr9)stdinstdoutstderrz Expected an exit code of {} != {}. stdout: {} stderr: {} )msg) r parse_argsupdate assertFalsehasattrr:r9rrun assertEqualrformatgetvalue) rargvfilesrF exit_codeoverride argumentsrGrHactual_exit_codes rrun_clizTestCLI.run_cliCsNN4( " f-.U# %ZFF"ww     iV F$OO%OO%  .   &//"333%s -C99Dc p|j|jdi|t|t|fy)Nr)rOrXr)rrGrHrs r assertOutputszTestCLI.assertOutputshs1  DLL "6 " F^VF^ , rctdd}|jtdtj|j t |ggddd y) NI am an error! r"*{"does not": "matter since it is stubbed"} some_schema some_instance-irbra12: I am an error! rS validatorrRrTrHrrZdictr<dumpsr"r1rrAs rtest_invalid_instancezTestCLI.test_invalid_instancensR 02> H"jj8%eW-7)  rctdd}|jtdtj|j t |ggddd y) Nr\r]r^r_r`rdrb--outputprettyrarez ===[ValidationError]===(some_instance)=== I am an error! ----------------------------- rgrirls r#test_invalid_instance_pretty_outputz+TestCLI.test_invalid_instance_pretty_output}sT 02> H"jj8%eW-M  rctdd}|jtdtj|j t |ggddd y) Nr\r]r^r_r`)rpplainrdrbrarerfrgrirls r+test_invalid_instance_explicit_plain_outputz3TestCLI.test_invalid_instance_explicit_plain_outputsR 02> H"jj8%eW-L)  rcd}td|}td|}|jtdtj|t ||ggddd y) Nr] First errorr^ Second errorr_r`rcrezM 12: First error 12: Second error rgrrZrjr<rkr1rr"firstseconds r%test_invalid_instance_multiple_errorsz-TestCLI.test_invalid_instance_multiple_errorssb A (C H"jj2%eV_57  rcd}td|}td|}|jtdtj|t ||ggddd y) Nr]rwr^rxr_r`rorea ===[ValidationError]===(some_instance)=== First error ----------------------------- ===[ValidationError]===(some_instance)=== Second error ----------------------------- rgryrzs r3test_invalid_instance_multiple_errors_pretty_outputz;TestCLI.test_invalid_instance_multiple_errors_pretty_outputsb A (C H"jj2%eV_5M   rc d}td|td|g}d}td|g}|jtdtj|tj|t ||gd d d y) Nr]An errorr^ Another errorfooBOOMr_rasome_first_instancesome_second_instance)rdrrdrrareze 12: An error 12: Another error foo: BOOM rgryrfirst_instance first_errorssecond_instance second_errorss rtest_multiple_invalid_instancesz'TestCLI.test_multiple_invalid_instancess J @ On E  (/JK  H$(JJ~$>%)ZZ%@ %\=A   rc d}td|td|g}d}td|g}|jtdtj|tj|t ||gd d d y) Nr]rr^rrrr_r)rprqrdrrdrrarea ===[ValidationError]===(some_first_instance)=== An error ----------------------------- ===[ValidationError]===(some_first_instance)=== Another error ----------------------------- ===[ValidationError]===(some_second_instance)=== BOOM ----------------------------- rgryrs r-test_multiple_invalid_instances_pretty_outputz5TestCLI.test_multiple_invalid_instances_pretty_outputs J @ On E  (/JK  H$(JJ~$>%)ZZ%@ %\=A !  rc d}td|td|g}d}td|g}|jtdtj|tj|t ||gd d d y) Nr]rr^rrrr_r)--error-format&:{error.message}._-_.{error.instance}:rdrrdrrarez5:An error._-_.12::Another error._-_.12::BOOM._-_.foo:rgryrs rtest_custom_error_formatz TestCLI.test_custom_error_formats J @ On E  (/JK  H$(JJ~$>%)ZZ%@ %\=AJ!  rcD|jtddgddy)Nz {"type": 12}rarareO 12: 12 is not valid under any of the given schemas rSrRrTrHrZrjrs rtest_invalid_schemazTestCLI.test_invalid_schema4s* >2  rc *ddi}|jt5}t|ddddtj}|j t tj|gdddt|zd z y#1swYbxYw) Ntyper]r'r"rrprqrare%===[SchemaError]===(some_schema)===  ----------------------------- r assertRaisesrrr? exceptionrZrjr<rkrr'erAs r!test_invalid_schema_pretty_outputz)TestCLI.test_invalid_schema_pretty_output?s"   { + 1q FR 0 1AKK  4::f#5669e*56   1 1s B  BcD|jtddgddy)Nz{"type": 12, "items": 57}rrarezF 57: 57 is not of type 'object', 'boolean' rrrs r#test_invalid_schema_multiple_errorsz+TestCLI.test_invalid_schema_multiple_errorsRs+ #>?  rc ,ddd}|jt5}t|ddddtj}|j t tj|gddd t|zd z y#1swYbxYw) Nr]9)ritemsrrrrrerrrrrs r1test_invalid_schema_multiple_errors_pretty_outputz9TestCLI.test_invalid_schema_multiple_errors_pretty_output]sr*   { + 1q FR 0 1AKK  4::f#5669e*56   1 1s B  BcH|jtddgdddy) zv "Validating" an instance that's invalid under an invalid schema just shows the schema error. z{"type": 12, "minimum": 30}13r`rcrerrNrrs r)test_invalid_schema_with_invalid_instancez1TestCLI.test_invalid_schema_with_invalid_instanceps2 9"8  rc Xdddd}}|jt5}t||dddtj}|j t tj|tj|gddd t|zd z y#1swYvxYw) N r]rminimumrr`rprqrdrbrarerrrr)rr"r'rrAs r7test_invalid_schema_with_invalid_instance_pretty_outputz?TestCLI.test_invalid_schema_with_invalid_instance_pretty_outputsr:&   { + 7q FX 6 7AKK   JJv."jj2N9e*56   7 7s B  B)c z|jtdddgdddjtdy) Nz{"minimum": 30}not valid JSON!12)rarr)rdrrdrrarezw Failed to parse 'first_instance': {} 12: 12 is less than the minimum of 30 rrZrjrPrBrs r-test_invalid_instance_continues_with_the_restz5TestCLI.test_invalid_instance_continues_with_the_restsG -0 $  |$567!  rcdddd}}|jt5t||ddd|jt t j |gddd y#1swY>xYw) Nrr]rrrr)rrrarez7:12 is not valid under any of the given schemas._-_.12:r)rrrrZrjr<rk)rr"r's r1test_custom_error_format_applies_to_schema_errorsz9TestCLI.test_custom_error_format_applies_to_schema_errorsspr:&   { + 7 FX 6 7 4::f#56 L   7 7s A((A1c |d}|jtd|gdddjt|y)Nr{}r`rcrez@ Failed to parse 'some_instance': {} rrrr"s rtest_instance_is_invalid_JSONz%TestCLI.test_instance_is_invalid_JSONs>$ 4x@7|H-.  rc|jtddgdd\}}|j||jd||j d|y) Nrrr`rrerSrRrTz7(some_instance)=== Traceback (most recent call last): rarXrjrLassertIn assertNotInrrGrHs r+test_instance_is_invalid_JSON_pretty_outputz3TestCLI.test_instance_is_invalid_JSON_pretty_outputsd / N&     H   /rc d}|jtdt|dgddjt |y)Nrrrrarez8 Failed to parse : {} )rSrFrRrTrH)rZrjrrPrBrs r&test_instance_is_invalid_JSON_on_stdinz.TestCLI.test_instance_is_invalid_JSON_on_stdinsF$ 4(8$|H-.  rc|jtdtdgdd\}}|j||j d||j d|y) Nrrrrre)rSrFrRrTz1()=== Traceback (most recent call last): ra)rXrjrrLrrrs r4test_instance_is_invalid_JSON_on_stdin_pretty_outputz Failed to parse 'some_schema': {} rrr&s rtest_schema_is_invalid_JSONz#TestCLI.test_schema_is_invalid_JSONs>" 6*|F+,  rc|jtdgdd\}}|j||jd|y)Nrrrrer5(some_schema)=== Traceback (most recent call last): )rXrjrLrrs r)test_schema_is_invalid_JSON_pretty_outputz1TestCLI.test_schema_is_invalid_JSON_pretty_output sJ#456 &    F  rc d\}}|jt||dgddjt|y)M Only the schema error is reported, as we abort immediately. )ralso not valid JSON!r`rarerrNrrr'r"s r.test_schema_and_instance_are_both_invalid_JSONz6TestCLI.test_schema_and_instance_are_both_invalid_JSONsHE 6B|F+,  rc|jtddgdd\}}|j||jd||j d|y ) rrrr`rrerrrbNrrs r  rcF|jtdgdddy)Nrr)rprqrdrrarez ===[FileNotFoundError]===(nonexisting_instance)=== 'nonexisting_instance' does not exist. ----------------------------- rrrs r*test_instance_does_not_exist_pretty_outputz2TestCLI.test_instance_does_not_exist_pretty_outputHs, 4(   rc.|jdgddy)Nnonexisting_schemare%'nonexisting_schema' does not exist. rRrTrHrZrs rtest_schema_does_not_existz"TestCLI.test_schema_does_not_existZs! &';  rc0|jgdddy)N)rprqrre ===[FileNotFoundError]===(nonexisting_schema)=== 'nonexisting_schema' does not exist. ----------------------------- rrrs r(test_schema_does_not_exist_pretty_outputz0TestCLI.test_schema_does_not_exist_pretty_outputbs  =  rc0|jgdddy)N)rdrrrerrrrs r&test_neither_instance_nor_schema_existz.TestCLI.test_neither_instance_nor_schema_existos E;  rc0|jgdddy)N)rprqrdrrrerrrrs r4test_neither_instance_nor_schema_exist_pretty_outputz)=== rrrrs r1test_successful_validation_of_stdin_pretty_outputz9TestCLI.test_successful_validation_of_stdin_pretty_outputs- 4(4.62  rcH|jtddgdddyrrrs r-test_successful_validation_of_just_the_schemaz5TestCLI.test_successful_validation_of_just_the_schemarrcH|jtddgdddyrrrs r;test_successful_validation_of_just_the_schema_pretty_outputzCTestCLI.test_successful_validation_of_just_the_schema_pretty_outputrrctjd}|j|jtj |j t|j }|jdd|j d}|jt|ddd d |jjd zd gd d y)NFdelete-{"definitions": {"num": {"type": "integer"}}} {"$ref": "#definitions/num"}1r`rdrb --base-uri/rarr tempfileNamedTemporaryFileclose addCleanuposremovenamer write_textrZrjparentas_urirref_schema_fileref_pathr's r0test_successful_validation_via_explicit_base_uriz8TestCLI.test_successful_validation_via_explicit_base_uris"55UC  ?#7#78,,-KLx}}o-@A 6=ohoo446<   rctjd}|j|jtj |j t|j }|jdd|j d}|jt|ddd d |jjd zd gd ddy)NFrrrrz"1"r`rdrbrrrarerz 1: '1' is not of type 'integer' rSrRrTrGrHr rs r2test_unsuccessful_validation_via_explicit_base_uriz:TestCLI.test_unsuccessful_validation_via_explicit_base_uris"55UC  ?#7#78,,-KLx}}o-@A 6?ohoo446< 6  rc Vd}d}|jt5}|jt||dddt j j dgdddtj}|jtjd |y#1swYBxYw) Nz4{"$ref": "someNonexistentFile.json#definitions/num"}rr`rdrbrrarSrRzsomeNonexistentFile.json') rrrZrjrcwdrr?rrrseprr'r"rrAs r,test_nonexistent_file_with_explicit_base_uriz4TestCLI.test_nonexistent_file_with_explicit_base_urisG   1 2 a    &"* / $((*"3"3"5!   AKK   9:EB  s ABB(cd}d}|jt5}|jt||gddddt j }|j |dy#1swY1xYw)Nz${"$ref": "foo.json#definitions/num"}rr`)rdrbrznot@UR1rarzunknown url type: 'foo.json')rrrZrjr?rrOrs rtest_invalid_exlicit_base_uriz%TestCLI.test_invalid_exlicit_base_uris|7   1 2 a    &"*   AKK   1   s !A++A4c~|jtt|jt ddgddddy) Nz{"const": "check"}z"a"r`rcrerza: 'check' was expected r)assertIsr rrZrjrs r=test_it_validates_using_the_latest_validator_when_unspecifiedzETestCLI.test_it_validates_using_the_latest_validator_when_unspecified s= *O< #7uM7.  rcRd}d}|jt||gddddy ) zG Specifically, `const` validation applies for Draft 7. z { "$schema": "http://json-schema.org/draft-07/schema#", "const": "check" } "foo"r`rcrerzfoo: 'check' was expected rNrrs r-test_it_validates_using_draft7_when_specifiedz5TestCLI.test_it_validates_using_draft7_when_specifieds9   6B70  rcPd}d}|jt||gdddy)zP Specifically, `const` validation *does not* apply for Draft 4. z { "$schema": "http://json-schema.org/draft-04/schema#", "const": "check" } r'r`rcrrNrrs r-test_it_validates_using_draft4_when_specifiedz5TestCLI.test_it_validates_using_draft4_when_specified0s6  6B7  rN)rr)2r)r*r+r rrXrZrmrrrur}rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr r"r%r(r*rrrrDrDBs#XZ1#4J   (   * 6 >' R 8   &   & $ , ( $  0$  0     2*   $     "       * ,C& *  & rrDc8eZdZeZdZdZdZdZdZ y) TestParsercptjgd}|j|d|jy)N) --validatorz2jsonschema.tests.test_cli.TestParser.FakeValidator --instancemem://some/instancemem://some/schemarh)rrJr$r-rrVs r2test_find_validator_by_fully_qualified_object_namez=TestParser.test_find_validator_by_fully_qualified_object_nameGs0NN   i ,d.@.@Arcdtjgd}|j|dty)N)r.r r/r0r1rh)rrJr$r r2s r!test_find_validator_in_jsonschemaz,TestParser.test_find_validator_in_jsonschemaRs*NN   i ,o>rchtt}}t|5t|5|jt5t j |ddddddddd|j|jfS#1swY9xYw#1swY=xYw#1swYAxYwr)rrrr SystemExitrrJrQ)rrRrGrHs rcli_output_forzTestParser.cli_output_for\s!XZ V $ %of&= %"":. %t$ % % % &//"333 % % % % % %s: B(BBB B(B BB% !B((B1cv|jddd\}}|jd||j|y)Nrprr1zinvalid choice: 'foo'r8rrLrs rtest_unknown_outputzTestParser.test_unknown_outputcs=,,    -v6  rcz|jddddd\}}|jd||j|y)Nrprqrrr1z3--error-format can only be used with --output plainr:rs rtest_useless_error_formatz$TestParser.test_useless_error_formatksG,,  e   A    rN) r)r*r+r1r-r3r5r8r;r=rrrr,r,Cs$"$M B?4! !rr,ceZdZdZdZdZy)TestCLIIntegrationctjtjddddgtj}|j d|y)N-mpipshow jsonschemarHs License: MIT) subprocess check_outputsys executableSTDOUTr)routputs r test_licensezTestCLIIntegration.test_licenseys=(( ^^T5&, ?$$  ov.rctjtjdddgtj}|j dj }|j|tjdy)NrArDz --versionrEzutf-8) rFrGrHrIrJdecodestriprOr version)rrPs r test_versionzTestCLIIntegration.test_versions_)) ^^T< =$$ ..)//1 ("2"2<"@Arctjtjddgtj}tjtjdddgtj}|j ||y)NrArDrEz--help)rFrGrHrIrJrO)rrKoutput_for_helps r#test_no_arguments_shows_usage_notesz6TestCLIIntegration.test_no_arguments_shows_usage_notessf(( ^^T< 0$$ %11 ^^T< :$$  1rN)r)r*r+rLrQrTrrrr?r?xs/B 2rr?)( contextlibrriorr<rpathlibrtextwraprunittestr rrFrHr  importlibr ImportErrorimportlib_metadata pyrsistentr rDr r rjsonschema.exceptionsrrrjsonschema.validatorsrrr1r:rBrDr,r?rrrr`s7   *"AA <&M~  h~  B2!2!j22S*)*sB BB