Repository: kedro-org/kedro
File: tests/framework/cli/test_cli.py (Python)

Code before:
        mocker.patch(
            "kedro.framework.cli.cli.bootstrap_project", return_value=fake_metadata
        )
        mocker.patch(
            "kedro.framework.cli.cli.importlib.import_module",
            return_value=Module(cli=cli),
        )
        kedro_cli = KedroCLI(fake_metadata.project_path)
        # The project group will now have two groups, the first from the project's cli.py and
        # the second is the lazy project command group
        assert len(kedro_cli.project_groups) == 2
        assert kedro_cli.project_groups == [
            cli,
            project_commands,
        ]

    def test_kedro_cli_no_project(self, mocker, tmp_path):
        mocker.patch("kedro.framework.cli.cli.is_kedro_project", return_value=False)
        kedro_cli = KedroCLI(tmp_path)
        assert len(kedro_cli.global_groups) == 2
        # The global groups will be the cli(group for info command) and the global commands (starter and new)
        assert kedro_cli.global_groups == [cli, global_commands]

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

        assert result.exit_code in (0, 2)
        assert "Global commands from kedro" in result.output
        assert "Project specific commands from kedro" not in result.output

    def test_kedro_run_no_project(self, mocker, tmp_path):
        mocker.patch("kedro.framework.cli.cli.is_kedro_project", return_value=False)
        kedro_cli = KedroCLI(tmp_path)

        result = CliRunner().invoke(kedro_cli, ["run"])
        assert (
            "Kedro project not found in this directory. Project specific commands such as 'run' or "
            "'jupyter' are only available within a project directory." in result.output
        )

        assert (
            "Hint: Kedro is looking for a file called 'pyproject.toml, is one present in"
            " your current working directory?" in result.output
        )

    def test_kedro_cli_with_project(self, mocker, fake_metadata):
        mocker.patch("kedro.framework.cli.cli.is_kedro_project", return_value=True)
        mocker.patch(
            "kedro.framework.cli.cli.bootstrap_project", return_value=fake_metadata
        )
        kedro_cli = KedroCLI(fake_metadata.project_path)


Review comment (suggestion):
```suggestion
        assert result.exit_code == 2
```

Code after:

    def test_project_commands_valid_clipy(self, mocker, fake_metadata):
        Module = namedtuple("Module", ["cli"])
        mocker.patch("kedro.framework.cli.cli.is_kedro_project", return_value=True)
        mocker.patch(
            "kedro.framework.cli.cli.bootstrap_project", return_value=fake_metadata
        )
        mocker.patch(
            "kedro.framework.cli.cli.importlib.import_module",
            return_value=Module(cli=cli),
        )
        kedro_cli = KedroCLI(fake_metadata.project_path)
        # The project group will now have two groups, the first from the project's cli.py and
        # the second is the lazy project command group
        assert len(kedro_cli.project_groups) == 2
        assert kedro_cli.project_groups == [
            cli,
            project_commands,
        ]

    def test_kedro_cli_no_project(self, mocker, tmp_path):
        mocker.patch("kedro.framework.cli.cli.is_kedro_project", return_value=False)
        kedro_cli = KedroCLI(tmp_path)
        assert len(kedro_cli.global_groups) == 2
        # The global groups will be the cli(group for info command) and the global commands (starter and new)
        assert kedro_cli.global_groups == [cli, global_commands]

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

        # Exit code 2: click 8.2+ exits with code 2 when a group is invoked
        # without a subcommand
        assert result.exit_code == 2
        assert "Global commands from kedro" in result.output
        assert "Project specific commands from kedro" not in result.output

    def test_kedro_run_no_project(self, mocker, tmp_path):
        mocker.patch("kedro.framework.cli.cli.is_kedro_project", return_value=False)
        kedro_cli = KedroCLI(tmp_path)

        result = CliRunner().invoke(kedro_cli, ["run"])
        assert (
            "Kedro project not found in this directory. Project specific commands such as 'run' or "
            "'jupyter' are only available within a project directory." in result.output
        )

        assert (
            "Hint: Kedro is looking for a file called 'pyproject.toml, is one present in"
            " your current working directory?" in result.output
        )

    def test_kedro_cli_with_project(self, mocker, fake_metadata):