diff options
author | Benjamin Peterson <devnull@localhost> | 2008-09-18 21:03:16 -0500 |
---|---|---|
committer | Benjamin Peterson <devnull@localhost> | 2008-09-18 21:03:16 -0500 |
commit | ae5c5fdb18fa618e84c408264f596ebdec96e5df (patch) | |
tree | a4feb5b6a971d2b72b4827bd7a33d33aa46fa9bd | |
parent | e6600347dac8dddc630cac6e55c08dea84b20f00 (diff) | |
download | pygments-ae5c5fdb18fa618e84c408264f596ebdec96e5df.tar.gz |
remove __builtin__.testdir/testfile magic
This adds a new file tests/support.py
-rw-r--r-- | tests/run.py | 5 | ||||
-rw-r--r-- | tests/support.py | 25 | ||||
-rw-r--r-- | tests/test_basic_api.py | 8 | ||||
-rw-r--r-- | tests/test_cmdline.py | 13 | ||||
-rw-r--r-- | tests/test_examplefiles.py | 1 | ||||
-rw-r--r-- | tests/test_html_formatter.py | 19 |
6 files changed, 50 insertions, 21 deletions
diff --git a/tests/run.py b/tests/run.py index b0244d60..534a22e5 100644 --- a/tests/run.py +++ b/tests/run.py @@ -14,7 +14,6 @@ import sys, os, new import unittest -import __builtin__ from os.path import dirname, basename, join, abspath @@ -27,9 +26,6 @@ except ImportError: testdir = abspath(dirname(__file__)) -# useful for all tests -__builtin__.testdir = testdir - failed = [] total_test_count = 0 error_test_count = 0 @@ -92,7 +88,6 @@ def run_tests(with_coverage=False): for testfile in files: globs = {} try: - __builtin__.testfile = testfile execfile(join(testdir, testfile), globs) except Exception, exc: raise diff --git a/tests/support.py b/tests/support.py new file mode 100644 index 00000000..f2df4747 --- /dev/null +++ b/tests/support.py @@ -0,0 +1,25 @@ +# coding: utf-8 +""" +Support for Pygments tests +""" + +import os +import random + + +def _get_all_test_files(): + tests = [] + here = os.path.abspath(os.path.dirname(__file__)) + for dirpath, dirs, files in os.walk(here): + tests.extend(os.path.join(dirpath, fn) for fn in files + if fn.startswith("test_") and fn.endswith(".py")) + dirs[:] = [d for d in dirs if d.startswith("test_")] + return tests + +_testfiles = _get_all_test_files() + +def test_file(): + """ + Randomly choose a file to test. + """ + return random.choice(_testfiles) diff --git a/tests/test_basic_api.py b/tests/test_basic_api.py index 760dda63..371e7ec3 100644 --- a/tests/test_basic_api.py +++ b/tests/test_basic_api.py @@ -17,6 +17,8 @@ from pygments.token import _TokenType, Text from pygments.lexer import RegexLexer from pygments.formatters.img import FontNotFound +from support import test_file + test_content = [chr(i) for i in xrange(33, 128)] * 5 random.shuffle(test_content) test_content = ''.join(test_content) + '\n' @@ -90,7 +92,7 @@ class FiltersTest(unittest.TestCase): for x in filters.FILTERS.keys(): lx = lexers.PythonLexer() lx.add_filter(x, **filter_args.get(x, {})) - text = file(os.path.join(testdir, testfile)).read().decode('utf-8') + text = file(test_file()).read().decode('utf-8') tokens = list(lx.get_tokens(text)) roundtext = ''.join([t[1] for t in tokens]) if x not in ('whitespace', 'keywordcase'): @@ -106,14 +108,14 @@ class FiltersTest(unittest.TestCase): def test_whitespace(self): lx = lexers.PythonLexer() lx.add_filter('whitespace', spaces='%') - text = file(os.path.join(testdir, testfile)).read().decode('utf-8') + text = file(test_file()).read().decode('utf-8') lxtext = ''.join([t[1] for t in list(lx.get_tokens(text))]) self.failIf(' ' in lxtext) def test_keywordcase(self): lx = lexers.PythonLexer() lx.add_filter('keywordcase', case='capitalize') - text = file(os.path.join(testdir, testfile)).read().decode('utf-8') + text = file(test_file()).read().decode('utf-8') lxtext = ''.join([t[1] for t in list(lx.get_tokens(text))]) self.assert_('Def' in lxtext and 'Class' in lxtext) diff --git a/tests/test_cmdline.py b/tests/test_cmdline.py index 46868cf1..8cc2938c 100644 --- a/tests/test_cmdline.py +++ b/tests/test_cmdline.py @@ -16,6 +16,8 @@ import StringIO from pygments import highlight from pygments.cmdline import main as cmdline_main +from support import test_file + def run_cmdline(*args): saved_stdout = sys.stdout @@ -44,21 +46,22 @@ class CmdLineTest(unittest.TestCase): self.assertEquals(c, 0) def test_O_opt(self): - filename = os.path.join(testdir, testfile) + filename = test_file() c, o, e = run_cmdline("-Ofull=1,linenos=true,foo=bar", "-fhtml", filename) self.assertEquals(c, 0) self.assert_("<html" in o) self.assert_('class="linenos"' in o) def test_P_opt(self): - filename = os.path.join(testdir, testfile) + filename = test_file() c, o, e = run_cmdline("-Pfull", "-Ptitle=foo, bar=baz=,", "-fhtml", filename) self.assertEquals(c, 0) self.assert_("<title>foo, bar=baz=,</title>" in o) def test_F_opt(self): - filename = os.path.join(testdir, testfile) - c, o, e = run_cmdline("-Fhighlight:tokentype=Name.Blubb,names=testfile testdir", + filename = __file__ + filename = filename[:-1] if filename.endswith("pyc") else filename + c, o, e = run_cmdline("-Fhighlight:tokentype=Name.Blubb,names=test_file filename", "-fhtml", filename) self.assertEquals(c, 0) self.assert_('<span class="n-Blubb' in o) @@ -82,7 +85,7 @@ class CmdLineTest(unittest.TestCase): # test that cmdline gives the same output as library api from pygments.lexers import PythonLexer from pygments.formatters import HtmlFormatter - filename = os.path.join(testdir, testfile) + filename = test_file() code = file(filename).read() output = highlight(code, PythonLexer(), HtmlFormatter()) diff --git a/tests/test_examplefiles.py b/tests/test_examplefiles.py index ee9af608..52ff3093 100644 --- a/tests/test_examplefiles.py +++ b/tests/test_examplefiles.py @@ -22,6 +22,7 @@ class ExampleFileTest(unittest.TestCase): lfd = 0 # generate methods +testdir = os.path.dirname(__file__) for fn in os.listdir(os.path.join(testdir, 'examplefiles')): absfn = os.path.join(testdir, 'examplefiles', fn) if not os.path.isfile(absfn): diff --git a/tests/test_html_formatter.py b/tests/test_html_formatter.py index ed949a88..f650ba61 100644 --- a/tests/test_html_formatter.py +++ b/tests/test_html_formatter.py @@ -12,14 +12,17 @@ import re import unittest import StringIO import tempfile -from os.path import join, dirname, isfile +from os.path import join, dirname, isfile, abspath from pygments.lexers import PythonLexer from pygments.formatters import HtmlFormatter, NullFormatter from pygments.formatters.html import escape_html -tokensource = list(PythonLexer().get_tokens(file( - os.path.join(testdir, testfile)).read())) +from support import test_file + +TEST_DIR = abspath(dirname(__file__)) + +tokensource = list(PythonLexer().get_tokens(file(test_file()).read())) class HtmlFormatterTest(unittest.TestCase): def test_correct_output(self): @@ -39,13 +42,13 @@ class HtmlFormatterTest(unittest.TestCase): # test correct behavior # CSS should be in /tmp directory fmt1 = HtmlFormatter(full=True, cssfile='fmt1.css') - # CSS should be in testdir (testdir is absolute) - fmt2 = HtmlFormatter(full=True, cssfile=join(testdir, 'fmt2.css')) + # CSS should be in TEST_DIR (TEST_DIR is absolute) + fmt2 = HtmlFormatter(full=True, cssfile=join(TEST_DIR, 'fmt2.css')) tfile = tempfile.NamedTemporaryFile(suffix='.html') fmt1.format(tokensource, tfile) try: fmt2.format(tokensource, tfile) - self.assert_(isfile(join(testdir, 'fmt2.css'))) + self.assert_(isfile(join(TEST_DIR, 'fmt2.css'))) except IOError: # test directory not writable pass @@ -54,7 +57,7 @@ class HtmlFormatterTest(unittest.TestCase): self.assert_(isfile(join(dirname(tfile.name), 'fmt1.css'))) os.unlink(join(dirname(tfile.name), 'fmt1.css')) try: - os.unlink(join(testdir, 'fmt2.css')) + os.unlink(join(TEST_DIR, 'fmt2.css')) except OSError: pass @@ -76,7 +79,7 @@ class HtmlFormatterTest(unittest.TestCase): tfile = os.fdopen(handle, 'w+b') fmt.format(tokensource, tfile) tfile.close() - catname = os.path.join(testdir, 'dtds', 'HTML4.soc') + catname = os.path.join(TEST_DIR, 'dtds', 'HTML4.soc') try: try: import subprocess |