This repository has been archived by the owner on Jan 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
test_notebooks.py
151 lines (129 loc) · 4.79 KB
/
test_notebooks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
"""
@brief test log(time=18s)
@author Xavier Dupre
"""
import sys
import os
import unittest
try:
import src
except ImportError:
path = os.path.normpath(
os.path.abspath(
os.path.join(
os.path.split(__file__)[0],
"..",
"..")))
if path not in sys.path:
sys.path.append(path)
import src
from src.pyquickhelper.loghelper.flog import fLOG, run_cmd
from src.pyquickhelper.helpgen.sphinx_main import process_notebooks, add_notebook_page
from src.pyquickhelper import get_temp_folder
if sys.version_info[0] == 2:
from codecs import open
class TestNotebookConversion (unittest.TestCase):
def test_notebook(self):
fLOG(
__file__,
self._testMethodName,
OutputPrint=__name__ == "__main__")
if sys.version_info[0] == 2:
return
path = os.path.abspath(os.path.split(__file__)[0])
fold = os.path.normpath(
os.path.join(
path,
"..",
"..",
"_doc",
"notebooks"))
nb = os.path.join(fold, "example_pyquickhelper.ipynb")
assert os.path.exists(nb)
temp = get_temp_folder(__file__, "temp_nb")
if sys.platform.startswith("win"):
p1 = r"C:\Program Files\MiKTeX 2.9\miktex\bin\x64"
p2 = r"%USERPROFILE%\AppData\Local\Pandoc"
else:
p1 = "."
p2 = "."
formats = ["slides", "ipynb", "html", "python", "rst"]
exp = ["example_pyquickhelper.html",
"example_pyquickhelper.ipynb",
"example_pyquickhelper.py",
"example_pyquickhelper.rst",
"example_pyquickhelper.ipynb",
"example_pyquickhelper.slides.html",
]
if sys.platform.startswith("win"):
formats.append("latex")
formats.append("pdf")
exp.append("example_pyquickhelper.tex")
exp.append("example_pyquickhelper.pdf")
# to be removed later
if not sys.platform.startswith("win"):
return
res = process_notebooks(
nb,
temp,
temp,
latex_path=p1,
pandoc_path=p2,
formats=formats)
for _ in res:
fLOG(_)
assert os.path.exists(_[0])
fou = [os.path.split(_[0])[-1] for _ in res]
fou = [_ for _ in fou if "png" not in _]
fou = list(sorted(set(fou)))
exp = list(sorted(set(exp)))
if len(fou) < len(exp):
raise Exception("length {0} != {1}\n{2}\n---\n{3}".format(len(fou), len(exp),
"\n".join(fou), "\n".join(exp)))
for i, j in zip(exp, fou):
if i != j:
raise Exception(
"{0} != {1}\nfou=\n{2}\nexp=\n{3}".format(
i,
j,
str(fou),
str(exp)))
file = os.path.join(temp, "all_notebooks.rst")
add_notebook_page([_[0] for _ in res], file)
assert os.path.exists(file)
with open(os.path.join(temp, "example_pyquickhelper.rst"), "r", encoding="utf8") as f:
text = f.read()
assert "from pyquickhelper import fLOG\n fLOG(OutputPrint=False) # by default" in text
assert ":linenos:" in text
def test_short_cmd(self):
fLOG(
__file__,
self._testMethodName,
OutputPrint=__name__ == "__main__")
return
if sys.platform.startswith("win"):
return
home = os.environ["HOME"]
f = "{0}/github/pyquickhelper/_doc/notebooks/example_pyquickhelper.ipynb".format(
home)
fo = "{0}/github/pyquickhelper/_unittests/ut_helpgen/temp_nb/example_pyquickhelper.html".format(
home)
if os.path.exists(fo):
os.remove(fo)
assert not os.path.exists(fo)
if os.path.exists(f):
cmd = '{0}/anaconda3/bin/ipython nbconvert --to html {0}/github/pyquickhelper/_doc/notebooks/example_pyquickhelper.ipynb --template full --output={0}/github/pyquickhelper/_unittests/ut_helpgen/temp_nb/example_pyquickhelper'
cmd = cmd.format(home)
out, err = run_cmd(cmd, shell=False, wait=True, communicate=False)
# fLOG(out)
# fLOG("******************",err)
assert "[NbConvertApp] Writing" in err
if not os.path.exists(fo):
fLOG(fo)
fLOG(os.path.abspath(os.path.dirname(fo)))
fLOG(os.listdir(os.path.dirname(fo)))
assert False
else:
fLOG("unfound ", f)
if __name__ == "__main__":
unittest.main()