| 1 |
import os.path |
| 2 |
import sys |
| 3 |
import tempfile |
| 4 |
import six |
| 5 |
from nose.tools import * |
| 6 |
from behave import configuration |
| 7 |
from behave.configuration import Configuration, UserData |
| 8 |
from unittest import TestCase |
| 9 |
|
| 10 |
|
| 11 |
# one entry of each kind handled |
| 12 |
TEST_CONFIG="""[behave] |
| 13 |
outfiles= /absolute/path1 |
| 14 |
relative/path2 |
| 15 |
paths = /absolute/path3 |
| 16 |
relative/path4 |
| 17 |
tags = @foo,~@bar |
| 18 |
@zap |
| 19 |
format=pretty |
| 20 |
tag-counter |
| 21 |
stdout_capture=no |
| 22 |
bogus=spam |
| 23 |
|
| 24 |
[behave.userdata] |
| 25 |
foo = bar |
| 26 |
answer = 42 |
| 27 |
""" |
| 28 |
|
| 29 |
|
| 30 |
ROOTDIR_PREFIX = "" |
| 31 |
if sys.platform.startswith("win"): |
| 32 |
# -- OR: ROOTDIR_PREFIX = os.path.splitdrive(sys.executable) |
| 33 |
# NOTE: python2 requires lower-case drive letter. |
| 34 |
ROOTDIR_PREFIX_DEFAULT = "C:" |
| 35 |
if six.PY2: |
| 36 |
ROOTDIR_PREFIX_DEFAULT = ROOTDIR_PREFIX_DEFAULT.lower() |
| 37 |
ROOTDIR_PREFIX = os.environ.get("BEHAVE_ROOTDIR_PREFIX", ROOTDIR_PREFIX_DEFAULT) |
| 38 |
|
| 39 |
class TestConfiguration(object): |
| 40 |
|
| 41 |
def test_read_file(self): |
| 42 |
tn = tempfile.mktemp() |
| 43 |
tndir = os.path.dirname(tn) |
| 44 |
with open(tn, "w") as f: |
| 45 |
f.write(TEST_CONFIG) |
| 46 |
|
| 47 |
# -- WINDOWS-REQUIRES: normpath |
| 48 |
d = configuration.read_configuration(tn) |
| 49 |
eq_(d["outfiles"], [ |
| 50 |
os.path.normpath(ROOTDIR_PREFIX + "/absolute/path1"), |
| 51 |
os.path.normpath(os.path.join(tndir, "relative/path2")), |
| 52 |
]) |
| 53 |
eq_(d["paths"], [ |
| 54 |
os.path.normpath(ROOTDIR_PREFIX + "/absolute/path3"), |
| 55 |
os.path.normpath(os.path.join(tndir, "relative/path4")), |
| 56 |
]) |
| 57 |
eq_(d["format"], ["pretty", "tag-counter"]) |
| 58 |
eq_(d["tags"], ["@foo,~@bar", "@zap"]) |
| 59 |
eq_(d["stdout_capture"], False) |
| 60 |
ok_("bogus" not in d) |
| 61 |
eq_(d["userdata"], {"foo": "bar", "answer": "42"}) |
| 62 |
|
| 63 |
def ensure_stage_environment_is_not_set(self): |
| 64 |
if "BEHAVE_STAGE" in os.environ: |
| 65 |
del os.environ["BEHAVE_STAGE"] |
| 66 |
|
| 67 |
def test_settings_without_stage(self): |
| 68 |
# -- OR: Setup with default, unnamed stage. |
| 69 |
self.ensure_stage_environment_is_not_set() |
| 70 |
assert "BEHAVE_STAGE" not in os.environ |
| 71 |
config = Configuration("") |
| 72 |
eq_("steps", config.steps_dir) |
| 73 |
eq_("environment.py", config.environment_file) |
| 74 |
|
| 75 |
def test_settings_with_stage(self): |
| 76 |
config = Configuration(["--stage=STAGE1"]) |
| 77 |
eq_("STAGE1_steps", config.steps_dir) |
| 78 |
eq_("STAGE1_environment.py", config.environment_file) |
| 79 |
|
| 80 |
def test_settings_with_stage_and_envvar(self): |
| 81 |
os.environ["BEHAVE_STAGE"] = "STAGE2" |
| 82 |
config = Configuration(["--stage=STAGE1"]) |
| 83 |
eq_("STAGE1_steps", config.steps_dir) |
| 84 |
eq_("STAGE1_environment.py", config.environment_file) |
| 85 |
del os.environ["BEHAVE_STAGE"] |
| 86 |
|
| 87 |
def test_settings_with_stage_from_envvar(self): |
| 88 |
os.environ["BEHAVE_STAGE"] = "STAGE2" |
| 89 |
config = Configuration("") |
| 90 |
eq_("STAGE2_steps", config.steps_dir) |
| 91 |
eq_("STAGE2_environment.py", config.environment_file) |
| 92 |
del os.environ["BEHAVE_STAGE"] |
| 93 |
|
| 94 |
|
| 95 |
class TestConfigurationUserData(TestCase): |
| 96 |
"""Test userdata aspects in behave.configuration.Configuration class.""" |
| 97 |
|
| 98 |
def test_cmdline_defines(self): |
| 99 |
config = Configuration([ |
| 100 |
"-D", "foo=foo_value", |
| 101 |
"--define=bar=bar_value", |
| 102 |
"--define", "baz=BAZ_VALUE", |
| 103 |
]) |
| 104 |
eq_("foo_value", config.userdata["foo"]) |
| 105 |
eq_("bar_value", config.userdata["bar"]) |
| 106 |
eq_("BAZ_VALUE", config.userdata["baz"]) |
| 107 |
|
| 108 |
def test_cmdline_defines_override_configfile(self): |
| 109 |
userdata_init = {"foo": "XXX", "bar": "ZZZ", "baz": 42} |
| 110 |
config = Configuration( |
| 111 |
"-D foo=foo_value --define bar=123", |
| 112 |
load_config=False, userdata=userdata_init) |
| 113 |
eq_("foo_value", config.userdata["foo"]) |
| 114 |
eq_("123", config.userdata["bar"]) |
| 115 |
eq_(42, config.userdata["baz"]) |
| 116 |
|
| 117 |
def test_cmdline_defines_without_value_are_true(self): |
| 118 |
config = Configuration("-D foo --define bar -Dbaz") |
| 119 |
eq_("true", config.userdata["foo"]) |
| 120 |
eq_("true", config.userdata["bar"]) |
| 121 |
eq_("true", config.userdata["baz"]) |
| 122 |
eq_(True, config.userdata.getbool("foo")) |
| 123 |
|
| 124 |
def test_cmdline_defines_with_empty_value(self): |
| 125 |
config = Configuration("-D foo=") |
| 126 |
eq_("", config.userdata["foo"]) |
| 127 |
|
| 128 |
def test_cmdline_defines_with_assign_character_as_value(self): |
| 129 |
config = Configuration("-D foo=bar=baz") |
| 130 |
eq_("bar=baz", config.userdata["foo"]) |
| 131 |
|
| 132 |
def test_cmdline_defines__with_quoted_name_value_pair(self): |
| 133 |
cmdlines = [ |
| 134 |
'-D "person=Alice and Bob"', |
| 135 |
"-D 'person=Alice and Bob'", |
| 136 |
] |
| 137 |
for cmdline in cmdlines: |
| 138 |
config = Configuration(cmdline, load_config=False) |
| 139 |
eq_(config.userdata, dict(person="Alice and Bob")) |
| 140 |
|
| 141 |
def test_cmdline_defines__with_quoted_value(self): |
| 142 |
cmdlines = [ |
| 143 |
'-D person="Alice and Bob"', |
| 144 |
"-D person='Alice and Bob'", |
| 145 |
] |
| 146 |
for cmdline in cmdlines: |
| 147 |
config = Configuration(cmdline, load_config=False) |
| 148 |
eq_(config.userdata, dict(person="Alice and Bob")) |
| 149 |
|
| 150 |
def test_setup_userdata(self): |
| 151 |
config = Configuration("", load_config=False) |
| 152 |
config.userdata = dict(person1="Alice", person2="Bob") |
| 153 |
config.userdata_defines = [("person2", "Charly")] |
| 154 |
config.setup_userdata() |
| 155 |
|
| 156 |
expected_data = dict(person1="Alice", person2="Charly") |
| 157 |
eq_(config.userdata, expected_data) |
| 158 |
|
| 159 |
def test_update_userdata__with_cmdline_defines(self): |
| 160 |
# -- NOTE: cmdline defines are reapplied. |
| 161 |
config = Configuration("-D person2=Bea", load_config=False) |
| 162 |
config.userdata = UserData(person1="AAA", person3="Charly") |
| 163 |
config.update_userdata(dict(person1="Alice", person2="Bob")) |
| 164 |
|
| 165 |
expected_data = dict(person1="Alice", person2="Bea", person3="Charly") |
| 166 |
eq_(config.userdata, expected_data) |
| 167 |
eq_(config.userdata_defines, [("person2", "Bea")]) |
| 168 |
|
| 169 |
def test_update_userdata__without_cmdline_defines(self): |
| 170 |
config = Configuration("", load_config=False) |
| 171 |
config.userdata = UserData(person1="AAA", person3="Charly") |
| 172 |
config.update_userdata(dict(person1="Alice", person2="Bob")) |
| 173 |
|
| 174 |
expected_data = dict(person1="Alice", person2="Bob", person3="Charly") |
| 175 |
eq_(config.userdata, expected_data) |
| 176 |
self.assertFalse(config.userdata_defines) |