rcBpRddlmZmZddlmZddlmZddlmZddl m Z ddl m Z ddlZddl Z ddlZddlZddlZ ddlmZn#e$rddlZYnwxYwdd lmZdd lmZmZmZdd lmZmZmZdd lm Z m!Z!d Z"dZ#dZ$Gdde Z%Gdde Z&Gdde Z'dS))redirect_stderrredirect_stdout)StringIO)JSONDecodeError)Path)dedent)TestCaseN)metadata)m)Draft4ValidatorDraft202012Validatorcli)RefResolutionError SchemaErrorValidationError)_LATEST_VERSIONvalidatecjttGfddt}|S)Nc6eZdZdZfdZedZdS)%fake_validator..FakeValidatorcdSN)selfargskwargss ;/usr/lib/python3/dist-packages/jsonschema/tests/test_cli.py__init__z.fake_validator..FakeValidator.__init__!s Dc4rSgSr)pop)rinstanceerrorss r iter_errorsz1fake_validator..FakeValidator.iter_errors$s $zz||#IrcdSrrrschemas r check_schemaz2fake_validator..FakeValidator.check_schema)s DrN)__name__ __module__ __qualname__rr$ classmethodr()r#sr FakeValidatorr sX               rr-)listreversedobject)r#r-s` rfake_validatorr1sT (6"" # #F           rcfd}|S)Ncn|}|t|t|Sr)getFileNotFoundErrorr)pathcontents all_contentss ropenzfake_open..open1s8##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_forrB9seM 8KLLL 5zzs% AAAAcFeZdZeedfdZd1dZdZdZdZ dZ d Z d Z d Z d Zd ZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZdZ 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-Z/d.Z0d/Z1d0S)2TestCLIrc vtj|}|||t tdt |t_ tt}}tj||||} t`n #t`wxYw| | |td || | | | | fS)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_cliCs(N4(( """ f--...U## %ZZFF"w     LLLL  iV F$OO%%OO%%  . .      &//"3"333s ,4B(( B1c ||jdi|t|t|fdS)Nr)rOrXr)rrGrHrs r assertOutputszTestCLI.assertOutputshsM  DL " "6 " " F^^VF^^ ,     rctdd}|tdtj|jt |ggddd dS) NI am an error! r"*{"does not": "matter since it is stubbed"} some_schema some_instance-ircrb12: I am an error! rS validatorrRrTrHrr[dictr<dumpsr"r1rrAs rtest_invalid_instancezTestCLI.test_invalid_instancens~ 02>>> H"j88%eW--777)  rctdd}|tdtj|jt |ggddd dS) Nr]r^r_r`rarerc--outputprettyrbrfz ===[ValidationError]===(some_instance)=== I am an error! ----------------------------- rhrjrms r#test_invalid_instance_pretty_outputz+TestCLI.test_invalid_instance_pretty_output}s 02>>> H"j88%eW--MMM      rctdd}|tdtj|jt |ggddd dS) Nr]r^r_r`ra)rqplainrercrbrfrgrhrjrms r+test_invalid_instance_explicit_plain_outputz3TestCLI.test_invalid_instance_explicit_plain_outputs~ 02>>> H"j88%eW--LLL)  rcd}td|}td|}|tdtj|t ||ggddd dS) Nr^ First errorr_ Second errorr`rardrfzM 12: First error 12: Second error rhrr[rkr<rlr1rr"firstseconds r%test_invalid_instance_multiple_errorsz-TestCLI.test_invalid_instance_multiple_errorss AAA (CCC H"j22%eV_55777      rcd}td|}td|}|tdtj|t ||ggddd dS) Nr^rxr_ryr`rarprfa ===[ValidationError]===(some_instance)=== First error ----------------------------- ===[ValidationError]===(some_instance)=== Second error ----------------------------- rhrzr{s r3test_invalid_instance_multiple_errors_pretty_outputz;TestCLI.test_invalid_instance_multiple_errors_pretty_outputs AAA (CCC H"j22%eV_55MMM       rc 2d}td|td|g}d}td|g}|tdtj|tj|t ||gd d d dS) Nr^An errorr_ Another errorfooBOOMr`rbsome_first_instancesome_second_instance)rerrerrbrfze 12: An error 12: Another error foo: BOOM rhrzrfirst_instance first_errorssecond_instance second_errorss rtest_multiple_invalid_instancesz'TestCLI.test_multiple_invalid_instancess J @ @ @ On E E E  (/JJJK  H$(J~$>$>%)Z%@%@ %\=AA       rc 2d}td|td|g}d}td|g}|tdtj|tj|t ||gd d d dS) Nr^rr_rrrr`r)rqrrrerrerrbrfa ===[ValidationError]===(some_first_instance)=== An error ----------------------------- ===[ValidationError]===(some_first_instance)=== Another error ----------------------------- ===[ValidationError]===(some_second_instance)=== BOOM ----------------------------- rhrzrs r-test_multiple_invalid_instances_pretty_outputz5TestCLI.test_multiple_invalid_instances_pretty_outputs J @ @ @ On E E E  (/JJJK  H$(J~$>$>%)Z%@%@ %\=AA !      rc 2d}td|td|g}d}td|g}|tdtj|tj|t ||gd d d dS) Nr^rr_rrrr`r)--error-format&:{error.message}._-_.{error.instance}:rerrerrbrfz5:An error._-_.12::Another error._-_.12::BOOM._-_.foo:rhrzrs rtest_custom_error_formatz TestCLI.test_custom_error_formats J @ @ @ On E E E  (/JJJK  H$(J~$>$>%)Z%@%@ %\=AAJ!      rcV|tddgdddS)Nz {"type": 12}rbrbrfO 12: 12 is not valid under any of the given schemas rSrRrTrHr[rkrs rtest_invalid_schemazTestCLI.test_invalid_schema4sD >222      rc Xddi}|t5}t|ddddn #1swxYwYt|j}|t tj|gdddt|zd z dS) Ntyper^rYr'r"rrqrrrbrf%===[SchemaError]===(some_schema)===  ----------------------------- r assertRaisesrrr? exceptionr[rkr<rlrr'erAs r!test_invalid_schema_pretty_outputz)TestCLI.test_invalid_schema_pretty_output?s"   { + + 1q FR 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1AK   4:f#5#56666669e**56  s=AAcV|tddgdddS)Nz{"type": 12, "items": 57}rrbrfzF 57: 57 is not of type 'object', 'boolean' rrrs r#test_invalid_schema_multiple_errorsz+TestCLI.test_invalid_schema_multiple_errorsRsE #>???      rc Zddd}|t5}t|ddddn #1swxYwYt|j}|t tj|gddd t|zd z dS) Nr^9)ritemsrYrrrrfrrrrrs r1test_invalid_schema_multiple_errors_pretty_outputz9TestCLI.test_invalid_schema_multiple_errors_pretty_output]sr**   { + + 1q FR 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1AK   4:f#5#56666669e**56  s>AAcZ|tddgddddS) zv "Validating" an instance that's invalid under an invalid schema just shows the schema error. z{"type": 12, "minimum": 30}13rardrfrrNrrs r)test_invalid_schema_with_invalid_instancez1TestCLI.test_invalid_schema_with_invalid_instancepsV 9"877  rc dddd}}|t5}t||dddn #1swxYwYt|j}|t tj|tj|gddd t|zd z dS) N r^rminimumrrarqrrrercrbrfrrrr)rr"r'rrAs r7test_invalid_schema_with_invalid_instance_pretty_outputz?TestCLI.test_invalid_schema_with_invalid_instance_pretty_outputsr::&   { + + 7q FX 6 6 6 6 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7AK    Jv.."j22NMM9e**56  AAAc |tdddgdddtddS) Nz{"minimum": 30}not valid JSON!12)rbrr)rerrerrbrfzw Failed to parse 'first_instance': {} 12: 12 is less than the minimum of 30 rr[rkrPrBrs r-test_invalid_instance_continues_with_the_restz5TestCLI.test_invalid_instance_continues_with_the_restsu -0 $  |$56677!      rcdddd}}|t5t||dddn #1swxYwY|t t j|gddd dS) Nrr^rrrr)rrrbrfz7:12 is not valid under any of the given schemas._-_.12:r)rrrr[rkr<rl)rr"r's r1test_custom_error_format_applies_to_schema_errorsz9TestCLI.test_custom_error_format_applies_to_schema_errorssr::&   { + + 7 7 FX 6 6 6 6 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 4:f#5#5666 L  rc d}|td|gdddt|dS)Nr{}rardrfz@ Failed to parse 'some_instance': {} rrrr"s rtest_instance_is_invalid_JSONz%TestCLI.test_instance_is_invalid_JSONsd$ 4x@@@777|H--..      rc|tddgdd\}}|||d||d|dS) NrrrarrfrSrRrTz7(some_instance)=== Traceback (most recent call last): rbrXrkrLassertIn assertNotInrrGrHs r+test_instance_is_invalid_JSON_pretty_outputz3TestCLI.test_instance_is_invalid_JSON_pretty_outputs / NMM&        H     /////rc d}|tdt|dgddt |dS)Nrrrrbrfz8 Failed to parse : {} )rSrFrRrTrH)r[rkrrPrBrs r&test_instance_is_invalid_JSON_on_stdinz.TestCLI.test_instance_is_invalid_JSON_on_stdinsi$ 4(((8$$|H--..  rc|tdtdgdd\}}|||d||d|dS) Nrrrrrf)rSrFrRrTz1()=== Traceback (most recent call last): rb)rXrkrrLrrrs 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|tdgdd\}}|||d|dS)Nrrrrfr5(some_schema)=== Traceback (most recent call last): )rXrkrLrrs r)test_schema_is_invalid_JSON_pretty_outputz1TestCLI.test_schema_is_invalid_JSON_pretty_output su#4555666 &       F      rc d\}}|t||dgddt|dS)M Only the schema error is reported, as we abort immediately. )ralso not valid JSON!rarbrfrrNrrr'r"s r.test_schema_and_instance_are_both_invalid_JSONz6TestCLI.test_schema_and_instance_are_both_invalid_JSONshE 6BBB|F++,,  rc|tddgdd\}}|||d||d|d S) rrrrarrfrrrcNrrs r>>      rcX|tdgddddS)Nrr)rqrrrerrbrfz ===[FileNotFoundError]===(nonexisting_instance)=== 'nonexisting_instance' does not exist. ----------------------------- rrrs r*test_instance_does_not_exist_pretty_outputz2TestCLI.test_instance_does_not_exist_pretty_outputHsP 4(((       rc8|dgdddS)Nnonexisting_schemarf%'nonexisting_schema' does not exist. rRrTrHr[rs rtest_schema_does_not_existz"TestCLI.test_schema_does_not_existZs5 &';      rc:|gddddS)N)rqrrrrf ===[FileNotFoundError]===(nonexisting_schema)=== 'nonexisting_schema' does not exist. ----------------------------- rrrs r(test_schema_does_not_exist_pretty_outputz0TestCLI.test_schema_does_not_exist_pretty_outputbs: ===  rc:|gddddS)N)rerrrfrrrrs r&test_neither_instance_nor_schema_existz.TestCLI.test_neither_instance_nor_schema_existos8 EEE;      rc:|gddddS)N)rqrrrerrrfrrrrs r4test_neither_instance_nor_schema_exist_pretty_outputz)=== rYrrrs r1test_successful_validation_of_stdin_pretty_outputz9TestCLI.test_successful_validation_of_stdin_pretty_outputsO 4(((4..6662      rcZ|tddgddddSrrrs r-test_successful_validation_of_just_the_schemaz5TestCLI.test_successful_validation_of_just_the_schemarrcZ|tddgddddSrrrs r;test_successful_validation_of_just_the_schema_pretty_outputzCTestCLI.test_successful_validation_of_just_the_schema_pretty_outputrrctjd}||tj|jt|j}|dd|jd}| t|ddd d |j d zd gd d dS)NFdelete-{"definitions": {"num": {"type": "integer"}}} {"$ref": "#definitions/num"}1rarerc --base-uri/rbrYr tempfileNamedTemporaryFileclose addCleanuposremovenamer write_textr[rkparentas_urirref_schema_fileref_pathr's r0test_successful_validation_via_explicit_base_uriz8TestCLI.test_successful_validation_via_explicit_base_uris"5UCCC  ?#7888,--KLLLAx}AAA 6===oho4466<   rctjd}||tj|jt|j}|dd|jd}| t|ddd d |j d zd gd dddS)NFrrrrz"1"rarercrr rbrfrYz 1: '1' is not of type 'integer' rSrRrTrGrHr rs r2test_unsuccessful_validation_via_explicit_base_uriz:TestCLI.test_unsuccessful_validation_via_explicit_base_uris"5UCCC  ?#7888,--KLLLAx}AAA 6???oho4466< 6  rc |d}d}|t5}|t||dddt jdgdddn #1swxYwYt|j}| tj d |dS) Nz4{"$ref": "someNonexistentFile.json#definitions/num"}rrarercrrbrSrRzsomeNonexistentFile.json') rrr[rkrcwdrr?rrrseprr'r"rrAs r,test_nonexistent_file_with_explicit_base_uriz4TestCLI.test_nonexistent_file_with_explicit_base_urisG   1 2 2 a    &"* / $(**"3"3"5"5!                   AK   :::EBBBBBsAA::A>A>cd}d}|t5}|t||gddddn #1swxYwYt |j}||ddS)Nz${"$ref": "foo.json#definitions/num"}rra)rercrznot@UR1rbrzunknown url type: 'foo.json')rrr[rkr?rrOr s rtest_invalid_exlicit_base_uriz%TestCLI.test_invalid_exlicit_base_uris7   1 2 2 a    &"*                   AK    1     s)AAAc|tt|t ddgdddddS) Nz{"const": "check"}z"a"rardrfrYza: 'check' was expected r)assertIsr rr[rkrs r=test_it_validates_using_the_latest_validator_when_unspecifiedzETestCLI.test_it_validates_using_the_latest_validator_when_unspecified sc *O<<< #7uMMM777.      rcdd}d}|t||gddddd S) zG Specifically, `const` validation applies for Draft 7. z { "$schema": "http://json-schema.org/draft-07/schema#", "const": "check" } "foo"rardrfrYzfoo: 'check' was expected rNrrs r-test_it_validates_using_draft7_when_specifiedz5TestCLI.test_it_validates_using_draft7_when_specifiedsY   6BBB7770      rcbd}d}|t||gddddS)zP Specifically, `const` validation *does not* apply for Draft 4. z { "$schema": "http://json-schema.org/draft-04/schema#", "const": "check" } r(rardrYrNrrs r-test_it_validates_using_draft4_when_specifiedz5TestCLI.test_it_validates_using_draft4_when_specified0sV  6BBB777      rN)rYrY)2r)r*r+r rrXr[rnrsrvr~rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr!r#r&r)r+rrrrDrDBs!##XXZZ1#4#4#4#4J          (       *   6   >' ' ' R   8       &       &   $   ,   (   $    000$    000           222*       $             "                     *   ,CCC&   *      &     rrDc@eZdZeZdZdZdZdZdZ dS) TestParsercttjgd}||d|jdS)N) --validatorz2jsonschema.tests.test_cli.TestParser.FakeValidator --instancemem://some/instancemem://some/schemari)rrJr%r-rrVs r2test_find_validator_by_fully_qualified_object_namez=TestParser.test_find_validator_by_fully_qualified_object_nameGsFN      i ,d.@AAAAArcttjgd}||dtdS)N)r/r r0r1r2ri)rrJr%r r3s r!test_find_validator_in_jsonschemaz,TestParser.test_find_validator_in_jsonschemaRsCN      i ,o>>>>>rctt}}t|5t|5|t5t j|dddn #1swxYwYdddn #1swxYwYdddn #1swxYwY||fSr)rrrr SystemExitrrJrQ)rrRrGrHs rcli_output_forzTestParser.cli_output_for\sr!XZZ V $ $ % %of&=&= % %"":.. % %t$$$ % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %  &//"3"333sXB&BA8, B8A< <B?A< B B&B B&B B&&B*-B*c|ddd\}}|d|||dS)Nrqrr2zinvalid choice: 'foo'r9rrLrs rtest_unknown_outputzTestParser.test_unknown_outputcsT,,     -v666      rc|ddddd\}}|d|||dS)Nrqrrrrr2z3--error-format can only be used with --output plainr;rs rtest_useless_error_formatz$TestParser.test_useless_error_formatksb,,  e    A          rN) r)r*r+r1r-r4r6r9r<r>rrrr-r-Cso"N$$M B B B???444!!! ! ! ! ! !rr-c eZdZdZdZdZdS)TestCLIIntegrationctjtjddddgtj}|d|dS)N-mpipshow jsonschemarHs License: MIT) subprocess check_outputsys executableSTDOUTr)routputs r test_licensezTestCLIIntegration.test_licenseysK( ^T5&, ?$    ov.....rctjtjdddgtj}|d}||tj ddS)NrBrEz --versionrFzutf-8) rGrHrIrJrKdecodestriprOr version)rrQs r test_versionzTestCLIIntegration.test_versionss) ^T< =$   ..))//11 ("2<"@"@AAAAArctjtjddgtj}tjtjdddgtj}|||dS)NrBrErFz--help)rGrHrIrJrKrO)rrLoutput_for_helps r#test_no_arguments_shows_usage_notesz6TestCLIIntegration.test_no_arguments_shows_usage_notessv( ^T< 0$   %1 ^T< :$    11111rN)r)r*r+rMrRrUrrrr@r@xsD///BBB 2 2 2 2 2rr@)( contextlibrriorr<rpathlibrtextwraprunittestr rrGrIr  importlibr ImportErrorimportlib_metadata pyrsistentr rEr r rjsonschema.exceptionsrrrjsonschema.validatorsrrr1r:rBrDr-r@rrrras.77777777   *"""""""***))))))*AAAAAAAAAA <;;;;;;;&MMM~  ~  ~  ~  ~  h~  ~  ~  B2!2!2!2!2!2!2!2!j2222222222sA AA