From 8da9448d02367deb2fdf7b6fc5ea58b3a1f2971a Mon Sep 17 00:00:00 2001 From: Ondřej Nosek Date: May 05 2020 07:44:26 +0000 Subject: Check repo name for correct format During 'clone' command, passing URL with as a 'repo' argument is a common mistake. Instead, just the name of the repository is sufficient. URL base of the repo is already specified in the config. This fix adds check for this mistake on command-line input. JIRA: RHELCMP-434 Signed-off-by: Ondřej Nosek --- diff --git a/pyrpkg/cli.py b/pyrpkg/cli.py index 61e2ae0..f34871f 100644 --- a/pyrpkg/cli.py +++ b/pyrpkg/cli.py @@ -650,9 +650,17 @@ class cliClient(object): clone_parser.add_argument( '--anonymous', '-a', action='store_true', help='Check out a module anonymously') + + def validator_not_url(raw_value): + """checks if input doesn't contain URL; URL as an input + is often mistake""" + if "://" in raw_value or "@" in raw_value: + raise argparse.ArgumentTypeError("argument can't contain an URL") + return raw_value # store the module to be cloned clone_parser.add_argument( - 'repo', nargs=1, help='Name of the repository to clone') + 'repo', nargs=1, type=validator_not_url, + help='Name of the repository to clone') # Eventually specify where to clone the module clone_parser.add_argument( "clone_target", default=None, nargs="?", diff --git a/tests/test_cli.py b/tests/test_cli.py index 20749ca..5215a7d 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -579,8 +579,21 @@ class TestPull(CliTestCase): class TestClone(CliTestCase): """Test clone command""" - def shortDescription(self): - return None + + @patch('sys.stderr', new=StringIO()) + def test_repo_arg(self): + """repo argument should contain just a name of the repo, not URL + and program should raise exception during argument parsing""" + cli_cmd = ['rpkg', '--path', self.cloned_repo_path, + 'clone', 'ssh://git@pagure.io/forks/user/xrepo.git', 'target_dir'] + + with patch('sys.argv', new=cli_cmd): + with self.assertRaises(SystemExit): + cli = self.new_cli() + cli.clone() # this is not gonna be called + + output = sys.stderr.getvalue().strip() + self.assertIn("argument can't contain an URL", output) @patch('sys.stderr', new=StringIO()) @patch('pyrpkg.Commands._clone_config', new_callable=Mock())