2021-04-13 10:45:05 +02:00
|
|
|
# Copyright: Ankitects Pty Ltd and contributors
|
|
|
|
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
# coding: utf-8
|
|
|
|
|
|
|
|
import os
|
2016-05-12 06:45:35 +02:00
|
|
|
import shutil
|
|
|
|
|
2020-11-17 10:23:06 +01:00
|
|
|
from anki.lang import without_unicode_isolation
|
2019-12-25 22:36:26 +01:00
|
|
|
from tests.shared import getEmptyCol
|
2012-12-21 08:51:59 +01:00
|
|
|
|
2019-12-25 05:18:34 +01:00
|
|
|
|
2012-12-21 08:51:59 +01:00
|
|
|
def test_latex():
|
2020-07-17 05:21:12 +02:00
|
|
|
col = getEmptyCol()
|
2012-12-21 08:51:59 +01:00
|
|
|
# change latex cmd to simulate broken build
|
|
|
|
import anki.latex
|
2019-12-25 05:18:34 +01:00
|
|
|
|
2017-08-01 06:25:49 +02:00
|
|
|
anki.latex.pngCommands[0][0] = "nolatex"
|
2012-12-21 08:51:59 +01:00
|
|
|
# add a note with latex
|
2020-07-17 05:21:12 +02:00
|
|
|
note = col.newNote()
|
2020-07-17 05:18:09 +02:00
|
|
|
note["Front"] = "[latex]hello[/latex]"
|
2020-07-17 05:21:12 +02:00
|
|
|
col.addNote(note)
|
2012-12-21 08:51:59 +01:00
|
|
|
# but since latex couldn't run, there's nothing there
|
2020-07-17 05:21:12 +02:00
|
|
|
assert len(os.listdir(col.media.dir())) == 0
|
2012-12-21 08:51:59 +01:00
|
|
|
# check the error message
|
2020-07-17 05:18:09 +02:00
|
|
|
msg = note.cards()[0].q()
|
2020-11-17 10:23:06 +01:00
|
|
|
assert "executing nolatex" in without_unicode_isolation(msg)
|
2012-12-21 08:51:59 +01:00
|
|
|
assert "installed" in msg
|
|
|
|
# check if we have latex installed, and abort test if we don't
|
2016-05-12 06:45:35 +02:00
|
|
|
if not shutil.which("latex") or not shutil.which("dvipng"):
|
2016-07-04 09:07:48 +02:00
|
|
|
print("aborting test; latex or dvipng is not installed")
|
2016-05-12 06:45:35 +02:00
|
|
|
return
|
2012-12-21 08:51:59 +01:00
|
|
|
# fix path
|
2017-08-01 06:25:49 +02:00
|
|
|
anki.latex.pngCommands[0][0] = "latex"
|
2012-12-21 08:51:59 +01:00
|
|
|
# check media db should cause latex to be generated
|
2020-07-17 05:21:12 +02:00
|
|
|
col.media.render_all_latex()
|
|
|
|
assert len(os.listdir(col.media.dir())) == 1
|
2020-07-17 05:18:09 +02:00
|
|
|
assert ".png" in note.cards()[0].q()
|
2012-12-21 08:51:59 +01:00
|
|
|
# adding new notes should cause generation on question display
|
2020-07-17 05:21:12 +02:00
|
|
|
note = col.newNote()
|
2020-07-17 05:18:09 +02:00
|
|
|
note["Front"] = "[latex]world[/latex]"
|
2020-07-17 05:21:12 +02:00
|
|
|
col.addNote(note)
|
2020-07-17 05:18:09 +02:00
|
|
|
note.cards()[0].q()
|
2020-07-17 05:21:12 +02:00
|
|
|
assert len(os.listdir(col.media.dir())) == 2
|
2012-12-21 08:51:59 +01:00
|
|
|
# another note with the same media should reuse
|
2020-07-17 05:21:12 +02:00
|
|
|
note = col.newNote()
|
2020-07-17 05:18:09 +02:00
|
|
|
note["Front"] = " [latex]world[/latex]"
|
2020-07-17 05:21:12 +02:00
|
|
|
col.addNote(note)
|
|
|
|
assert len(os.listdir(col.media.dir())) == 2
|
2020-07-17 05:18:09 +02:00
|
|
|
oldcard = note.cards()[0]
|
2012-12-21 08:51:59 +01:00
|
|
|
assert ".png" in oldcard.q()
|
|
|
|
# if we turn off building, then previous cards should work, but cards with
|
2020-02-11 06:09:33 +01:00
|
|
|
# missing media will show a broken image
|
2012-12-21 08:51:59 +01:00
|
|
|
anki.latex.build = False
|
2020-07-17 05:21:12 +02:00
|
|
|
note = col.newNote()
|
2020-07-17 05:18:09 +02:00
|
|
|
note["Front"] = "[latex]foo[/latex]"
|
2020-07-17 05:21:12 +02:00
|
|
|
col.addNote(note)
|
|
|
|
assert len(os.listdir(col.media.dir())) == 2
|
2012-12-21 08:51:59 +01:00
|
|
|
assert ".png" in oldcard.q()
|
2015-07-30 12:43:41 +02:00
|
|
|
# turn it on again so other test don't suffer
|
|
|
|
anki.latex.build = True
|
|
|
|
|
2017-10-05 06:14:56 +02:00
|
|
|
# bad commands
|
2015-07-30 12:43:41 +02:00
|
|
|
(result, msg) = _test_includes_bad_command("\\write18")
|
|
|
|
assert result, msg
|
|
|
|
(result, msg) = _test_includes_bad_command("\\readline")
|
|
|
|
assert result, msg
|
|
|
|
(result, msg) = _test_includes_bad_command("\\input")
|
|
|
|
assert result, msg
|
|
|
|
(result, msg) = _test_includes_bad_command("\\include")
|
|
|
|
assert result, msg
|
|
|
|
(result, msg) = _test_includes_bad_command("\\catcode")
|
|
|
|
assert result, msg
|
|
|
|
(result, msg) = _test_includes_bad_command("\\openout")
|
|
|
|
assert result, msg
|
|
|
|
(result, msg) = _test_includes_bad_command("\\write")
|
|
|
|
assert result, msg
|
|
|
|
(result, msg) = _test_includes_bad_command("\\loop")
|
|
|
|
assert result, msg
|
|
|
|
(result, msg) = _test_includes_bad_command("\\def")
|
|
|
|
assert result, msg
|
|
|
|
(result, msg) = _test_includes_bad_command("\\shipout")
|
|
|
|
assert result, msg
|
|
|
|
|
|
|
|
# inserting commands beginning with a bad name should not raise an error
|
|
|
|
(result, msg) = _test_includes_bad_command("\\defeq")
|
|
|
|
assert not result, msg
|
|
|
|
# normal commands should not either
|
|
|
|
(result, msg) = _test_includes_bad_command("\\emph")
|
|
|
|
assert not result, msg
|
|
|
|
|
2019-12-25 05:18:34 +01:00
|
|
|
|
2015-07-30 12:43:41 +02:00
|
|
|
def _test_includes_bad_command(bad):
|
2020-07-17 05:21:12 +02:00
|
|
|
col = getEmptyCol()
|
|
|
|
note = col.newNote()
|
2020-07-17 05:18:09 +02:00
|
|
|
note["Front"] = "[latex]%s[/latex]" % bad
|
2020-07-17 05:21:12 +02:00
|
|
|
col.addNote(note)
|
2020-11-17 10:23:06 +01:00
|
|
|
q = without_unicode_isolation(note.cards()[0].q())
|
2016-07-04 09:07:48 +02:00
|
|
|
return ("'%s' is not allowed on cards" % bad in q, "Card content: %s" % q)
|