| 
									
										
										
										
											2023-05-03 01:38:27 +08:00
										 |  |  | import os | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-04 21:43:18 +08:00
										 |  |  | import pytest | 
					
						
							| 
									
										
										
										
											2019-06-01 23:35:03 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-04 21:43:18 +08:00
										 |  |  | import flask | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-03 01:38:27 +08:00
										 |  |  | def test_explicit_instance_paths(modules_tmp_path): | 
					
						
							|  |  |  |     with pytest.raises(ValueError, match=".*must be absolute"): | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         flask.Flask(__name__, instance_path="instance") | 
					
						
							| 
									
										
										
										
											2014-09-04 21:43:18 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-03 01:38:27 +08:00
										 |  |  |     app = flask.Flask(__name__, instance_path=os.fspath(modules_tmp_path)) | 
					
						
							|  |  |  |     assert app.instance_path == os.fspath(modules_tmp_path) | 
					
						
							| 
									
										
										
										
											2014-09-04 21:43:18 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-03 01:38:27 +08:00
										 |  |  | def test_uninstalled_module_paths(modules_tmp_path, purge_module): | 
					
						
							|  |  |  |     (modules_tmp_path / "config_module_app.py").write_text( | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         "import os\n" | 
					
						
							|  |  |  |         "import flask\n" | 
					
						
							|  |  |  |         "here = os.path.abspath(os.path.dirname(__file__))\n" | 
					
						
							|  |  |  |         "app = flask.Flask(__name__)\n" | 
					
						
							| 
									
										
										
										
											2014-09-04 21:43:18 +08:00
										 |  |  |     ) | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     purge_module("config_module_app") | 
					
						
							| 
									
										
										
										
											2014-09-04 21:43:18 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     from config_module_app import app | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-03 01:38:27 +08:00
										 |  |  |     assert app.instance_path == os.fspath(modules_tmp_path / "instance") | 
					
						
							| 
									
										
										
										
											2014-09-04 21:43:18 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-03 01:38:27 +08:00
										 |  |  | def test_uninstalled_package_paths(modules_tmp_path, purge_module): | 
					
						
							|  |  |  |     app = modules_tmp_path / "config_package_app" | 
					
						
							|  |  |  |     app.mkdir() | 
					
						
							|  |  |  |     (app / "__init__.py").write_text( | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |         "import os\n" | 
					
						
							|  |  |  |         "import flask\n" | 
					
						
							|  |  |  |         "here = os.path.abspath(os.path.dirname(__file__))\n" | 
					
						
							|  |  |  |         "app = flask.Flask(__name__)\n" | 
					
						
							| 
									
										
										
										
											2014-09-04 21:43:18 +08:00
										 |  |  |     ) | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     purge_module("config_package_app") | 
					
						
							| 
									
										
										
										
											2014-09-04 21:43:18 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     from config_package_app import app | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-03 01:38:27 +08:00
										 |  |  |     assert app.instance_path == os.fspath(modules_tmp_path / "instance") | 
					
						
							| 
									
										
										
										
											2014-09-04 21:43:18 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-03 01:38:27 +08:00
										 |  |  | def test_uninstalled_namespace_paths(tmp_path, monkeypatch, purge_module): | 
					
						
							| 
									
										
										
										
											2022-05-27 03:12:36 +08:00
										 |  |  |     def create_namespace(package): | 
					
						
							| 
									
										
										
										
											2023-05-03 01:38:27 +08:00
										 |  |  |         project = tmp_path / f"project-{package}" | 
					
						
							|  |  |  |         monkeypatch.syspath_prepend(os.fspath(project)) | 
					
						
							|  |  |  |         ns = project / "namespace" / package | 
					
						
							|  |  |  |         ns.mkdir(parents=True) | 
					
						
							|  |  |  |         (ns / "__init__.py").write_text("import flask\napp = flask.Flask(__name__)\n") | 
					
						
							| 
									
										
										
										
											2022-05-27 03:12:36 +08:00
										 |  |  |         return project | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     _ = create_namespace("package1") | 
					
						
							|  |  |  |     project2 = create_namespace("package2") | 
					
						
							|  |  |  |     purge_module("namespace.package2") | 
					
						
							|  |  |  |     purge_module("namespace") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     from namespace.package2 import app | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-03 01:38:27 +08:00
										 |  |  |     assert app.instance_path == os.fspath(project2 / "instance") | 
					
						
							| 
									
										
										
										
											2022-05-27 03:12:36 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  | def test_installed_module_paths( | 
					
						
							| 
									
										
										
										
											2025-03-30 06:42:58 +08:00
										 |  |  |     modules_tmp_path, modules_tmp_path_prefix, purge_module, site_packages | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  | ): | 
					
						
							| 
									
										
										
										
											2023-05-03 01:38:27 +08:00
										 |  |  |     (site_packages / "site_app.py").write_text( | 
					
						
							| 
									
										
										
										
											2019-06-02 00:22:20 +08:00
										 |  |  |         "import flask\napp = flask.Flask(__name__)\n" | 
					
						
							| 
									
										
										
										
											2014-09-04 21:43:18 +08:00
										 |  |  |     ) | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     purge_module("site_app") | 
					
						
							| 
									
										
										
										
											2014-09-04 21:43:18 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     from site_app import app | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-03 01:38:27 +08:00
										 |  |  |     assert app.instance_path == os.fspath( | 
					
						
							|  |  |  |         modules_tmp_path / "var" / "site_app-instance" | 
					
						
							|  |  |  |     ) | 
					
						
							| 
									
										
										
										
											2014-09-04 21:43:18 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | def test_installed_package_paths( | 
					
						
							| 
									
										
										
										
											2025-03-30 06:42:58 +08:00
										 |  |  |     modules_tmp_path, modules_tmp_path_prefix, purge_module, monkeypatch | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  | ): | 
					
						
							| 
									
										
										
										
											2023-05-03 01:38:27 +08:00
										 |  |  |     installed_path = modules_tmp_path / "path" | 
					
						
							|  |  |  |     installed_path.mkdir() | 
					
						
							| 
									
										
										
										
											2014-09-04 21:43:18 +08:00
										 |  |  |     monkeypatch.syspath_prepend(installed_path) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-03 01:38:27 +08:00
										 |  |  |     app = installed_path / "installed_package" | 
					
						
							|  |  |  |     app.mkdir() | 
					
						
							|  |  |  |     (app / "__init__.py").write_text("import flask\napp = flask.Flask(__name__)\n") | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     purge_module("installed_package") | 
					
						
							| 
									
										
										
										
											2014-09-04 21:43:18 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     from installed_package import app | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-03 01:38:27 +08:00
										 |  |  |     assert app.instance_path == os.fspath( | 
					
						
							|  |  |  |         modules_tmp_path / "var" / "installed_package-instance" | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     ) | 
					
						
							| 
									
										
										
										
											2014-09-04 21:43:18 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | def test_prefix_package_paths( | 
					
						
							| 
									
										
										
										
											2025-03-30 06:42:58 +08:00
										 |  |  |     modules_tmp_path, modules_tmp_path_prefix, purge_module, site_packages | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  | ): | 
					
						
							| 
									
										
										
										
											2023-05-03 01:38:27 +08:00
										 |  |  |     app = site_packages / "site_package" | 
					
						
							|  |  |  |     app.mkdir() | 
					
						
							|  |  |  |     (app / "__init__.py").write_text("import flask\napp = flask.Flask(__name__)\n") | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     purge_module("site_package") | 
					
						
							| 
									
										
										
										
											2014-09-04 21:43:18 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     import site_package | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-03 01:38:27 +08:00
										 |  |  |     assert site_package.app.instance_path == os.fspath( | 
					
						
							|  |  |  |         modules_tmp_path / "var" / "site_package-instance" | 
					
						
							| 
									
										
										
										
											2019-05-07 03:39:41 +08:00
										 |  |  |     ) |