Skip to content

Commit

Permalink
- automatically remove old files from COFFEESCRIPT_CACHE
Browse files Browse the repository at this point in the history
 - increase version number
  • Loading branch information
Andrey Fedoseev committed Jun 15, 2011
1 parent 21b4c2a commit a3939ce
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 11 deletions.
13 changes: 13 additions & 0 deletions CHANGES.rst
@@ -0,0 +1,13 @@
Changes
=======

0.2
---

- Automatically remove old files from COFFEESCRIPT_CACHE
- Add basic unit tests

0.1
---

- Initial release
6 changes: 6 additions & 0 deletions coffeescript/templatetags/coffeescript.py
Expand Up @@ -83,4 +83,10 @@ def coffeescript(path):
compiled_file.write(out)
compiled_file.close()

# Remove old files
compiled_filename = os.path.split(output_path)[-1]
for filename in os.listdir(output_directory):
if filename.startswith(base_filename) and filename != compiled_filename:
os.remove(os.path.join(output_directory, filename))

return output_path[len(settings.MEDIA_ROOT):].lstrip("/")
4 changes: 3 additions & 1 deletion coffeescript/tests/django_settings.py
Expand Up @@ -5,4 +5,6 @@
MEDIA_ROOT = os.path.join(os.path.dirname(__file__), 'media')
INSTALLED_APPS = (
"coffeescript",
)
)
COFFEESCRIPT_MTIME_DELAY = 2
COFFEESCRIPT_OUTPUT_DIR = "COFFEESCRIPT_CACHE"
28 changes: 20 additions & 8 deletions coffeescript/tests/tests.py
Expand Up @@ -4,6 +4,7 @@
import os
import re
import time
import shutil


os.environ["DJANGO_SETTINGS_MODULE"] = "coffeescript.tests.django_settings"
Expand All @@ -12,7 +13,15 @@
class CoffeeScriptTestCase(TestCase):

def setUp(self):
pass
from django.conf import settings as django_settings
self.django_settings = django_settings

output_dir = os.path.join(self.django_settings.MEDIA_ROOT,
self.django_settings.COFFEESCRIPT_OUTPUT_DIR)

# Remove the output directory if it exists to start from scratch
if os.path.exists(output_dir):
shutil.rmtree(output_dir)

def test_inline_coffeescript(self):
template = Template("""
Expand All @@ -27,8 +36,6 @@ def test_inline_coffeescript(self):
self.assertEqual(template.render(RequestContext({})).strip(), rendered)

def test_external_coffeescript(self):
from coffeescript import settings as coffeescript_settings
coffeescript_settings.COFFEESCRIPT_MTIME_DELAY = 2

template = Template("""
{% load coffeescript %}
Expand All @@ -38,8 +45,7 @@ def test_external_coffeescript(self):
compiled_filename = template.render(RequestContext({})).strip()
self.assertTrue(bool(compiled_filename_re.match(compiled_filename)))

from django.conf import settings
compiled_path = os.path.join(settings.MEDIA_ROOT, compiled_filename)
compiled_path = os.path.join(self.django_settings.MEDIA_ROOT, compiled_filename)
compiled_content = open(compiled_path).read()
compiled = """(function() {
console.log("Hello, World!");
Expand All @@ -48,7 +54,7 @@ def test_external_coffeescript(self):
self.assertEquals(compiled_content, compiled)

# Change the modification time
source_path = os.path.join(settings.MEDIA_ROOT, "scripts/test.coffee")
source_path = os.path.join(self.django_settings.MEDIA_ROOT, "scripts/test.coffee")
os.utime(source_path, None)

# The modification time is cached so the compiled file is not updated
Expand All @@ -57,13 +63,19 @@ def test_external_coffeescript(self):
self.assertEquals(compiled_filename, compiled_filename_2)

# Wait to invalidate the cached modification time
time.sleep(coffeescript_settings.COFFEESCRIPT_MTIME_DELAY)
time.sleep(self.django_settings.COFFEESCRIPT_MTIME_DELAY)

# Now the file is re-compiled
compiled_filename_3 = template.render(RequestContext({})).strip()
self.assertTrue(bool(compiled_filename_re.match(compiled_filename_3)))
self.assertNotEquals(compiled_filename, compiled_filename_3)


# Check that we have only one compiled file, old files should be removed

compiled_file_dir = os.path.dirname(os.path.join(self.django_settings.MEDIA_ROOT,
compiled_filename_3))
self.assertEquals(len(os.listdir(compiled_file_dir)), 1)


if __name__ == '__main__':
main()
5 changes: 3 additions & 2 deletions setup.py
Expand Up @@ -7,17 +7,18 @@ def read(fname):


README = read('README.rst')
CHANGES = read('CHANGES.rst')


setup(
name = "django-coffeescript",
packages = find_packages(),
version = "0.1",
version = "0.2",
author = "Andrey Fedoseev",
author_email = "andrey.fedoseev@gmail.com",
url = "https://github.com/andreyfedoseev/django-coffeescript",
description = "Django template tags to compile CoffeeScript",
long_description = README,
long_description = "\n\n".join([README, CHANGES]),
classifiers = [
'Development Status :: 4 - Beta',
'Framework :: Django',
Expand Down

0 comments on commit a3939ce

Please sign in to comment.