Skip to content

Commit 1fe6ae4

Browse files
waprinJon Wayne Parrott
authored andcommitted
Fix local unit test sample (#433)
1 parent 4f9b318 commit 1fe6ae4

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

appengine/standard/localtesting/runner.py

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,34 @@
2828
TEST_PATH Path to package containing test modules"""
2929

3030

31+
def append_or_insert_path(path):
32+
"""Adds GAE path to system path, or appends it to the google path
33+
if that already exists.
34+
35+
Not all Google packages are inside namespaced packages, which means
36+
there might be another named `google` on the path, and simply appending
37+
the App Engine SDK to the path will not work since the other package
38+
will get discovered and used first. This ugly hack emulates namespace
39+
packages by first searching if a `google` package already exists by
40+
importing it, and if so appending to its path, otherwise it just
41+
inserts it into the import path by itself.
42+
"""
43+
try:
44+
import google
45+
google.__path__.append("{0}/google".format(path))
46+
except ImportError:
47+
sys.path.insert(0, path)
48+
49+
3150
def main(sdk_path, test_path):
3251
# If the sdk path points to a google cloud sdk installation
3352
# then we should alter it to point to the GAE platform location.
53+
3454
if os.path.exists(os.path.join(sdk_path, 'platform/google_appengine')):
35-
sys.path.insert(0, os.path.join(sdk_path, 'platform/google_appengine'))
55+
append_or_insert_path(
56+
os.path.join(sdk_path, 'platform/google_appengine'))
3657
else:
37-
sys.path.insert(0, sdk_path)
58+
append_or_insert_path(sdk_path)
3859

3960
# Ensure that the google.appengine.* packages are available
4061
# in tests as well as all bundled third-party packages.
@@ -51,7 +72,7 @@ def main(sdk_path, test_path):
5172
print "Note: unable to import appengine_config."
5273

5374
# Discover and run tests.
54-
suite = unittest.loader.TestLoader().discover(test_path)
75+
suite = unittest.loader.TestLoader().discover(test_path, "*_test.py")
5576
unittest.TextTestRunner(verbosity=2).run(suite)
5677

5778

0 commit comments

Comments
 (0)