def test_ensure_files_exist(self): """Test _ensure_files_exist raises exception with a non-existent filepath. """ non_existent_filepaths = [INVALID_INPUT_FILEPATH] # Exception will be raised at first file determined to be non-existent. with self.assertRaisesRegexp( OSError, ('File %s does not exist.') % non_existent_filepaths[0]): build._ensure_files_exist(non_existent_filepaths)
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)