blob: 75456cd9ad0fbe06317016e18a2fe81dcfeff6ab [file] [log] [blame]
Copybara854996b2021-09-07 19:36:02 +00001# 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
7import os
8import sys
9
10
11def _fix_sys_path_for_appengine(pretest_filename):
12 """Adds the App Engine built-in libraries to sys.path."""
13 # Scan the path to this file to locate the infra repo base directory.
14 infra_base_dir = os.path.abspath(pretest_filename)
15 pos = infra_base_dir.rfind('/infra/appengine')
16 if pos == -1:
17 return
18 infra_base_dir = infra_base_dir[:pos + len('/infra')]
19
20 # Remove the base infra directory from the path, since this isn't available
21 # on appengine.
22 sys.path.remove(infra_base_dir)
23
24 # Add the google_appengine directory.
25 pretest_APPENGINE_ENV_PATH = os.path.join(
26 os.path.dirname(infra_base_dir), 'gcloud', 'platform', 'google_appengine')
27 sys.path.insert(0, pretest_APPENGINE_ENV_PATH)
28
29 # Unfortunate hack, because of appengine.
30 import dev_appserver as pretest_dev_appserver
31 pretest_dev_appserver.fix_sys_path()
32
33 # Remove google_appengine SDK from sys.path after use.
34 sys.path.remove(pretest_APPENGINE_ENV_PATH)
35
36 # This is not added by fix_sys_path.
37 sys.path.append(os.path.join(pretest_APPENGINE_ENV_PATH, 'lib', 'mox'))
38
39
40def _load_appengine_config(pretest_filename):
41 """Runs appengine_config.py to reproduce the App Engine environment."""
42 app_dir = os.path.abspath(os.path.dirname(pretest_filename))
43
44 # Add the application directory to sys.path.
45 inserted = False
46 if app_dir not in sys.path:
47 sys.path.insert(0, app_dir)
48 inserted = True
49
50 # import appengine_config.py, thus executing its contents.
51 import appengine_config # Unused Variable pylint: disable=W0612
52
53 # Clean up.
54 if inserted:
55 sys.path.remove(app_dir)
56
57
58# Using pretest_filename is magic, because it is available in the locals() of
59# the script which execfiles this file.
60_fix_sys_path_for_appengine(pretest_filename)
61
62os.environ['SERVER_SOFTWARE'] = 'test ' + os.environ.get('SERVER_SOFTWARE', '')
63os.environ['CURRENT_VERSION_ID'] = 'test.123'
64os.environ.setdefault('NO_GCE_CHECK', 'True')
65
66# Load appengine_config from the appengine project to ensure that any changes to
67# configuration there are available to the tests (e.g. sys.path modifications,
68# namespaces, etc.). This is according to
69# https://cloud.google.com/appengine/docs/python/tools/localunittesting
70_load_appengine_config(pretest_filename)