Skip to content

Commit

Permalink
Improve Console Output to Indicate Hook Usage in Kedro-Viz (#2235)
Browse files Browse the repository at this point in the history
Resolves #2145
  • Loading branch information
rashidakanchwala authored Jan 13, 2025
1 parent a9f8fc7 commit e9542ad
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
12 changes: 11 additions & 1 deletion package/kedro_viz/launchers/cli/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
is_flag=True,
help="An experimental flag to open Kedro-Viz without Kedro project dependencies",
)
def run(
def run( # noqa: PLR0915
host,
port,
browser,
Expand Down Expand Up @@ -155,6 +155,16 @@ def run(
port = _find_available_port(host, port)

try:
if include_hooks:
hooks_message = "INFO: Running Kedro-Viz with hooks."
else:
hooks_message = (
"INFO: Running Kedro-Viz without hooks. "
"Try `kedro viz run --include-hooks` to include hook functionality."
)

display_cli_message(hooks_message, "yellow")

if port in _VIZ_PROCESSES and _VIZ_PROCESSES[port].is_alive():
_VIZ_PROCESSES[port].terminate()

Expand Down
41 changes: 40 additions & 1 deletion package/tests/test_launchers/test_cli/test_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def mock_click_echo(mocker):

@pytest.fixture
def mock_project_path(mocker):
mock_path = "/tmp/project_path"
mock_path = Path("/tmp/project_path")
mocker.patch("pathlib.Path.cwd", return_value=mock_path)
return mock_path

Expand Down Expand Up @@ -259,6 +259,45 @@ def test_kedro_viz_command_should_log_project_not_found(

mock_click_echo.assert_has_calls(mock_click_echo_calls)

def test_kedro_viz_command_logs_hooks_message(
self, mocker, mock_project_path, mock_click_echo
):
"""
Test that Kedro-Viz logs the correct message when
the `--include-hooks` flag is used or omitted.
"""
# Mock server setup and readiness checks
mocker.patch("kedro_viz.server.run_server")
mocker.patch(
"kedro_viz.launchers.utils._wait_for.__defaults__", (True, 1, True, 1)
)
mocker.patch(
"kedro_viz.launchers.utils._find_kedro_project",
return_value=mock_project_path,
)

runner = CliRunner()

# Test with --include-hooks
with runner.isolated_filesystem():
runner.invoke(main.viz_cli, ["viz", "run", "--include-hooks"])

assert any(
"INFO: Running Kedro-Viz with hooks." in call.args[0]
for call in mock_click_echo.mock_calls
), "Expected message about running with hooks not found."

# Test without --include-hooks
with runner.isolated_filesystem():
runner.invoke(main.viz_cli, ["viz", "run"])

assert any(
"INFO: Running Kedro-Viz without hooks. "
"Try `kedro viz run --include-hooks` to include hook functionality."
in call.args[0]
for call in mock_click_echo.mock_calls
), "Expected message about running without hooks not found."

def test_kedro_viz_command_should_log_outdated_version(
self, mocker, mock_http_response, mock_click_echo, mock_project_path
):
Expand Down

0 comments on commit e9542ad

Please sign in to comment.