forked from oppia/oppia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build_test.py
103 lines (93 loc) · 4.62 KB
/
build_test.py
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
# coding: utf-8
#
# Copyright 2014 The Oppia Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS-IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# pylint: disable=relative-import
import build
from core.tests import test_utils
# pylint: enable=relative-import
class BuildTests(test_utils.GenericTestBase):
"""Test the build methods."""
def test_insert_hash(self):
# pylint: disable=protected-access
self.assertEquals(build._insert_hash('file.js', '123456'),
'file.123456.js')
self.assertEquals(build._insert_hash('path/to/file.js', '654321'),
'path/to/file.654321.js')
self.assertEquals(build._insert_hash('file.min.js', 'abcdef'),
'file.min.abcdef.js')
self.assertEquals(build._insert_hash('path/to/file.min.js', 'fedcba'),
'path/to/file.min.fedcba.js')
# pylint: enable=protected-access
def test_is_file_hash_provided_to_frontend(self):
with self.swap(
build, 'FILEPATHS_PROVIDED_TO_FRONTEND',
('path/to/file.js', 'path/to/file.html', 'file.js')):
self.assertTrue(
build.is_file_hash_provided_to_frontend('path/to/file.js'))
self.assertTrue(
build.is_file_hash_provided_to_frontend('path/to/file.html'))
self.assertTrue(build.is_file_hash_provided_to_frontend('file.js'))
with self.swap(
build, 'FILEPATHS_PROVIDED_TO_FRONTEND',
('path/to/*', '*.js', '*_end.html')):
self.assertTrue(
build.is_file_hash_provided_to_frontend('path/to/file.js'))
self.assertTrue(
build.is_file_hash_provided_to_frontend('path/to/file.html'))
self.assertTrue(build.is_file_hash_provided_to_frontend('file.js'))
self.assertFalse(
build.is_file_hash_provided_to_frontend('path/file.css'))
self.assertTrue(
build.is_file_hash_provided_to_frontend('good_end.html'))
self.assertFalse(
build.is_file_hash_provided_to_frontend('bad_end.css'))
def test_filter_hashes(self):
# set constant to provide everything to frontend.
with self.swap(build, 'FILEPATHS_PROVIDED_TO_FRONTEND', ('*',)):
hashes = {'path/to/file.js': '123456',
'path/file.min.js': '123456'}
filtered_hashes = build.filter_hashes(hashes)
self.assertEquals(
filtered_hashes['/path/to/file.js'],
hashes['path/to/file.js'])
self.assertEquals(
filtered_hashes['/path/file.min.js'],
hashes['path/file.min.js'])
with self.swap(
build, 'FILEPATHS_PROVIDED_TO_FRONTEND',
('test_path/*', 'path/to/file.js')):
hashes = {'path/to/file.js': '123456',
'test_path/to/file.html': '123456',
'test_path/to/file.js': 'abcdef',
'path/path/file.js': 'zyx123',
'file.html': '321xyz'}
filtered_hashes = build.filter_hashes(hashes)
self.assertTrue(filtered_hashes.has_key('/path/to/file.js'))
self.assertTrue(filtered_hashes.has_key('/test_path/to/file.html'))
self.assertTrue(filtered_hashes.has_key('/test_path/to/file.js'))
self.assertFalse(filtered_hashes.has_key('/path/path/file.js'))
self.assertFalse(filtered_hashes.has_key('/file.html'))
def test_get_hashes_json_file_contents(self):
# set constant to provide everything to frontend.
with self.swap(build, 'FILEPATHS_PROVIDED_TO_FRONTEND', ('*',)):
hashes = {'path/file.js': '123456'}
self.assertEqual(
build.get_hashes_json_file_contents(hashes),
'var hashes = JSON.parse(\'{"/path/file.js": "123456"}\');')
hashes = {'file.js': '123456', 'file.min.js': '654321'}
self.assertEqual(
build.get_hashes_json_file_contents(hashes),
('var hashes = JSON.parse(\'{"/file.min.js": "654321", '
'"/file.js": "123456"}\');'))