├── AUTHORS ├── tests └── __init__.py ├── license.txt ├── setup.py ├── README.rst └── bazinga └── __init__.py /AUTHORS: -------------------------------------------------------------------------------- 1 | Juarez Bochi 2 | Christoph Rauch 3 | -------------------------------------------------------------------------------- /tests/__init__.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | from bazinga import Bazinga 3 | from nose.plugins import Plugin, PluginTester 4 | 5 | class TestBazingaPlugin(PluginTester, unittest.TestCase): 6 | activate = '--with-bazinga' 7 | plugins = [Bazinga()] 8 | 9 | def makeSuite(self): 10 | class TC(unittest.TestCase): 11 | def runTest(self): 12 | raise ValueError("I thought the 'bazinga' was implied.") 13 | return unittest.TestSuite([TC()]) 14 | 15 | def test_error_on_output(self): 16 | assert "ValueError" in self.output 17 | -------------------------------------------------------------------------------- /license.txt: -------------------------------------------------------------------------------- 1 | Copyright (C) 2011 by Juarez Bochi 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in 11 | all copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | THE SOFTWARE. 20 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | import codecs 3 | import os 4 | 5 | try: 6 | from setuptools import setup, find_packages 7 | except ImportError: 8 | raise 9 | from ez_setup import use_setuptools 10 | use_setuptools() 11 | from setuptools import setup, find_packages 12 | 13 | url = "https://github.com/jbochi/bazinga" 14 | if os.path.exists("README.rst"): 15 | long_description = codecs.open("README.rst", "r", "utf-8").read() 16 | else: 17 | long_description = "See %s" % (url,) 18 | 19 | setup( 20 | name="Bazinga", 21 | version="0.2.4", 22 | description="Bazinga is a nose plugin to run tests only if their dependencies were modified", 23 | author="Juarez Bochi", 24 | author_email="jbochi@gmail.com", 25 | url=url, 26 | platforms=["any"], 27 | license="MIT", 28 | packages=['bazinga'], 29 | install_requires=["nose", "snakefood"], 30 | entry_points = { 31 | 'nose.plugins.0.10': [ 32 | 'bazinga = bazinga:Bazinga' 33 | ] 34 | }, 35 | classifiers=[ 36 | "Development Status :: 4 - Beta", 37 | "Operating System :: OS Independent", 38 | "Environment :: No Input/Output (Daemon)", 39 | "Intended Audience :: Developers", 40 | "License :: OSI Approved :: MIT License", 41 | "Natural Language :: English", 42 | "Programming Language :: Python :: 2.5", 43 | "Programming Language :: Python :: 2.6", 44 | "Programming Language :: Python :: 2.7", 45 | "Topic :: Internet", 46 | ], 47 | long_description=long_description 48 | ) 49 | -------------------------------------------------------------------------------- /README.rst: -------------------------------------------------------------------------------- 1 | ======= 2 | Bazinga 3 | ======= 4 | 5 | Bazinga is a nose plugin that does dependency analysis to run incremental tests. 6 | 7 | Motivation 8 | ========== 9 | 10 | Running the complete test suite on large projects can take a significantly large time. This can affect your work flow, specially if you are doing TDD. Some people choose to run only a subset of the tests, specifying them explicitly on the command line, but you can easily forget to run affected tests after any particular change and things can break unnoticed. Using "bazinga" you can rest assured that all (and only) the affected tests will be run. It was inspired by the continuous integration system that Google built: http://googletesting.blogspot.com/2011/06/testing-at-speed-and-scale-of-google.html. 11 | 12 | How it works 13 | ============ 14 | 15 | Statically inspecting what is imported by each module, "bazinga" recursively detects what are the dependencies for each test. Only tests that failed, were modified, or had a file that they depend on changed, are run. Each time nose is run with bazinga, a md5 hash for each module that your project depends on will be stored on a file named `.nosebazinga` on the working directory. This file also contains a dependency graph that is used as cache. Bazinga will run all the tests that are needed if a third party package is updated, but the standard library is considered "stable" and is not checked for modifications. 16 | 17 | Installation 18 | ============ 19 | 20 | :: 21 | 22 | pip install Bazinga 23 | 24 | 25 | Usage 26 | ===== 27 | 28 | :: 29 | 30 | nosetests --with-bazinga 31 | 32 | 33 | Debugging 34 | ========= 35 | 36 | :: 37 | 38 | nosetests --with-bazinga --debug=bazinga 39 | 40 | 41 | Requirements 42 | ============ 43 | 44 | * Nose 45 | * Snakefood 46 | 47 | LICENSE 48 | ======= 49 | 50 | * MIT License -------------------------------------------------------------------------------- /bazinga/__init__.py: -------------------------------------------------------------------------------- 1 | import hashlib 2 | import inspect 3 | import imp 4 | import logging 5 | from nose.plugins import Plugin 6 | import os 7 | from snakefood.find import find_dependencies 8 | import sys 9 | 10 | try: 11 | from cpickle import dump, load 12 | except ImportError: 13 | from pickle import dump, load 14 | 15 | log = logging.getLogger(__name__) 16 | 17 | def file_hash(path): 18 | f = open(path, 'rb') 19 | h = hashlib.md5(f.read()).hexdigest() 20 | f.close() 21 | return h 22 | 23 | class Bazinga(Plugin): 24 | name = 'bazinga' 25 | hash_file = '.nosebazinga' 26 | _graph = {} 27 | _hashes = {} 28 | _known_graph = {} 29 | _known_hashes = {} 30 | _failed_test_modules = set() 31 | _file_status = {} 32 | _ignored_files = set() 33 | 34 | def configure(self, options, conf): 35 | self.hash_file = os.path.join(conf.workingDir, self.hash_file) 36 | if os.path.isfile(self.hash_file): 37 | log.debug("Loading last known hashes and dependency graph") 38 | f = open(self.hash_file, 'r') 39 | data = load(f) 40 | f.close() 41 | self._known_hashes = data['hashes'] 42 | self._known_graph = data['graph'] 43 | Plugin.configure(self, options, conf) 44 | 45 | def afterTest(self, test): 46 | # None means test never ran, False means failed/err 47 | if test.passed is False: 48 | filename = test.address()[0] 49 | self._failed_test_modules.add(filename) 50 | 51 | def inspectDependencies(self, path): 52 | try: 53 | files, _ = find_dependencies(path, verbose=False, process_pragmas=False) 54 | log.debug('Dependencies found for file %s: %s' % (path, files)) 55 | except TypeError, err: 56 | if path not in self._ignored_files: 57 | self._ignored_files.add(path) 58 | log.debug('Snakefood raised an error (%s) parsing path %s' % (err, path)) 59 | return [] 60 | 61 | valid_files = [] 62 | for f in files: 63 | if not os.path.isfile(f) and f not in self._ignored_files: 64 | self._ignored_files.add(f) 65 | log.debug('Snakefood returned a wrong path: %s' % (f,)) 66 | elif (f in self._ignored_files): 67 | self._ignored_files.add(f) 68 | log.debug('Ignoring built-in module: %s' % (f,)) 69 | else: 70 | valid_files.append(f) 71 | 72 | return valid_files 73 | 74 | def updateGraph(self, path): 75 | log.debug(path) 76 | if path not in self._graph: 77 | if not self.fileChanged(path) and path in self._known_graph: 78 | files = self._known_graph[path] 79 | else: 80 | files = self.inspectDependencies(path) 81 | self._graph[path] = files 82 | for f in files: 83 | self.updateGraph(f) 84 | 85 | def fileChanged(self, path): 86 | if path in self._hashes: 87 | hash = self._hashes[path] 88 | else: 89 | hash = file_hash(path) 90 | self._hashes[path] = hash 91 | 92 | return path not in self._known_hashes or hash != self._known_hashes[path] 93 | 94 | def dependenciesChanged(self, path, parents=None): 95 | parents = parents or [] 96 | 97 | if path in self._file_status: 98 | return self._file_status[path] 99 | elif self.fileChanged(path): 100 | log.debug('File has been modified or failed: %s' % (path,)) 101 | changed = True 102 | else: 103 | childs = self._graph[path] 104 | new_parents = parents + [path, ] 105 | changed = any(self.dependenciesChanged(f, new_parents) for f in childs if 106 | f not in new_parents) 107 | 108 | if changed: 109 | log.debug('File depends on modified file: %s' % (path,)) 110 | 111 | self._file_status[path] = changed 112 | return changed 113 | 114 | def finalize(self, result): 115 | for k, v in self._known_hashes.iteritems(): 116 | self._hashes.setdefault(k, v) 117 | 118 | for k, v in self._known_graph.iteritems(): 119 | self._graph.setdefault(k, v) 120 | 121 | for m in self._failed_test_modules: 122 | log.debug('Module failed: %s' % (m,)) 123 | self._hashes.pop(m, None) 124 | 125 | f = open(self.hash_file, 'w') 126 | dump({'hashes': self._hashes, 'graph': self._graph}, f) 127 | f.close() 128 | 129 | def wantModule(self, m): 130 | source = inspect.getsourcefile(m) 131 | self.updateGraph(source) 132 | if not self.dependenciesChanged(source): 133 | log.debug('Ignoring module %s, since no dependencies have changed' % (source,)) 134 | return False 135 | 136 | def wantClass(self, cls): 137 | source = inspect.getsourcefile(cls) 138 | self.updateGraph(source) 139 | if not self.dependenciesChanged(source): 140 | log.debug('Ignoring class %s, since no dependencies have changed' % (source,)) 141 | return False --------------------------------------------------------------------------------