def test_handles_manifest_v_3_4_0_homepage(self): self.maxDiff = None with patch('builtins.open', open_mock_v3_4_0_homepage): self.assertEqual( asset_manifest.generate_manifest(server_url, '.'), { 'main_css': 'css/main.d1b05096.chunk.css', 'main_js': 'js/main.86964f0e.chunk.js', 'main_js_map': 'js/main.86964f0e.chunk.js.map', 'runtime_main_js': 'js/runtime-main.d13328da.js', 'runtime_main_js_map': 'js/runtime-main.d13328da.js.map', 'static_css_main_d1b05096_chunk_css_map': 'css/main.d1b05096.chunk.css.map', 'static_js_2_95e01bf1_chunk_js': 'js/2.95e01bf1.chunk.js', 'static_js_2_95e01bf1_chunk_js_map': 'js/2.95e01bf1.chunk.js.map', 'static_js_2_95e01bf1_chunk_js_LICENSE_txt': 'js/2.95e01bf1.chunk.js.LICENSE.txt', 'static_media_logo_svg': 'media/logo.5d5d9eef.svg', 'entrypoints': { 'css': ['css/main.d1b05096.chunk.css'], 'js': [ 'js/runtime-main.d13328da.js', 'js/2.95e01bf1.chunk.js', 'js/main.86964f0e.chunk.js' ] } })
def test_handles_manifest_v_3_2_0(self): self.maxDiff = None with patch('builtins.open', open_mock_v_3_2_0): self.assertEqual( asset_manifest.generate_manifest(server_url, '.'), { 'main_css': 'css/main.b100e6da.chunk.css', 'main_js': 'js/main.f245946a.chunk.js', 'main_js_map': 'js/main.f245946a.chunk.js.map', 'runtime_main_js': 'js/runtime-main.7b0131e1.js', 'runtime_main_js_map': 'js/runtime-main.7b0131e1.js.map', 'static_css_main_b100e6da_chunk_css_map': 'css/main.b100e6da.chunk.css.map', 'static_js_2_7059510f_chunk_js': 'js/2.7059510f.chunk.js', 'static_js_2_7059510f_chunk_js_map': 'js/2.7059510f.chunk.js.map', 'static_media_logo_svg': 'media/logo.25bf045c.svg', 'entrypoints': { 'css': ['css/main.b100e6da.chunk.css'], 'js': [ 'js/runtime-main.7b0131e1.js', 'js/2.7059510f.chunk.js', 'js/main.f245946a.chunk.js' ] } })
def test_checks_static_root_when_manifest_missing_from_cra_build_dir(self): # Pretend this folder is STATIC_ROOT to emulate falling back and accessing the # collected asset-manifest.json _here_path = os.path.dirname(os.path.realpath(__file__)) setattr(settings, 'BASE_DIR', _here_path) setattr(settings, 'STATIC_ROOT', os.path.join(_here_path, 'static')) self.assertEqual( asset_manifest.generate_manifest(server_url, 'not_a_real_dir'), { 'main_js': 'js/main.1234.js', 'main_css': 'css/main.1234.css', })
def test_returns_bundle_url_if_cra_is_running(self, mock_hosted_check): with mock_hosted_check: self.assertEqual( asset_manifest.generate_manifest(server_url, ''), { 'bundle_js': [ 'http://foo.bar:9999/static/js/bundle.js', 'http://foo.bar:9999/static/js/0.chunk.js', 'http://foo.bar:9999/static/js/1.chunk.js', 'http://foo.bar:9999/static/js/main.chunk.js' ] })
def test_returns_main_paths_if_cra_is_not_running(self): open_mock = mock_open(read_data='''{ "main.js": "static/js/main.1234.js", "main.css": "static/css/main.1234.css" }''') with patch('builtins.open', open_mock): self.assertEqual( asset_manifest.generate_manifest(False, server_url, '.'), { 'main_js': 'js/main.1234.js', 'main_css': 'css/main.1234.css', })
def test_handles_manifest_v_3_1_2(self): with patch('builtins.open', open_mock_v3_1_2): self.assertEqual( asset_manifest.generate_manifest(server_url, '.'), { 'main_css': 'css/main.b100e6da.chunk.css', 'main_js': 'js/main.5745ba44.chunk.js', 'main_js_map': 'js/main.5745ba44.chunk.js.map', 'runtime_main_js': 'js/runtime-main.e5179790.js', 'runtime_main_js_map': 'js/runtime-main.e5179790.js.map', 'static_css_main_b100e6da_chunk_css_map': 'css/main.b100e6da.chunk.css.map', 'static_js_2_24ceb05b_chunk_js': 'js/2.24ceb05b.chunk.js', 'static_js_2_24ceb05b_chunk_js_map': 'js/2.24ceb05b.chunk.js.map', 'static_media_logo_svg': 'media/logo.25bf045c.svg' })
def test_returns_manifest_paths_when_cra_is_not_running(self): with patch('builtins.open', open_mock_v2): self.assertEqual( asset_manifest.generate_manifest(server_url, '.'), { 'main_css': 'css/main.80e572c9.chunk.css', 'main_js': 'js/main.ef0788cc.chunk.js', 'main_js_map': 'js/main.ef0788cc.chunk.js.map', 'runtime_main_js': 'js/runtime~main.a8a9905a.js', 'runtime_main_js_map': 'js/runtime~main.a8a9905a.js.map', 'static_css_main_80e572c9_chunk_css_map': 'css/main.80e572c9.chunk.css.map', 'static_js_2_597565cd_chunk_js': 'js/2.597565cd.chunk.js', 'static_js_2_597565cd_chunk_js_map': 'js/2.597565cd.chunk.js.map', 'static_media_logo_svg': 'media/logo.25bf045c.svg' })
_host = _CRA_DEFAULT_HOST # Allow the user to specify the port create-react-app is running on if hasattr(settings, 'CRA_PORT') and type(settings.CRA_PORT) is int: _port = settings.CRA_PORT # Allow the user to specify the host create-react-app is running on if hasattr(settings, 'CRA_HOST'): _host = settings.CRA_HOST # The URL the create-react-app liveserver is accessible at CRA_URL = 'http://{}:{}'.format(_host, _port) if hasattr(settings, 'CRA_APP_NAME'): CRA_APP_NAME = settings.CRA_APP_NAME else: CRA_APP_NAME = 'react' # The ability to access this file means the create-react-app liveserver is running CRA_BUNDLE_PATH = '{}/static/js/bundle.js'.format(CRA_URL) # Check if Create-React-App live server is up and running CRA_LIVE = is_server_live(CRA_BUNDLE_PATH) # The path to the CRA project directory, relative to the Django project's base directory CRA_FS_APP_DIR = os.path.join(settings.BASE_DIR, CRA_APP_NAME) # A list of entries in CRA's build bundle's STATIC_ASSET_MANIFEST = generate_manifest(CRA_LIVE, CRA_BUNDLE_PATH, CRA_FS_APP_DIR)
def test_returns_empty_dict_if_file_not_found(self): open_mock = MagicMock(side_effect=Exception) with patch('builtins.open', open_mock): self.assertEqual(asset_manifest.generate_manifest(server_url, '.'), {})
def test_returns_dict(self): self.assertIsInstance(asset_manifest.generate_manifest('', ''), dict)
# Allow the user to specify a different server-side port _server_port = _port if hasattr(settings, 'CRA_SERVER_PORT') and type(settings.CRA_SERVER_PORT) is int: _server_port = settings.CRA_SERVER_PORT # Allow the user to specify the host create-react-app is running on if hasattr(settings, 'CRA_HOST'): _host = settings.CRA_HOST # Allow the user to specify a different server-side host _server_host = _host if hasattr(settings, 'CRA_SERVER_HOST'): _server_host = settings.CRA_SERVER_HOST # The URL the create-react-app liveserver is accessible at CRA_URL = 'http://{}:{}'.format(_host, _port) CRA_SERVER_URL = 'http://{}:{}'.format(_server_host, _server_port) if hasattr(settings, 'CRA_APP_NAME'): CRA_APP_NAME = settings.CRA_APP_NAME else: CRA_APP_NAME = 'react' # The path to the CRA project directory, relative to the Django project's base directory CRA_FS_APP_DIR = os.path.join(settings.BASE_DIR, CRA_APP_NAME) # A list of entries in CRA's build bundle's STATIC_ASSET_MANIFEST = generate_manifest(CRA_URL, CRA_FS_APP_DIR, CRA_SERVER_URL)
_host = settings.CRA_HOST # The URL the create-react-app liveserver is accessible at CRA_URL = 'http://{}:{}'.format(_host, _port) if hasattr(settings, 'CRA_APP_NAME'): CRA_APP_NAME = settings.CRA_APP_NAME else: CRA_APP_NAME = 'react' # Be mindful of CRA's relative paths when bootstraping from the CRA liveserver _cra_liveserver_url = CRA_URL if hasattr(settings, 'CRA_PACKAGE_JSON_HOMEPAGE'): relative_path = str(settings.CRA_PACKAGE_JSON_HOMEPAGE) # Normalize homepage path if it starts with / or ends with / if relative_path.startswith('/'): # Strip leading / relative_path = relative_path[1:] if relative_path.endswith('/'): # Strip trailing / relative_path = relative_path[0:-1] # Should result in something like 'http://localhost:3000/frontend' _cra_liveserver_url = '{}/{}'.format(_cra_liveserver_url, relative_path) # The path to the CRA project directory, relative to the Django project's base directory CRA_FS_APP_DIR = os.path.join(settings.BASE_DIR, CRA_APP_NAME) # A list of entries in CRA's build bundle's STATIC_ASSET_MANIFEST = generate_manifest(_cra_liveserver_url, CRA_FS_APP_DIR)
def test_returns_bundle_url_if_cra_is_running(self): self.assertEqual( asset_manifest.generate_manifest(True, server_url, ''), {'bundle_js': server_url})