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
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import io
import os
from unittest import mock
from stestr.tests import base
from stestr import user_config
FULL_YAML = """
run:
concurrency: 42 # This can be any integer value
random: True
no-subunit-trace: True
color: True
abbreviate: True
slowest: True
suppress-attachments: True
all-attachments: True
failing:
list: True
last:
no-subunit-trace: True
color: True
suppress-attachments: True
all-attachments: True
load:
force-init: True
subunit-trace: True
color: True
abbreviate: True
suppress-attachments: True
all-attachments: True
history-list:
show-metadata: True
history-show:
no-subunit-trace: True
color: True
suppress-attachments: True
all-attachments: True
"""
INVALID_YAML_FIELD = """
run:
color: True,
"""
YAML_NOT_INT = """
run:
concurrency: Two
"""
class TestUserConfig(base.TestCase):
def setUp(self):
super().setUp()
home_dir = os.path.expanduser("~")
self.xdg_path = os.path.join(os.path.join(home_dir, ".config"), "stestr.yaml")
self.home_path = os.path.join(home_dir, ".stestr.yaml")
@mock.patch("sys.exit")
@mock.patch("stestr.user_config.UserConfig")
def test_get_user_config_invalid_path(self, user_mock, exit_mock):
user_config.get_user_config("/i_am_an_invalid_path")
msg = "The specified stestr user config is not a valid path"
exit_mock.assert_called_once_with(msg)
@mock.patch("os.path.isfile")
@mock.patch("stestr.user_config.UserConfig")
def test_get_user_config_xdg_file(self, user_mock, path_mock):
def fake_isfile(path):
if path == self.xdg_path:
return True
else:
return False
path_mock.side_effect = fake_isfile
user_config.get_user_config()
user_mock.assert_called_once_with(self.xdg_path)
@mock.patch("os.path.isfile")
@mock.patch("stestr.user_config.UserConfig")
def test_get_default_user_config_file(self, user_mock, path_mock):
def fake_isfile(path):
if path == self.home_path:
return True
else:
return False
path_mock.side_effect = fake_isfile
user_config.get_user_config()
user_mock.assert_called_once_with(self.home_path)
@mock.patch("yaml.safe_load", return_value={})
@mock.patch("builtins.open", mock.mock_open())
def test_user_config_empty_schema(self, yaml_mock):
user_conf = user_config.UserConfig("/path")
self.assertEqual({}, user_conf.config)
@mock.patch("yaml.safe_load", return_value={"init": {"subunit-trace": True}})
@mock.patch("sys.exit")
@mock.patch("builtins.open", mock.mock_open())
def test_user_config_invalid_command(self, exit_mock, yaml_mock):
user_config.UserConfig("/path")
error_string = (
"Provided user config file /path is invalid because:\n"
"extra keys not allowed @ data['init']"
)
exit_mock.assert_called_once_with(error_string)
@mock.patch("yaml.safe_load", return_value={"run": {"subunit-trace": True}})
@mock.patch("sys.exit")
@mock.patch("builtins.open", mock.mock_open())
def test_user_config_invalid_option(self, exit_mock, yaml_mock):
user_config.UserConfig("/path")
error_string = (
"Provided user config file /path is invalid because:\n"
"extra keys not allowed @ "
"data['run']['subunit-trace']"
)
exit_mock.assert_called_once_with(error_string)
@mock.patch("builtins.open", return_value=io.BytesIO(FULL_YAML.encode("utf-8")))
def test_user_config_full_config(self, open_mock):
user_conf = user_config.UserConfig("/path")
full_dict = {
"run": {
"concurrency": 42,
"random": True,
"no-subunit-trace": True,
"color": True,
"abbreviate": True,
"slowest": True,
"suppress-attachments": True,
"all-attachments": True,
},
"failing": {"list": True},
"last": {
"no-subunit-trace": True,
"color": True,
"suppress-attachments": True,
"all-attachments": True,
},
"load": {
"force-init": True,
"subunit-trace": True,
"color": True,
"abbreviate": True,
"suppress-attachments": True,
"all-attachments": True,
},
"history-list": {"show-metadata": True},
"history-show": {
"no-subunit-trace": True,
"color": True,
"suppress-attachments": True,
"all-attachments": True,
},
}
self.assertEqual(full_dict, user_conf.config)
@mock.patch("sys.exit")
@mock.patch(
"builtins.open", return_value=io.BytesIO(INVALID_YAML_FIELD.encode("utf-8"))
)
def test_user_config_invalid_value_type(self, open_mock, exit_mock):
user_config.UserConfig("/path")
error_string = (
"Provided user config file /path is invalid because:\n"
"expected bool for dictionary value @ "
"data['run']['color']"
)
exit_mock.assert_called_once_with(error_string)
@mock.patch("sys.exit")
@mock.patch("builtins.open", return_value=io.BytesIO(YAML_NOT_INT.encode("utf-8")))
def test_user_config_invalid_integer(self, open_mock, exit_mock):
user_config.UserConfig("/path")
error_string = (
"Provided user config file /path is invalid because:\n"
"expected int for dictionary value @ "
"data['run']['concurrency']"
)
exit_mock.assert_called_once_with(error_string)
|