Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 1 | # Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
| 5 | # pylint: disable=undefined-variable |
| 6 | |
| 7 | import os |
| 8 | import sys |
Adrià Vilanova MartÃnez | ac4a644 | 2022-05-15 19:05:13 +0200 | [diff] [blame] | 9 | lib_path = os.path.join( |
| 10 | os.path.dirname(os.path.realpath(pretest_filename)), 'lib') |
| 11 | sys.path.insert(0, lib_path) |
| 12 | import google |
| 13 | google.__path__.insert(0, os.path.join(lib_path, 'google')) |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 14 | |
| 15 | |
| 16 | def _fix_sys_path_for_appengine(pretest_filename): |
| 17 | """Adds the App Engine built-in libraries to sys.path.""" |
| 18 | # Scan the path to this file to locate the infra repo base directory. |
| 19 | infra_base_dir = os.path.abspath(pretest_filename) |
| 20 | pos = infra_base_dir.rfind('/infra/appengine') |
| 21 | if pos == -1: |
| 22 | return |
| 23 | infra_base_dir = infra_base_dir[:pos + len('/infra')] |
| 24 | |
| 25 | # Remove the base infra directory from the path, since this isn't available |
| 26 | # on appengine. |
| 27 | sys.path.remove(infra_base_dir) |
| 28 | |
| 29 | # Add the google_appengine directory. |
| 30 | pretest_APPENGINE_ENV_PATH = os.path.join( |
| 31 | os.path.dirname(infra_base_dir), 'gcloud', 'platform', 'google_appengine') |
| 32 | sys.path.insert(0, pretest_APPENGINE_ENV_PATH) |
| 33 | |
| 34 | # Unfortunate hack, because of appengine. |
| 35 | import dev_appserver as pretest_dev_appserver |
| 36 | pretest_dev_appserver.fix_sys_path() |
| 37 | |
| 38 | # Remove google_appengine SDK from sys.path after use. |
| 39 | sys.path.remove(pretest_APPENGINE_ENV_PATH) |
| 40 | |
| 41 | # This is not added by fix_sys_path. |
| 42 | sys.path.append(os.path.join(pretest_APPENGINE_ENV_PATH, 'lib', 'mox')) |
| 43 | |
| 44 | |
| 45 | def _load_appengine_config(pretest_filename): |
| 46 | """Runs appengine_config.py to reproduce the App Engine environment.""" |
| 47 | app_dir = os.path.abspath(os.path.dirname(pretest_filename)) |
| 48 | |
| 49 | # Add the application directory to sys.path. |
| 50 | inserted = False |
| 51 | if app_dir not in sys.path: |
| 52 | sys.path.insert(0, app_dir) |
| 53 | inserted = True |
| 54 | |
| 55 | # import appengine_config.py, thus executing its contents. |
| 56 | import appengine_config # Unused Variable pylint: disable=W0612 |
| 57 | |
| 58 | # Clean up. |
| 59 | if inserted: |
| 60 | sys.path.remove(app_dir) |
| 61 | |
| 62 | |
| 63 | # Using pretest_filename is magic, because it is available in the locals() of |
| 64 | # the script which execfiles this file. |
| 65 | _fix_sys_path_for_appengine(pretest_filename) |
| 66 | |
| 67 | os.environ['SERVER_SOFTWARE'] = 'test ' + os.environ.get('SERVER_SOFTWARE', '') |
| 68 | os.environ['CURRENT_VERSION_ID'] = 'test.123' |
| 69 | os.environ.setdefault('NO_GCE_CHECK', 'True') |
| 70 | |
| 71 | # Load appengine_config from the appengine project to ensure that any changes to |
| 72 | # configuration there are available to the tests (e.g. sys.path modifications, |
| 73 | # namespaces, etc.). This is according to |
| 74 | # https://cloud.google.com/appengine/docs/python/tools/localunittesting |
| 75 | _load_appengine_config(pretest_filename) |