Beispiel #1
0
    def test_kedro_cli_with_project(self, mocker, fake_metadata, cli_runner):
        Module = namedtuple("Module", ["cli"])
        mocker.patch(
            "kedro.framework.cli.cli.importlib.import_module",
            return_value=Module(cli=cli),
        )
        mocker.patch(
            "kedro.framework.cli.cli.KedroCLI._load_project",
            return_value=fake_metadata,
        )
        kedro_cli = KedroCLI(fake_metadata.project_path)

        assert len(kedro_cli.global_groups) == 2
        assert kedro_cli.global_groups == [
            cli,
            create_cli,
        ]
        assert len(kedro_cli.project_groups) == 5
        assert kedro_cli.project_groups == [
            catalog_cli,
            jupyter_cli,
            pipeline_cli,
            project_group,
            cli,
        ]

        result = cli_runner.invoke(kedro_cli, [])
        assert result.exit_code == 0
        assert "Global commands from Kedro" in result.output
        assert "Project specific commands from Kedro" in result.output
Beispiel #2
0
 def test_kedro_cli_should_invoke_cli_hooks_from_plugin(
     self,
     caplog,
     command,
     mocker,
     fake_metadata,
     fake_plugin_distribution,
 ):
     Module = namedtuple("Module", ["cli"])
     mocker.patch(
         "kedro.framework.cli.cli.importlib.import_module",
         return_value=Module(cli=cli),
     )
     mocker.patch(
         "kedro.framework.cli.cli._is_project",
         return_value=True,
     )
     mocker.patch(
         "kedro.framework.cli.cli.bootstrap_project",
         return_value=fake_metadata,
     )
     kedro_cli = KedroCLI(fake_metadata.project_path)
     result = CliRunner().invoke(kedro_cli, [command])
     assert (
         f"Registered CLI hooks from 1 installed plugin(s): "
         f"{fake_plugin_distribution.metadata['name']}-{fake_plugin_distribution.version}"
         in caplog.text)
     assert (f"Before command `{command}` run for project {fake_metadata}"
             in result.output)
Beispiel #3
0
 def test_get_cli_structure_depth(self, mocker, fake_metadata):
     Module = namedtuple("Module", ["cli"])
     mocker.patch(
         "kedro.framework.cli.cli.importlib.import_module",
         return_value=Module(cli=cli),
     )
     mocker.patch(
         "kedro.framework.cli.cli._is_project",
         return_value=True,
     )
     mocker.patch(
         "kedro.framework.cli.cli.bootstrap_project",
         return_value=fake_metadata,
     )
     kedro_cli = KedroCLI(fake_metadata.project_path)
     raw_cli_structure = get_cli_structure(kedro_cli, get_help=False)
     assert type(raw_cli_structure["kedro"]["new"]) == dict
     assert sorted(list(
         raw_cli_structure["kedro"]["new"].keys())) == sorted([
             "--verbose",
             "-v",
             "--config",
             "-c",
             "--starter",
             "-s",
             "--checkout",
             "--directory",
             "--help",
         ])
     # now check that once params and args are reached, the values are None
     assert raw_cli_structure["kedro"]["new"]["--starter"] is None
     assert raw_cli_structure["kedro"]["new"]["--checkout"] is None
     assert raw_cli_structure["kedro"]["new"]["--help"] is None
     assert raw_cli_structure["kedro"]["new"]["-c"] is None
Beispiel #4
0
    def test_get_cli_structure_raw(self, mocker, fake_metadata):
        Module = namedtuple("Module", ["cli"])
        mocker.patch(
            "kedro.framework.cli.cli.importlib.import_module",
            return_value=Module(cli=cli),
        )
        mocker.patch(
            "kedro.framework.cli.cli._is_project",
            return_value=True,
        )
        mocker.patch(
            "kedro.framework.cli.cli.bootstrap_project",
            return_value=fake_metadata,
        )
        kedro_cli = KedroCLI(fake_metadata.project_path)
        raw_cli_structure = get_cli_structure(kedro_cli, get_help=False)

        # raw CLI structure tests
        assert isinstance(raw_cli_structure, dict)
        assert isinstance(raw_cli_structure["kedro"], dict)

        for k, v in raw_cli_structure["kedro"].items():
            assert isinstance(k, str)
            assert isinstance(v, dict)

        assert sorted(list(
            raw_cli_structure["kedro"])) == sorted(DEFAULT_KEDRO_COMMANDS)
Beispiel #5
0
    def test_get_cli_structure_help(self, mocker, fake_metadata):
        Module = namedtuple("Module", ["cli"])
        mocker.patch(
            "kedro.framework.cli.cli.importlib.import_module",
            return_value=Module(cli=cli),
        )
        mocker.patch(
            "kedro.framework.cli.cli._is_project",
            return_value=True,
        )
        mocker.patch(
            "kedro.framework.cli.cli.bootstrap_project",
            return_value=fake_metadata,
        )
        kedro_cli = KedroCLI(fake_metadata.project_path)
        help_cli_structure = get_cli_structure(kedro_cli, get_help=True)

        assert isinstance(help_cli_structure, dict)
        assert isinstance(help_cli_structure["kedro"], dict)

        for k, v in help_cli_structure["kedro"].items():
            assert isinstance(k, str)
            if isinstance(v, dict):
                for sub_key in v:
                    assert isinstance(help_cli_structure["kedro"][k][sub_key],
                                      str)
                    assert help_cli_structure["kedro"][k][sub_key].startswith(
                        "Usage:  [OPTIONS]")
            elif isinstance(v, str):
                assert v.startswith("Usage:  [OPTIONS]")

        assert sorted(list(
            help_cli_structure["kedro"])) == sorted(DEFAULT_KEDRO_COMMANDS)
Beispiel #6
0
    def test_kedro_cli_should_invoke_cli_hooks_from_plugin(
        self,
        caplog,
        command,
        mocker,
        fake_metadata,
        fake_plugin_distribution,
    ):
        # Workaround to ensure that the log messages are picked up by caplog.
        # https://github.com/pytest-dev/pytest/issues/3697
        logging.getLogger("kedro.framework.cli.hooks.manager").propagate = True

        Module = namedtuple("Module", ["cli"])
        mocker.patch(
            "kedro.framework.cli.cli.importlib.import_module",
            return_value=Module(cli=cli),
        )
        mocker.patch(
            "kedro.framework.cli.cli._is_project",
            return_value=True,
        )
        mocker.patch(
            "kedro.framework.cli.cli.bootstrap_project",
            return_value=fake_metadata,
        )
        kedro_cli = KedroCLI(fake_metadata.project_path)
        result = CliRunner().invoke(kedro_cli, [command])
        assert (
            f"Registered CLI hooks from 1 installed plugin(s): "
            f"{fake_plugin_distribution.metadata['name']}-{fake_plugin_distribution.version}"
            in caplog.text)
        assert (f"Before command `{command}` run for project {fake_metadata}"
                in result.output)
Beispiel #7
0
 def test_project_commands_invalid_clipy(self, mocker, fake_metadata):
     mocker.patch("kedro.framework.cli.cli.importlib.import_module",
                  return_value=None)
     mocker.patch("kedro.framework.cli.cli._is_project", return_value=True)
     mocker.patch("kedro.framework.cli.cli.bootstrap_project",
                  return_value=fake_metadata)
     with raises(KedroCliError, match="Cannot load commands from"):
         _ = KedroCLI(fake_metadata.project_path)
Beispiel #8
0
    def test_kedro_cli_no_project(self, mocker, tmp_path):
        mocker.patch("kedro.framework.cli.cli._is_project", return_value=False)
        kedro_cli = KedroCLI(tmp_path)
        assert len(kedro_cli.global_groups) == 2
        assert kedro_cli.global_groups == [cli, create_cli]

        result = CliRunner().invoke(kedro_cli, [])

        assert result.exit_code == 0
        assert "Global commands from Kedro" in result.output
        assert "Project specific commands from Kedro" not in result.output
Beispiel #9
0
 def test_project_commands_no_clipy(self, mocker, fake_metadata):
     mocker.patch(
         "kedro.framework.cli.cli.importlib.import_module",
         side_effect=cycle([ModuleNotFoundError()]),
     )
     mocker.patch("kedro.framework.cli.cli._is_project", return_value=True)
     mocker.patch("kedro.framework.cli.cli.bootstrap_project",
                  return_value=fake_metadata)
     kedro_cli = KedroCLI(fake_metadata.project_path)
     assert len(kedro_cli.project_groups) == 4
     assert kedro_cli.project_groups == [
         catalog_cli,
         jupyter_cli,
         pipeline_cli,
         project_group,
     ]
Beispiel #10
0
 def test_project_commands_valid_clipy(self, mocker, fake_metadata):
     Module = namedtuple("Module", ["cli"])
     mocker.patch(
         "kedro.framework.cli.cli.importlib.import_module",
         return_value=Module(cli=cli),
     )
     mocker.patch(
         "kedro.framework.cli.cli.KedroCLI._load_project", return_value=fake_metadata
     )
     kedro_cli = KedroCLI(fake_metadata.project_path)
     assert len(kedro_cli.project_groups) == 5
     assert kedro_cli.project_groups == [
         catalog_cli,
         jupyter_cli,
         pipeline_cli,
         project_group,
         cli,
     ]
Beispiel #11
0
 def test_project_commands_no_project(self, mocker, tmp_path):
     mocker.patch(
         "kedro.framework.cli.cli.KedroCLI._load_project", return_value=None
     )
     kedro_cli = KedroCLI(tmp_path)
     assert len(kedro_cli.project_groups) == 0
Beispiel #12
0
 def test_project_commands_no_project(self, mocker, tmp_path):
     mocker.patch("kedro.framework.cli.cli._is_project", return_value=False)
     kedro_cli = KedroCLI(tmp_path)
     assert len(kedro_cli.project_groups) == 0
     assert kedro_cli._metadata is None