Permalink
Browse files

Initial commit

  • Loading branch information...
0 parents commit 68af892722f8db6128094f700ba83108bdadd9f8 @joymax committed Dec 30, 2011
@@ -0,0 +1 @@
+*.pyc
32 LICENSE
@@ -0,0 +1,32 @@
+Copyright (c) 2011 by oDesk and Maksym Klymyshyn
+
+Some rights reserved.
+
+Redistribution and use in source and binary forms of the software as well
+as documentation, with or without modification, are permitted provided
+that the following conditions are met:
+
+* Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+
+* Redistributions in binary form must reproduce the above
+ copyright notice, this list of conditions and the following
+ disclaimer in the documentation and/or other materials provided
+ with the distribution.
+
+* The names of the contributors may not be used to endorse or
+ promote products derived from this software without specific
+ prior written permission.
+
+THIS SOFTWARE AND DOCUMENTATION IS PROVIDED BY THE COPYRIGHT HOLDERS AND
+CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
+NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
+OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+SOFTWARE AND DOCUMENTATION, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
+DAMAGE.
6 README
@@ -0,0 +1,6 @@
+Flask-Jasmine
+-------------
+
+This is extension to execute BDD tests with Jasmine BDD Framework.
+Also extension support for Bundles created by ``Flask-Assets``.
+
@@ -0,0 +1,71 @@
+# Flask-Jasmine
+
+Extension to execute **[Jasmine](http://pivotal.github.com/jasmine/)** JavaScript Tests. At the moment it's a bit annoying to integrate Jasmine within well organized JS sources using **[Flask-Assets](http://flask-assets.readthedocs.org/en/latest/index.html)**
+
+**Flask-Jasmine** work only in **debug** mode of the App.
+
+## Installation
+
+Install the extension with one of the following commands:
+
+ easy_install Flask-Assets
+
+or alternatively if you have pip installed:
+
+ pip install Flask-Assets
+
+
+## Usage
+
+You initialize app by creating Jasmine instance and set specs and sources of your JavaScript:
+
+ from flask import Flask
+ from flask.ext.jasmine import Jasmine
+
+ app = Flask('sample_app')
+
+ jasmine = Jasmine(app)
+
+ jasmine.specs(
+ 'src/specs/spec1.js',
+ 'src/specs/spec2.js',
+ )
+
+ jasmine.sources(
+ 'src/js/file1.js'
+ 'src/js/file2.js'
+ )
+
+### With Flask-Assets
+
+To using **Flask-Jasmine** with **Flask-Assets** you need to create instance of `Asset`
+with name of appropriate `Bundle`. At the moment unnamed bundles are not supported.
+
+ from flask import Flask
+ from flask.ext.jasmine import Jasmine, Asset
+ from flaskext.assets import Environment, Bundle
+
+ app = Flask('sample_app')
+
+ assets = Environment(app)
+ bundle1 = Bundle(
+ 'src/libs/jquery.cookie.js',
+ 'src/libs/jquery.tmpl.js',
+ output='utils.js',
+ filters='yui_js'
+ )
+ assets.register('utils', bundle1)
+
+ jasmine = Jasmine(app)
+ jasmine.specs(
+ 'src/specs/spec1.js',
+ 'src/specs/spec2.js',
+ )
+ jasmine.sources(
+ Asset('utils')
+ )
+
+
+## Start tests
+
+To start tests go to `http://127.0.0.1:5000/jasmine/testrunner/`
@@ -0,0 +1 @@
+from jasmine import Jasmine, Asset
@@ -0,0 +1,106 @@
+from __future__ import absolute_import
+
+import os
+
+from jinja2 import Environment, PackageLoader
+from flask import Blueprint, send_from_directory
+
+
+__all__ = ('Jasmine', 'Asset')
+
+module = Blueprint('jasmine', __name__)
+
+
+class Asset(object):
+ def __init__(self, name):
+ self.name = name
+
+ def build(self, app):
+ try:
+ bundles = app.jinja_env.assets_environment._named_bundles
+ except AttributeError:
+ raise ImportError(u"Looks like Flask-Assets not initialized")
+
+ try:
+ contents = bundles.get(self.name).contents
+ except AttributeError:
+ raise ValueError(
+ u"Can't find `%s` Flask-Assets bundle" % self.name)
+
+ return contents
+
+
+class Jasmine(object):
+ _static_dir = os.path.realpath(
+ os.path.join(os.path.dirname(__file__), 'static'))
+ _media_url = '/_jasmine/testrunner/media/'
+
+ def __init__(self, app):
+ self.app = app
+
+ self.app.config.setdefault('JASMINE_SPECS', [])
+ self.app.config.setdefault('JASMINE_SOURCES', [])
+
+ if not app.debug:
+ return
+
+ self.jinja_env = Environment(
+ autoescape=True,
+ extensions=['jinja2.ext.i18n'],
+ loader=PackageLoader(__name__, 'templates'))
+
+ app.add_url_rule('%s<path:filename>' % self._media_url,
+ '_jasmine.static', self.send_static_file)
+
+ app.add_url_rule('/jasmine/testrunner/',
+ '_jasmine.runner', self.runner_view)
+
+ app.register_blueprint(module,
+ url_prefix=self._media_url)
+
+ def specs(self, *args):
+ self.app.config['JASMINE_SPECS'].extend(list(args))
+
+ def sources(self, *args):
+ self.app.config['JASMINE_SOURCES'].extend(list(args))
+
+ def build_sources(self, data):
+ """
+ Build list of sources from plain configs or
+ build assets
+ """
+
+ lst = []
+
+ for item in data:
+ if isinstance(item, (str, unicode)):
+ lst.append("%s/%s" % (self.app.static_url_path, item))
+ continue
+
+ if isinstance(item, Asset):
+ contents = item.build(self.app)
+ for asset_item in contents:
+ lst.append(
+ "%s/%s" % (self.app.static_url_path, asset_item))
+
+ return lst
+
+ def runner_view(self):
+ """
+ Render runner view
+ """
+
+ template_name = "runner.html"
+ template = self.jinja_env.get_template(template_name)
+
+ return template.render({
+ 'specs': self.build_sources(self.app.config['JASMINE_SPECS']),
+ 'sources': self.build_sources(self.app.config['JASMINE_SOURCES']),
+ 'media_url': self._media_url
+ })
+
+ def send_static_file(self, filename):
+ """
+ Send a static file from the flask-jasmine static directory
+ """
+ return send_from_directory(self._static_dir, filename)
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@@ -0,0 +1,20 @@
+Copyright (c) 2008-2011 Pivotal Labs
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Oops, something went wrong.

0 comments on commit 68af892

Please sign in to comment.