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

Code before:
        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)

        assert len(kedro_cli.global_groups) == 2
        assert kedro_cli.global_groups == [cli, global_commands]
        assert len(kedro_cli.project_groups) == 1
        assert kedro_cli.project_groups == [
            project_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" in result.output

    def test_main_hook_exception_handling(self, fake_metadata):
        kedro_cli = KedroCLI(fake_metadata.project_path)
        kedro_cli._cli_hook_manager.hook.after_command_run = MagicMock()

        with patch.object(
            click.CommandCollection, "main", side_effect=Exception("Test Exception")
        ):
            result = CliRunner().invoke(kedro_cli, [])

        kedro_cli._cli_hook_manager.hook.after_command_run.assert_called_once_with(
            project_metadata=kedro_cli._metadata, command_args=[], exit_code=1
        )

        assert result.exit_code == 1


@mark.usefixtures("chdir_to_dummy_project")
class TestRunCommand:
    @staticmethod
    @fixture(params=["run_config.yml", "run_config.json"])
    def fake_run_config(request, fake_root_dir):
        config_path = str(fake_root_dir / request.param)

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

Code after:

    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)

        assert len(kedro_cli.global_groups) == 2
        assert kedro_cli.global_groups == [cli, global_commands]
        assert len(kedro_cli.project_groups) == 1
        assert kedro_cli.project_groups == [
            project_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" in result.output

    def test_main_hook_exception_handling(self, fake_metadata):
        kedro_cli = KedroCLI(fake_metadata.project_path)
        kedro_cli._cli_hook_manager.hook.after_command_run = MagicMock()

        with patch.object(
            click.CommandCollection, "main", side_effect=Exception("Test Exception")
        ):
            result = CliRunner().invoke(kedro_cli, [])

        kedro_cli._cli_hook_manager.hook.after_command_run.assert_called_once_with(
            project_metadata=kedro_cli._metadata, command_args=[], exit_code=1
        )

        assert result.exit_code == 1