|
| 1 | +import subprocess |
| 2 | +import os |
| 3 | +import pathlib |
| 4 | +import re |
| 5 | +import sys |
| 6 | + |
| 7 | + |
| 8 | +def find_settings_module(path_to_manage_py): |
| 9 | + dj_settings_module = None |
| 10 | + with open(path_to_manage_py, "r") as manage_py: |
| 11 | + pattern = r"^os\.environ\.setdefault\((['\"])(DJANGO_SETTINGS_MODULE)\1, (['\"])(?P<settings_path>[\w.]+)\3\)$" |
| 12 | + for line in manage_py.readlines(): |
| 13 | + match_result = re.match(pattern, line.strip()) |
| 14 | + if match_result is not None: |
| 15 | + dj_settings_module = match_result.groupdict().get("settings_path", None) |
| 16 | + break |
| 17 | + return dj_settings_module |
| 18 | + |
| 19 | + |
| 20 | +def configure_test_runner(path_to_manage_py): |
| 21 | + # Getting the DJANGO_SETTINGS_MODULE from manage.py |
| 22 | + dj_settings_module = find_settings_module(path_to_manage_py) |
| 23 | + if dj_settings_module is None: |
| 24 | + raise Exception("DJANGO_SETTINGS_MODULE not found in manage.py") |
| 25 | + |
| 26 | + # Construct the path to the settings.py file |
| 27 | + settings_file = os.path.join( |
| 28 | + os.path.dirname(dj_settings_module.replace(".", os.sep)), "settings.py" |
| 29 | + ) |
| 30 | + # Check if the settings.py file exists |
| 31 | + if not os.path.exists(settings_file): |
| 32 | + raise Exception(f"settings.py file not found at {settings_file}") |
| 33 | + # Read the content of the existing settings.py file |
| 34 | + with open(settings_file, "r") as f: |
| 35 | + original_settings_content = f.read() |
| 36 | + |
| 37 | + # Check if TEST_RUNNER is already defined in the settings |
| 38 | + if "TEST_RUNNER" in original_settings_content: |
| 39 | + print("TEST_RUNNER is already defined in settings.py. but continuing") |
| 40 | + print("settings_content: ", original_settings_content) |
| 41 | + else: |
| 42 | + # Add the custom test runner to the settings.py file |
| 43 | + |
| 44 | + # Get path to the custom_test_runner.py parent folder, add to sys.path |
| 45 | + custom_test_runner_dir = pathlib.Path(__file__).parent |
| 46 | + sys.path.insert(0, custom_test_runner_dir) |
| 47 | + |
| 48 | + # Import your custom test runner class |
| 49 | + # from execution import UnittestTestResult |
| 50 | + |
| 51 | + # Set the TEST_RUNNER setting |
| 52 | + setting_content = original_settings_content + ( |
| 53 | + "\n\n" |
| 54 | + + "# Use custom test runner\n" |
| 55 | + + "import sys\n" |
| 56 | + + f"sys.path.insert(0, '{custom_test_runner_dir}')\n" |
| 57 | + + f"TEST_RUNNER = 'django_test_runner.CustomTestRunner'\n" |
| 58 | + ) |
| 59 | + |
| 60 | + # Write the updated content back to the settings.py file |
| 61 | + with open(settings_file, "w") as f: |
| 62 | + f.write(setting_content) |
| 63 | + |
| 64 | + print("TEST_RUNNER setting added to settings.py.") |
| 65 | + return settings_file, original_settings_content |
| 66 | + |
| 67 | + |
| 68 | +# Define a cleanup method |
| 69 | +def cleanup(settings_file, original_settings_content): |
| 70 | + # Restore the original content of settings.py |
| 71 | + with open(settings_file, "w") as f: |
| 72 | + f.write(original_settings_content) |
| 73 | + print("Settings.py has been restored to its original state.") |
| 74 | + |
| 75 | + return True |
| 76 | + |
| 77 | + |
| 78 | +def runner(): |
| 79 | + # Define the path to your manage.py file |
| 80 | + # could get path to manage.py from environment variable |
| 81 | + # get Django test boolean |
| 82 | + django_test_enabled = os.environ.get("DJANGO_TEST_ENABLED") |
| 83 | + manage_py_path = os.environ.get("MANAGE_PY_PATH") |
| 84 | + |
| 85 | + if ( |
| 86 | + django_test_enabled is not None |
| 87 | + and django_test_enabled.lower() == "true" |
| 88 | + and manage_py_path is not None |
| 89 | + ): |
| 90 | + # attempt to configure and run tests as django tests |
| 91 | + try: |
| 92 | + settings_file, original_settings_content = configure_test_runner( |
| 93 | + manage_py_path |
| 94 | + ) |
| 95 | + # Command to run 'python manage.py test' |
| 96 | + python_executable = sys.executable |
| 97 | + command = [python_executable, "manage.py", "test"] |
| 98 | + print("running test command: ", command) |
| 99 | + # Run the command |
| 100 | + try: |
| 101 | + subprocess.run(" ".join(command), shell=True, check=True) |
| 102 | + # Cleanup |
| 103 | + cleanup(settings_file, original_settings_content) |
| 104 | + except subprocess.CalledProcessError as e: |
| 105 | + print(f"Error running 'manage.py test': {e}") |
| 106 | + except Exception as e: |
| 107 | + print(f"Error configuring Django test runner: {e}") |
0 commit comments