Skip to content

Commit

Permalink
fix: Raise exception when meltano.yml is empty (meltano#6627)
Browse files Browse the repository at this point in the history
* Raise exception when empty meltano.yml

* tests for empty meltano.yml

* Update tests/meltano/cli/test_cli.py

Co-authored-by: Florian Hines <syn@ronin.io>

* remove unnecessary tests

Co-authored-by: Edgar R. M <edgarrm358@gmail.com>
Co-authored-by: Florian Hines <syn@ronin.io>
  • Loading branch information
3 people committed Aug 23, 2022
1 parent 39eddf1 commit 7454c39
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 6 deletions.
14 changes: 9 additions & 5 deletions src/meltano/core/error.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from enum import Enum


class ExitCode(int, Enum):
class ExitCode(int, Enum): # noqa: D101
OK = 0
FAIL = 1
NO_RETRY = 2
Expand All @@ -16,14 +16,14 @@ class ExitCode(int, Enum):
class Error(Exception):
"""Base exception for ELT errors."""

def exit_code(self):
def exit_code(self): # noqa: D102
return ExitCode.FAIL


class ExtractError(Error):
"""Error in the extraction process, like API errors."""

def exit_code(self):
def exit_code(self): # noqa: D102
return ExitCode.NO_RETRY


Expand All @@ -35,7 +35,7 @@ def __init__(
message: str,
process: Process,
stderr: str | None = None,
):
): # noqa: DAR101
"""Initialize AsyncSubprocessError."""
self.process = process
self._stderr: str | StreamReader | None = stderr or process.stderr
Expand All @@ -44,7 +44,7 @@ def __init__(
@property
async def stderr(self) -> str | None:
"""Return the output of the process to stderr."""
if not self._stderr:
if not self._stderr: # noqa: DAR201
return None
elif not isinstance(self._stderr, str):
stream = await self._stderr.read()
Expand All @@ -59,3 +59,7 @@ class PluginInstallError(Exception):

class PluginInstallWarning(Exception):
"""Exception for when a plugin optional optional step fails to install."""


class EmptyMeltanoFileException(Exception):
"""Exception for empty meltano.yml file."""
7 changes: 6 additions & 1 deletion src/meltano/core/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from meltano.core.plugin.base import PluginRef

from .behavior.versioned import Versioned
from .error import Error
from .error import EmptyMeltanoFileException, Error
from .project_files import ProjectFiles
from .utils import makedirs, truthy

Expand Down Expand Up @@ -227,6 +227,9 @@ def project_files(self):
def meltano(self) -> MeltanoFileHint:
"""Return a copy of the current meltano config.
Raises:
EmptyMeltanoFileException: For empty meltano.yml
Returns:
the current meltano config
"""
Expand All @@ -236,6 +239,8 @@ def meltano(self) -> MeltanoFileHint:

with open(self.meltanofile) as melt_ff:
meltano_ff: dict = configure_yaml().load(melt_ff)
if meltano_ff is None:
raise EmptyMeltanoFileException

uvicorn_enabled = (
meltano_ff.get(f"{FEATURE_FLAG_PREFIX}.{FeatureFlags.ENABLE_UVICORN}")
Expand Down
7 changes: 7 additions & 0 deletions tests/fixtures/fs.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ def test_dir(tmp_path_factory) -> Path:
os.chdir(cwd)


@pytest.fixture(scope="session")
def test_empty_meltano_yml(test_dir):
meltano_file = test_dir / "meltano.yml"
meltano_file.write_text("")
return test_dir


@pytest.fixture
def pushd(request):
def _pushd(path):
Expand Down
14 changes: 14 additions & 0 deletions tests/meltano/cli/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import meltano
from meltano.cli import cli
from meltano.core.error import EmptyMeltanoFileException
from meltano.core.project import PROJECT_READONLY_ENV, Project
from meltano.core.project_settings_service import ProjectSettingsService

Expand All @@ -28,6 +29,14 @@ def project(self, test_dir, project_init_service):
def deactivate_project(self):
Project.deactivate()

@pytest.fixture()
def empty_project(self, test_empty_meltano_yml, pushd):
project = Project(test_empty_meltano_yml)
try:
yield project
finally:
Project.deactivate()

def test_activate_project(self, project, cli_runner, pushd):
assert Project._default is None

Expand All @@ -38,6 +47,11 @@ def test_activate_project(self, project, cli_runner, pushd):
assert Project._default.root == project.root
assert Project._default.readonly is False

def test_empty_meltano_yml_project(self, empty_project, cli_runner, pushd):
pushd(empty_project.root)
with pytest.raises(EmptyMeltanoFileException):
cli_runner.invoke(cli, ["config"], catch_exceptions=False)

def test_activate_project_readonly_env(
self, project, cli_runner, pushd, monkeypatch
):
Expand Down

0 comments on commit 7454c39

Please sign in to comment.