def test_rst2html_png(self): fLOG( __file__, self._testMethodName, OutputPrint=__name__ == "__main__") if is_travis_or_appveyor() in ('travis', 'appveyor'): # It requires latex. return temp = get_temp_folder(__file__, "temp_rst2html_png") rst = os.path.join(os.path.abspath( os.path.dirname(__file__)), "data", "hermionne.rst") with open(rst, "r", encoding="utf-8") as f: content = f.read() text = rst2html(content) ji = os.path.join(temp, "out.html") with open(ji, "w", encoding="utf-8") as f: f.write(text) text2 = rst2html(content, layout="sphinx") ji = os.path.join(temp, "out_sphinx.html") with open(ji, "w", encoding="utf-8") as f: f.write(text) self.assertTrue(len(text2) > len(text))
def test_post_parse(self): fLOG(__file__, self._testMethodName, OutputPrint=__name__ == "__main__") directives.register_directive("runpython", RunPythonDirective) directives.register_directive("postcontents", PostContentsDirective)
def test_bug_accent(self): fLOG(__file__, self._testMethodName, OutputPrint=__name__ == "__main__") data = os.path.abspath(os.path.dirname(__file__)) fold = os.path.join(data, "data", "bug") assert os.path.exists(fold) to = os.path.join(data, "temp_bug") if os.path.exists(to): remove_folder(to) os.mkdir(to) assert os.path.exists(to) synchronize_folder(fold, to, hash_size=0, repo1=False, repo2=False, size_different=True, no_deletion=False, filter=None, filter_copy=None, avoid_copy=False, operations=None, file_date=None, log1=False) assert os.path.exists( os.path.join(to, "bugged", "Présentation.pdf.txt"))
def test_enumerate_notebooks_link(self): fLOG( __file__, self._testMethodName, OutputPrint=__name__ == "__main__") this = os.path.abspath(os.path.dirname(__file__)) nb_folder = os.path.join(this, "..", "..", "_doc", "notebooks") self.assertTrue(os.path.exists(nb_folder)) nb_doc = os.path.join(this, "..", "..", "_doc", "sphinxdoc", "source") self.assertTrue(os.path.exists(nb_doc)) nb = 0 counts = {'title': 0} nbfound = set() for r in enumerate_notebooks_link(nb_folder, nb_doc): rl = list(r) rl[0] = None if r[0] is None else os.path.split(r[0])[-1] rl[1] = os.path.split(r[1])[-1] nb += 1 m = rl[2] counts[m] = counts.get(m, 0) + 1 self.assertTrue(r[-2] is None or isinstance(r[-2], str)) self.assertTrue(r[-1] is None or isinstance(r[-1], str)) if r[-1] is not None: counts["title"] += 1 nbfound.add(rl[1]) self.assertTrue(counts.get("ref", 0) > 0) self.assertIn(counts.get(None, 0), (0, 10)) self.assertTrue(counts["title"] > 0) self.assertTrue(len(nbfound) > 8) # self.assertTrue(counts.get("refn", 0) > 0) self.assertIn(counts.get("toctree", 0), (0, 14))
def test_get_default_value_type(self): fLOG(__file__, self._testMethodName, OutputPrint=__name__ == "__main__") self.assertIsNone(get_default_value_type(None)) self.assertEqual(get_default_value_type(int), 0)
def test_param_sphinx(self): fLOG( __file__, self._testMethodName, OutputPrint=__name__ == "__main__") from docutils import nodes as skip_ content = """ Addition 4 :param a: parameter a :param b: parameter b :returns: ``a+b`` """.replace(" ", "") html = rst2html(content, # fLOG=fLOG, writer="custom", keep_warnings=True, directives=None, layout="sphinx") temp = get_temp_folder(__file__, "temp_param_sphinx") with open(os.path.join(temp, "out_param_sphinx.html"), "w", encoding="utf8") as f: f.write(html) t1 = ":param a:" if t1 in html: raise Exception(html)
def test_run_cmd_exc(self): fLOG(__file__, self._testMethodName, OutputPrint=__name__ == "__main__") cmd = "unexpectedcommand" ex = "not affected" try: out, err = run_cmd(cmd, wait=True, log_error=False, catch_exit=True, communicate=False, tell_if_no_output=120, fLOG=fLOG) no_exception = True ex = None except Exception as e: no_exception = False out, err = parse_exception_message(e) ex = e self.assertTrue(not no_exception) if sys.platform.startswith("win"): if out is None or err is None: raise Exception("A\n" + str(ex)) if len(out) > 0: raise Exception("B\n" + str(ex)) if len(err) == 0: raise Exception("C\n" + str(ex)) else: self.assertTrue(out is None) self.assertTrue(err is None) self.assertTrue(isinstance(ex, Exception))
def test_thumbnail(self): fLOG( __file__, self._testMethodName, OutputPrint=__name__ == "__main__") from docutils import nodes as skip_ content = """ test a directive ================ before .. thumbnail:: http://www.xavierdupre.fr/app/pyquickhelper/helpsphinx/_static/project_ico.png :width: 10 :height: 20 :download: 1 after this code shoud appear """.replace(" ", "") if sys.version_info[0] >= 3: content = content.replace('u"', '"') logger2 = logging.getLogger("video") log_capture_string = StringIO() ch = logging.StreamHandler(log_capture_string) ch.setLevel(logging.DEBUG) logger2.addHandler(ch) with warnings.catch_warnings(record=True): html = rst2html(content, # fLOG=fLOG, writer="custom", keep_warnings=True, directives=None) warns = log_capture_string.getvalue() if warns: raise Exception(warns) t1 = "this code shoud not appear" if t1 in html: raise Exception(html) t1 = "this code shoud appear" if t1 not in html: raise Exception(html) t1 = "_images" if t1 not in html: raise Exception(html) t1 = "linkedin" if t1 in html: raise Exception(html) temp = get_temp_folder(__file__, "temp_sphinx_thumbnail") with open(os.path.join(temp, "out_image.html"), "w", encoding="utf8") as f: f.write(html)
def test_video_url(self): fLOG(__file__, self._testMethodName, OutputPrint=__name__ == "__main__") from docutils import nodes as skip_ content = """ test a directive ================ before .. video:: http://www.xavierdupre.fr/ensae/video_hackathon_ensae_ey_label_emmaus_2017.mp4 :width: 300 after this code shoud appear """.replace(" ", "") if sys.version_info[0] >= 3: content = content.replace('u"', '"') logger2 = logging.getLogger("video") log_capture_string = StringIO() ch = logging.StreamHandler(log_capture_string) ch.setLevel(logging.DEBUG) logger2.addHandler(ch) with warnings.catch_warnings(record=True): html = rst2html( content, # fLOG=fLOG, writer="custom", keep_warnings=True, directives=None) warns = log_capture_string.getvalue().strip("\n\r\t ") if len(warns) != 0 and 'Unable to find' not in warns: raise Exception("warnings '{0}'".format(warns)) t1 = "this code shoud not appear" if t1 in html: raise Exception(html) t1 = "this code shoud appear" if t1 not in html: raise Exception(html) t1 = "video_hackathon_ensae_ey_label_emmaus_2017.mp4" if t1 not in html: raise Exception(html) t1 = "unable to find" if t1 in html: raise Exception(html) temp = get_temp_folder(__file__, "temp_video_url") with open(os.path.join(temp, "out_video.html"), "w", encoding="utf8") as f: f.write(html)
def test__private_migrating_doxygen_doc(self): """First line. Second line. @param self self """ fLOG(__file__, self._testMethodName, OutputPrint=__name__ == "__main__") doc = TestHelpGenPrivate.__doc__.split("\n") res = _private_migrating_doxygen_doc(doc, 0, "<test>") exp = """ First line. Second line. :githublink:`%|py|0` """.replace(" ", "") self.assertEqual("\n".join(res).strip("\n "), exp.strip("\n ")) doc = TestHelpGenPrivate.test__private_migrating_doxygen_doc.__doc__.split( "\n") res = _private_migrating_doxygen_doc(doc, 0, "<test>") exp = """ First line. Second line. :param self: self :githublink:`%|py|0` """.replace(" ", "") self.assertEqual("\n".join(res).strip("\n "), exp.strip("\n "))
def test_pyensae_links(self): fLOG( __file__, self._testMethodName, OutputPrint=__name__ == "__main__") this = os.path.join(os.path.dirname(__file__), '..', '..', '_doc', 'notebooks') checked = 0 missed = [] tolook = ['http://files.grouplens.org/datasets/movielens/ml-1m.zip', 'http://www.xavierdupre.fr/', 'url=\\"http', '\\"Skin_NonSkin.txt\\", website=\\"https://archive.ics', "website='http://telechargement.insee.fr/fichiersdetail", 'https://archive.ics.uci.edu/ml/machine-learning-databases'] for note in explore_folder_iterfile(this, ".*[.]ipynb$", ".ipynb_checkpoints", fullname=True): with open(note, 'r', encoding='utf-8') as f: content = f.read() if "datasource import download_data" in content or "pyensae.download_data(" in content: checked += 1 found = False for to in tolook: if to in content: found = True if not found: missed.append(note) self.assertGreater(checked, 1) self.assertEmpty(missed)
def test_sphinx_doc(self): fLOG( __file__, self._testMethodName, OutputPrint=__name__ == "__main__") path = os.path.split(__file__)[0] file = os.path.join( path, "..", "..", "src", "pyquickhelper", "helpgen", "utils_sphinx_doc.py") assert os.path.exists(file) with open(file, "r", encoding="utf8") as f: content = f.read() stats, newc = utils_sphinx_doc.migrating_doxygen_doc(content, file) snewc = newc[:len(newc) // 2] assert "pass" in newc assert ":param" in newc if "@param" in snewc: raise Exception(snewc) assert "docrows" in stats
def test_post_parse_sn_youtube(self): fLOG( __file__, self._testMethodName, OutputPrint=__name__ == "__main__") directives.register_directive("youtube", YoutubeDirective)
def test_runpython_image(self): """ this test also test the extension runpython """ fLOG( __file__, self._testMethodName, OutputPrint=__name__ == "__main__") from docutils import nodes class runpythonthis_node(nodes.Structural, nodes.Element): pass class RunPythonThisDirective (RunPythonDirective): runpython_class = runpythonthis_node def visit_rp_node(self, node): self.body.append("<p><b>visit_rp_node</b></p>") def depart_rp_node(self, node): self.body.append("<p><b>depart_rp_node</b></p>") if "enable_disabled_documented_pieces_of_code" in sys.__dict__: raise Exception("this case shoud not be") temp = get_temp_folder(__file__, "temp_runpython_image") content = """ test a directive ================ .. runpythonthis:: :rst: :showcode: import matplotlib.pyplot as plt fig, ax = plt.subplots(1, 1, figsize=(4, 4)) ax.plot([0, 1], [0, 1], '--') if __WD__ is None: raise Exception(__WD__) fig.savefig("__FOLD__/oo.png") text = ".. image:: oo.png\\n :width: 200px" print(text) """.replace(" ", "").replace("__FOLD__", temp.replace("\\", "/")) if sys.version_info[0] >= 3: content = content.replace('u"', '"') tives = [("runpythonthis", RunPythonThisDirective, runpythonthis_node, visit_rp_node, depart_rp_node)] html = rst2html(content, # fLOG=fLOG, writer="custom", keep_warnings=True, directives=tives) with open(os.path.join(temp, "out.html"), "w", encoding="utf8") as f: f.write(html) img = os.path.join(temp, "oo.png") self.assertExists(img)
def test_rst2html_autoclass(self): fLOG( __file__, self._testMethodName, OutputPrint=__name__ == "__main__") if is_travis_or_appveyor() in ('travis', 'appveyor'): # It requires latex. return if sys.version_info[:2] <= (2, 7): # i don't want to fix it for Python 2.7 return content = """ ====== title1 ====== .. autoclass:: pyquickhelper.sphinxext.sphinx_runpython_extension.RunPythonDirective :members: """.replace(" ", "") temp = get_temp_folder(__file__, "temp_rst2html_autoclass") text = rst2html(content, outdir=temp, layout="sphinx", writer="rst") ji = os.path.join(temp, "out.rst") with open(ji, "w", encoding="utf-8") as f: f.write(text) self.assertIn("* ``:indent:<int>`` to indent the output", text)
def test_param_sphinx(self): fLOG(__file__, self._testMethodName, OutputPrint=__name__ == "__main__") from docutils import nodes as skip_ content = """ Addition 4 :param a: parameter a :param b: parameter b :returns: ``a+b`` """.replace(" ", "") html = rst2html( content, # fLOG=fLOG, writer="custom", keep_warnings=True, directives=None, layout="sphinx") temp = get_temp_folder(__file__, "temp_param_sphinx") with open(os.path.join(temp, "out_param_sphinx.html"), "w", encoding="utf8") as f: f.write(html) t1 = ":param a:" if t1 in html: raise Exception(html)
def test_reap_children(self): fLOG(__file__, self._testMethodName, OutputPrint=__name__ == "__main__") if sys.platform.startswith("win"): cmd = "pause" else: cmd = 'ls -la | less' temp = get_temp_folder(__file__, "temp_reap_children") clog = CustomLog(temp) proc, _ = run_cmd(cmd, wait=False, fLOG=clog) self.assertTrue(_ is None) clog('proc={} pid={}'.format(proc, proc.pid)) ki = reap_children(fLOG=clog, subset={proc.pid}) clog('ki={0}'.format(ki)) if ki is None and not is_travis_or_appveyor( ) and __name__ != '__main__': warnings.warn( "reap_children could not be fully tested ki is None.") return self.assertTrue(ki is not None) self.assertEqual(len(ki), 1) # fLOG(ki) # To avoid a warning. proc.returncode = 0
def test_rst2html_png(self): fLOG(__file__, self._testMethodName, OutputPrint=__name__ == "__main__") if is_travis_or_appveyor() in ('travis', 'appveyor'): # It requires latex. return temp = get_temp_folder(__file__, "temp_rst2html_png") rst = os.path.join(os.path.abspath(os.path.dirname(__file__)), "data", "hermionne.rst") with open(rst, "r", encoding="utf-8") as f: content = f.read() text = rst2html(content) ji = os.path.join(temp, "out.html") with open(ji, "w", encoding="utf-8") as f: f.write(text) text2 = rst2html(content, layout="sphinx") ji = os.path.join(temp, "out_sphinx.html") with open(ji, "w", encoding="utf-8") as f: f.write(text) self.assertTrue(len(text2) > len(text))
def test_synchronize_nosvn(self): fLOG(__file__, self._testMethodName, OutputPrint=__name__ == "__main__") thispath = os.path.abspath(os.path.split(__file__)[0]) if len(thispath) == 0: thispath = "./../ut_loghelper" cache = os.path.join(thispath, "temp_synchro_nosvn") if not os.path.exists(cache): os.mkdir(cache) remone = os.path.join(cache, "test_log.py") fLOG("existing file", remone) if os.path.exists(remone): os.remove(remone) action = foldermod.synchronize_folder(os.path.join( thispath, "..", "ut_loghelper"), cache, hash_size=0, repo1=False, filter=lambda v: "temp" not in v) assert os.path.exists(cache) assert len(os.listdir(cache)) > 0 assert os.path.exists(os.path.join(cache, "data")) assert len(os.listdir(os.path.join(cache, "data"))) > 0 assert len(action) > 0
def test_numpy_random(self): fLOG( __file__, self._testMethodName, OutputPrint=__name__ == "__main__") np.random.seed(42)
def test_load_content_file_with_encoding(self): fLOG(__file__, self._testMethodName, OutputPrint=__name__ == "__main__") file = __file__.replace(".pyc", ".py") cont, enc = load_content_file_with_encoding(file) assert len(file) > 0
def test_parse_readme_cb(self): fLOG( __file__, self._testMethodName, OutputPrint=__name__ == "__main__") fold = os.path.dirname(os.path.abspath(__file__)) readme = os.path.join(fold, "data", "README.rst") fLOG(readme) assert os.path.exists(readme) with open(readme, "r", encoding="utf8") as f: content = f.read() r = parse_markdown(content) if "<p>.. _l-README:</p>" not in str(r): m = [ord(c) for c in content] m = ",".join(str(_) for _ in m[:20]) raise Exception("IN\n{0}\nOUT:{1}".format(m, str(r))) ht = rst2html(content) # fLOG(ht) assert len(ht) > 0 spl = content.split("\n") r = list(yield_sphinx_only_markup_for_pipy(spl)) assert len(r) == len(spl)
def test_pyensae_links(self): fLOG(__file__, self._testMethodName, OutputPrint=__name__ == "__main__") this = os.path.join(os.path.dirname(__file__), '..', '..', '_doc', 'notebooks') checked = 0 missed = [] tolook = [ 'http://files.grouplens.org/datasets/movielens/ml-1m.zip', 'http://www.xavierdupre.fr/', 'url=\\"http', '\\"Skin_NonSkin.txt\\", website=\\"https://archive.ics', "website='http://telechargement.insee.fr/fichiersdetail", 'https://archive.ics.uci.edu/ml/machine-learning-databases' ] for note in explore_folder_iterfile(this, ".*[.]ipynb$", ".ipynb_checkpoints", fullname=True): with open(note, 'r', encoding='utf-8') as f: content = f.read() if "from pyensae.datasource import download_data" in content: checked += 1 found = False for to in tolook: if to in content: found = True if not found: missed.append(note) self.assertGreater(checked, 1) self.assertNotEmpty(missed)
def test_runpython_numpy(self): """ this test also test the extension runpython """ fLOG(__file__, self._testMethodName, OutputPrint=__name__ == "__main__") from docutils import nodes if "enable_disabled_documented_pieces_of_code" in sys.__dict__: raise Exception("this case shoud not be") content = """ test a directive ================ .. runpythonthis:: :setsysvar: :rst: :showcode: :numpy_precision: 2 import numpy print(numpy.array([1.123456789, 1.987654321])) """.replace(" ", "") html = rst2html( content, # fLOG=fLOG, writer="rst", keep_warnings=True) if "[1.12 1.99]" not in html: raise Exception(html)
def test__private_migrating_doxygen_doc(self): """First line. Second line. @param self self """ fLOG( __file__, self._testMethodName, OutputPrint=__name__ == "__main__") doc = TestHelpGenPrivate.__doc__.split("\n") res = _private_migrating_doxygen_doc(doc, 0, "<test>") exp = """ First line. Second line. :githublink:`%|py|0` """.replace(" ", "") self.assertEqual("\n".join(res).strip("\n "), exp.strip("\n ")) doc = TestHelpGenPrivate.test__private_migrating_doxygen_doc.__doc__.split( "\n") res = _private_migrating_doxygen_doc(doc, 0, "<test>") exp = """ First line. Second line. :param self: self :githublink:`%|py|0` """.replace(" ", "") self.assertEqual("\n".join(res).strip("\n "), exp.strip("\n "))
def test_post_parse_blocref(self): fLOG(__file__, self._testMethodName, OutputPrint=__name__ == "__main__") directives.register_directive("blocref", BlocRef) directives.register_directive("blocreflist", BlocRefList)
def test_rst2html_png_bug(self): fLOG( __file__, self._testMethodName, OutputPrint=__name__ == "__main__") if is_travis_or_appveyor() in ('travis', 'appveyor'): # It requires latex. return if sys.version_info[:2] <= (2, 7): # i don't want to fix it for Python 2.7 return temp = get_temp_folder(__file__, "temp_rst2html_png_latex") rst = os.path.join(os.path.abspath( os.path.dirname(__file__)), "data", "puzzle_girafe.rst") with open(rst, "r", encoding="utf-8") as f: content = f.read() text = rst2html(content, fLOG=fLOG, outdir=temp, warnings_log=True, imgmath_latex_preamble=""" \\newcommand{\\acc}[1]{\\left\\{#1\\right\\}} \\newcommand{\\cro}[1]{\\left[#1\\right]} \\newcommand{\\pa}[1]{\\left(#1\\right)} \\newcommand{\\girafedec}[3]{ \\begin{array}{ccccc} #1 &=& #2 &+& #3 \\\\ a' &=& a &-& o \\end{array}} \\newcommand{\\vecteur}[2]{\\pa{#1,\\dots,#2}} \\newcommand{\\R}[0]{\\mathbb{R}} \\newcommand{\\N}[0]{\\mathbb{N}} """) # fLOG(text) ji = os.path.join(temp, "out.html") with open(ji, "w", encoding="utf-8") as f: f.write(text)
def test_post_parse_sn(self): fLOG( __file__, self._testMethodName, OutputPrint=__name__ == "__main__") directives.register_directive("video", VideoDirective)
def test_module_c(self): fLOG( __file__, self._testMethodName, OutputPrint=__name__ == "__main__") path = os.path.abspath(os.path.split(__file__)[0]) file = os.path.join(path, "data", "pyd", "stdchelper.cp37-win_amd64.pyd") self.assertExists(file) mo = import_module( None, file, fLOG, additional_sys_path=None, first_try=True) self.assertIsInstance(mo, tuple) self.assertEqual(len(mo), 2) self.assertTrue(hasattr(mo[0], '__doc__')) if 'stdchelper' in sys.modules: del sys.modules['stdchelper'] temp = get_temp_folder(__file__, "temp_module_c") store_obj = {} actions = copy_source_files(os.path.dirname(file), temp, fLOG=fLOG) store_obj = {} indexes = {} add_file_rst(temp, store_obj, actions, fLOG=fLOG, rootrep=("module_c.", ""), indexes=indexes) self.assertNotEmpty(store_obj) self.assertEqual(len(store_obj), 4) if len(actions) != 2: raise Exception("{0}\n{1}".format( len(actions), "\n".join(str(_) for _ in actions))) self.assertEqual(len(indexes), 1)
def test_rst2html_png_bug(self): fLOG(__file__, self._testMethodName, OutputPrint=__name__ == "__main__") if is_travis_or_appveyor() in ('travis', 'appveyor'): # It requires latex. return if sys.version_info[:2] <= (2, 7): # i don't want to fix it for Python 2.7 return temp = get_temp_folder(__file__, "temp_rst2html_png_latex") rst = os.path.join(os.path.abspath(os.path.dirname(__file__)), "data", "puzzle_girafe.rst") with open(rst, "r", encoding="utf-8") as f: content = f.read() text = rst2html(content, fLOG=fLOG, outdir=temp, warnings_log=True, imgmath_latex_preamble=""" \\newcommand{\\acc}[1]{\\left\\{#1\\right\\}} \\newcommand{\\cro}[1]{\\left[#1\\right]} \\newcommand{\\pa}[1]{\\left(#1\\right)} \\newcommand{\\girafedec}[3]{ \\begin{array}{ccccc} #1 &=& #2 &+& #3 \\\\ a' &=& a &-& o \\end{array}} \\newcommand{\\vecteur}[2]{\\pa{#1,\\dots,#2}} \\newcommand{\\R}[0]{\\mathbb{R}} \\newcommand{\\N}[0]{\\mathbb{N}} """) # fLOG(text) ji = os.path.join(temp, "out.html") with open(ji, "w", encoding="utf-8") as f: f.write(text)
def test_enumerate_notebooks_link(self): fLOG(__file__, self._testMethodName, OutputPrint=__name__ == "__main__") this = os.path.abspath(os.path.dirname(__file__)) nb_folder = os.path.join(this, "..", "..", "_doc", "notebooks") self.assertTrue(os.path.exists(nb_folder)) nb_doc = os.path.join(this, "..", "..", "_doc", "sphinxdoc", "source") self.assertTrue(os.path.exists(nb_doc)) nb = 0 counts = {'title': 0} nbfound = set() for r in enumerate_notebooks_link(nb_folder, nb_doc): rl = list(r) rl[0] = None if r[0] is None else os.path.split(r[0])[-1] rl[1] = os.path.split(r[1])[-1] nb += 1 m = rl[2] counts[m] = counts.get(m, 0) + 1 self.assertTrue(r[-2] is None or isinstance(r[-2], str)) self.assertTrue(r[-1] is None or isinstance(r[-1], str)) if r[-1] is not None: counts["title"] += 1 nbfound.add(rl[1]) self.assertTrue(counts.get("ref", 0) > 0) self.assertIn(counts.get(None, 0), (0, 10)) self.assertTrue(counts["title"] > 0) self.assertTrue(len(nbfound) > 8) # self.assertTrue(counts.get("refn", 0) > 0) self.assertIn(counts.get("toctree", 0), (0, 14))
def test_post_parse_sn_todoext(self): fLOG(__file__, self._testMethodName, OutputPrint=__name__ == "__main__") directives.register_directive("todoext", TodoExt) directives.register_directive("todoextlist", TodoExtList)
def test_post_parse(self): fLOG( __file__, self._testMethodName, OutputPrint=__name__ == "__main__") directives.register_directive("runpython", RunPythonDirective)
def test_runpython_numpy(self): """ this test also test the extension runpython """ fLOG( __file__, self._testMethodName, OutputPrint=__name__ == "__main__") from docutils import nodes if "enable_disabled_documented_pieces_of_code" in sys.__dict__: raise Exception("this case shoud not be") content = """ test a directive ================ .. runpythonthis:: :setsysvar: :rst: :showcode: :numpy_precision: 2 import numpy print(numpy.array([1.123456789, 1.987654321])) """.replace(" ", "") html = rst2html(content, # fLOG=fLOG, writer="rst", keep_warnings=True) if "[1.12 1.99]" not in html: raise Exception(html)
def test_post_parse_sn(self): fLOG( __file__, self._testMethodName, OutputPrint=__name__ == "__main__") register_canonical_role("githublink", githublink_role)
def test_module_c(self): fLOG( __file__, self._testMethodName, OutputPrint=__name__ == "__main__") path = os.path.abspath(os.path.split(__file__)[0]) vers = "%d%d" % sys.version_info[:2] file = os.path.join(path, "data", "pyd", "stdchelper.cp%s-win_amd64.pyd" % vers) if os.path.exists(file): mo = import_module( None, file, fLOG, additional_sys_path=None, first_try=True) self.assertIsInstance(mo, tuple) self.assertEqual(len(mo), 2) self.assertTrue(hasattr(mo[0], '__doc__')) if 'stdchelper' in sys.modules: del sys.modules['stdchelper'] temp = get_temp_folder(__file__, "temp_module_c") store_obj = {} actions = copy_source_files(os.path.dirname(file), temp, fLOG=fLOG) store_obj = {} indexes = {} add_file_rst(temp, store_obj, actions, fLOG=fLOG, rootrep=("module_c.", ""), indexes=indexes) self.assertNotEmpty(store_obj) self.assertEqual(len(store_obj), 4) if len(actions) != 2: raise Exception("{0}\n{1}".format( len(actions), "\n".join(str(_) for _ in actions))) self.assertEqual(len(indexes), 1)
def test_bokeh(self): fLOG( __file__, self._testMethodName, OutputPrint=__name__ == "__main__") from docutils import nodes as skip_ from pyquickhelper.sphinxext.bokeh.bokeh_plot import BokehPlotDirective self.assertTrue(BokehPlotDirective is not None) content = """ ======= History ======= "this code should appear .. bokeh-plot:: from bokeh.plotting import figure, output_file, show output_file("temp_unittest.html") x = [1, 2, 3, 4, 5] y = [6, 7, 6, 4, 5] p = figure(title="example_bokeh", plot_width=300, plot_height=300) p.line(x, y, line_width=2) p.circle(x, y, size=10, fill_color="white") show(p) """.replace(" ", "") if sys.version_info[0] >= 3: content = content.replace('u"', '"') html = rst2html(content, # fLOG=fLOG, writer="html", keep_warnings=True, directives=None, layout="sphinx", load_bokeh=True) t1 = "this code should appear" if t1 not in html: raise Exception(html) temp = get_temp_folder(__file__, "temp_bokeh_extension") with open(os.path.join(temp, "page_bokeh.html"), "w", encoding="utf-8") as f: f.write(html) if 'Unknown directive type' in html: raise Exception(html) if '<<string>>#document-<<string>>' in html: pass else: raise Exception(html) if 'bokeh-plot-string-inline' not in html: raise Exception(html)
def test_runpython_process_exception(self): fLOG( __file__, self._testMethodName, OutputPrint=__name__ == "__main__") from docutils import nodes class runpythonthis_node(nodes.Structural, nodes.Element): pass class RunPythonThisDirective (RunPythonDirective): runpython_class = runpythonthis_node def visit_rp_node(self, node): "local function" self.body.append("<p><b>visit_rp_node</b></p>") def depart_rp_node(self, node): self.body.append("<p><b>depart_rp_node</b></p>") if "enable_disabled_documented_pieces_of_code" in sys.__dict__: raise Exception("this case shoud not be") content = """ test a directive ================ .. runpythonthis:: :showcode: :exception: :process: print(u"this code shoud" + u" appear") z = 1/0 print(u"this one should" + u" not") """.replace(" ", "") if sys.version_info[0] >= 3: content = content.replace('u"', '"') tives = [("runpythonthis", RunPythonThisDirective, runpythonthis_node, visit_rp_node, depart_rp_node)] html = rst2html(content, # fLOG=fLOG, writer="custom", keep_warnings=True, directives=tives) t2 = "<p><<<</p>" if t2 not in html: raise Exception(html) t2 = "<p>>>></p>" if t2 not in html: raise Exception(html) if "ZeroDivisionError" not in html: temp = get_temp_folder( __file__, "temp_runpython_process_exception") with open(os.path.join(temp, "out.html"), "w", encoding="utf8") as f: f.write(html) raise Exception(html)
def test_todoext_done(self): fLOG(__file__, self._testMethodName, OutputPrint=__name__ == "__main__") from docutils import nodes as skip_ content = """ test a directive ================ before .. todoext:: :title: first todo :tag: bug :issue: 7 :hidden: this code shoud appear___ after """.replace(" ", "") if sys.version_info[0] >= 3: content = content.replace('u"', '"') tives = [("todoext", TodoExt, todoext_node, visit_todoext_node, depart_todoext_node)] html = rst2html(content, writer="custom", keep_warnings=True, directives=tives, extlinks={'issue': ('http://%s', '_issue_')}) temp = get_temp_folder(__file__, "temp_todoext") with open(os.path.join(temp, "out_todoext.html"), "w", encoding="utf8") as f: f.write(html) t1 = "this code shoud appear" if t1 in html: raise Exception(html) t1 = "after" if t1 not in html: raise Exception(html) t1 = "first todo" if t1 in html: raise Exception(html) t1 = "(bug)" if t1 in html: raise Exception(html) t1 = 'href="http://7"' if t1 in html: raise Exception(html)
def test_nbreflist(self): fLOG(__file__, self._testMethodName, OutputPrint=__name__ == "__main__") from docutils import nodes as skip_ content = """ test a directive ================ before .. nbref:: :title: first todo :tag: freg :lid: id3 this code shoud appear___ middle .. nbreflist:: :tag: freg :sort: title after """.replace(" ", "") if sys.version_info[0] >= 3: content = content.replace('u"', '"') tives = [("nbref", NbRef, nbref_node, visit_nbref_node, depart_nbref_node)] html = rst2html( content, # fLOG=fLOG, writer="custom", keep_warnings=True, directives=tives, layout="sphinx") if "admonition-nbref nbref_node admonition" not in html: raise html temp = get_temp_folder(__file__, "temp_nbreflist") with open(os.path.join(temp, "out_nbref.html"), "w", encoding="utf8") as f: f.write(html) t1 = "this code shoud appear" if t1 not in html: raise Exception(html) t1 = "after" if t1 not in html: raise Exception(html) t1 = "first todo" if t1 not in html: raise Exception(html)
def test_post_parse_sn_todoext(self): fLOG( __file__, self._testMethodName, OutputPrint=__name__ == "__main__") directives.register_directive("todoext", TodoExt) directives.register_directive("todoextlist", TodoExtList)
def test_prefix(self): fLOG(__file__, self._testMethodName, OutputPrint=__name__ == "__main__") p1 = get_prefix() time.sleep(1) p2 = get_prefix() self.assertNotEqual(p1, p2)
def test_is_missing(self): fLOG(__file__, self._testMethodName, OutputPrint=__name__ == "__main__") assert IsEmptyString("") assert IsEmptyString(None) assert not IsEmptyString("-")
def test_get_relative_path(self): fLOG(__file__, self._testMethodName, OutputPrint=__name__ == "__main__") fold = os.path.abspath(os.path.dirname(__file__)) file = os.path.abspath(__file__) rel = get_relative_path(fold, file) fLOG("-----", rel)
def test_post_parse_exref(self): fLOG( __file__, self._testMethodName, OutputPrint=__name__ == "__main__") directives.register_directive("exref", ExRef) directives.register_directive("exreflist", ExRefList)
def test_post_parse_sn_todoext(self): fLOG( __file__, self._testMethodName, OutputPrint=__name__ == "__main__") directives.register_directive("mathdef", MathDef) directives.register_directive("mathdeflist", MathDefList)
def test_make_label_index(self): fLOG(__file__, self._testMethodName, OutputPrint=__name__ == "__main__") title = "abAB_-()56$?" res = make_label_index(title, "") fLOG("***", title, res) assert res == "abAB_-56"
def test_rst2html_deps(self): fLOG(__file__, self._testMethodName, OutputPrint=__name__ == "__main__") if is_travis_or_appveyor() in ("travis", "appveyor"): # it requires latex return preamble = """ \\newcommand{\\acc}[1]{\\left\\{#1\\right\\}} \\newcommand{\\abs}[1]{\\left\\{#1\\right\\}} \\newcommand{\\cro}[1]{\\left[#1\\right]} \\newcommand{\\pa}[1]{\\left(#1\\right)} \\newcommand{\\girafedec}[3]{ \\begin{array}{ccccc} #1 &=& #2 &+& #3 \\\\ a' &=& a &-& o \\end{array}} \\newcommand{\\vecteur}[2]{\\pa{#1,\\dots,#2}} \\newcommand{\\R}[0]{\\mathbb{R}} \\newcommand{\\N}[0]{\\mathbb{N}} \\newcommand{\\indicatrice}[1]{\\mathbf{1\\!\\!1}_{\\acc{#1}}} \\usepackage[all]{xy} \\newcommand{\\infegal}[0]{\\leqslant} \\newcommand{\\supegal}[0]{\\geqslant} \\newcommand{\\ensemble}[2]{\\acc{#1,\\dots,#2}} \\newcommand{\\fleche}[1]{\\overrightarrow{ #1 }} \\newcommand{\\esp}{\\mathbb{E}} """ temp = get_temp_folder(__file__, "temp_rst2html_deps") doc = os.path.join(temp, "..", "..", "..", "_doc", "sphinxdoc", "source", "ci_status.rst") fulls = [doc] links = dict(jyquickhelper="http", pyquickhelper="http", pymyinstall="http", Jenkins="http", docutils="http", Jupyter="http") for full in fulls: last = os.path.split(full)[-1] fLOG("process", last) with open(full, "r", encoding="utf-8") as f: content = f.read() writer = "rst" text = rst2html(content, outdir=temp, writer=writer, imgmath_latex_preamble=preamble, layout="sphinx", extlinks=dict(issue=('https://link/%s', 'issue {0} on GitHub')), epkg_dictionary=links) ji = os.path.join(temp, last + "." + writer) with open(ji, "w", encoding="utf-8") as f: f.write(text) if "ERROR/" in text: raise Exception(text)
def test_runpython_exception(self): fLOG(__file__, self._testMethodName, OutputPrint=__name__ == "__main__") from docutils import nodes class runpythonthis_node(nodes.Structural, nodes.Element): pass class RunPythonThisDirective(RunPythonDirective): runpython_class = runpythonthis_node def visit_rp_node(self, node): self.body.append("<p><b>visit_rp_node</b></p>") def depart_rp_node(self, node): self.body.append("<p><b>depart_rp_node</b></p>") if "enable_disabled_documented_pieces_of_code" in sys.__dict__: raise Exception("this case shoud not be") content = """ test a directive ================ .. runpythonthis:: :showcode: :exception: print(u"this code shoud" + u" appear") z = 1/0 print(u"this one should" + u" not") """.replace(" ", "") if sys.version_info[0] >= 3: content = content.replace('u"', '"') tives = [("runpythonthis", RunPythonThisDirective, runpythonthis_node, visit_rp_node, depart_rp_node)] html = rst2html( content, # fLOG=fLOG, writer="custom", keep_warnings=True, directives=tives) t2 = "<p><<<</p>" if t2 not in html: raise Exception(html) t2 = "<p>>>></p>" if t2 not in html: raise Exception(html) if "ZeroDivisionError" not in html: temp = get_temp_folder(__file__, "temp_runpython_exception") with open(os.path.join(temp, "out.html"), "w", encoding="utf8") as f: f.write(html) raise Exception(html)
def test_video_url(self): fLOG( __file__, self._testMethodName, OutputPrint=__name__ == "__main__") from docutils import nodes as skip_ content = """ test a directive ================ before .. video:: http://www.xavierdupre.fr/ensae/video_hackathon_ensae_ey_label_emmaus_2017.mp4 :width: 300 after this code shoud appear """.replace(" ", "") if sys.version_info[0] >= 3: content = content.replace('u"', '"') logger2 = logging.getLogger("video") log_capture_string = StringIO() ch = logging.StreamHandler(log_capture_string) ch.setLevel(logging.DEBUG) logger2.addHandler(ch) with warnings.catch_warnings(record=True): html = rst2html(content, # fLOG=fLOG, writer="custom", keep_warnings=True, directives=None) warns = log_capture_string.getvalue().strip("\n\r\t ") if len(warns) != 0 and 'Unable to find' not in warns: raise Exception("warnings '{0}'".format(warns)) t1 = "this code shoud not appear" if t1 in html: raise Exception(html) t1 = "this code shoud appear" if t1 not in html: raise Exception(html) t1 = "video_hackathon_ensae_ey_label_emmaus_2017.mp4" if t1 not in html: raise Exception(html) t1 = "unable to find" if t1 in html: raise Exception(html) temp = get_temp_folder(__file__, "temp_video_url") with open(os.path.join(temp, "out_video.html"), "w", encoding="utf8") as f: f.write(html)
def test_make_label_index(self): fLOG( __file__, self._testMethodName, OutputPrint=__name__ == "__main__") title = "abAB_-()56$?" res = make_label_index(title, "") fLOG("***", title, res) assert res == "abAB_-56"
def test_bokeh(self): fLOG(__file__, self._testMethodName, OutputPrint=__name__ == "__main__") from docutils import nodes as skip_ from pyquickhelper.sphinxext.bokeh.bokeh_plot import BokehPlotDirective self.assertTrue(BokehPlotDirective is not None) content = """ ======= History ======= "this code should appear .. bokeh-plot:: from bokeh.plotting import figure, output_file, show output_file("temp_unittest.html") x = [1, 2, 3, 4, 5] y = [6, 7, 6, 4, 5] p = figure(title="example_bokeh", plot_width=300, plot_height=300) p.line(x, y, line_width=2) p.circle(x, y, size=10, fill_color="white") show(p) """.replace(" ", "") if sys.version_info[0] >= 3: content = content.replace('u"', '"') html = rst2html( content, # fLOG=fLOG, writer="html", keep_warnings=True, directives=None, layout="sphinx", load_bokeh=True) t1 = "this code should appear" if t1 not in html: raise Exception(html) temp = get_temp_folder(__file__, "temp_bokeh_extension") with open(os.path.join(temp, "page_bokeh.html"), "w", encoding="utf-8") as f: f.write(html) if 'Unknown directive type' in html: raise Exception(html) if 'bokeh-plot-' not in html: raise Exception(html)
def test_parse_markdown(self): fLOG( __file__, self._testMethodName, OutputPrint=__name__ == "__main__") r = parse_markdown("**r**") if str(r).strip("\n\r ") != "<p><strong>r</strong></p>": raise Exception([str(r)])
def test_todoext_done(self): fLOG( __file__, self._testMethodName, OutputPrint=__name__ == "__main__") from docutils import nodes as skip_ content = """ test a directive ================ before .. todoext:: :title: first todo :tag: bug :issue: 7 :hidden: this code shoud appear___ after """.replace(" ", "") if sys.version_info[0] >= 3: content = content.replace('u"', '"') tives = [("todoext", TodoExt, todoext_node, visit_todoext_node, depart_todoext_node)] html = rst2html(content, writer="custom", keep_warnings=True, directives=tives, extlinks={'issue': ('http://%s', '_issue_')}) temp = get_temp_folder(__file__, "temp_todoext") with open(os.path.join(temp, "out_todoext.html"), "w", encoding="utf8") as f: f.write(html) t1 = "this code shoud appear" if t1 in html: raise Exception(html) t1 = "after" if t1 not in html: raise Exception(html) t1 = "first todo" if t1 in html: raise Exception(html) t1 = "(bug)" if t1 in html: raise Exception(html) t1 = 'href="http://7"' if t1 in html: raise Exception(html)
def test_exreflist(self): fLOG( __file__, self._testMethodName, OutputPrint=__name__ == "__main__") from docutils import nodes as skip_ content = """ test a directive ================ before .. exref:: :title: first todo :tag: freg :lid: id3 this code shoud appear___ middle .. exreflist:: :tag: freg :sort: title after """.replace(" ", "") if sys.version_info[0] >= 3: content = content.replace('u"', '"') tives = [("exref", ExRef, exref_node, visit_exref_node, depart_exref_node)] html = rst2html(content, fLOG=fLOG, writer="custom", keep_warnings=True, directives=tives) if "admonition-exref exref_node admonition" not in html: raise html temp = get_temp_folder(__file__, "temp_exreflist") with open(os.path.join(temp, "out_exref.html"), "w", encoding="utf8") as f: f.write(html) t1 = "this code shoud appear" if t1 not in html: raise Exception(html) t1 = "after" if t1 not in html: raise Exception(html) t1 = "first todo" if t1 not in html: raise Exception(html)
def test_doctring2html(self): fLOG( __file__, self._testMethodName, OutputPrint=__name__ == "__main__") html = docstring2html(df2rst, "rawhtml") self.assertNotEmpty(html) self.assertNotIn("<p>@code", html) html = docstring2html(df2rst, "html") self.assertIsInstance(html, HTML)
def test_format_history_release(self): fLOG( __file__, self._testMethodName, OutputPrint=__name__ == "__main__") history = """ .. _l-history-l: ======= History ======= 1.5.???? (2017-??-??) ===================== **Bugfix** * `46`: update to Sphinx 1.6 * `54`: fix searchbox for `sphinx_rtd_theme <https://github.com/rtfd/sphinx_rtd_theme>`_ **Features** * `36`: add support for sphinx-gallery 1.4.2 (2016-09-18) ================== """.replace(" ", "") temp = get_temp_folder(__file__, "temp_history_release") fsrc = os.path.join(temp, "src.rst") fdst = os.path.join(temp, "dst.rst") with open(fsrc, "w", encoding="utf-8") as f: f.write(history) format_history(fsrc, fdst, format="release") with open(fdst, "r", encoding="utf-8") as f: content = f.read() expect = """ .. _l-history-l: ======= History ======= * :release:`1.5.???? <2017-??-??>` * :bug:`46`: update to Sphinx 1.6 * :bug:`54`: fix searchbox for `sphinx_rtd_theme <https://github.com/rtfd/sphinx_rtd_theme>`_ * :feature:`36`: add support for sphinx-gallery * :release:`1.4.2 <2016-09-18>` """.replace(" ", "") self.assertEqual(content.strip(" \r\n\t"), expect.strip(" \r\n\t"))