Debian Codegraph @

setuptools_behave.py

raw
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 """
4 Setuptools command for behave.
5
6 .. code-block:: console
7
8 python setup.py behave_test
9 python setup.py behave_test --format=progress3
10 python setup.py behave_test --args=features/one.feature
11 python setup.py behave_test --tags=-xfail --args=features
12
13 .. seealso::
14
15 * http://pypi.python.org/pypi/behave
16 * http://github.com/behave/behave
17 """
18
19 from setuptools import Command
20 from distutils import dir_util
21 from fnmatch import fnmatch
22 import os.path
23 import sys
24 import shlex
25 import subprocess
26
27
28 class behave_test(Command):
29 """
30 Simple test runner that runs 'behave' via a "setup.py" file.
31 This ensures that all requirements are provided before the tests are run.
32 """
33 command_name = "behave_test"
34 description = "Run feature tests with behave"
35 default_format = "progress"
36 default_args = "features"
37 local_egg_dir = ".eggs"
38 command_consumes_arguments = False
39 user_options = [
40 ("format=", "f", "Use formatter (default: %s)" % default_format),
41 ("tags=", "t", "Use tags to select/exclude features/scenarios"),
42 ("dry-run", "d", "Use dry-run mode"),
43 ("args=", None,
44 "Features to run (default: %s)" % default_args),
45 ]
46 boolean_options = [ "dry-run" ]
47
48 def initialize_options(self):
49 self.format = self.default_format
50 self.tags = None
51 self.dry_run = None
52 self.args = self.default_args
53
54 def finalize_options(self):
55 self.ensure_string("format", self.default_format)
56 self.ensure_string_list_with_comma_words("tags")
57 self.ensure_string_list("args")
58
59 def ensure_string_list_with_comma_words(self, option):
60 """Ensures that a string with whitespace separated words
61 is converted into list of strings.
62 Note that commas are allowed in words
63 (compared :meth:`ensure_string_list()`).
64 """
65 value = getattr(self, option, None)
66 if not value:
67 return
68 parts = shlex.split(value)
69 setattr(self, option, parts)
70
71 def _ensure_required_packages_are_installed(self, install_dir="."):
72 # -- NOTE: Packages are downloaded and provided as local eggs.
73 self.announce("ensure_required_packages_are_installed")
74 initial_dir = os.getcwd()
75 try:
76 dir_util.mkpath(install_dir)
77 # -- NO LONGER NEEDED: os.chdir(self.local_egg_dir)
78 if self.distribution.install_requires:
79 self.distribution.fetch_build_eggs(self.distribution.install_requires)
80 if self.distribution.tests_require:
81 self.distribution.fetch_build_eggs(self.distribution.tests_require)
82 finally:
83 os.chdir(initial_dir)
84
85 def _select_paths(self, path=".", pattern="*"):
86 selected = [ os.path.join(path, f)
87 for f in os.listdir(path) if fnmatch(f, pattern)]
88 return selected
89
90 def _setup_env_with_local_python_path(self, egg_install_dir):
91 PYTHONPATH = os.environ.get("PYTHONPATH", "")
92 pathsep = os.pathsep
93 PPATH=[x for x in PYTHONPATH.split(pathsep) if x]
94 PPATH.insert(0, os.getcwd())
95 local_eggs = self._select_paths(egg_install_dir, "*.egg")
96 if local_eggs:
97 PPATH[1:1] = [ os.path.abspath(p) for p in local_eggs]
98 os.environ["PYTHONPATH"] = pathsep.join(PPATH)
99 self.announce("Use PYTHONPATH=%s" % os.environ["PYTHONPATH"], level=3)
100 return PYTHONPATH
101
102 def run(self):
103 # -- UPDATE SEARCHPATH: Ensure that local dir and local eggs are used.
104 egg_install_dir = self.local_egg_dir
105 self._ensure_required_packages_are_installed(egg_install_dir)
106 OLD_PYTHONPATH = self._setup_env_with_local_python_path(egg_install_dir)
107 for path in self.args:
108 returncode = self.behave(path)
109 if returncode:
110 self.announce("FAILED", level=4)
111 raise SystemExit(returncode)
112 # -- FINALLY: Restore
113 os.environ["PYTHONPATH"] = OLD_PYTHONPATH
114 return returncode
115
116 def behave(self, path):
117 behave = os.path.join("bin", "behave")
118 if not os.path.exists(behave):
119 # -- ALTERNATIVE: USE: behave script: behave = "behave"
120 # -- USE: behave module (main)
121 behave = "-m behave"
122 cmd_options = ""
123 if self.tags:
124 cmd_options = "--tags=" + " --tags=".join(self.tags)
125 if self.dry_run:
126 cmd_options += " --dry-run"
127 cmd_options += " --format=%s %s" % (self.format, path)
128 self.announce("CMDLINE: python %s %s" % (behave, cmd_options), level=3)
129 behave_cmd = shlex.split(behave)
130 return subprocess.call([sys.executable] + behave_cmd + shlex.split(cmd_options))