<commit_before># -*- coding: utf-8 -*-
from django.conf import settings

if __name__ == '__main__':
    # When running this file in isolation, we need to set up the configuration
    # before importing 'template'.
    settings.configure()

from datetime import datetime, timedelta
import os
import sys
import traceback
import unittest

from django import template
from django.core import urlresolvers
from django.template import loader
from django.template.loaders import app_directories, filesystem
from django.utils.translation import activate, deactivate, ugettext as _
from django.utils.safestring import mark_safe
from django.utils.tzinfo import LocalTimezone

from context import context_tests
from custom import custom_filters
from parser import filter_parsing, variable_parsing
from unicode import unicode_tests

try:
    from loaders import *
except ImportError:
    pass # If setuptools isn't installed, that's fine. Just move on.

import filters

# Some other tests we would like to run
__test__ = {
    'unicode': unicode_tests,
    'context': context_tests,
    'filter_parsing': filter_parsing,
    'custom_filters': custom_filters,
}

#################################
# Custom template tag for tests #
#################################

register = template.Library()

class EchoNode(template.Node):
    def __init__(self, contents):
        self.contents = contents

    def render(self, context):
        return " ".join(self.contents)

def do_echo(parser, token):
    return EchoNode(token.contents.split()[1:])

register.tag("echo", do_echo)

template.libraries['django.templatetags.testtags'] = register

#####################################
# Helper objects for template tests #
#####################################

class SomeException(Exception):
    silent_variable_failure = True

class SomeOtherException(Exception):
    pass

class SomeClass:
    def __init__(self):
        self.otherclass = OtherClass()

    def method(self):
        return "SomeClass.method"

    def method2(self, o):
        return o

    def method3(self):
        raise SomeException

    def method4(self):
        raise SomeOtherException

class OtherClass:
    def method(self):
        return "OtherClass.method"

class UTF8Class:
    "Class whose __str__ returns non-ASCII data"
    def __str__(self):
        return u'ŠĐĆŽćžšđ'.encode('utf-8')

class Templates(unittest.TestCase):
    def test_loaders_security(self):
        def test_template_sources(path, template_dirs, expected_sources):
            if isinstance(expected_sources, list):
                # Fix expected sources so they are normcased and abspathed
                expected_sources = [os.path.normcase(os.path.abspath(s)) for s in expected_sources]
            # Test the two loaders (app_directores and filesystem).
            func1 = lambda p, t: list(app_directories.get_template_sources(p, t))
            func2 = lambda p, t: list(filesystem.get_template_sources(p, t))
            for func in (func1, func2):
                if isinstance(expected_sources, list):
                    self.assertEqual(func(path, template_dirs), expected_sources)
                else:
                    self.assertRaises(expected_sources, func, path, template_dirs)

        template_dirs = ['/dir1', '/dir2']
        test_template_sources('index.html', template_dirs,
                              ['/dir1/index.html', '/dir2/index.html'])
        test_template_sources('/etc/passwd', template_dirs, [])
        test_template_sources('etc/passwd', template_dirs,
                              ['/dir1/etc/passwd', '/dir2/etc/passwd'])
        test_template_sources('../etc/passwd', template_dirs, [])
        test_template_sources('../../../etc/passwd', template_dirs, [])
        test_template_sources('/dir1/index.html', template_dirs,
                              ['/dir1/index.html'])
        test_template_sources('../dir2/index.html', template_dirs,
                              ['/dir2/index.html'])
        test_template_sources('/dir1blah', template_dirs, [])
        test_template_sources('../dir1blah', template_dirs, [])

        # UTF-8 bytestrings are permitted.
        test_template_sources('\xc3\x85ngstr\xc3\xb6m', template_dirs,
                              [u'/dir1/Ångström', u'/dir2/Ångström'])
        # Unicode strings are permitted.
        test_template_sources(u'Ångström', template_dirs,
                              [u'/dir1/Ångström', u'/dir2/Ångström'])
        test_template_sources(u'Ångström', ['/Straße'], [u'/Straße/Ångström'])
        test_template_sources('\xc3\x85ngstr\xc3\xb6m', ['/Straße'],
                              [u'/Straße/Ångström'])
        # Invalid UTF-8 encoding in bytestrings is not. Should raise a
        # semi-useful error message.
        test_template_sources('\xc3\xc3', template_dirs, UnicodeDecodeError)

        # Case insensitive tests (for win32). Not run unless we're on
        # a case insensitive operating system.
        if os.path.normcase('/TEST') == os.path.normpath('/test'):
            template_dirs = ['/dir1', '/DIR2']
            test_template_sources('index.html', template_dirs,
                                  ['/dir1/index.html', '/dir2/index.html'])
            test_template_sources('/DIR1/index.HTML', template_dirs,
                                  ['/dir1/index.html'])

    def test_token_smart_split(self):
        # Regression test for #7027
        token = template.Token(template.TOKEN_BLOCK, 'sometag _("Page not found") value|yesno:_("yes,no")')
        split = token.split_contents()
        self.assertEqual(split, ["sometag", '_("Page not found")', 'value|yesno:_("yes,no")'])

    def test_url_reverse_no_settings_module(self):
        #Regression test for #9005
        from django.template import Template, Context, TemplateSyntaxError
        old_settings_module = settings.SETTINGS_MODULE
        settings.SETTINGS_MODULE = None
        t = Template('{% url will_not_match %}')
        c = Context()
        try:
            rendered = t.render(c)
        except TemplateSyntaxError, e:
            #Assert that we are getting the template syntax error and not the
            #string encoding error.
            self.assertEquals(e.message, "Caught an exception while rendering: Reverse for 'will_not_match' with arguments '()' and keyword arguments '{}' not found.")
        settings.SETTINGS_MODULE = old_settings_module

    def test_templates(self):
        template_tests = self.get_template_tests()
        filter_tests = filters.get_filter_tests()

        # Quickly check that we aren't accidentally using a name in both
        # template and filter tests.
        overlapping_names = [name for name in filter_tests if name in template_tests]
        assert not overlapping_names, 'Duplicate test name(s): %s' % ', '.join(overlapping_names)

        template_tests.update(filter_tests)

        # Register our custom template loader.
        def test_template_loader(template_name, template_dirs=None):
            "A custom template loader that loads the unit-test templates."
            try:
                return (template_tests[template_name][0] , "test:%s" % template_name)
            except KeyError:
                raise template.TemplateDoesNotExist, template_name

        old_template_loaders = loader.template_source_loaders
        loader.template_source_loaders = [test_template_loader]
<commit_msg>Fixed the test in [10351] to work reguardless of the test settings.<commit_after># -*- coding: utf-8 -*-
from django.conf import settings

if __name__ == '__main__':
    # When running this file in isolation, we need to set up the configuration
    # before importing 'template'.
    settings.configure()

from datetime import datetime, timedelta
import os
import sys
import traceback
import unittest

from django import template
from django.core import urlresolvers
from django.template import loader
from django.template.loaders import app_directories, filesystem
from django.utils.translation import activate, deactivate, ugettext as _
from django.utils.safestring import mark_safe
from django.utils.tzinfo import LocalTimezone

from context import context_tests
from custom import custom_filters
from parser import filter_parsing, variable_parsing
from unicode import unicode_tests

try:
    from loaders import *
except ImportError:
    pass # If setuptools isn't installed, that's fine. Just move on.

import filters

# Some other tests we would like to run
__test__ = {
    'unicode': unicode_tests,
    'context': context_tests,
    'filter_parsing': filter_parsing,
    'custom_filters': custom_filters,
}

#################################
# Custom template tag for tests #
#################################

register = template.Library()

class EchoNode(template.Node):
    def __init__(self, contents):
        self.contents = contents

    def render(self, context):
        return " ".join(self.contents)

def do_echo(parser, token):
    return EchoNode(token.contents.split()[1:])

register.tag("echo", do_echo)

template.libraries['django.templatetags.testtags'] = register

#####################################
# Helper objects for template tests #
#####################################

class SomeException(Exception):
    silent_variable_failure = True

class SomeOtherException(Exception):
    pass

class SomeClass:
    def __init__(self):
        self.otherclass = OtherClass()

    def method(self):
        return "SomeClass.method"

    def method2(self, o):
        return o

    def method3(self):
        raise SomeException

    def method4(self):
        raise SomeOtherException

class OtherClass:
    def method(self):
        return "OtherClass.method"

class UTF8Class:
    "Class whose __str__ returns non-ASCII data"
    def __str__(self):
        return u'ŠĐĆŽćžšđ'.encode('utf-8')

class Templates(unittest.TestCase):
    def test_loaders_security(self):
        def test_template_sources(path, template_dirs, expected_sources):
            if isinstance(expected_sources, list):
                # Fix expected sources so they are normcased and abspathed
                expected_sources = [os.path.normcase(os.path.abspath(s)) for s in expected_sources]
            # Test the two loaders (app_directores and filesystem).
            func1 = lambda p, t: list(app_directories.get_template_sources(p, t))
            func2 = lambda p, t: list(filesystem.get_template_sources(p, t))
            for func in (func1, func2):
                if isinstance(expected_sources, list):
                    self.assertEqual(func(path, template_dirs), expected_sources)
                else:
                    self.assertRaises(expected_sources, func, path, template_dirs)

        template_dirs = ['/dir1', '/dir2']
        test_template_sources('index.html', template_dirs,
                              ['/dir1/index.html', '/dir2/index.html'])
        test_template_sources('/etc/passwd', template_dirs, [])
        test_template_sources('etc/passwd', template_dirs,
                              ['/dir1/etc/passwd', '/dir2/etc/passwd'])
        test_template_sources('../etc/passwd', template_dirs, [])
        test_template_sources('../../../etc/passwd', template_dirs, [])
        test_template_sources('/dir1/index.html', template_dirs,
                              ['/dir1/index.html'])
        test_template_sources('../dir2/index.html', template_dirs,
                              ['/dir2/index.html'])
        test_template_sources('/dir1blah', template_dirs, [])
        test_template_sources('../dir1blah', template_dirs, [])

        # UTF-8 bytestrings are permitted.
        test_template_sources('\xc3\x85ngstr\xc3\xb6m', template_dirs,
                              [u'/dir1/Ångström', u'/dir2/Ångström'])
        # Unicode strings are permitted.
        test_template_sources(u'Ångström', template_dirs,
                              [u'/dir1/Ångström', u'/dir2/Ångström'])
        test_template_sources(u'Ångström', ['/Straße'], [u'/Straße/Ångström'])
        test_template_sources('\xc3\x85ngstr\xc3\xb6m', ['/Straße'],
                              [u'/Straße/Ångström'])
        # Invalid UTF-8 encoding in bytestrings is not. Should raise a
        # semi-useful error message.
        test_template_sources('\xc3\xc3', template_dirs, UnicodeDecodeError)

        # Case insensitive tests (for win32). Not run unless we're on
        # a case insensitive operating system.
        if os.path.normcase('/TEST') == os.path.normpath('/test'):
            template_dirs = ['/dir1', '/DIR2']
            test_template_sources('index.html', template_dirs,
                                  ['/dir1/index.html', '/dir2/index.html'])
            test_template_sources('/DIR1/index.HTML', template_dirs,
                                  ['/dir1/index.html'])

    def test_token_smart_split(self):
        # Regression test for #7027
        token = template.Token(template.TOKEN_BLOCK, 'sometag _("Page not found") value|yesno:_("yes,no")')
        split = token.split_contents()
        self.assertEqual(split, ["sometag", '_("Page not found")', 'value|yesno:_("yes,no")'])

    def test_url_reverse_no_settings_module(self):
        #Regression test for #9005
        from django.template import Template, Context, TemplateSyntaxError
        
        old_settings_module = settings.SETTINGS_MODULE
        old_template_debug = settings.TEMPLATE_DEBUG
        
        settings.SETTINGS_MODULE = None
        settings.TEMPLATE_DEBUG = True
        
        t = Template('{% url will_not_match %}')
        c = Context()
        try:
            rendered = t.render(c)
        except TemplateSyntaxError, e:
            #Assert that we are getting the template syntax error and not the
            #string encoding error.
            self.assertEquals(e.message, "Caught an exception while rendering: Reverse for 'will_not_match' with arguments '()' and keyword arguments '{}' not found.")
        
        settings.SETTINGS_MODULE = old_settings_module
        settings.TEMPLATE_DEBUG = old_template_debug

    def test_templates(self):
        template_tests = self.get_template_tests()
        filter_tests = filters.get_filter_tests()

        # Quickly check that we aren't accidentally using a name in both
        # template and filter tests.
        overlapping_names = [name for name in filter_tests if name in template_tests]
        assert not overlapping_names, 'Duplicate test name(s): %s' % ', '.join(overlapping_names)

        template_tests.update(filter_tests)

        # Register our custom template loader.
        def test_template_loader(template_name, template_dirs=None):
            "A custom template loader that loads the unit-test templates."
            try:
                return (template_tests[template_name][0] , "test:%s" % template_name)
            except KeyError:
                raise template.TemplateDoesNotExist, template_name

        old_template_loaders = loader.template_source_loaders
        loader.template_source_loaders = [test_template_loader]

{'fraction_non_alphanumeric': 0.11690898227444732, 'fraction_numerical': 0.00803292836752307, 'mean_word_length': 2.805001262945188, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6875493', 'n_tokens_mistral': 4305, 'n_tokens_neox': 4163, 'n_words': 1041}