def test_insert_hash(self): """Test _insert_hash returns correct filenames with provided hashes.""" self.assertEqual(build._insert_hash('file.js', '123456'), 'file.123456.js') self.assertEqual(build._insert_hash('path/to/file.js', '654321'), 'path/to/file.654321.js') self.assertEqual(build._insert_hash('file.min.js', 'abcdef'), 'file.min.abcdef.js') self.assertEqual(build._insert_hash('path/to/file.min.js', 'fedcba'), 'path/to/file.min.fedcba.js')
def test_insert_hash(self): # pylint: disable=protected-access self.assertEqual(build._insert_hash('file.js', '123456'), 'file.123456.js') self.assertEqual(build._insert_hash('path/to/file.js', '654321'), 'path/to/file.654321.js') self.assertEqual(build._insert_hash('file.min.js', 'abcdef'), 'file.min.abcdef.js') self.assertEqual(build._insert_hash('path/to/file.min.js', 'fedcba'), 'path/to/file.min.fedcba.js')
def test_verify_filepath_hash(self): """Test _verify_filepath_hash raises exception: 1) When there is an empty hash dict. 2) When a filename is expected to contain hash but does not. 3) When there is a hash in filename that cannot be found in hash dict. """ # Final filepath example: base.240933e7564bd72a4dde42ee23260c5f.html. file_hashes = dict() base_filename = 'base.html' with self.assertRaisesRegexp(ValueError, 'Hash dict is empty'): build._verify_filepath_hash(base_filename, file_hashes) # Generate a random hash dict for base.html. file_hashes = {base_filename: random.getrandbits(128)} with self.assertRaisesRegexp( ValueError, '%s is expected to contain MD5 hash' % base_filename): build._verify_filepath_hash(base_filename, file_hashes) bad_filepath = 'README' with self.assertRaisesRegexp( ValueError, 'Filepath has less than 2 partitions after splitting'): build._verify_filepath_hash(bad_filepath, file_hashes) hashed_base_filename = build._insert_hash( base_filename, random.getrandbits(128)) with self.assertRaisesRegexp( KeyError, 'Hash from file named %s does not match hash dict values' % hashed_base_filename): build._verify_filepath_hash(hashed_base_filename, file_hashes)
def test_process_html(self): """Test process_html removes whitespaces and adds hash to filepaths.""" BASE_HTML_SOURCE_PATH = os.path.join(MOCK_TEMPLATES_DEV_DIR, 'base.html') BASE_JS_RELATIVE_PATH = os.path.join('pages', 'Base.js') BASE_JS_SOURCE_PATH = os.path.join(MOCK_TEMPLATES_COMPILED_JS_DIR, BASE_JS_RELATIVE_PATH) build._ensure_files_exist([BASE_HTML_SOURCE_PATH, BASE_JS_SOURCE_PATH]) # Prepare a file_stream object from StringIO. minified_html_file_stream = StringIO.StringIO() # Obtain actual file hashes of /templates to add hash to all filepaths # within the HTML file. The end result will look like: # E.g <script ... App.js></script> # --> <script ... App.[hash].js></script>. # Only need to hash Base.js. with self.swap(build, 'FILE_EXTENSIONS_TO_IGNORE', ('.html', )): file_hashes = build.get_file_hashes(MOCK_TEMPLATES_DEV_DIR) file_hashes.update( build.get_file_hashes(MOCK_TEMPLATES_COMPILED_JS_DIR)) # Assert that base.html has white spaces and has original filepaths. with open(BASE_HTML_SOURCE_PATH, 'r') as source_base_file: source_base_file_content = source_base_file.read() self.assertRegexpMatches( source_base_file_content, r'\s{2,}', msg='No white spaces detected in %s unexpectedly' % BASE_HTML_SOURCE_PATH) # Look for templates/pages/Base.js in source_base_file_content. self.assertIn(BASE_JS_RELATIVE_PATH, source_base_file_content) # Build base.html file. with open(BASE_HTML_SOURCE_PATH, 'r') as source_base_file: build.process_html(source_base_file, minified_html_file_stream, file_hashes) minified_html_file_content = minified_html_file_stream.getvalue() self.assertNotRegexpMatches( minified_html_file_content, r'\s{2,}', msg='All white spaces must be removed from %s' % BASE_HTML_SOURCE_PATH) # Assert that hashes are inserted into filenames in base.html. # Final filepath in base.html example: # /build/templates/head/pages/Base.081ce90f17ecdf07701d83cb860985c2.js. final_filename = build._insert_hash(BASE_JS_RELATIVE_PATH, file_hashes[BASE_JS_RELATIVE_PATH]) # Look for templates/pages/Base.081ce90f17ecdf07701d83cb860985c2.js in # minified_html_file_content. self.assertIn(final_filename, minified_html_file_content)