clean up temporary dirs in tearDown to force cleanup even if tests fail

This commit is contained in:
Bruno Binet 2012-05-12 02:03:18 +02:00
commit e82c6512b4
3 changed files with 69 additions and 75 deletions

View file

@ -1,14 +1,10 @@
__all__ = [ __all__ = [
'temporary_folder',
'get_article', 'get_article',
'unittest', 'unittest',
] ]
import os import os
import subprocess import subprocess
from contextlib import contextmanager
from tempfile import mkdtemp
from shutil import rmtree
from pelican.contents import Article from pelican.contents import Article
@ -18,20 +14,6 @@ except ImportError:
import unittest import unittest
@contextmanager
def temporary_folder():
"""creates a temporary folder, return it and delete it afterwards.
This allows to do something like this in tests:
>>> with temporary_folder() as d:
# do whatever you want
"""
tempdir = mkdtemp()
yield tempdir
rmtree(tempdir)
def get_article(title, slug, content, lang, extra_metadata=None): def get_article(title, slug, content, lang, extra_metadata=None):
metadata = {'slug': slug, 'title': title, 'lang': lang} metadata = {'slug': slug, 'title': title, 'lang': lang}
if extra_metadata is not None: if extra_metadata is not None:

View file

@ -3,10 +3,12 @@
from mock import MagicMock from mock import MagicMock
import os import os
import re import re
from tempfile import mkdtemp
from shutil import rmtree
from pelican.generators import ArticlesGenerator, LessCSSGenerator from pelican.generators import ArticlesGenerator, LessCSSGenerator
from pelican.settings import _DEFAULT_CONFIG from pelican.settings import _DEFAULT_CONFIG
from .support import unittest, temporary_folder, skipIfNoExecutable from .support import unittest, skipIfNoExecutable
CUR_DIR = os.path.dirname(__file__) CUR_DIR = os.path.dirname(__file__)
@ -107,6 +109,14 @@ class TestLessCSSGenerator(unittest.TestCase):
} }
""" """
def setUp(self):
self.temp_content = mkdtemp()
self.temp_output = mkdtemp()
def tearDown(self):
rmtree(self.temp_content)
rmtree(self.temp_output)
@skipIfNoExecutable('lessc') @skipIfNoExecutable('lessc')
def test_less_compiler(self): def test_less_compiler(self):
@ -114,28 +124,25 @@ class TestLessCSSGenerator(unittest.TestCase):
settings['STATIC_PATHS'] = ['static'] settings['STATIC_PATHS'] = ['static']
settings['LESS_GENERATOR'] = True settings['LESS_GENERATOR'] = True
# we'll nest here for py < 2.7 compat generator = LessCSSGenerator(None, settings, self.temp_content,
with temporary_folder() as temp_content: _DEFAULT_CONFIG['THEME'], self.temp_output, None)
with temporary_folder() as temp_output:
generator = LessCSSGenerator(None, settings, temp_content,
_DEFAULT_CONFIG['THEME'], temp_output, None)
# create a dummy less file # create a dummy less file
less_dir = os.path.join(temp_content, 'static', 'css') less_dir = os.path.join(self.temp_content, 'static', 'css')
less_filename = os.path.join(less_dir, 'test.less') less_filename = os.path.join(less_dir, 'test.less')
less_output = os.path.join(temp_output, 'static', 'css', less_output = os.path.join(self.temp_output, 'static', 'css',
'test.css') 'test.css')
os.makedirs(less_dir) os.makedirs(less_dir)
with open(less_filename, 'w') as less_file: with open(less_filename, 'w') as less_file:
less_file.write(self.LESS_CONTENT) less_file.write(self.LESS_CONTENT)
generator.generate_output() generator.generate_output()
# we have the file ? # we have the file ?
self.assertTrue(os.path.exists(less_output)) self.assertTrue(os.path.exists(less_output))
# was it compiled ? # was it compiled ?
self.assertIsNotNone(re.search(r'^\s+color:\s*#4D926F;$', self.assertIsNotNone(re.search(r'^\s+color:\s*#4D926F;$',
open(less_output).read(), re.MULTILINE | re.IGNORECASE)) open(less_output).read(), re.MULTILINE | re.IGNORECASE))

View file

@ -5,11 +5,11 @@ except ImportError:
import os import os
from filecmp import dircmp from filecmp import dircmp
from tempfile import mkdtemp
from shutil import rmtree
from mock import patch from mock import patch
from .support import temporary_folder
from pelican import Pelican from pelican import Pelican
from pelican.settings import read_settings from pelican.settings import read_settings
@ -25,46 +25,23 @@ class TestPelican(unittest.TestCase):
# general functional testing for pelican. Basically, this test case tries # general functional testing for pelican. Basically, this test case tries
# to run pelican in different situations and see how it behaves # to run pelican in different situations and see how it behaves
def setUp(self):
self.temp_path = mkdtemp()
def tearDown(self):
rmtree(self.temp_path)
def test_basic_generation_works(self): def test_basic_generation_works(self):
# when running pelican without settings, it should pick up the default # when running pelican without settings, it should pick up the default
# ones and generate the output without raising any exception / issuing # ones and generate the output without raising any exception / issuing
# any warning. # any warning.
with temporary_folder() as temp_path: with patch("pelican.contents.getenv") as mock_getenv:
with patch("pelican.contents.getenv") as mock_getenv: # force getenv('USER') to always return the same value
# force getenv('USER') to always return the same value mock_getenv.return_value = "Dummy Author"
mock_getenv.return_value = "Dummy Author" pelican = Pelican(path=INPUT_PATH, output_path=self.temp_path)
pelican = Pelican(path=INPUT_PATH, output_path=temp_path)
pelican.run()
diff = dircmp(temp_path, os.sep.join((OUTPUT_PATH, "basic")))
self.assertEqual(diff.left_only, [], msg="some generated " \
"files are absent from the expected functional " \
"tests output.\n" \
"This is probably because the HTML generated files " \
"changed. If these changes are normal, please refer " \
"to docs/contribute.rst to update the expected " \
"output of the functional tests.")
self.assertEqual(diff.right_only, [], msg="some files from " \
"the expected functional tests output are absent " \
"from the current output.\n" \
"This is probably because the HTML generated files " \
"changed. If these changes are normal, please refer " \
"to docs/contribute.rst to update the expected " \
"output of the functional tests.")
self.assertEqual(diff.diff_files, [], msg="some generated " \
"files differ from the expected functional tests " \
"output.\n" \
"This is probably because the HTML generated files " \
"changed. If these changes are normal, please refer " \
"to docs/contribute.rst to update the expected " \
"output of the functional tests.")
def test_custom_generation_works(self):
# the same thing with a specified set of settings should work
with temporary_folder() as temp_path:
pelican = Pelican(path=INPUT_PATH, output_path=temp_path,
settings=read_settings(SAMPLE_CONFIG))
pelican.run() pelican.run()
diff = dircmp(temp_path, os.sep.join((OUTPUT_PATH, "custom"))) diff = dircmp(
self.temp_path, os.sep.join((OUTPUT_PATH, "basic")))
self.assertEqual(diff.left_only, [], msg="some generated " \ self.assertEqual(diff.left_only, [], msg="some generated " \
"files are absent from the expected functional " \ "files are absent from the expected functional " \
"tests output.\n" \ "tests output.\n" \
@ -86,3 +63,31 @@ class TestPelican(unittest.TestCase):
"changed. If these changes are normal, please refer " \ "changed. If these changes are normal, please refer " \
"to docs/contribute.rst to update the expected " \ "to docs/contribute.rst to update the expected " \
"output of the functional tests.") "output of the functional tests.")
def test_custom_generation_works(self):
# the same thing with a specified set of settings should work
pelican = Pelican(path=INPUT_PATH, output_path=self.temp_path,
settings=read_settings(SAMPLE_CONFIG))
pelican.run()
diff = dircmp(self.temp_path, os.sep.join((OUTPUT_PATH, "custom")))
self.assertEqual(diff.left_only, [], msg="some generated " \
"files are absent from the expected functional " \
"tests output.\n" \
"This is probably because the HTML generated files " \
"changed. If these changes are normal, please refer " \
"to docs/contribute.rst to update the expected " \
"output of the functional tests.")
self.assertEqual(diff.right_only, [], msg="some files from " \
"the expected functional tests output are absent " \
"from the current output.\n" \
"This is probably because the HTML generated files " \
"changed. If these changes are normal, please refer " \
"to docs/contribute.rst to update the expected " \
"output of the functional tests.")
self.assertEqual(diff.diff_files, [], msg="some generated " \
"files differ from the expected functional tests " \
"output.\n" \
"This is probably because the HTML generated files " \
"changed. If these changes are normal, please refer " \
"to docs/contribute.rst to update the expected " \
"output of the functional tests.")