summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMike Frysinger <vapier@google.com>2020-08-27 01:50:12 -0400
committerMike Frysinger <vapier@google.com>2020-09-02 07:53:16 +0000
commit0578ebf61a6269e4045153c7d618baebaf00d869 (patch)
tree58c6b2b7679a40143178dd4126932f42ed3b5afa
parent65f51ad29b69986148a3eebd58683f0c4755d10a (diff)
downloadgit-repo-0578ebf61a6269e4045153c7d618baebaf00d869.tar.gz
init: reject unknown args
If you pass args to `repo init` when first creating a checkout, the repo launcher throws an error. But the init subcommand that runs in an existing checkout silently ignores them. Throw a proper error. Change-Id: I433bfcc73902d25f6b6a2974e77f6a977a75ed16 Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/279696 Reviewed-by: Jonathan Nieder <jrn@google.com> Tested-by: Mike Frysinger <vapier@google.com>
-rw-r--r--subcmds/init.py3
-rw-r--r--tests/test_subcmds.py43
-rw-r--r--tests/test_subcmds_init.py49
3 files changed, 95 insertions, 0 deletions
diff --git a/subcmds/init.py b/subcmds/init.py
index eb82e2e4..41578076 100644
--- a/subcmds/init.py
+++ b/subcmds/init.py
@@ -488,6 +488,9 @@ to update the working directory files.
488 if opt.archive and opt.mirror: 488 if opt.archive and opt.mirror:
489 self.OptionParser.error('--mirror and --archive cannot be used together.') 489 self.OptionParser.error('--mirror and --archive cannot be used together.')
490 490
491 if args:
492 self.OptionParser.error('init takes no arguments')
493
491 def Execute(self, opt, args): 494 def Execute(self, opt, args):
492 git_require(MIN_GIT_VERSION_HARD, fail=True) 495 git_require(MIN_GIT_VERSION_HARD, fail=True)
493 if not git_require(MIN_GIT_VERSION_SOFT): 496 if not git_require(MIN_GIT_VERSION_SOFT):
diff --git a/tests/test_subcmds.py b/tests/test_subcmds.py
new file mode 100644
index 00000000..2234e646
--- /dev/null
+++ b/tests/test_subcmds.py
@@ -0,0 +1,43 @@
1# Copyright (C) 2020 The Android Open Source Project
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15"""Unittests for the subcmds module (mostly __init__.py than subcommands)."""
16
17import unittest
18
19import subcmds
20
21
22class AllCommands(unittest.TestCase):
23 """Check registered all_commands."""
24
25 def test_required_basic(self):
26 """Basic checking of registered commands."""
27 # NB: We don't test all subcommands as we want to avoid "change detection"
28 # tests, so we just look for the most common/important ones here that are
29 # unlikely to ever change.
30 for cmd in {'cherry-pick', 'help', 'init', 'start', 'sync', 'upload'}:
31 self.assertIn(cmd, subcmds.all_commands)
32
33 def test_naming(self):
34 """Verify we don't add things that we shouldn't."""
35 for cmd in subcmds.all_commands:
36 # Reject filename suffixes like "help.py".
37 self.assertNotIn('.', cmd)
38
39 # Make sure all '_' were converted to '-'.
40 self.assertNotIn('_', cmd)
41
42 # Reject internal python paths like "__init__".
43 self.assertFalse(cmd.startswith('__'))
diff --git a/tests/test_subcmds_init.py b/tests/test_subcmds_init.py
new file mode 100644
index 00000000..3a5ca3c2
--- /dev/null
+++ b/tests/test_subcmds_init.py
@@ -0,0 +1,49 @@
1# Copyright (C) 2020 The Android Open Source Project
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15"""Unittests for the subcmds/init.py module."""
16
17import unittest
18
19from subcmds import init
20
21
22class InitCommand(unittest.TestCase):
23 """Check registered all_commands."""
24
25 def setUp(self):
26 self.cmd = init.Init()
27
28 def test_cli_parser_good(self):
29 """Check valid command line options."""
30 ARGV = (
31 [],
32 )
33 for argv in ARGV:
34 opts, args = self.cmd.OptionParser.parse_args(argv)
35 self.cmd.ValidateOptions(opts, args)
36
37 def test_cli_parser_bad(self):
38 """Check invalid command line options."""
39 ARGV = (
40 # Too many arguments.
41 ['asdf'],
42
43 # Conflicting options.
44 ['--mirror', '--archive'],
45 )
46 for argv in ARGV:
47 opts, args = self.cmd.OptionParser.parse_args(argv)
48 with self.assertRaises(SystemExit):
49 self.cmd.ValidateOptions(opts, args)