def test_jinja_pass_through(): with wrap() as wrapper: with open("_template.html", "w") as f: f.write("{{ content }}") wrapper.reports = 'ws' contents = u"{{ link('input.txt') }}" doc = Doc("lines.html|jinja", wrapper, [ Doc("input.txt", wrapper, [], contents = "nothing to see here" ) ], contents=contents, apply_ws_to_content = True ) wrapper.run_docs(doc) assert unicode(doc.output_data()) == contents wrapper.report() with open("output-site/lines.html", 'r') as f: lines_html = f.read() assert lines_html == """<a href="/input.txt">Input</a>"""
def test_access_other_documents(): with temprun() as runner: doc = Doc("hello.txt|newdoc", contents="hello", runner=runner) parent = Doc("test.txt|others", doc, contents="hello", runner=runner) runner.docs = [parent] runner.run() assert parent.output().data() == """Here is a list of previous docs in this tree (not including test.txt|others).
def test_regetron_filter(): with wrap() as wrapper: wrapper.debug = False node = Doc("example.regex|regetron", wrapper, [ Doc("input1.txt", wrapper, [], contents=REGETRON_INPUT_1), Doc("input2.txt", wrapper, [], contents=REGETRON_INPUT_2) ], contents="^[a-z\s]+$" ) wrapper.run_docs(node) if not wrapper.state == 'error': assert str(node.output_data()['input1.txt']) == """\ > ^[a-z\s]+$ 0000: hello > """ assert str(node.output_data()['input2.txt']) == """\
def test_yamlargs_filterargs(): with wrap() as wrapper: doc = Doc("example.txt|yamlargs|filterargs", wrapper, [], contents = "%s\n---\r\nThis is the content." % YAML, ) wrapper.run_docs(doc) output = doc.output_data().as_text() assert "abc: xyz" in output assert "foo: 5" in output wrapper = Wrapper() doc = Doc("example.txt|yamlargs|filterargs", wrapper, [], contents = "%s\n---\r\nThis is the content." % YAML, ) wrapper.run_docs(doc) output = doc.output_data().as_text() assert "abc: xyz" in output assert "foo: 5" in output
def test_pydoc_filter_on_module_names(): with wrap() as wrapper: doc = Doc("modules.txt|pydoc", wrapper, [], contents="os math") wrapper.run_docs(doc) data = doc.output_data() assert len(data.keys()) > 100 assert data["math.e:value"].startswith("2.71828")
def test_split_html_filter(): with wrap() as wrapper: contents=""" <p>This is at the top.</p> <!-- split "a-page" --> some content on a page <!-- split "another-page" --> some content on another page <!-- endsplit --> bottom """ doc = Doc("subdir/example.html|splithtml", contents=contents, wrapper=wrapper) wrapper.docs = [doc] wrapper.run() assert doc.children[2].key == "subdir/a-page.html" assert doc.children[3].key == "subdir/another-page.html" od = doc.output().data() assert "<p>This is at the top.</p>" in od assert '<a href="a-page.html">' in od assert '<a href="another-page.html">' in od assert "bottom" in od assert "<p>This is at the top.</p>" in doc.children[2].output().data() assert "some content on a page" in doc.children[2].output().data() assert "bottom" in doc.children[2].output().data() assert "<p>This is at the top.</p>" in doc.children[3].output().data() assert "some content on another page" in doc.children[3].output().data() assert "bottom" in doc.children[3].output().data()
def test_java_filter(): with wrap() as wrapper: doc = Doc("hello.java|java", contents=JAVA_SRC, wrapper=wrapper) wrapper.docs = [doc] wrapper.run() assert doc.output().data() == "Java Hello World!\n"
def test_pydoc_filter(): with wrap() as wrapper: doc = Doc("modules.txt|pydoc", contents="os math", wrapper=wrapper) wrapper.docs = [doc] wrapper.run() assert "os.ttyname:html-source" in doc.output().keys() print doc.output().query("math.log")
def test_rst2odt(): with wrap() as wrapper: doc = Doc("example.txt|rst2odt", contents=RST, wrapper=wrapper) wrapper.docs = [doc] wrapper.run() assert doc.output().filesize() > 8000
def test_choose_extension_from_overlap(): with wrap() as wrapper: doc = Doc("hello.py|pyg|forcelatex", contents="""print "hello, world" """, wrapper=wrapper) wrapper.docs = [doc] wrapper.run() assert "begin{Verbatim}" in doc.output().as_text()
def test_key_value_example(): with wrap() as wrapper: doc = Doc("hello.txt|keyvalueexample", contents="hello", wrapper=wrapper) wrapper.docs = [doc] wrapper.run() assert doc.output().as_text() == "foo: bar"
def test_ps2pdf_filter(): with wrap() as wrapper: node = Doc("hello.ps|ps2pdf", wrapper, [], contents = PS) wrapper.run_docs(node) assert node.output_data().is_cached() assert node.output_data().filesize() > 1000
def test_rst2odt(): with wrap() as wrapper: node = Doc("example.txt|rst2odt", wrapper, [], contents=RST) wrapper.run_docs(node) assert node.output_data().filesize() > 8000
def test_wordpress(MockXmlrpclib): with wrap() as wrapper: with open("wordpress.json", "wb") as f: json.dump({}, f) with open(".dexyapis", "wb") as f: json.dump({ 'wordpress' : { 'url' : 'http://example.com', 'username' : 'foo', 'password' : 'bar' }}, f) # Create new (unpublished) draft doc = mk_wp_doc(wrapper) wrapper.run_docs(doc) with open("wordpress.json", "rb") as f: result = json.load(f) assert result['postid'] == 42 assert result['publish'] == False # Update existing draft doc = mk_wp_doc(wrapper) wrapper.run_docs(doc) assert doc.output().json_as_dict().keys() == ['permaLink'] result['publish'] = True with open("wordpress.json", "wb") as f: json.dump(result, f) # Publish existing draft doc = mk_wp_doc(wrapper) wrapper.run_docs(doc) assert doc.output().as_text() == "http://example.com/blog/42" # Now, separately, test an image upload. orig = os.path.join(TEST_DATA_DIR, 'color-graph.pdf') shutil.copyfile(orig, 'example.pdf') doc = Doc("example.pdf|wp", wrapper=wrapper) with open(".dexyapis", "wb") as f: json.dump({ 'wordpress' : { 'url' : 'http://example.com', 'username' : 'foo', 'password' : 'bar' }}, f) wrapper.run_docs(doc) assert doc.output().as_text() == "http://example.com/example.pdf" # test list categories with divert_stdout() as stdout: WordPressFilter.docmd_list_categories() assert stdout.getvalue() == "categoryName\nfoo\nbar\n"
def test_dot_filter(): with wrap() as wrapper: doc = Doc("graph.dot|dot", contents = "digraph { a -> b } ", wrapper=wrapper) wrapper.docs = [doc] wrapper.run() assert doc.output().is_cached() assert doc.output().filesize() > 1000
def test_force_text(): with wrap() as wrapper: node = Doc("example.py|idio|t", wrapper, [], contents="print 'hello'\n") wrapper.run_docs(node) assert str(node.output_data()) == "print 'hello'\n"
def test_html(): with wrap() as wrapper: doc = Doc("example.py|pyg|h", contents="print 'hello'\n", wrapper=wrapper) wrapper.docs = [doc] wrapper.run() assert """<div class="highlight">""" in doc.output().as_text()
def test_pdfcrop_filter(): with wrap() as wrapper: orig = os.path.join(TEST_DATA_DIR, 'color-graph.pdf') shutil.copyfile(orig, 'example.pdf') wrapper=Wrapper() node = Doc("example.pdf|pdfcrop|pdfinfo", wrapper) wrapper.run_docs(node) assert node.output_data().is_cached()
def test_yamlargs_no_yaml(): with wrap() as wrapper: doc = Doc("example.txt|yamlargs", wrapper, [], contents = "This is the content.") wrapper.run_docs(doc) assert doc.output_data().as_text() == "This is the content."
def test_force_text(): with wrap() as wrapper: doc = Doc("example.py|idio|t", contents="print 'hello'\n", wrapper=wrapper) wrapper.docs = [doc] wrapper.run() assert doc.output().as_text() == "print 'hello'\n"
def test_rust(): with wrap() as wrapper: doc = Doc("example.rs|rustc", wrapper, [], contents = RUST ) wrapper.run_docs(doc) assert str(doc.output_data()) == "hello?\n"
def test_javac_filter(): # not using runfilter() because file has to be named 'hello.java' with wrap() as wrapper: doc = Doc("hello.java|javac", wrapper, [], contents=JAVA_SRC) wrapper.run_docs(doc) assert doc.output_data().is_cached()
def test_force_latex(): with wrap() as wrapper: doc = Doc("example.py|idio|l", contents="print 'hello'\n", wrapper=wrapper) wrapper.docs = [doc] wrapper.run() assert "begin{Verbatim}" in doc.output().as_text()
def test_scala_repl(): with wrap() as wrapper: doc = Doc("HelloWorld.scala|scalai", wrapper, [], contents = SCALA ) wrapper.run_docs(doc) assert "defined module HelloWorld" in str(doc.output_data())
def test_ht_latex(): with wrap() as wrapper: doc = Doc("article.tex|htlatex", contents=LATEX, wrapper=wrapper) wrapper.docs = [doc] wrapper.run() assert doc.output().is_cached()
def test_ps2pdf_filter(): with wrap() as wrapper: doc = Doc("hello.ps|ps2pdf", contents = PS, wrapper=wrapper) wrapper.docs = [doc] wrapper.run() assert doc.output().is_cached() assert doc.output().filesize() > 1000
def test_jinja_filter_tex_extension(): with wrap() as wrapper: node = Doc("template.tex|jinja", wrapper, [], contents = "1 + 1 is << 1+1 >>") wrapper.run_docs(node) assert node.output_data().as_text() == "1 + 1 is 2"
def test_html2pdf_filter(): with wrap() as wrapper: doc = Doc("hello.html|html2pdf", contents = "<p>hello</p>", wrapper=wrapper) wrapper.docs = [doc] wrapper.run() assert doc.output().is_cached() assert doc.output().filesize() > 1000
def test_custom_file_extension(): with wrap() as wrapper: doc = Doc("hello.py|pyg", contents="""print "hello, world" """, pyg = { "ext" : ".tex" }, wrapper=wrapper) wrapper.docs = [doc] wrapper.run() assert "begin{Verbatim}" in doc.output().as_text()
def test_java_filter(): # not using runfilter() because file has to be named 'hello.java' with wrap() as wrapper: doc = Doc("hello.java|java", wrapper, [], contents=JAVA_SRC) wrapper.run_docs(doc) assert str(doc.output_data()) == "Java Hello World!\n"
def test_jinja_sectioned_invalid_section(): with wrap() as wrapper: wrapper.debug = False doc = Doc("hello.txt|jinja", wrapper, [ Doc("lines.txt|lines", wrapper, [], contents = "line one\nline two" ) ], contents = """first line is '{{ d['lines.txt|lines']['3'] }}'""" ) wrapper.run_docs(doc) assert wrapper.state == 'error'
def test_shint_filter(): with wrap() as wrapper: src = """ ### @export "touch" touch newfile.txt ### @export "ls" ls """ doc = Doc("example.sh|idio|shint|pyg", wrapper, [], contents = src) wrapper.run_docs(doc) assert list(doc.output_data().keys()) == ['1', 'touch', 'ls']
def test_jinja_undefined(): with wrap() as wrapper: wrapper.debug = False node = Doc("template.txt|jinja", wrapper, [], contents="""{{ foo }}""") wrapper.run_docs(node) assert wrapper.state == 'error'
def test_casperjs_svg2pdf_filter(): raise SkipTest() # TODO fix this - if casper is missing should raise error before reach assertions # TODO find smaller file - make test go faster? with wrap() as wrapper: orig = os.path.join(TEST_DATA_DIR, 'butterfly.svg') shutil.copyfile(orig, 'butterfly.svg') from dexy.wrapper import Wrapper wrapper = Wrapper() node = Doc("butterfly.svg|svg2pdf", wrapper) wrapper.run_docs(node) assert node.output_data().is_cached() assert node.output_data().filesize() > 1000
def test_nonzero_exit(): with wrap() as wrapper: wrapper.debug = False node = Doc("example.py|py", wrapper, [], contents="import sys\nsys.exit(1)") wrapper.run_docs(node) assert wrapper.state == 'error'
def test_rst_meta(): with wrap() as wrapper: node = Doc("example.rst|rstmeta", wrapper, [], contents = rst_meta ) wrapper.run_docs(node) assert node.setting('author') == "J Random Hacker" assert node.setting('authors') == "Bert & Ernie" assert node.setting('subtitle') == "Foo" assert node.setting('title') == "Main Title" assert node.setting('date') == "2002-08-18" assert node.setting('status') == "Work In Progress" assert node.setting('version') == "1" assert node.setting('copyright') == "This document has been placed in the public domain."
def test_ignore_nonzero_exit(): with wrap() as wrapper: wrapper.ignore_nonzero_exit = True node = Doc("example.py|py", wrapper, [], contents="import sys\nsys.exit(1)") wrapper.run_docs(node) assert True # no NonzeroExit was raised...
def test_create_virtual_initial_artifact_with_dict(): with temprun() as runner: od_contents = OrderedDict() od_contents['1'] = "these are the contents" doc = Doc("abc.txt", contents=od_contents, runner=runner) runner.run_tasks(doc) assert doc.children[ 0].output_data.__class__.__name__ == "SectionedData"
def test_keyvalue_data_stores_dict(): with wrap() as wrapper: doc = Doc("hello.json", wrapper, [], data_type="keyvalue", contents="dummy contents") wrapper.run_docs(doc) data = doc.output_data() assert data.alias == 'keyvalue' assert list(data.keys()) == [] data.append("foo", 123) data.append("bar", 456) assert sorted(data.keys()) == ["bar", "foo"]
def test_assert_selector(): with wrap() as wrapper: node = Doc( "hello.txt|jinja", wrapper, [ Doc("input.html", wrapper, [], contents=inspect.cleandoc(""" <div id="foo"> This is contents of foo div. </div> """)) ], contents= "{{ d['input.html'] | assert_selector_text('#foo', 'This is contents of foo div.') }}" ) wrapper.run_docs(node)
def test_idio_invalid_input(): with wrap() as wrapper: wrapper.debug = False doc = Doc("hello.py|idio", wrapper, [], contents="### @ ") wrapper.run_docs(doc) assert wrapper.state == 'error'
def test_split_html_additional_filters(): with wrap() as wrapper: contents = """ <p>This is at the top.</p> <!-- split "a-page" --> some content on a page <!-- split "another-page" --> some content on another page <!-- endsplit --> bottom """ node = Doc( "example.html|splithtml", wrapper, [], contents=contents, splithtml={ "keep-originals": False, "additional-doc-filters": "processtext" }, ) wrapper.run_docs(node) assert node.children[0].key == "a-page.html|processtext" assert node.children[1].key == "another-page.html|processtext" od = str(node.output_data()) assert "<p>This is at the top.</p>" in od assert '<a href="a-page.html">' in od assert '<a href="another-page.html">' in od assert "bottom" in od a_page = node.children[0] a_page_data = str(a_page.output_data()) assert "<p>This is at the top.</p>" in a_page_data assert "some content on a page" in a_page_data assert "bottom" in a_page_data assert "Dexy processed the text" in a_page_data another_page = node.children[1] another_page_data = str(another_page.output_data()) assert "<p>This is at the top.</p>" in another_page_data assert "some content on another page" in another_page_data assert "bottom" in another_page_data assert "Dexy processed the text" in another_page_data
def test_output_reporter(): with wrap() as wrapper: wrapper.reports = "output" doc = Doc("hello.txt", wrapper, [], contents="hello") wrapper.run_docs(doc) wrapper.report() assert os.path.exists("output") assert os.path.exists("output/hello.txt")
def test_taverna(): raise SkipTest() with wrap() as wrapper: orig = os.path.join(TEST_DATA_DIR, 'simple_python_example_285475.t2flow') shutil.copyfile(orig, 'simple-python.t2flow') node = Doc("simple-python.t2flow|taverna", wrapper) wrapper.run_docs(node)
def test_globals(): with wrap() as wrapper: wrapper.globals = "foo=bar" node = Doc("hello.txt|testglobals", wrapper, [], contents="hello") wrapper.run_docs(node) env = node.filters[-1].run_plugins() assert env['foo'][1] == 'bar'
def test_cfussy_filter(): raise SkipTest() assert_output('cfussy', C_FUSSY_HELLO_WORLD, "HELLO, world\n", ext=".c") with wrap() as wrapper: wrapper.debug = False doc = Doc("hello.c|cfussy", contents=C_HELLO_WORLD, wrapper=wrapper) wrapper.run_docs(doc) assert wrapper.state == 'error'
def test_split_html_filter(): with wrap() as wrapper: contents = """ <p>This is at the top.</p> <!-- split "index" --> index page content only <!-- split "a-page" --> some content on a page <!-- split "another-page" --> This is information about "another-page" which should appear on the index page. <!-- content --> some content on another page <!-- footer --> footer on index page only <!-- endsplit --> bottom """ node = Doc("subdir/example.html|splithtml", wrapper, [], contents=contents) wrapper.run_docs(node) assert node.children[0].key == "subdir/a-page.html" assert node.children[1].key == "subdir/another-page.html" od = str(node.output_data()) assert "<p>This is at the top.</p>" in od assert 'index page content only' in od assert '<a href="a-page.html">' in od assert '<a href="another-page.html">' in od assert "This is information about \"another-page\"" in od assert "bottom" in od od = str(node.children[0].output_data()) assert "<p>This is at the top.</p>" in od assert not 'index page content only' in od assert "some content on a page" in od assert "bottom" in od od = str(node.children[1].output_data()) assert "<p>This is at the top.</p>" in od assert "some content on another page" in od assert "bottom" in od
def setup_docs(self): """ Processes args which may be doc objects or filenames with wildcards. """ if not hasattr(self, 'docs'): self.docs = [] for arg in self.args: self.log.debug("Processing arg %s" % arg) if isinstance(arg, Doc) or isinstance(arg, PatternDoc): doc = arg elif isinstance(arg, list): if not isinstance(arg[0], basestring): raise Exception("First arg %s should be a string" % arg[0]) if not isinstance(arg[1], dict): raise Exception("Second arg %s should be a dict" % arg[1]) if not "*" in arg[0]: doc = Doc(arg[0], **arg[1]) else: # This is a pattern doc or real doc TODO better way to verify? doc = PatternDoc(arg[0], **arg[1]) elif isinstance(arg, basestring): doc = PatternDoc(arg) else: raise Exception("unknown arg type %s for arg %s" % (arg.__class__.__name__, arg)) doc.runner = self doc.setup() self.docs.append(doc)
def test_doc_children_artifacts(): with temprun() as runner: doc = Doc("hello.txt|newdoc", contents="hello", runner=runner) parent = Doc("parent.txt|process", doc, contents="hello", runner=runner) runner.docs = [parent] assert len(doc.children) == 2 assert isinstance(doc.children[0], InitialVirtualArtifact) assert isinstance(doc.children[1], FilterArtifact) assert len(doc.artifacts) == 2 assert isinstance(doc.artifacts[0], InitialVirtualArtifact) assert isinstance(doc.artifacts[1], FilterArtifact) assert len(parent.children) == 3 assert isinstance(parent.children[0], Doc) assert parent.children[0] == doc assert isinstance(parent.children[1], InitialVirtualArtifact) assert isinstance(parent.children[2], FilterArtifact) assert len(parent.artifacts) == 2 assert isinstance(parent.artifacts[0], InitialVirtualArtifact) assert isinstance(parent.artifacts[1], FilterArtifact) runner.run() assert len(doc.children) == 3 assert isinstance(doc.children[0], InitialVirtualArtifact) assert isinstance(doc.children[1], FilterArtifact) assert isinstance(doc.children[2], Doc) assert len(doc.artifacts) == 2 assert len(parent.children) == 3 assert len(parent.artifacts) == 2 assert runner.registered_docs()[0].key == "hello.txt|newdoc" assert runner.registered_docs()[1].key == "parent.txt|process" assert runner.registered_docs()[2].key == "newfile.txt|processtext"
def test_pygments_bad_file_extension(): with wrap() as wrapper: wrapper.debug = False doc = Doc("hello.xyz|pyg", wrapper, [], contents=" ", pyg={'allow_unknown_ext': False}) wrapper.run_docs(doc) assert wrapper.state == 'error'
def test_jinja_syntax_error(): with wrap() as wrapper: wrapper.debug = False node = Doc("template.txt|jinja", wrapper, [], contents="""{% < set foo = 'bar' -%}\nfoo is {{ foo }}\n""") wrapper.run_docs(node) assert wrapper.state == 'error'
def test_multiple_sections(): with wrap() as wrapper: src = """ ### @export "vars" x = 6 y = 7 ### @export "multiply" x*y """ doc = Doc("example.py|idio", wrapper, [], contents=src) wrapper.run_docs(doc) assert doc.output_data().keys() == ['1', 'vars', 'multiply']
def test_pandoc_filter_pdf(): with wrap() as wrapper: node = Doc("hello.md|pandoc", wrapper, [], contents="hello", pandoc={"ext": ".pdf"}) wrapper.run_docs(node) wrapper.report() assert os.path.exists("output/hello.pdf")
def test_python_filter_record_vars(): with wrap() as wrapper: doc = Doc("example.py|pycon", wrapper, [], pycon={'record-vars': True}, contents=PYTHON_CONTENT) wrapper.run_docs(doc) assert "doc:example.py-vars.json" in wrapper.nodes
def test_wordpress_without_doc_config_file(): with wrap() as wrapper: wrapper.debug = False doc = Doc("hello.txt|wp", contents="hello, this is a blog post", wrapper=wrapper) wrapper.run_docs(doc) assert wrapper.state == 'error'
def test_custom_name_with_evil_args(): with wrap() as wrapper: doc = Doc("data.txt", wrapper, [], output_name="/%(bar)s/data-%(foo)s.abc", foo='bar', bar='baz', contents="12345.67") wrapper.run_docs(doc)
def test_output_reporter(): with temprun() as runner: doc = Doc("hello.txt", contents="hello", runner=runner) reporter = OutputReporter() runner.docs = [doc] runner.run() runner.report(reporter) assert os.path.exists("output") assert os.path.exists("output/hello.txt")
def test_jinja_pass_through_fails_if_not_whitelisted(): with wrap() as wrapper: contents = "{{ linxxx('foo') }}" doc = Doc("lines.txt|jinja", wrapper, [], contents=contents) try: wrapper.run_docs(doc) except UserFeedback as e: assert "a UndefinedError problem" in str(e) assert "'linxxx' is undefined" in str(e)
def test_sed_filter_single_input_file_with_sections(): contents = json.loads("""[{}, { "name" : "foo", "contents" : "hello" }, { "name" : "bar", "contents" : "telephone" } ]""") with wrap() as wrapper: node = Doc("example.sed|sed", wrapper, [ Doc("input.txt", wrapper, [], contents=contents, data_class_alias='sectioned') ], contents="s/e/E/g") wrapper.run_docs(node) assert str(node.output_data()['foo']) == 'hEllo' assert str(node.output_data()['bar']) == 'tElEphonE'
def test_pandoc_filter_odt(): # TODO Why isn't this checking for inactive filters? with wrap() as wrapper: node = Doc("hello.md|pandoc", wrapper, [], contents="hello", pandoc={"ext": ".odt"}) wrapper.run_docs(node) wrapper.report() assert os.path.exists("output/hello.odt")