diff options
author | Mike Frysinger <vapier@google.com> | 2025-05-22 14:14:45 -0400 |
---|---|---|
committer | LUCI <gerrit-scoped@luci-project-accounts.iam.gserviceaccount.com> | 2025-05-27 09:26:43 -0700 |
commit | 06338abe79f1fbef61f7297530ebf11139654d6c (patch) | |
tree | 738d8ba21689be7b2e9f37b0143717c210690a95 /tests/test_subcmds.py | |
parent | 8d37f6147174fe170cc00a1d82b1cc5fe8ec0a7b (diff) | |
download | git-repo-06338abe79f1fbef61f7297530ebf11139654d6c.tar.gz |
subcmds: delete redundant dest= settings
Add a test to enforce this too.
Change-Id: I80b5cf567aa33db9c24b53428c66d69f9c1d8d74
Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/478481
Commit-Queue: Mike Frysinger <vapier@google.com>
Tested-by: Mike Frysinger <vapier@google.com>
Reviewed-by: Scott Lee <ddoman@google.com>
Diffstat (limited to 'tests/test_subcmds.py')
-rw-r--r-- | tests/test_subcmds.py | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/tests/test_subcmds.py b/tests/test_subcmds.py index 5ce0776f..2d680fb7 100644 --- a/tests/test_subcmds.py +++ b/tests/test_subcmds.py | |||
@@ -89,3 +89,44 @@ class AllCommands(unittest.TestCase): | |||
89 | msg=f"subcmds/{name}.py: {opt}: only use dashes in " | 89 | msg=f"subcmds/{name}.py: {opt}: only use dashes in " |
90 | "options, not underscores", | 90 | "options, not underscores", |
91 | ) | 91 | ) |
92 | |||
93 | def test_cli_option_dest(self): | ||
94 | """Block redundant dest= arguments.""" | ||
95 | |||
96 | def _check_dest(opt): | ||
97 | if opt.dest is None or not opt._long_opts: | ||
98 | return | ||
99 | |||
100 | long = opt._long_opts[0] | ||
101 | assert long.startswith("--") | ||
102 | # This matches optparse's behavior. | ||
103 | implicit_dest = long[2:].replace("-", "_") | ||
104 | if implicit_dest == opt.dest: | ||
105 | bad_opts.append((str(opt), opt.dest)) | ||
106 | |||
107 | # Hook the option check list. | ||
108 | optparse.Option.CHECK_METHODS.insert(0, _check_dest) | ||
109 | |||
110 | # Gather all the bad options up front so people can see all bad options | ||
111 | # instead of failing at the first one. | ||
112 | all_bad_opts = {} | ||
113 | for name, cls in subcmds.all_commands.items(): | ||
114 | bad_opts = all_bad_opts[name] = [] | ||
115 | cmd = cls() | ||
116 | # Trigger construction of parser. | ||
117 | cmd.OptionParser | ||
118 | |||
119 | errmsg = None | ||
120 | for name, bad_opts in sorted(all_bad_opts.items()): | ||
121 | if bad_opts: | ||
122 | if not errmsg: | ||
123 | errmsg = "Omit redundant dest= when defining options.\n" | ||
124 | errmsg += f"\nSubcommand {name} (subcmds/{name}.py):\n" | ||
125 | errmsg += "".join( | ||
126 | f" {opt}: dest='{dest}'\n" for opt, dest in bad_opts | ||
127 | ) | ||
128 | if errmsg: | ||
129 | self.fail(errmsg) | ||
130 | |||
131 | # Make sure we aren't popping the wrong stuff. | ||
132 | assert optparse.Option.CHECK_METHODS.pop(0) is _check_dest | ||