markos pygments tests

This commit is contained in:
tom christie tom@tomchristie.com 2011-03-10 08:12:50 +00:00
commit 9ed34be766
2 changed files with 11 additions and 7 deletions

View File

@ -1,7 +1,7 @@
from django.test import TestCase from django.test import TestCase
from djangorestframework.compat import RequestFactory from djangorestframework.compat import RequestFactory
from pygments_api import views from pygments_api import views
import os, tempfile, shutil import os, tempfile, shutil, time, json
class TestPygmentsExample(TestCase): class TestPygmentsExample(TestCase):
@ -35,7 +35,8 @@ class TestPygmentsExample(TestCase):
request = self.factory.get('/pygments') request = self.factory.get('/pygments')
view = views.PygmentsRoot.as_view() view = views.PygmentsRoot.as_view()
response = view(request) response = view(request)
self.assertEquals(locations, response.content) response_locations = json.loads(response.content)
self.assertEquals(locations, response_locations)

View File

@ -21,13 +21,15 @@ import operator
HIGHLIGHTED_CODE_DIR = os.path.join(settings.MEDIA_ROOT, 'pygments') HIGHLIGHTED_CODE_DIR = os.path.join(settings.MEDIA_ROOT, 'pygments')
MAX_FILES = 20 MAX_FILES = 20
def list_dir_sorted_by_ctime(dir):
"""Return a list of files sorted by creation time"""
filepaths = [os.path.join(dir, file) for file in os.listdir(dir)]
return [item[0] for item in sorted([(path, os.path.getctime(path)) for path in filepaths],
key=operator.itemgetter(1), reverse=True)]
def remove_oldest_files(dir, max_files): def remove_oldest_files(dir, max_files):
"""Remove the oldest files in a directory 'dir', leaving at most 'max_files' remaining. """Remove the oldest files in a directory 'dir', leaving at most 'max_files' remaining.
We use this to limit the number of resources in the sandbox.""" We use this to limit the number of resources in the sandbox."""
filepaths = [os.path.join(dir, file) for file in os.listdir(dir)] [os.remove(path) for path in list_dir_sorted_by_ctime(dir)[max_files:]]
ctime_sorted_paths = [item[0] for item in sorted([(path, os.path.getctime(path)) for path in filepaths],
key=operator.itemgetter(1), reverse=True)]
[os.remove(path) for path in ctime_sorted_paths[max_files:]]
class HTMLEmitter(BaseEmitter): class HTMLEmitter(BaseEmitter):
@ -43,7 +45,8 @@ class PygmentsRoot(Resource):
def get(self, request, auth): def get(self, request, auth):
"""Return a list of all currently existing snippets.""" """Return a list of all currently existing snippets."""
unique_ids = sorted(os.listdir(HIGHLIGHTED_CODE_DIR)) unique_ids = [os.path.split(f)[1] for f in list_dir_sorted_by_ctime(HIGHLIGHTED_CODE_DIR)]
unique_ids.reverse()
return [reverse('pygments-instance', args=[unique_id]) for unique_id in unique_ids] return [reverse('pygments-instance', args=[unique_id]) for unique_id in unique_ids]
def post(self, request, auth, content): def post(self, request, auth, content):