test_fixtures: Add run_generate_fixtures_if_required function.

In this commit we are adding run_generate_fixtures_if_required,
a new function which is meant to de-duplicate a bit of code
between test-server and test-backend which is essentially
responsible for rebuilding the test database if that was required.
This commit is contained in:
Aditya Bansal 2018-06-06 04:19:48 +05:30 committed by Tim Abbott
parent f7c11d1747
commit 65dc80fe9d
3 changed files with 12 additions and 12 deletions

View File

@ -19,7 +19,7 @@ TOOLS_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
if TOOLS_DIR not in sys.path: if TOOLS_DIR not in sys.path:
sys.path.insert(0, os.path.dirname(TOOLS_DIR)) sys.path.insert(0, os.path.dirname(TOOLS_DIR))
from zerver.lib.test_fixtures import template_database_status from zerver.lib.test_fixtures import run_generate_fixtures_if_required
def set_up_django(external_host): def set_up_django(external_host):
# type: (str) -> None # type: (str) -> None
@ -62,11 +62,7 @@ def test_server_running(force: bool=False, external_host: str='testserver',
set_up_django(external_host) set_up_django(external_host)
if use_db: if use_db:
generate_fixtures_command = ['tools/setup/generate-fixtures'] run_generate_fixtures_if_required()
test_template_db_status = template_database_status()
if test_template_db_status == 'needs_rebuild':
generate_fixtures_command.append('--force')
subprocess.check_call(generate_fixtures_command)
# Run this not through the shell, so that we have the actual PID. # Run this not through the shell, so that we have the actual PID.
run_dev_server_command = ['tools/run-dev.py', '--test'] run_dev_server_command = ['tools/run-dev.py', '--test']

View File

@ -171,7 +171,7 @@ if __name__ == "__main__":
os.environ["http_proxy"] = "" os.environ["http_proxy"] = ""
os.environ["https_proxy"] = "" os.environ["https_proxy"] = ""
from zerver.lib.test_fixtures import template_database_status from zerver.lib.test_fixtures import run_generate_fixtures_if_required
from tools.lib.test_script import ( from tools.lib.test_script import (
get_provisioning_status, get_provisioning_status,
@ -344,11 +344,7 @@ if __name__ == "__main__":
# files, since part of setup is importing the models for all applications in INSTALLED_APPS. # files, since part of setup is importing the models for all applications in INSTALLED_APPS.
django.setup() django.setup()
test_template_db_status = template_database_status() run_generate_fixtures_if_required(options.generate_fixtures)
if options.generate_fixtures or test_template_db_status == 'needs_rebuild':
generate_fixtures_command = [os.path.join(TOOLS_DIR, 'setup', 'generate-fixtures')]
generate_fixtures_command.append('--force')
subprocess.call(generate_fixtures_command)
subprocess.check_call(['tools/webpack', '--test']) subprocess.check_call(['tools/webpack', '--test'])

View File

@ -3,6 +3,7 @@ import json
import os import os
import re import re
import hashlib import hashlib
import subprocess
import sys import sys
from typing import Any, List, Optional from typing import Any, List, Optional
from importlib import import_module from importlib import import_module
@ -21,6 +22,13 @@ from scripts.lib.zulip_tools import get_dev_uuid_var_path
UUID_VAR_DIR = get_dev_uuid_var_path() UUID_VAR_DIR = get_dev_uuid_var_path()
FILENAME_SPLITTER = re.compile('[\W\-_]') FILENAME_SPLITTER = re.compile('[\W\-_]')
def run_generate_fixtures_if_required(use_force: bool=False) -> None:
generate_fixtures_command = ['tools/setup/generate-fixtures']
test_template_db_status = template_database_status()
if use_force or test_template_db_status == 'needs_rebuild':
generate_fixtures_command.append('--force')
subprocess.check_call(generate_fixtures_command)
def database_exists(database_name: str, **options: Any) -> bool: def database_exists(database_name: str, **options: Any) -> bool:
db = options.get('database', DEFAULT_DB_ALIAS) db = options.get('database', DEFAULT_DB_ALIAS)
try: try: