Show More
@@ -0,0 +1,119 b'' | |||
|
1 | # encoding: utf-8 | |
|
2 | """Tests for IPython.utils.path.py""" | |
|
3 | ||
|
4 | #----------------------------------------------------------------------------- | |
|
5 | # Copyright (C) 2008-2011 The IPython Development Team | |
|
6 | # | |
|
7 | # Distributed under the terms of the BSD License. The full license is in | |
|
8 | # the file COPYING, distributed as part of this software. | |
|
9 | #----------------------------------------------------------------------------- | |
|
10 | ||
|
11 | import nose.tools as nt | |
|
12 | ||
|
13 | from IPython.lib import latextools | |
|
14 | from IPython.testing.decorators import onlyif_cmds_exist | |
|
15 | from IPython.testing.tools import monkeypatch | |
|
16 | from IPython.utils.process import FindCmdError | |
|
17 | ||
|
18 | ||
|
19 | def test_latex_to_png_dvipng_fails_when_no_cmd(): | |
|
20 | """ | |
|
21 | `latex_to_png_dvipng` should return None when there is no required command | |
|
22 | """ | |
|
23 | for command in ['latex', 'dvipng']: | |
|
24 | yield (check_latex_to_png_dvipng_fails_when_no_cmd, command) | |
|
25 | ||
|
26 | ||
|
27 | def check_latex_to_png_dvipng_fails_when_no_cmd(command): | |
|
28 | def mock_find_cmd(arg): | |
|
29 | if arg == command: | |
|
30 | raise FindCmdError | |
|
31 | ||
|
32 | with monkeypatch(latextools, "find_cmd", mock_find_cmd): | |
|
33 | nt.assert_equals(latextools.latex_to_png_dvipng("whatever", True), | |
|
34 | None) | |
|
35 | ||
|
36 | ||
|
37 | @onlyif_cmds_exist('latex', 'dvipng') | |
|
38 | def test_latex_to_png_dvipng_runs(): | |
|
39 | """ | |
|
40 | Test that latex_to_png_dvipng just runs without error. | |
|
41 | """ | |
|
42 | def mock_kpsewhich(filename): | |
|
43 | nt.assert_equals(filename, "breqn.sty") | |
|
44 | return None | |
|
45 | ||
|
46 | for (s, wrap) in [("$$x^2$$", False), ("x^2", True)]: | |
|
47 | yield (latextools.latex_to_png_dvipng, s, wrap) | |
|
48 | ||
|
49 | with monkeypatch(latextools, "kpsewhich", mock_kpsewhich): | |
|
50 | yield (latextools.latex_to_png_dvipng, s, wrap) | |
|
51 | ||
|
52 | ||
|
53 | def test_genelatex_no_wrap(): | |
|
54 | """ | |
|
55 | Test genelatex with wrap=False. | |
|
56 | """ | |
|
57 | def mock_kpsewhich(filename): | |
|
58 | assert False, ("kpsewhich should not be called " | |
|
59 | "(called with {0})".format(filename)) | |
|
60 | ||
|
61 | with monkeypatch(latextools, "kpsewhich", mock_kpsewhich): | |
|
62 | nt.assert_equals( | |
|
63 | '\n'.join(latextools.genelatex("body text", False)), | |
|
64 | r'''\documentclass{article} | |
|
65 | \usepackage{amsmath} | |
|
66 | \usepackage{amsthm} | |
|
67 | \usepackage{amssymb} | |
|
68 | \usepackage{bm} | |
|
69 | \pagestyle{empty} | |
|
70 | \begin{document} | |
|
71 | body text | |
|
72 | \end{document}''') | |
|
73 | ||
|
74 | ||
|
75 | def test_genelatex_wrap_with_breqn(): | |
|
76 | """ | |
|
77 | Test genelatex with wrap=True for the case breqn.sty is installed. | |
|
78 | """ | |
|
79 | def mock_kpsewhich(filename): | |
|
80 | nt.assert_equals(filename, "breqn.sty") | |
|
81 | return "path/to/breqn.sty" | |
|
82 | ||
|
83 | with monkeypatch(latextools, "kpsewhich", mock_kpsewhich): | |
|
84 | nt.assert_equals( | |
|
85 | '\n'.join(latextools.genelatex("x^2", True)), | |
|
86 | r'''\documentclass{article} | |
|
87 | \usepackage{amsmath} | |
|
88 | \usepackage{amsthm} | |
|
89 | \usepackage{amssymb} | |
|
90 | \usepackage{bm} | |
|
91 | \usepackage{breqn} | |
|
92 | \pagestyle{empty} | |
|
93 | \begin{document} | |
|
94 | \begin{dmath*} | |
|
95 | x^2 | |
|
96 | \end{dmath*} | |
|
97 | \end{document}''') | |
|
98 | ||
|
99 | ||
|
100 | def test_genelatex_wrap_without_breqn(): | |
|
101 | """ | |
|
102 | Test genelatex with wrap=True for the case breqn.sty is not installed. | |
|
103 | """ | |
|
104 | def mock_kpsewhich(filename): | |
|
105 | nt.assert_equals(filename, "breqn.sty") | |
|
106 | return None | |
|
107 | ||
|
108 | with monkeypatch(latextools, "kpsewhich", mock_kpsewhich): | |
|
109 | nt.assert_equals( | |
|
110 | '\n'.join(latextools.genelatex("x^2", True)), | |
|
111 | r'''\documentclass{article} | |
|
112 | \usepackage{amsmath} | |
|
113 | \usepackage{amsthm} | |
|
114 | \usepackage{amssymb} | |
|
115 | \usepackage{bm} | |
|
116 | \pagestyle{empty} | |
|
117 | \begin{document} | |
|
118 | $$x^2$$ | |
|
119 | \end{document}''') |
@@ -72,6 +72,9 b' from ipunittest import ipdoctest, ipdocstring' | |||
|
72 | 72 | # numpy.testing.decorators, we expose all of it here. |
|
73 | 73 | from IPython.external.decorators import * |
|
74 | 74 | |
|
75 | # For onlyif_cmd_exists decorator | |
|
76 | from IPython.utils.process import is_cmd_found | |
|
77 | ||
|
75 | 78 | #----------------------------------------------------------------------------- |
|
76 | 79 | # Classes and functions |
|
77 | 80 | #----------------------------------------------------------------------------- |
@@ -342,3 +345,14 b' else:' | |||
|
342 | 345 | |
|
343 | 346 | onlyif_unicode_paths = onlyif(unicode_paths, ("This test is only applicable " |
|
344 | 347 | "where we can use unicode in filenames.")) |
|
348 | ||
|
349 | ||
|
350 | def onlyif_cmds_exist(*commands): | |
|
351 | """ | |
|
352 | Decorator to skip test when at least one of `commands` is not found. | |
|
353 | """ | |
|
354 | for cmd in commands: | |
|
355 | if not is_cmd_found(cmd): | |
|
356 | return skip("This test runs only if command '{0}' " | |
|
357 | "is installed".format(cmd)) | |
|
358 | return null_deco |
@@ -390,3 +390,14 b' def make_tempfile(name):' | |||
|
390 | 390 | yield |
|
391 | 391 | finally: |
|
392 | 392 | os.unlink(name) |
|
393 | ||
|
394 | ||
|
395 | @contextmanager | |
|
396 | def monkeypatch(obj, name, attr): | |
|
397 | """ | |
|
398 | Context manager to replace attribute named `name` in `obj` with `attr`. | |
|
399 | """ | |
|
400 | orig = getattr(obj, name) | |
|
401 | setattr(obj, name, attr) | |
|
402 | yield | |
|
403 | setattr(obj, name, orig) |
@@ -72,6 +72,15 b' def find_cmd(cmd):' | |||
|
72 | 72 | return os.path.abspath(path) |
|
73 | 73 | |
|
74 | 74 | |
|
75 | def is_cmd_found(cmd): | |
|
76 | """Check whether executable `cmd` exists or not and return a bool.""" | |
|
77 | try: | |
|
78 | find_cmd(cmd) | |
|
79 | return True | |
|
80 | except FindCmdError: | |
|
81 | return False | |
|
82 | ||
|
83 | ||
|
75 | 84 | def pycmd2argv(cmd): |
|
76 | 85 | r"""Take the path of a python command and return a list (argv-style). |
|
77 | 86 |
General Comments 0
You need to be logged in to leave comments.
Login now