| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 41c3954 commit 5fd795a
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -956,11 +956,16 @@ def _string_to_value(self, valuestr: str) -> Union[int, float, str, bool]: | |||
| 956 | 956 | continue | |
| 957 | 957 | # END for each numeric type | |
| 958 | 958 | ||
| 959 | - # Try boolean values as git uses them. | ||
| 959 | + # Try boolean values as git uses them. git accepts yes/no and on/off as | ||
| 960 | + # well as true/false (git_parse_maybe_bool_text in parse.c), and so does | ||
| 961 | + # ConfigParser.getboolean on this class, so only get_value lagged behind. | ||
| 962 | + # Leaving them as strings was worse than merely inexact: "no" and "off" | ||
| 963 | + # are non-empty, so a caller testing the result got True for a value git | ||
| 964 | + # reads as false. | ||
| 960 | 965 | vl = valuestr.lower() | |
| 961 | - if vl == "false": | ||
| 966 | + if vl in ("false", "no", "off"): | ||
| 962 | 967 | return False | |
| 963 | - if vl == "true": | ||
| 968 | + if vl in ("true", "yes", "on"): | ||
| 964 | 969 | return True | |
| 965 | 970 | ||
| 966 | 971 | if not isinstance(valuestr, str): | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -167,6 +167,38 @@ def test_backslash_line_continuation(self): | |||
| 167 | 167 | key = "co" if section == "alias" else "k" | |
| 168 | 168 | self.assertEqual(config.get_value(section, key), expected) | |
| 169 | 169 | ||
| 170 | + def test_get_value_reads_git_boolean_spellings(self): | ||
| 171 | + # git accepts yes/no and on/off as well as true/false, and getboolean on | ||
| 172 | + # this class already did. get_value returned them as strings, so "no" and | ||
| 173 | + # "off" arrived as non-empty (truthy) values for a caller testing them. | ||
| 174 | + cases = [ | ||
| 175 | + (b"true", True), | ||
| 176 | + (b"TRUE", True), | ||
| 177 | + (b"yes", True), | ||
| 178 | + (b"Yes", True), | ||
| 179 | + (b"on", True), | ||
| 180 | + (b"On", True), | ||
| 181 | + (b"false", False), | ||
| 182 | + (b"no", False), | ||
| 183 | + (b"off", False), | ||
| 184 | + (b"Off", False), | ||
| 185 | + ] | ||
| 186 | + for raw, expected in cases: | ||
| 187 | + config_file = io.BytesIO(b"[core]\n\tflag = " + raw + b"\n") | ||
| 188 | + config_file.name = "boolean_spellings.config" | ||
| 189 | + config = GitConfigParser(config_file) | ||
| 190 | + config.read() | ||
| 191 | + self.assertIs(config.get_value("core", "flag"), expected, raw.decode()) | ||
| 192 | + # The two accessors must not disagree about the same value. | ||
| 193 | + self.assertIs(config.getboolean("core", "flag"), expected, raw.decode()) | ||
| 194 | + | ||
| 195 | + # A value that is not a boolean at all still comes back untouched. | ||
| 196 | + config_file = io.BytesIO(b"[core]\n\tflag = meld\n") | ||
| 197 | + config_file.name = "boolean_spellings.config" | ||
| 198 | + config = GitConfigParser(config_file) | ||
| 199 | + config.read() | ||
| 200 | + self.assertEqual(config.get_value("core", "flag"), "meld") | ||
| 201 | + | ||
| 170 | 202 | @with_rw_directory | |
| 171 | 203 | def test_comment_backslash_does_not_continue_value(self, rw_dir): | |
| 172 | 204 | config_path = osp.join(rw_dir, "config") | |
| Back | FazBrowse Home | New Git URL |
0 commit comments