def test_multiple_references_to_same_uri_results_in_1_fetch(): handler = urlhandlers.PackageDataUrlHandler() # Mock the dereference method to allow calls to be observed handler.dereference = mock.Mock(side_effect=handler.dereference) url = urlhandlers.pydata.makeurl(TESTPKG, "data/multiple-ref-1-fetch/schema.rng") rnginline.inline(url=url, handlers=[handler]) # schema.rng, indrect.rng & 5x popular.rng = 3 fetches total assert len(handler.dereference.mock_calls) == 3
def test_overridden_base_uri_must_be_uri_ref(): """ The base URI, if specified, must match the URI-reference grammar. i.e. it is a relative or absolute URI. """ bad_uri = "x:/some/path/spaces not allowed/oops" assert not uri.is_uri_reference(bad_uri) with pytest.raises(ValueError): rnginline.inline( # some random schema, not of any significance uri.resolve(DATA_URI, "testcases/include-1/schema.rng"), base_uri=bad_uri)
def test_inline_args_fs_path_as_src(): grammar_xml = b""" <element name="start" xmlns="http://relaxng.org/ns/structure/1.0"> <empty/> </element> """ path = "/some/dir/Filename with spaces.rng" handler = urlhandlers.FilesystemUrlHandler() handler.dereference = mock.Mock(side_effect=[grammar_xml]) rnginline.inline(path, handlers=[handler]) handler.dereference.assert_called_once_with( urlhandlers.file.makeurl(path, abs=True))
def test_foreign_attrs_cant_be_in_default_ns(): xml = """\ <grammar xmlns="http://relaxng.org/ns/structure/1.0"> <start {0}> <element name="foo"> <empty/> </element> </start> </grammar> """ rnginline.inline(etree=etree.XML(xml.format(""))) with pytest.raises(InvalidGrammarError): rnginline.inline(etree=etree.XML(xml.format('illegal-attr="abc"')))
def test_include_cant_inline_non_grammar_elements(): """ Verify that <include>s can't pull in a RNG file that doesn't start with a <grammar>. """ illegal_url = urlhandlers.pydata.makeurl( TESTPKG, "data/inline-non-grammar-els/illegal.rng") with pytest.raises(InvalidGrammarError): rnginline.inline(url=illegal_url) legal_url = urlhandlers.pydata.makeurl( TESTPKG, "data/inline-non-grammar-els/legal.rng") schema = rnginline.inline(url=legal_url) assert schema(etree.XML("<foo/>"))
def test_inlined_files_dont_inherit_datatype(schema_path): illegal_url = urlhandlers.pydata.makeurl(TESTPKG, schema_path) # Inlining will succeed grammar = rnginline.inline(url=illegal_url, create_validator=False) # But constructing a validator from the grammar XML will fail with pytest.raises(etree.RelaxNGError): etree.RelaxNG(grammar)
def test_override_default_base_uri(): default_base_uri = urlhandlers.pydata.makeurl(TESTPKG, "data/testcases/xml-base/") schema = rnginline.inline(url="schema.rng", default_base_uri=default_base_uri) xml_url = uri.resolve(default_base_uri, "positive-1.xml") xml = etree.fromstring(urlhandlers.pydata.dereference(xml_url)) assert schema(xml)
def test_inline_url_arguments_are_resolved_against_default_base_uri(): class Stop(Exception): pass # Mock the dereference method to allow calls to be observed handler = mock.MagicMock() handler.can_handle.return_value = True handler.dereference.side_effect = Stop url = "somefile.txt" with pytest.raises(Stop): rnginline.inline(url=url, handlers=[handler]) # The default base URL is the cwd expected_base = urlhandlers.file.makeurl(rnginline._get_cwd(), abs=True) # The url we provide should be resolved against the default base: expected_url = uri.resolve(expected_base, url) assert expected_url.startswith(expected_base) assert expected_url.endswith("somefile.txt") handler.dereference.assert_called_once_with(expected_url)
def test_inline_etree_el_with_no_base_uri_uses_default_base_uri(): base_url = urlhandlers.pydata.makeurl(TESTPKG, "data/testcases/xml-base/") schema_bytes = urlhandlers.pydata.dereference( uri.resolve(base_url, "schema.rng")) schema_el = etree.fromstring(schema_bytes) assert schema_el.getroottree().docinfo.URL is None # The default-default base URI is the pwd, so let's use something else # to demonstrate this. An unhandlable URI will result in a # NoAvailableHandlerError when the first href is dereferenced. with pytest.raises(NoAvailableHandlerError): rnginline.inline(etree=schema_el, default_base_uri="x:") # If we use a sensible default base URI the references will be resolved OK, # even though the XML document itself has no base URI schema = rnginline.inline(etree=schema_el, default_base_uri=base_url) assert schema( etree.fromstring( urlhandlers.pydata.dereference( uri.resolve(base_url, "positive-1.xml"))))
def test_inline_args_etree_as_src(): url = uri.resolve(DATA_URI, "testcases/xml-base/schema.rng") schema_el = etree.fromstring(urlhandlers.pydata.dereference( uri.resolve(DATA_URI, "testcases/xml-base/schema.rng")), base_url=url) assert etree.iselement(schema_el) # pass schema_el as src, should be detected as an Element schema = rnginline.inline(schema_el) assert schema( etree.fromstring( urlhandlers.pydata.dereference(uri.resolve(url, "positive-1.xml"))))
def test_inline_args_etree_doc_as_src(): url = uri.resolve(DATA_URI, "testcases/xml-base/schema.rng") schema_el = etree.fromstring(urlhandlers.pydata.dereference( uri.resolve(DATA_URI, "testcases/xml-base/schema.rng")), base_url=url) schema_root = schema_el.getroottree() assert not etree.iselement(schema_root) # pass etree document (not el) as src, should pull out root el and use it schema = rnginline.inline(schema_root) assert schema( etree.fromstring( urlhandlers.pydata.dereference(uri.resolve(url, "positive-1.xml"))))
def test_testcases(schema_file, test_file, should_match): schema = rnginline.inline(urlhandlers.pydata.makeurl(TESTPKG, schema_file)) xml = etree.parse(pr.resource_stream(TESTPKG, test_file)) if should_match: try: # Should match schema.assertValid(xml) except etree.DocumentInvalid: pytest.fail("{0} should match {1} but didn't: {2}".format( test_file, schema_file, schema.error_log)) else: with pytest.raises(etree.DocumentInvalid): # Shouldn't match schema.assertValid(xml) pytest.fail("{0} shouldn't match {1} but did".format( test_file, schema_file))
def test_provide_base_uri(): """ This tests manually specifying a base URI to use for the source. """ base_uri = urlhandlers.pydata.makeurl( TESTPKG, "data/testcases/xml-base/schema.rng") # Use a file object so that the inliner won't know the URI of the src fileobj = io.BytesIO(urlhandlers.pydata.dereference(base_uri)) schema = rnginline.inline( fileobj, base_uri=base_uri, # our base URI is absolute, so the default # base won't have any effect. default_base_uri="x:/blah") xml_url = uri.resolve(base_uri, "positive-1.xml") xml = etree.fromstring(urlhandlers.pydata.dereference(xml_url)) assert schema(xml)
def _main(args): if args["--stdin"]: src = sys.stdin.buffer if six.PY3 else sys.stdin else: src = py2_decode_bytes(args["<rng-src>"]) outfile = py2_decode_bytes(args["<rng-output>"]) if outfile is None or outfile == "-": outfile = sys.stdout.buffer if six.PY3 else sys.stdout default_base_uri = None if args["--default-base-uri"]: default_base_uri = py2_decode_bytes(args["--default-base-uri"]) # Need to validate this here, as it's considered a coding error to pass # an invalid URI to Inliner as default_base_uri, but this URI is # user-provided. if not uri.is_uri(default_base_uri): raise RelaxngInlineError( "The --default-base-uri provided is not a valid URI: {0}". format(default_base_uri)) base_uri = None if args["--base-uri"]: base_uri = py2_decode_bytes(args["--base-uri"]) if not uri.is_uri_reference(base_uri): raise RelaxngInlineError("The --base-uri provided is not a valid " "URI-reference: {0}".format(base_uri)) postprocessors = None # defaults if args["--no-libxml2-compat"]: postprocessors = [] schema = inline(src, postprocessors=postprocessors, create_validator=False, base_uri=base_uri, default_base_uri=default_base_uri) schema.getroottree().write(outfile)
def test_include_loops_trigger_error(): with pytest.raises(SchemaIncludesSelfError): rnginline.inline( url=urlhandlers.pydata.makeurl(TESTPKG, "data/loops/start.rng"))
def test_unhandleable_url_raises_error(): with pytest.raises(NoAvailableHandlerError): rnginline.inline(url="my-fancy-url-scheme:/foo")
def test_include_cant_override_define_if_no_matching_define_in_included_file(): with pytest.raises(InvalidGrammarError): rnginline.inline(url=urlhandlers.pydata.makeurl( TESTPKG, "data/include-override-define/start.rng"))
def test_inline_args_passing_garbage(): with pytest.raises(ValueError): # pass a useless arg as src rnginline.inline(1234)
def test_inline_args_url_refs_must_be_valid(): bad_url = "/tmp/File Name With Spaces" assert not uri.is_uri_reference(bad_url) with pytest.raises(ValueError): rnginline.inline(url=bad_url)
import rnginline from lxml import etree svg_file = 'sign-abc.svg' html_file = 'iframe.html' rng_dir = './TinySVG-rng' rng_file = 'Tiny-1.2.rng' svg_validator = rnginline.inline(path=rng_dir + '/' + rng_file) #print(dir(svg_validator)) svg_doc = etree.parse(svg_file) #print(etree.tostring(svg_doc)) v1 = svg_validator.validate(svg_doc) print('SVG valudate %s' % v1) html_doc = etree.parse(html_file) #print(etree.tostring(html_doc)) v2 = svg_validator.validate(html_doc) print('HTML valudate %s' % v2)
def test_including_invalid_xml_file_raises_parse_error(): url = urlhandlers.pydata.makeurl(TESTPKG, "data/include-invalid-xml/ok.rng") with pytest.raises(ParseError): rnginline.inline(url=url)
def test_including_non_rng_xml_file_raises_invalid_grammar_error(): url = urlhandlers.pydata.makeurl(TESTPKG, "data/include-non-rng-xml/ok.rng") with pytest.raises(InvalidGrammarError): rnginline.inline(url=url)
def test_calling_inline_with_0_args_raises_value_error(): with pytest.raises(ValueError): rnginline.inline()