Skip to content

Commit

Permalink
reverting project not found bug fix
Browse files Browse the repository at this point in the history
Signed-off-by: ravi-kumar-pilla <[email protected]>
  • Loading branch information
ravi-kumar-pilla committed Apr 22, 2024
1 parent dede026 commit 4ca1bf8
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 154 deletions.
3 changes: 1 addition & 2 deletions RELEASE.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ Please follow the established format:
## Major features and improvements

## Bug fixes and other changes

- Upgrade the gitpod workspace-full to a newer version which includes both Node 18 and Python 3.11.5. (#1862)
- Refactor backend integration with Kedro by replacing bootstrap_project with configure_project. (#1796)

# Release 9.0.0

Expand All @@ -29,6 +27,7 @@ Please follow the established format:
- Increase Kedro-Viz timeout. (#1803)
- Remove demo data source and update feature hints. (#1804)
- Add markdown support for backticks in the pop-up reminder. (#1826)
- Refactor backend integration with Kedro by replacing bootstrap_project with configure_project. (#1796)
- Fix posix path conversion on Windows in DatasetStatsHook. (#1843)
- Add `pydantic` pin to requirements. (#1861)
- Fix TRANSCODING_SEPARATOR import error. (#1866)
Expand Down
5 changes: 3 additions & 2 deletions package/kedro_viz/integrations/kedro/data_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
load data from projects created in a range of Kedro versions.
"""

# pylint: disable=protected-access
# pylint: disable=import-outside-toplevel, protected-access

import json
import logging
Expand All @@ -14,7 +14,6 @@
from kedro.framework.project import configure_project, pipelines
from kedro.framework.session import KedroSession
from kedro.framework.session.store import BaseSessionStore
from kedro.framework.startup import bootstrap_project
from kedro.io import DataCatalog
from kedro.pipeline import Pipeline

Expand Down Expand Up @@ -92,6 +91,8 @@ def load_data(
if package_name:
configure_project(package_name)
else:
from kedro.framework.startup import bootstrap_project

# bootstrap project when viz is run in dev mode
bootstrap_project(project_path)

Expand Down
23 changes: 3 additions & 20 deletions package/kedro_viz/launchers/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@
from kedro_viz.integrations.deployment.deployer_factory import DeployerFactory
from kedro_viz.integrations.pypi import get_latest_version, is_running_outdated_version
from kedro_viz.launchers.utils import (
_PYPROJECT,
_check_viz_up,
_find_kedro_project,
_start_browser,
_wait_for,
viz_deploy_progress_timer,
Expand Down Expand Up @@ -131,22 +129,6 @@ def run(
"""Launch local Kedro Viz instance"""
from kedro_viz.server import run_server

kedro_project_path = _find_kedro_project(Path.cwd())

if kedro_project_path is None:
display_cli_message(
"ERROR: Failed to start Kedro-Viz : "
"Could not find the project configuration "
f"file '{_PYPROJECT}' in '{Path.cwd()}'. "
f"If you have created your project with Kedro "
f"version <0.17.0, make sure to update your project template. "
f"See https://github.com/kedro-org/kedro/blob/main/RELEASE.md"
f"#migration-guide-from-kedro-016-to-kedro-0170 "
f"for how to migrate your Kedro project.",
"red",
)
return

installed_version = parse(__version__)
latest_version = get_latest_version()
if is_running_outdated_version(installed_version, latest_version):
Expand All @@ -170,15 +152,16 @@ def run(
"save_file": save_file,
"pipeline_name": pipeline,
"env": env,
"project_path": kedro_project_path,
"autoreload": autoreload,
"include_hooks": include_hooks,
"package_name": PACKAGE_NAME,
"extra_params": params,
}
if autoreload:
project_path = Path.cwd()
run_server_kwargs["project_path"] = project_path
run_process_kwargs = {
"path": kedro_project_path,
"path": project_path,
"target": run_server,
"kwargs": run_server_kwargs,
"watcher_cls": RegExpWatcher,
Expand Down
24 changes: 1 addition & 23 deletions package/kedro_viz/launchers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@

import logging
import webbrowser
from pathlib import Path
from time import sleep, time
from typing import Any, Callable, Union
from typing import Any, Callable

import requests

logger = logging.getLogger(__name__)
_PYPROJECT = "pyproject.toml"


class WaitForException(Exception):
Expand Down Expand Up @@ -107,23 +105,3 @@ def viz_deploy_progress_timer(process_completed, timeout):
)
sleep(1)
elapsed_time += 1


def _is_project(project_path: Union[str, Path]) -> bool:
metadata_file = Path(project_path).expanduser().resolve() / _PYPROJECT
if not metadata_file.is_file():
return False

try:
return "[tool.kedro]" in metadata_file.read_text(encoding="utf-8")
# pylint: disable=broad-exception-caught
except Exception:
return False


def _find_kedro_project(current_dir: Path) -> Any:
paths_to_check = [current_dir] + list(current_dir.parents)
for project_dir in paths_to_check:
if _is_project(project_dir):
return project_dir
return None
60 changes: 3 additions & 57 deletions package/tests/test_launchers/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
from kedro_viz import __version__
from kedro_viz.constants import SHAREABLEVIZ_SUPPORTED_PLATFORMS, VIZ_DEPLOY_TIME_LIMIT
from kedro_viz.launchers import cli
from kedro_viz.launchers.utils import _PYPROJECT
from kedro_viz.server import run_server


Expand Down Expand Up @@ -86,7 +85,6 @@ def mock_project_path(mocker):
"save_file": None,
"pipeline_name": None,
"env": None,
"project_path": "testPath",
"autoreload": False,
"include_hooks": False,
"package_name": None,
Expand All @@ -102,7 +100,6 @@ def mock_project_path(mocker):
"save_file": None,
"pipeline_name": None,
"env": None,
"project_path": "testPath",
"autoreload": False,
"include_hooks": False,
"package_name": None,
Expand All @@ -123,7 +120,6 @@ def mock_project_path(mocker):
"save_file": None,
"pipeline_name": None,
"env": None,
"project_path": "testPath",
"autoreload": False,
"include_hooks": False,
"package_name": None,
Expand Down Expand Up @@ -155,7 +151,6 @@ def mock_project_path(mocker):
"save_file": "save_dir",
"pipeline_name": "data_science",
"env": "local",
"project_path": "testPath",
"autoreload": False,
"include_hooks": False,
"package_name": None,
Expand All @@ -171,7 +166,6 @@ def mock_project_path(mocker):
"save_file": None,
"pipeline_name": None,
"env": None,
"project_path": "testPath",
"autoreload": False,
"include_hooks": True,
"package_name": None,
Expand All @@ -191,11 +185,6 @@ def test_kedro_viz_command_run_server(
runner = CliRunner()
# Reduce the timeout argument from 600 to 1 to make test run faster.
mocker.patch("kedro_viz.launchers.cli._wait_for.__defaults__", (True, 1, True, 1))
# Mock finding kedro project
mocker.patch(
"kedro_viz.launchers.cli._find_kedro_project",
return_value=run_server_args["project_path"],
)

with runner.isolated_filesystem():
runner.invoke(cli.viz_cli, command_options)
Expand All @@ -206,35 +195,8 @@ def test_kedro_viz_command_run_server(
assert run_server_args["port"] in cli._VIZ_PROCESSES


def test_kedro_viz_command_should_log_project_not_found(
mocker, mock_project_path, mock_click_echo
):
# Reduce the timeout argument from 600 to 1 to make test run faster.
mocker.patch("kedro_viz.launchers.cli._wait_for.__defaults__", (True, 1, True, 1))
# Mock finding kedro project
mocker.patch("kedro_viz.launchers.cli._find_kedro_project", return_value=None)
runner = CliRunner()
with runner.isolated_filesystem():
runner.invoke(cli.viz_cli, ["viz", "run"])

mock_click_echo_calls = [
call(
"\x1b[31mERROR: Failed to start Kedro-Viz : "
"Could not find the project configuration "
f"file '{_PYPROJECT}' in '{mock_project_path}'. "
f"If you have created your project with Kedro "
f"version <0.17.0, make sure to update your project template. "
f"See https://github.com/kedro-org/kedro/blob/main/RELEASE.md"
f"#migration-guide-from-kedro-016-to-kedro-0170 "
f"for how to migrate your Kedro project.\x1b[0m"
)
]

mock_click_echo.assert_has_calls(mock_click_echo_calls)


def test_kedro_viz_command_should_log_outdated_version(
mocker, mock_http_response, mock_click_echo, mock_project_path
mocker, mock_http_response, mock_click_echo
):
installed_version = parse(__version__)
mock_version = f"{installed_version.major + 1}.0.0"
Expand All @@ -247,10 +209,6 @@ def test_kedro_viz_command_should_log_outdated_version(

# Reduce the timeout argument from 600 to 1 to make test run faster.
mocker.patch("kedro_viz.launchers.cli._wait_for.__defaults__", (True, 1, True, 1))
# Mock finding kedro project
mocker.patch(
"kedro_viz.launchers.cli._find_kedro_project", return_value=mock_project_path
)
runner = CliRunner()
with runner.isolated_filesystem():
runner.invoke(cli.viz_cli, ["viz", "run"])
Expand All @@ -270,7 +228,7 @@ def test_kedro_viz_command_should_log_outdated_version(


def test_kedro_viz_command_should_not_log_latest_version(
mocker, mock_http_response, mock_click_echo, mock_project_path
mocker, mock_http_response, mock_click_echo
):
requests_get = mocker.patch("requests.get")
requests_get.return_value = mock_http_response(
Expand All @@ -280,10 +238,6 @@ def test_kedro_viz_command_should_not_log_latest_version(
mocker.patch("kedro_viz.server.run_server")
# Reduce the timeout argument from 600 to 1 to make test run faster.
mocker.patch("kedro_viz.launchers.cli._wait_for.__defaults__", (True, 1, True, 1))
# Mock finding kedro project
mocker.patch(
"kedro_viz.launchers.cli._find_kedro_project", return_value=mock_project_path
)
runner = CliRunner()
with runner.isolated_filesystem():
runner.invoke(cli.viz_cli, ["viz", "run"])
Expand All @@ -294,18 +248,14 @@ def test_kedro_viz_command_should_not_log_latest_version(


def test_kedro_viz_command_should_not_log_if_pypi_is_down(
mocker, mock_http_response, mock_click_echo, mock_project_path
mocker, mock_http_response, mock_click_echo
):
requests_get = mocker.patch("requests.get")
requests_get.side_effect = requests.exceptions.RequestException("PyPI is down")

mocker.patch("kedro_viz.server.run_server")
# Reduce the timeout argument from 600 to 1 to make test run faster.
mocker.patch("kedro_viz.launchers.cli._wait_for.__defaults__", (True, 1, True, 1))
# Mock finding kedro project
mocker.patch(
"kedro_viz.launchers.cli._find_kedro_project", return_value=mock_project_path
)
runner = CliRunner()
with runner.isolated_filesystem():
runner.invoke(cli.viz_cli, ["viz", "run"])
Expand All @@ -322,10 +272,6 @@ def test_kedro_viz_command_with_autoreload(

# Reduce the timeout argument from 600 to 1 to make test run faster.
mocker.patch("kedro_viz.launchers.cli._wait_for.__defaults__", (True, 1, True, 1))
# Mock finding kedro project
mocker.patch(
"kedro_viz.launchers.cli._find_kedro_project", return_value=mock_project_path
)
runner = CliRunner()
with runner.isolated_filesystem():
runner.invoke(cli.viz_cli, ["viz", "run", "--autoreload"])
Expand Down
50 changes: 0 additions & 50 deletions package/tests/test_launchers/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from pathlib import Path
from unittest import mock
from unittest.mock import Mock, call, patch

Expand All @@ -8,8 +7,6 @@
from kedro_viz.constants import VIZ_DEPLOY_TIME_LIMIT
from kedro_viz.launchers.utils import (
_check_viz_up,
_find_kedro_project,
_is_project,
_start_browser,
viz_deploy_progress_timer,
)
Expand Down Expand Up @@ -72,50 +69,3 @@ def test_viz_deploy_progress_timer(capsys):
for second in range(1, VIZ_DEPLOY_TIME_LIMIT + 1):
expected_output = f"...Creating your build/deploy Kedro-Viz ({second}s)"
assert expected_output in captured.out


class TestIsProject:
project_path = Path.cwd()

def test_no_metadata_file(self, mocker):
mocker.patch.object(Path, "is_file", return_value=False)

assert not _is_project(self.project_path)

def test_toml_invalid_format(self, tmp_path):
"""Test for loading context from an invalid path."""
toml_path = tmp_path / "pyproject.toml"
toml_path.write_text("!!") # Invalid TOML

assert not _is_project(tmp_path)

def test_non_kedro_project(self, mocker):
mocker.patch.object(Path, "is_file", return_value=True)
mocker.patch.object(Path, "read_text", return_value="[tool]")

assert not _is_project(self.project_path)

def test_valid_toml_file(self, mocker):
mocker.patch.object(Path, "is_file", return_value=True)
pyproject_toml_payload = "[tool.kedro]" # \nproject_name = 'proj'"
mocker.patch.object(Path, "read_text", return_value=pyproject_toml_payload)

assert _is_project(self.project_path)

def test_toml_bad_encoding(self, mocker):
mocker.patch.object(Path, "is_file", return_value=True)
mocker.patch.object(Path, "read_text", side_effect=UnicodeDecodeError)

assert not _is_project(self.project_path)


@pytest.mark.parametrize(
"project_dir, is_project_found, expected",
[
("/path/to/valid/project", True, Path("/path/to/valid/project")),
("/path/to/nonexistent/project", False, None),
],
)
def test_find_kedro_project(project_dir, is_project_found, expected, mocker):
mocker.patch("kedro_viz.launchers.utils._is_project", return_value=is_project_found)
assert _find_kedro_project(Path(project_dir)) == expected

0 comments on commit 4ca1bf8

Please sign in to comment.