Skip to content

Commit

Permalink
Merge pull request #2120 from nchammas/dbt-debug-empty-project
Browse files Browse the repository at this point in the history
Correctly handle an empty dbt_project.yml in `dbt debug`
  • Loading branch information
beckjake authored Feb 18, 2020
2 parents 542a653 + 5dc9393 commit ea4948f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 8 deletions.
6 changes: 6 additions & 0 deletions core/dbt/config/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
17 changes: 9 additions & 8 deletions core/dbt/task/debug.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 = (
Expand Down
12 changes: 12 additions & 0 deletions test/integration/049_dbt_debug_test/test_debug.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ea4948f

Please sign in to comment.