def main(): # pragma: no cover """Main entry point. Look for a ``cli.py``, and, if found, add its commands to `kedro`'s before invoking the CLI. """ _init_plugins() global_groups = [cli] global_groups.extend(load_entry_points("global")) project_groups = [] cli_context = dict() path = Path.cwd() if _is_project(path): # load project commands from cli.py metadata = _get_project_metadata(path) cli_context = dict(obj=metadata) _add_src_to_path(metadata.source_dir, path) project_groups.extend(load_entry_points("project")) package_name = metadata.package_name try: project_cli = importlib.import_module(f"{package_name}.cli") project_groups.append(project_cli.cli) except Exception as exc: raise KedroCliError( f"Cannot load commands from {package_name}.cli" ) from exc cli_collection = CommandCollection( ("Global commands", global_groups), ("Project specific commands", project_groups), ) cli_collection(**cli_context)
def test_found_reverse(self, cli_runner): """Test calling existing command.""" cmd_collection = CommandCollection(("Commands", [stub_cli, cli])) invoke_result = cli_runner.invoke(cmd_collection, ["stub_command"]) assert invoke_result.exit_code == 0 assert "group callback" in invoke_result.output assert "command callback" in invoke_result.output
def main(): # pragma: no cover """Main entry point, look for a `kedro_cli.py` and if found add its commands to `kedro`'s then invoke the cli. """ _init_plugins() global_groups = [cli] global_groups.extend(load_entry_points("global")) project_groups = [] # load project commands from kedro_cli.py path = Path.cwd() kedro_cli_path = path / "kedro_cli.py" if kedro_cli_path.exists(): try: sys.path.append(str(path)) kedro_cli = importlib.import_module("kedro_cli") project_groups.extend(load_entry_points("project")) project_groups.append(kedro_cli.cli) except Exception: # pylint: disable=broad-except _handle_exception(f"Cannot load commands from {kedro_cli_path}") cli_collection = CommandCollection( ("Global commands", global_groups), ("Project specific commands", project_groups), ) cli_collection()
def test_help(self): """Check that help output includes stub_cli group description.""" cmd_collection = CommandCollection(("Commands", [cli, stub_cli])) result = CliRunner().invoke(cmd_collection, []) assert result.exit_code == 0 assert "Stub CLI group description" in result.output assert "Kedro is a CLI" in result.output
def test_not_found(self): """Test calling nonexistent command.""" cmd_collection = CommandCollection(("Commands", [cli, stub_cli])) result = CliRunner().invoke(cmd_collection, ["not_found"]) assert result.exit_code == 2 assert "No such command" in result.output assert "Did you mean one of these" not in result.output
def test_found(self): """Test calling existing command.""" cmd_collection = CommandCollection(("Commands", [cli, stub_cli])) result = CliRunner().invoke(cmd_collection, ["stub_command"]) assert result.exit_code == 0 assert "group callback" not in result.output assert "command callback" in result.output
def test_not_found_closet_match_singular(self, cli_runner, mocker): """Check that calling a nonexistent command with a close match has the proper wording""" patched_difflib = mocker.patch( "kedro.framework.cli.utils.difflib.get_close_matches", return_value=["suggestion_1"], ) cmd_collection = CommandCollection(("Commands", [stub_cli, cli])) invoke_result = cli_runner.invoke(cmd_collection, ["not_found"]) patched_difflib.assert_called_once_with("not_found", mocker.ANY, mocker.ANY, mocker.ANY) assert invoke_result.exit_code == 2 assert "No such command" in invoke_result.output assert "Did you mean this?" in invoke_result.output assert "suggestion_1" in invoke_result.output
def test_not_found_closest_match(self, mocker): """Check that calling a nonexistent command with a close match returns the close match""" patched_difflib = mocker.patch( "kedro.framework.cli.utils.difflib.get_close_matches", return_value=["suggestion_1", "suggestion_2"], ) cmd_collection = CommandCollection(("Commands", [cli, stub_cli])) result = CliRunner().invoke(cmd_collection, ["not_found"]) patched_difflib.assert_called_once_with("not_found", mocker.ANY, mocker.ANY, mocker.ANY) assert result.exit_code == 2 assert "No such command" in result.output assert "Did you mean one of these?" in result.output assert "suggestion_1" in result.output assert "suggestion_2" in result.output
def invoke_result(cli_runner, request): cmd_collection = CommandCollection(("Commands", [cli, stub_cli])) return cli_runner.invoke(cmd_collection, request.param)