petraszd avatar petraszd committed 7e9021e

Add error messages on bad configs.

Comments (0)

Files changed (2)

 import sys
 import os
 import logging
+import pytest
 
 
 def pytest_addoption(parser):
     group = parser.getgroup("gae", "google app engine plugin")
-    group.addoption('--gae-dir', action='store', dest='gae_dir',
-                    metavar='path', default=None,
-                    help="Google App Engine's source dir")
-    group.addoption('--gae-project-dir', action='store', dest='gae_prj_dir',
-                    metavar='path', default=None,
-                    help="Your app's source code's dir")
+    group.addoption('--with-gae', action='store_true', dest='use_gae',
+                    default=False, help='Use pytest_gae plugin')
+    group.addoption('--gae-path', action='store', dest='gae_path',
+                    metavar='PATH', default='./google_appengine/',
+                    help="Google App Engine's source PATH")
+    group.addoption('--gae-project-path', action='store', dest='gae_prj_path',
+                    metavar='PATH', default='./',
+                    help="Your project's source code's PATH")
 
 
 def pytest_configure(config):
-    gae_path = config.option.gae_dir
-    project_path = config.option.gae_prj_dir
-    if not gae_path or not project_path:
+    if not config.option.use_gae:
         return
 
-    sys.path.append(gae_path)
-    sys.path.append(os.path.join(gae_path, 'google'))
-    sys.path.append(os.path.join(gae_path, 'lib/antlr3'))
-    sys.path.append(os.path.join(gae_path, 'lib/django'))
-    sys.path.append(os.path.join(gae_path, 'lib/fancy_urllib'))
-    sys.path.append(os.path.join(gae_path, 'lib/graphy'))
-    sys.path.append(os.path.join(gae_path, 'lib/ipaddr'))
-    sys.path.append(os.path.join(gae_path, 'lib/simplejson'))
-    sys.path.append(os.path.join(gae_path, 'lib/webob'))
-    sys.path.append(os.path.join(gae_path, 'lib/yaml/lib'))
+    _add_gae_to_syspath(config.option.gae_path)
+    _add_project_to_syspath(config.option.gae_prj_path)
 
-    sys.path.append(project_path)
+    _validate_gae_path(config.option.gae_path)
+    _validate_project_path(config.option.gae_prj_path)
+
 
 
 def pytest_runtest_setup(item):
-    gae_path = item.config.option.gae_dir
-    project_path = item.config.option.gae_prj_dir
+    if not item.config.option.use_gae:
+        return
 
-    if not gae_path or not project_path:
-        return
+    gae_path = item.config.option.gae_path
+    project_path = item.config.option.gae_prj_path
 
     from google.appengine.tools import dev_appserver
     from google.appengine.tools.dev_appserver_main import DEFAULT_ARGS
     # closed stream handler to be flushed.
     #
     # Wich of course causes Exception and
-    # some nasty errors at the end of passes tests
+    # some nasty errors at the end of testing.
+    #
+    # This nasty hack prevents that error to be
+    # displayed.
     for h in logging.getLogger().handlers:
         if isinstance(h, logging.StreamHandler):
-            attach_save_flush(h)
+            _attach_save_flush(h)
 
 
-def attach_save_flush(handler):
+def _add_gae_to_path(path):
+    """ Adds Google App Engine and libs that comes with GAE to sys.path
+
+    It is hardcoded and Google may change its internal structure anytime.
+    So, it is not the safetest method to do it
+    """
+
+    sys.path.append(path)
+    sys.path.append(os.path.join(path, 'google'))
+    sys.path.append(os.path.join(path, 'lib/antlr3'))
+    sys.path.append(os.path.join(path, 'lib/django'))
+    sys.path.append(os.path.join(path, 'lib/fancy_urllib'))
+    sys.path.append(os.path.join(path, 'lib/graphy'))
+    sys.path.append(os.path.join(path, 'lib/ipaddr'))
+    sys.path.append(os.path.join(path, 'lib/simplejson'))
+    sys.path.append(os.path.join(path, 'lib/webob'))
+    sys.path.append(os.path.join(path, 'lib/yaml/lib'))
+
+
+def _add_project_to_syspath(path):
+    sys.path.append(path)
+
+
+def _validate_gae_path(path):
+    try:
+        import google.appengine
+    except ImportError:
+        raise pytest.UsageError("google.appengine lib can not be imported. "
+                                "Try to use --gae-path option. "
+                                "Current path: <%s> " % path)
+
+def _validate_project_path(path):
+    # Google App Engine projects must contain app.yaml at their roots.
+    # So, this code just checks if app.yaml exists
+    if not os.path.exists(os.path.join(path, 'app.yaml')):
+        raise pytest.UsageError("Your Google App Engine's project can not "
+                                "be found. Try to use --gae-project-path "
+                                "option. Current path: <%s>" % path)
+
+
+def _attach_save_flush(handler):
     def save_flush():
         if not handler.stream.closed:
             handler.stream.flush()
 from setuptools import setup
 
 
-with open('README.rst', 'r') as f:
-    long_desc = f.read()
+fhandler = open('README.rst', 'r')
+long_desc = fhandler.read()
+fhandler.close()
 
 setup(name='pytest_gae',
       version='pre-release',
-      description="py.test plugin for google app engine",
+      description="pytest plugin for Google App Engine",
       long_description=long_desc,
       author='Petras Zdanavicius (petraszd)',
       author_email='petras@gmail.com',
-      #url='',
+      url='http://bitbucket.org/petraszd/pytest_gae/',
       py_modules=['pytest_gae'],
       install_requires=['pytest'],
       entry_points={'pytest11': ['pytest_gae = pytest_gae']},
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.