blob: 29dea43096be5cbb0138f60224cb94b995a49ef2 [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
Adrià Vilanova Martínezac4a6442022-05-15 19:05:13 +02009lib_path = os.path.join(
10 os.path.dirname(os.path.realpath(pretest_filename)), 'lib')
11sys.path.insert(0, lib_path)
12import google
13google.__path__.insert(0, os.path.join(lib_path, 'google'))
Copybara854996b2021-09-07 19:36:02 +000014
15
16def _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
45def _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
67os.environ['SERVER_SOFTWARE'] = 'test ' + os.environ.get('SERVER_SOFTWARE', '')
68os.environ['CURRENT_VERSION_ID'] = 'test.123'
69os.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)