From 5dc939355afe6f15bde2b531a3d66feed074b905 Mon Sep 17 00:00:00 2001 From: Nicholas Chammas Date: Tue, 11 Feb 2020 13:23:14 -0500 Subject: [PATCH] handle empty dbt_project --- core/dbt/config/project.py | 6 ++++++ core/dbt/task/debug.py | 17 +++++++++-------- .../049_dbt_debug_test/test_debug.py | 12 ++++++++++++ 3 files changed, 27 insertions(+), 8 deletions(-) diff --git a/core/dbt/config/project.py b/core/dbt/config/project.py index 7a0060d1333..ab2e14d37fd 100644 --- a/core/dbt/config/project.py +++ b/core/dbt/config/project.py @@ -188,6 +188,12 @@ def _raw_project_from(project_root: str) -> Dict[str, Any]: ) project_dict = _load_yaml(project_yaml_filepath) + + if not isinstance(project_dict, dict): + raise DbtProjectError( + 'dbt_project.yml does not parse to a dictionary' + ) + return project_dict diff --git a/core/dbt/task/debug.py b/core/dbt/task/debug.py index 8e15fca73a1..5456583f8f7 100644 --- a/core/dbt/task/debug.py +++ b/core/dbt/task/debug.py @@ -182,10 +182,10 @@ def _choose_profile_names(self) -> Optional[List[str]]: project_profile: Optional[str] = None if os.path.exists(self.project_path): try: - project_profile = load_yaml_text( - dbt.clients.system.load_file_contents(self.project_path) - ).get('profile') - except dbt.exceptions.Exception: + project_profile = Project.partial_load( + os.path.dirname(self.project_path) + ).profile_name + except dbt.exceptions.DbtProjectError: pass args_profile: Optional[str] = getattr(self.args, 'profile', None) @@ -196,19 +196,20 @@ def _choose_profile_names(self) -> Optional[List[str]]: pass # try to guess + profiles = [] if self.raw_profile_data: profiles = [k for k in self.raw_profile_data if k != 'config'] - if len(profiles) == 0: + if project_profile is None: + self.messages.append('Could not load dbt_project.yml') + elif len(profiles) == 0: self.messages.append('The profiles.yml has no profiles') elif len(profiles) == 1: self.messages.append(ONLY_PROFILE_MESSAGE.format(profiles[0])) - return profiles else: self.messages.append(MULTIPLE_PROFILE_MESSAGE.format( '\n'.join(' - {}'.format(o) for o in profiles) )) - return profiles - return None + return profiles def _choose_target_name(self, profile_name: str): has_raw_profile = ( diff --git a/test/integration/049_dbt_debug_test/test_debug.py b/test/integration/049_dbt_debug_test/test_debug.py index 1f41a75ee77..131accee723 100644 --- a/test/integration/049_dbt_debug_test/test_debug.py +++ b/test/integration/049_dbt_debug_test/test_debug.py @@ -91,6 +91,18 @@ def models(self): def capsys(self, capsys): self.capsys = capsys + @use_profile('postgres') + def test_postgres_empty_project(self): + with open('dbt_project.yml', 'w') as f: + pass + self.run_dbt(['debug', '--profile', 'test']) + splitout = self.capsys.readouterr().out.split('\n') + for line in splitout: + if line.strip().startswith('dbt_project.yml file'): + self.assertIn('ERROR invalid', line) + elif line.strip().startswith('profiles.yml file'): + self.assertNotIn('ERROR invalid', line) + @use_profile('postgres') def test_postgres_badproject(self): # load a special project that is an error