Anonymous avatar Anonymous committed 51fad2f

[svn] A test case for the external CSS function of the HTML formatter.

Comments (0)

Files changed (2)

pygments/formatters/html.py

 
     `cssfile`
         If the `full` option is true and this option is given, it must be the
-        name of an external file. The stylesheet is then written to this file
-        instead of the HTML file. *New in Pygments 0.6.*
+        name of an external file. If the filename does not include an absolute
+        path, the file's path will be assumed to be relative to the main output
+        file's path, if the latter can be found. The stylesheet is then written
+        to this file instead of the HTML file. *New in Pygments 0.6.*
 
     `linenospecial`
         If set to a number n > 0, every nth line number is given the CSS
 
     def _wrap_full(self, inner, outfile):
         if self.cssfile:
-            try:
-                filename = outfile.name
-                cssfilename = os.path.join(os.path.dirname(filename), self.cssfile)
-            except AttributeError:
-                print >>sys.stderr, 'Note: Cannot determine output file name, ' \
-                      'using current directory as base for the CSS file name'
+            if os.path.isabs(self.cssfile):
+                # it's an absolute filename
                 cssfilename = self.cssfile
+            else:
+                try:
+                    filename = outfile.name
+                    if not filename or filename[0] == '<':
+                        # pseudo files, e.g. name == '<fdopen>'
+                        raise AttributeError
+                    cssfilename = os.path.join(os.path.dirname(filename), self.cssfile)
+                except AttributeError:
+                    print >>sys.stderr, 'Note: Cannot determine output file name, ' \
+                          'using current directory as base for the CSS file name'
+                    cssfilename = self.cssfile
             # write CSS file 
             try:
                 cf = open(cssfilename, "w")

tests/test_html_formatter.py

 import StringIO
 import random
 import tempfile
+from os.path import join, dirname, isfile
 
 from pygments import lexers, formatters
 from pygments.token import _TokenType
 from pygments.formatters import HtmlFormatter
 from pygments.lexers import PythonLexer
 
+tokensource = list(PythonLexer().get_tokens(file(os.path.join(testdir, testfile)).read()))
+
 class HtmlFormatterTest(unittest.TestCase):
 
-# TODO: write this test.
-#    def test_external_css(self):
-#        pass
+    def test_external_css(self):
+        # 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'))
+        tfile = tempfile.NamedTemporaryFile(suffix='.html')
+        fmt1.format(tokensource, tfile)
+        try:
+            fmt2.format(tokensource, tfile)
+        except IOError:
+            # test directory not writable
+            pass
+        tfile.close()
+
+        self.assert_(isfile(join(dirname(tfile.name), 'my.css')))
+
+        os.unlink(join(dirname(tfile.name), 'fmt1.css'))
+        try:
+            os.unlink(join(testdir, 'fmt2.css'))
+        except OSError:
+            pass
+
 
     def test_all_options(self):
-        tokensource = list(PythonLexer().get_tokens(file(os.path.join(testdir, testfile)).read()))
-        
         for optdict in [dict(nowrap=True),
                         dict(linenos=True),
                         dict(linenos=True, full=True),
             fmt.format(tokensource, outfile)
 
     def test_valid_output(self):
-        tokensource = list(PythonLexer().get_tokens(file(os.path.join(testdir, testfile)).read()))
+        # test all available wrappers
         fmt = HtmlFormatter(full=True, linenos=True, noclasses=True)
 
         handle, pathname = tempfile.mkstemp('.html')
-        # place all output files in /tmp too
-        old_wd = os.getcwd()
-        os.chdir(os.path.dirname(pathname))
         tfile = os.fdopen(handle, 'w+b')
         fmt.format(tokensource, tfile)
         tfile.close()
             self.failIf(ret, 'nsgmls run reported errors')
 
         os.unlink(pathname)
-        os.chdir(old_wd)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.