This file is indexed.

/usr/lib/python3/dist-packages/sekizai/tests.py is in python3-django-sekizai 0.9.0-2.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
from __future__ import with_statement
from difflib import SequenceMatcher
import os
from unittest import TestCase
import sys

from django import template
from django.conf import settings
from django.template.loader import render_to_string
import pep8

from sekizai.context import SekizaiContext
from sekizai.helpers import get_namespaces
from sekizai.helpers import get_varname
from sekizai.helpers import validate_template
from sekizai.helpers import Watcher
from sekizai.templatetags.sekizai_tags import import_processor
from sekizai.templatetags.sekizai_tags import validate_context

try:
    unicode_compat = unicode
except NameError:
    unicode_compat = str

try:
    from io import StringIO
except ImportError:
    from StringIO import StringIO


def null_processor(context, data, namespace):
    return ''


def namespace_processor(context, data, namespace):
    return namespace


class SettingsOverride(object):
    """
    Overrides Django settings within a context and resets them to their inital
    values on exit.

    Example:

        with SettingsOverride(DEBUG=True):
            # do something
    """
    class NULL:
        pass

    def __init__(self, **overrides):
        self.overrides = overrides

    def __enter__(self):
        self.old = {}
        for key, value in self.overrides.items():
            self.old[key] = getattr(settings, key, self.NULL)
            setattr(settings, key, value)

    def __exit__(self, type, value, traceback):
        for key, value in self.old.items():
            if value is self.NULL:
                delattr(settings, key)
            else:
                setattr(settings, key, value)


class CaptureStdout(object):
    """
    Overrides sys.stdout with a StringIO stream.
    """
    def __init__(self):
        self.old = None

    def __enter__(self):
        self.old = sys.stdout
        new = sys.stdout = StringIO()
        return new

    def __exit__(self, exc_type, exc_val, exc_tb):
        sys.stdout = self.old


class Match(tuple):  # pragma: no cover
    @property
    def a(self):
        return self[0]

    @property
    def b(self):
        return self[1]

    @property
    def size(self):
        return self[2]


def _backwards_compat_match(thing):  # pragma: no cover
    if isinstance(thing, tuple):
        return Match(thing)
    return thing


class BitDiffResult(object):

    def __init__(self, status, message):
        self.status = status
        self.message = message


class BitDiff(object):

    """
    Visual aid for failing tests
    """

    def __init__(self, expected):
        self.expected = [repr(unicode_compat(bit)) for bit in expected]

    def test(self, result):
        result = [repr(unicode_compat(bit)) for bit in result]
        if self.expected == result:
            return BitDiffResult(True, "success")
        else:  # pragma: no cover
            longest = max(
                [len(x) for x in self.expected] +
                [len(x) for x in result] +
                [len('Expected')]
            )
            sm = SequenceMatcher()
            sm.set_seqs(self.expected, result)
            matches = sm.get_matching_blocks()
            lasta = 0
            lastb = 0
            data = []
            for match in [_backwards_compat_match(match) for match in matches]:
                unmatcheda = self.expected[lasta:match.a]
                unmatchedb = result[lastb:match.b]
                unmatchedlen = max([len(unmatcheda), len(unmatchedb)])
                unmatcheda += ['' for x in range(unmatchedlen)]
                unmatchedb += ['' for x in range(unmatchedlen)]
                for i in range(unmatchedlen):
                    data.append((False, unmatcheda[i], unmatchedb[i]))
                for i in range(match.size):
                    data.append((
                        True, self.expected[match.a + i], result[match.b + i]
                    ))
                lasta = match.a + match.size
                lastb = match.b + match.size
            padlen = (longest - len('Expected'))
            padding = ' ' * padlen
            line1 = '-' * padlen
            line2 = '-' * (longest - len('Result'))
            msg = '\nExpected%s |   | Result' % padding
            msg += '\n--------%s-|---|-------%s' % (line1, line2)
            for success, a, b in data:
                pad = ' ' * (longest - len(a))
                if success:
                    msg += '\n%s%s |   | %s' % (a, pad, b)
                else:
                    msg += '\n%s%s | ! | %s' % (a, pad, b)
            return BitDiffResult(False, msg)


class SekizaiTestCase(TestCase):
    @classmethod
    def setUpClass(cls):
        cls._template_dirs = settings.TEMPLATE_DIRS
        template_dir = os.path.join(
            os.path.dirname(__file__),
            'test_templates'
        )
        settings.TEMPLATE_DIRS = list(cls._template_dirs) + [template_dir]

    @classmethod
    def tearDownClass(cls):
        settings.TEMPLATE_DIRS = cls._template_dirs

    def _render(self, tpl, ctx=None, ctxclass=SekizaiContext):
        ctx = ctx or {}
        return render_to_string(tpl, ctxclass(ctx))

    def _get_bits(self, tpl, ctx=None, ctxclass=SekizaiContext):
        ctx = ctx or {}
        rendered = self._render(tpl, ctx, ctxclass)
        bits = [
            bit for bit in [bit.strip('\n')
                            for bit in rendered.split('\n')] if bit
        ]
        return bits, rendered

    def _test(self, tpl, res, ctx=None, ctxclass=SekizaiContext):
        """
        Helper method to render template and compare it's bits
        """
        ctx = ctx or {}
        bits, rendered = self._get_bits(tpl, ctx, ctxclass)
        differ = BitDiff(res)
        result = differ.test(bits)
        self.assertTrue(result.status, result.message)
        return rendered

    def test_pep8(self):
        sekizai_dir = os.path.dirname(os.path.abspath(__file__))
        pep8style = pep8.StyleGuide()
        with CaptureStdout() as stdout:
            result = pep8style.check_files([sekizai_dir])
            errors = stdout.getvalue()
        self.assertEqual(
            result.total_errors, 0,
            "Code not PEP8 compliant:\n{0}".format(errors)
        )

    def test_basic_dual_block(self):
        """
        Basic dual block testing
        """
        bits = [
            'my css file', 'some content', 'more content', 'final content',
            'my js file'
        ]
        self._test('basic.html', bits)

    def test_named_endaddtoblock(self):
        """
        Testing with named endaddblock
        """
        bits = ["mycontent"]
        self._test('named_end.html', bits)

    def test_eat_content_before_render_block(self):
        """
        Testing that content get's eaten if no render_blocks is available
        """
        bits = ["mycontent"]
        self._test("eat.html", bits)

    def test_sekizai_context_required(self):
        """
        Test that the template tags properly fail if not used with either
        SekizaiContext or the context processor.
        """
        self.assertRaises(
            template.TemplateSyntaxError,
            self._render, 'basic.html', {}, template.Context
        )

    def test_complex_template_inheritance(self):
        """
        Test that (complex) template inheritances work properly
        """
        bits = [
            "head start",
            "some css file",
            "head end",
            "include start",
            "inc add js",
            "include end",
            "block main start",
            "extinc",
            "block main end",
            "body pre-end",
            "inc js file",
            "body end"
        ]
        self._test("inherit/extend.html", bits)
        """
        Test that blocks (and block.super) work properly with sekizai
        """
        bits = [
            "head start",
            "visible css file",
            "some css file",
            "head end",
            "include start",
            "inc add js",
            "include end",
            "block main start",
            "block main base contents",
            "more contents",
            "block main end",
            "body pre-end",
            "inc js file",
            "body end"
        ]
        self._test("inherit/super_blocks.html", bits)

    def test_namespace_isolation(self):
        """
        Tests that namespace isolation works
        """
        bits = ["the same file", "the same file"]
        self._test('namespaces.html', bits)

    def test_variable_namespaces(self):
        """
        Tests variables and filtered variables as block names.
        """
        bits = ["file one", "file two"]
        self._test('variables.html', bits, {'blockname': 'one'})

    def test_invalid_addtoblock(self):
        """
        Tests that template syntax errors are raised properly in templates
        rendered by sekizai tags
        """
        self.assertRaises(
            template.TemplateSyntaxError,
            self._render, 'errors/failadd.html'
        )

    def test_invalid_renderblock(self):
        self.assertRaises(
            template.TemplateSyntaxError,
            self._render, 'errors/failrender.html'
        )

    def test_invalid_include(self):
        self.assertRaises(
            template.TemplateSyntaxError,
            self._render, 'errors/failinc.html'
        )

    def test_invalid_basetemplate(self):
        self.assertRaises(
            template.TemplateSyntaxError,
            self._render, 'errors/failbase.html'
        )

    def test_invalid_basetemplate_two(self):
        self.assertRaises(
            template.TemplateSyntaxError,
            self._render, 'errors/failbase2.html'
        )

    def test_with_data(self):
        """
        Tests the with_data/add_data tags.
        """
        bits = ["1", "2"]
        self._test('with_data.html', bits)

    def test_easy_inheritance(self):
        self.assertEqual('content', self._render("easy_inherit.html").strip())

    def test_validate_context(self):
        sekizai_ctx = SekizaiContext()
        django_ctx = template.Context()
        self.assertRaises(
            template.TemplateSyntaxError,
            validate_context, django_ctx
        )
        self.assertEqual(validate_context(sekizai_ctx), True)
        with SettingsOverride(TEMPLATE_DEBUG=False):
            self.assertEqual(validate_context(django_ctx), False)
            self.assertEqual(validate_context(sekizai_ctx), True)
            bits = ['some content', 'more content', 'final content']
            self._test('basic.html', bits, ctxclass=template.Context)

    def test_post_processor_null(self):
        bits = ['header', 'footer']
        self._test('processors/null.html', bits)

    def test_post_processor_namespace(self):
        bits = ['header', 'footer', 'js']
        self._test('processors/namespace.html', bits)

    def test_import_processor_failfast(self):
        self.assertRaises(TypeError, import_processor, 'invalidpath')

    def test_unique(self):
        bits = ['unique data']
        self._test('unique.html', bits)

    def test_strip(self):
        tpl = template.Template("""
            {% load sekizai_tags %}
            {% addtoblock 'a' strip %} test{% endaddtoblock %}
            {% addtoblock 'a' strip %}test {% endaddtoblock %}
            {% render_block 'a' %}""")
        context = SekizaiContext()
        output = tpl.render(context)
        self.assertEqual(output.count('test'), 1, output)

    def test_addtoblock_processor_null(self):
        bits = ['header', 'footer']
        self._test('processors/addtoblock_null.html', bits)

    def test_addtoblock_processor_namespace(self):
        bits = ['header', 'footer', 'js']
        self._test('processors/addtoblock_namespace.html', bits)


class HelperTests(TestCase):

    def test_validate_template_js_css(self):
        self.assertTrue(validate_template('basic.html', ['js', 'css']))

    def test_validate_template_js(self):
        self.assertTrue(validate_template('basic.html', ['js']))

    def test_validate_template_css(self):
        self.assertTrue(validate_template('basic.html', ['css']))

    def test_validate_template_empty(self):
        self.assertTrue(validate_template('basic.html', []))

    def test_validate_template_notfound(self):
        self.assertFalse(validate_template('basic.html', ['notfound']))

    def test_get_namespaces_easy_inherit(self):
        self.assertEqual(get_namespaces('easy_inherit.html'), ['css'])

    def test_get_namespaces_chain_inherit(self):
        self.assertEqual(get_namespaces('inherit/chain.html'), ['css', 'js'])

    def test_get_namespaces_space_chain_inherit(self):
        self.assertEqual(
            get_namespaces('inherit/spacechain.html'),
            ['css', 'js']
        )

    def test_get_namespaces_var_inherit(self):
        self.assertEqual(get_namespaces('inherit/varchain.html'), [])

    def test_get_namespaces_sub_var_inherit(self):
        self.assertEqual(get_namespaces('inherit/subvarchain.html'), [])

    def test_get_namespaces_null_ext(self):
        self.assertEqual(get_namespaces('inherit/nullext.html'), [])

    def test_deactivate_validate_template(self):
        with SettingsOverride(SEKIZAI_IGNORE_VALIDATION=True):
            self.assertTrue(validate_template('basic.html', ['js', 'css']))
            self.assertTrue(validate_template('basic.html', ['js']))
            self.assertTrue(validate_template('basic.html', ['css']))
            self.assertTrue(validate_template('basic.html', []))
            self.assertTrue(validate_template('basic.html', ['notfound']))

    def test_watcher_add_namespace(self):
        context = SekizaiContext()
        watcher = Watcher(context)
        varname = get_varname()
        context[varname]['key'].append('value')
        changes = watcher.get_changes()
        self.assertEqual(changes, {'key': ['value']})

    def test_watcher_add_data(self):
        context = SekizaiContext()
        varname = get_varname()
        context[varname]['key'].append('value')
        watcher = Watcher(context)
        context[varname]['key'].append('value2')
        changes = watcher.get_changes()
        self.assertEqual(changes, {'key': ['value2']})