def test_pandoc_filter_txt(): with wrap() as wrapper: node = Doc("hello.md|pandoc", wrapper, [], contents = "hello", pandoc = { "ext" : ".txt"}, ) wrapper.run_docs(node) wrapper.report() assert os.path.exists("output/hello.txt") assert str(node.output_data()) == 'hello\n'
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_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 run_jinja_filter(contents): with wrap() as wrapper: doc = Doc("hello.txt|jinja", wrapper, [], contents = contents ) wrapper.run_docs(doc) data = doc.output_data() data.data() # make sure is loaded return data
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_access_other_documents(): with wrap() as wrapper: node = Doc("hello.txt|newdoc", wrapper, [], contents="hello") parent = Doc("test.txt|others", wrapper, [node], contents="hello" ) wrapper.run_docs(parent) expected_items = [ "Here is a list of previous docs in this tree (not including test.txt|others).", "newfile.txt|processtext (0 children, 0 inputs, length 33)", "hello.txt|newdoc (1 children, 0 inputs, length 19)" ] output = unicode(parent.output_data()) for item in expected_items: assert item in output
def test_node_caching__slow(): with wrap() as wrapper: with open("hello.py", "w") as f: f.write("print 1+2\n") with open("doc.txt", "w") as f: f.write("1 + 1 = {{ d['hello.py|py'] }}") wrapper = Wrapper(log_level='DEBUG') hello_py = Doc("hello.py|py", wrapper) doc_txt = Doc("doc.txt|jinja", wrapper, [hello_py]) wrapper.run_docs(doc_txt) assert str(doc_txt.output_data()) == "1 + 1 = 3\n" assert str(hello_py.output_data()) == "3\n" assert hello_py.state == 'ran' assert doc_txt.state == 'ran' wrapper = Wrapper(log_level='DEBUG') hello_py = Doc("hello.py|py", wrapper) doc_txt = Doc("doc.txt|jinja", wrapper, [hello_py]) wrapper.run_docs(doc_txt) assert hello_py.state == 'consolidated' assert doc_txt.state == 'consolidated' time.sleep(1.1) with open("doc.txt", "w") as f: f.write("1 + 1 = {{ d['hello.py|py'] }}\n") wrapper = Wrapper(log_level='DEBUG') hello_py = Doc("hello.py|py", wrapper) doc_txt = Doc("doc.txt|jinja", wrapper, [hello_py]) wrapper.run_docs(doc_txt) assert hello_py.state == 'consolidated' assert doc_txt.state == 'ran' time.sleep(1.1) with open("hello.py", "w") as f: f.write("print 1+1\n") wrapper = Wrapper(log_level='DEBUG') hello_py = Doc("hello.py|py", wrapper) doc_txt = Doc("doc.txt|jinja", wrapper, [hello_py]) wrapper.run_docs(doc_txt) assert hello_py.state == 'ran' assert doc_txt.state == 'ran'
def test_create_doc_with_one_filter(): with wrap() as wrapper: doc = Doc("foo.txt|dexy", wrapper, [], contents="foo") assert len(doc.filters) == 1 f = doc.filters[0] assert f.doc == doc assert not f.prev_filter assert not f.next_filter wrapper.run_docs(doc)
def test_generic_data_unicode(): with wrap() as wrapper: doc = Doc("hello.txt", wrapper, [], contents=u"\u2042 we know\n") wrapper.run_docs(doc) data = doc.output_data() assert data.alias == 'generic' assert unicode(data) == u"\u2042 we know\n" assert isinstance(str(data), str) assert isinstance(unicode(data), unicode)
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")
def test_mkdirs(): with wrap() as wrapper: doc = Doc("hello.c|c", wrapper, contents = C_HELLO_WORLD, c = {'mkdir' : 'foo', 'mkdirs' : ['bar', 'baz']} ) wrapper.run_docs(doc) dirs = os.listdir(doc.filters[-1].workspace()) assert 'foo' in dirs assert 'bar' in dirs assert 'baz' in dirs
def test_rint_mock(): with wrap() as wrapper: node = Doc("example.R|idio|rintmock", wrapper, [], contents=R_SECTIONS ) wrapper.run_docs(node) assert node.output_data().is_cached() assert unicode(node.output_data()['assign-vars']) == u"> x <- 6\n> y <- 7\n> \n" assert unicode(node.output_data()['multiply']) == u"> x * y\n[1] 42\n> \n"
def test_subdirectories(): with wrap() as wrapper: os.makedirs("s1") os.makedirs("s2") node = Doc("file.txt|testsubdir", wrapper, [], contents="hello") wrapper.run_docs(node) env = node.filters[-1].run_plugins() assert 's1' in env['subdirectories'][1] assert 's2' in env['subdirectories'][1]
def test_key_value_example(): with wrap() as wrapper: doc = Doc( "hello.txt|keyvalueexample", wrapper, [], contents="hello" ) wrapper.run_docs(doc) assert str(doc.output_data()) == "foo: bar"
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_jinja_filter_custom_delims(): with wrap() as wrapper: node = Doc("template.tex|jinja", wrapper, [], contents="1 + 1 is %- 1+1 -%", jinja={ "variable_start_string": "%-", "variable_end_string": "-%" }) wrapper.run_docs(node) assert node.output_data().as_text() == "1 + 1 is 2"
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_pass_through(): with wrap() as wrapper: with open("_template.html", "w") as f: f.write("{{ content }}") wrapper.reports = 'ws' contents = "{{ 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 str(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_command_line_args(): with wrap() as wrapper: node = Doc("example.py|py", wrapper, [], py={"args": "-B"}, contents="print('hello')") wrapper.run_docs(node) assert str(node.output_data()) == "hello" + os.linesep command_used = node.filters[-1].command_string() assert command_used == "python -B \"example.py\" "
def test_template_filter_with_custom_filter_only(): with wrap() as wrapper: node = Doc("hello.txt|testtemplatefilter", wrapper, [], contents="aaa equals %(aaa)s", testtemplatefilter={"plugins": ["testtemplate"]}) wrapper.run_docs(node) assert node.output_data().as_text() == "aaa equals 1" plugins_used = node.filters[-1].template_plugins() assert len(plugins_used) == 1 assert isinstance(plugins_used[0], TestSimple)
def test_assert_selector_invalid(): 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', 'Not right.') }}" ) try: wrapper.run_docs(node) raise Exception("should raise AssertionError") except AssertionError as e: assert str(e) == "element '#foo' did not contain 'Not right.'"
def run_filter(alias): with open(markdown_file, 'r') as f: example_markdown = f.read() with wrap() as wrapper: node = Doc("example.md|%s" % alias, wrapper, [], contents = example_markdown ) wrapper.run_docs(node) assert node.output_data().is_cached() return node.output_data()
def test_scriptargs(): with wrap() as wrapper: node = Doc( "example.py|py", wrapper, [], py={"scriptargs": "--foo"}, contents="""import sys\nprint("args are: '%s'" % sys.argv[1])""") wrapper.run_docs(node) assert "args are: '--foo'" in str(node.output_data()) command_used = node.filters[-1].command_string() assert command_used == "python \"example.py\" --foo"
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 doc.output_data().keys() == ['1', 'touch', 'ls']
def test_walk_working_dir(): with wrap() as wrapper: node = Doc("example.sh|sh", wrapper, [], contents="echo 'hello' > newfile.txt", sh={ "walk-working-dir": True, }) wrapper.run_docs(node) files_list = wrapper.nodes['doc:example.sh-sh.txt-files'] assert files_list.output_data()['newfile.txt'] == "hello" + os.linesep
def test_pytest_filter(): raise SkipTest() # this is running dexy's tests, not cashew's tests with wrap() as wrapper: doc = Doc("modules.txt|pytest", wrapper, [], contents="cashew") wrapper.run_docs(doc) data = doc.output_data() testname = "test_cashew.test_standardize_alias_or_aliases" assert data[testname + ':doc'] == "docstring for test" assert data[testname + ':name'] == "test_standardize_alias_or_aliases" assert data[testname + ':comments'] == "# comment before test\n" assert bool(data[testname + ':passed']) assert "def test_standardize_alias_or_aliases():" in data[testname + ':source']
def test_header_footer_filters(): with wrap() as wrapper: os.makedirs('subdir/subsubdir') node = Doc("subdir/file.txt|hd|ft", wrapper, [ Doc("_header.txt", wrapper, [], contents="This is a header in parent dir."), Doc("subdir/_header.txt|jinja", wrapper, [], contents="This is a header."), Doc("subdir/_footer.txt|jinja", wrapper, [], contents="This is a footer."), Doc("subdir/subsubdir/_header.txt", wrapper, [], contents="This is a header in a subdirectory.") ], contents="These are main contents.") wrapper.run_docs(node) assert str(node.output_data( )) == "This is a header.\nThese are main contents.\nThis is a footer."
def run_example(doc_key, doc_contents): wrapper = make_wrapper() if isinstance(doc_contents, str): data_class = 'generic' else: data_class = 'sectioned' doc = Doc(doc_key, wrapper, [], contents=doc_contents, data_class=data_class) wrapper.run_docs(doc) return doc
def test_xxml_no_pygments(): with wrap() as wrapper: doc = Doc("example.xml|xxml", wrapper, [], contents=XML, xxml={ 'pygments': False, 'ext': '.sqlite3' }) wrapper.run_docs(doc) assert "foo:source" in doc.output_data().keys() assert not "foo:html-source" in doc.output_data().keys() assert not "foo:latex-source" in doc.output_data().keys()
def test_custom_name_with_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) wrapper.report() assert doc.output_data().output_name() == "baz/data-bar.abc" assert doc.output_data().parent_output_dir() == "baz" assert os.path.exists("output/baz/data-bar.abc")