1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
|
import json
import sys
import unittest
from os import path
from textwrap import dedent
import mozunit
import toml
import voluptuous
from io import StringIO
FEATURE_GATES_ROOT_PATH = path.abspath(
path.join(path.dirname(__file__), path.pardir, path.pardir)
)
sys.path.append(FEATURE_GATES_ROOT_PATH)
from gen_feature_definitions import (
ExceptionGroup,
expand_feature,
feature_schema,
FeatureGateException,
hyphens_to_camel_case,
main,
process_files,
)
def make_test_file_path(name):
return path.join(FEATURE_GATES_ROOT_PATH, "test", "python", "data", name + ".toml")
def minimal_definition(**kwargs):
defaults = {
"id": "test-feature",
"title": "Test Feature",
"description": "A feature for testing things",
"bug-numbers": [1479127],
"restart-required": False,
"type": "boolean",
}
defaults.update(dict([(k.replace("_", "-"), v) for k, v in kwargs.items()]))
return defaults
class TestHyphensToCamelCase(unittest.TestCase):
simple_cases = [
("", ""),
("singleword", "singleword"),
("more-than-one-word", "moreThanOneWord"),
]
def test_simple_cases(self):
for in_string, out_string in self.simple_cases:
assert hyphens_to_camel_case(in_string) == out_string
class TestExceptionGroup(unittest.TestCase):
def test_str_indentation_of_grouped_lines(self):
errors = [
Exception("single line error 1"),
Exception("single line error 2"),
Exception("multiline\nerror 1"),
Exception("multiline\nerror 2"),
]
assert str(ExceptionGroup(errors)) == dedent(
"""\
There were errors while processing feature definitions:
* single line error 1
* single line error 2
* multiline
error 1
* multiline
error 2"""
)
class TestFeatureGateException(unittest.TestCase):
def test_str_no_file(self):
error = FeatureGateException("oops")
assert str(error) == "In unknown file: oops"
def test_str_with_file(self):
error = FeatureGateException("oops", filename="some/bad/file.txt")
assert str(error) == 'In file "some/bad/file.txt":\n oops'
def test_repr_no_file(self):
error = FeatureGateException("oops")
assert repr(error) == "FeatureGateException('oops', filename=None)"
def test_repr_with_file(self):
error = FeatureGateException("oops", filename="some/bad/file.txt")
assert (
repr(error) == "FeatureGateException('oops', filename='some/bad/file.txt')"
)
class TestProcessFiles(unittest.TestCase):
def test_valid_file(self):
filename = make_test_file_path("good")
result = process_files([filename])
assert result == {
"demo-feature": {
"id": "demo-feature",
"title": "Demo Feature",
"description": "A no-op feature to demo the feature gate system.",
"restartRequired": False,
"preference": "foo.bar.baz",
"type": "boolean",
"bugNumbers": [1479127],
"isPublicJexl": "true",
"defaultValueJexl": "false",
},
"minimal-feature": {
"id": "minimal-feature",
"title": "Minimal Feature",
"description": "The smallest feature that is valid",
"restartRequired": True,
"preference": "features.minimal-feature.enabled",
"type": "boolean",
"bugNumbers": [1479127],
"isPublicJexl": "false",
"defaultValueJexl": None,
},
}
def test_invalid_toml(self):
filename = make_test_file_path("invalid_toml")
with self.assertRaises(ExceptionGroup) as context:
process_files([filename])
error_group = context.exception
assert len(error_group.errors) == 1
assert type(error_group.errors[0]) == FeatureGateException
def test_empty_feature(self):
filename = make_test_file_path("empty_feature")
with self.assertRaises(ExceptionGroup) as context:
process_files([filename])
error_group = context.exception
assert len(error_group.errors) == 1
assert type(error_group.errors[0]) == FeatureGateException
assert "required key not provided" in str(error_group.errors[0])
def test_missing_file(self):
filename = make_test_file_path("file_does_not_exist")
with self.assertRaises(ExceptionGroup) as context:
process_files([filename])
error_group = context.exception
assert len(error_group.errors) == 1
assert type(error_group.errors[0]) == FeatureGateException
assert "No such file or directory" in str(error_group.errors[0])
class TestFeatureSchema(unittest.TestCase):
def make_test_features(self, *overrides):
if len(overrides) == 0:
overrides = [{}]
features = {}
for override in overrides:
feature = minimal_definition(**override)
feature_id = feature.pop("id")
features[feature_id] = feature
return features
def test_minimal_valid(self):
definition = self.make_test_features()
# should not raise an exception
feature_schema(definition)
def test_extra_keys_not_allowed(self):
definition = self.make_test_features({"unexpected_key": "oh no!"})
with self.assertRaises(voluptuous.Error) as context:
feature_schema(definition)
assert "extra keys not allowed" in str(context.exception)
def test_required_fields(self):
required_keys = [
"title",
"description",
"bug-numbers",
"restart-required",
"type",
]
for key in required_keys:
definition = self.make_test_features({"id": "test-feature"})
del definition["test-feature"][key]
with self.assertRaises(voluptuous.Error) as context:
feature_schema(definition)
assert "required key not provided" in str(context.exception)
assert key in str(context.exception)
def test_nonempty_keys(self):
test_parameters = [("title", ""), ("description", ""), ("bug-numbers", [])]
for key, empty in test_parameters:
definition = self.make_test_features({key: empty})
with self.assertRaises(voluptuous.Error) as context:
feature_schema(definition)
assert "length of value must be at least" in str(context.exception)
assert "['{}']".format(key) in str(context.exception)
class ExpandFeatureTests(unittest.TestCase):
def test_hyphenation_to_snake_case(self):
feature = minimal_definition()
assert "bug-numbers" in feature
assert "bugNumbers" in expand_feature(feature)
def test_default_value_default(self):
feature = minimal_definition(type="boolean")
assert "default-value" not in feature
assert "defaultValue" not in feature
assert "default-value-jexl" not in feature
assert "defaultValueJexl" not in feature
assert expand_feature(feature)["defaultValueJexl"] == None
def test_default_value_override_constant(self):
feature = minimal_definition(type="boolean", default_value_jexl="true")
assert expand_feature(feature)["defaultValueJexl"] == "true"
def test_default_value_override_configured_value(self):
feature = minimal_definition(
type="boolean", default_value_jexl="channel == nightly"
)
assert expand_feature(feature)["defaultValueJexl"] == "channel == nightly"
def test_preference_default(self):
feature = minimal_definition(type="boolean")
assert "preference" not in feature
assert expand_feature(feature)["preference"] == "features.test-feature.enabled"
def test_preference_override(self):
feature = minimal_definition(preference="test.feature.a")
assert expand_feature(feature)["preference"] == "test.feature.a"
class MainTests(unittest.TestCase):
def test_it_outputs_json(self):
output = StringIO()
filename = make_test_file_path("good")
main(output, filename)
output.seek(0)
results = json.load(output)
assert results == {
"demo-feature": {
"id": "demo-feature",
"title": "Demo Feature",
"description": "A no-op feature to demo the feature gate system.",
"restartRequired": False,
"preference": "foo.bar.baz",
"type": "boolean",
"bugNumbers": [1479127],
"isPublicJexl": "true",
"defaultValueJexl": "false",
},
"minimal-feature": {
"id": "minimal-feature",
"title": "Minimal Feature",
"description": "The smallest feature that is valid",
"restartRequired": True,
"preference": "features.minimal-feature.enabled",
"type": "boolean",
"bugNumbers": [1479127],
"isPublicJexl": "false",
"defaultValueJexl": None,
},
}
def test_it_returns_1_for_errors(self):
output = StringIO()
filename = make_test_file_path("invalid_toml")
assert main(output, filename) == 1
assert output.getvalue() == ""
if __name__ == "__main__":
mozunit.main(*sys.argv[1:])
|