-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[CT-756] Stringify user-provided messages to {{ log() }} #1
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ | |
from dbt.exceptions import ( | ||
CompilationException, | ||
MacroReturn, | ||
RuntimeException, | ||
raise_compiler_error, | ||
raise_parsing_error, | ||
disallow_secret_env_var, | ||
|
@@ -543,10 +544,11 @@ def zip_strict(*args: Iterable[Any]) -> Iterable[Any]: | |
|
||
@contextmember | ||
@staticmethod | ||
def log(msg: str, info: bool = False) -> str: | ||
def log(msg: Any, info: bool = False) -> str: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TODO: figure out / what is the workflow to get https://github.com/dbt-labs/docs.getdbt.com/blob/current/website/docs/reference/dbt-jinja-functions/log.md get updated from this? |
||
"""Logs a line to either the log file or stdout. | ||
|
||
:param msg: The message to log | ||
:param msg: The message to log. If not a string, then translated into | ||
a string via a call to the python builtin str() function. | ||
:param info: If `False`, write to the log file. If `True`, write to | ||
both the log file and stdout. | ||
|
||
|
@@ -556,6 +558,10 @@ def log(msg: str, info: bool = False) -> str: | |
{{ log("Running some_macro: " ~ arg1 ~ ", " ~ arg2) }} | ||
{% endmacro %}" | ||
""" | ||
try: | ||
msg = str(msg) | ||
except Exception as e: | ||
raise RuntimeException("log failed to stringify the given object") from e | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
if info: | ||
fire_event(MacroEventInfo(msg=msg)) | ||
else: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,7 +43,7 @@ def test__override_vars_global(self, project): | |
|
||
|
||
# This one switches to setting a var in 'test' | ||
class TestCLIVarOverridePorject: | ||
class TestCLIVarOverrideProject: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a completely unrelated change to fix this typo. I'm curious what the culture is about bundling in tiny unrelated changes like this. Is it fine or is it expected to be broken out to a separate PR? |
||
@pytest.fixture(scope="class") | ||
def models(self): | ||
return { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import pytest | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure if this is even the appropriate variation of test for this part of the codebase, I found this directory through randomly wandering, and then I copied adjacent files to get it to work. |
||
import os | ||
|
||
from dbt.tests.util import run_dbt, get_manifest, run_dbt_and_capture | ||
|
||
str_case_sql = """ | ||
{{ log('str987654321') }} | ||
select NULL | ||
""" | ||
|
||
int_case_sql = """ | ||
{{ log(987654321) }} | ||
select NULL | ||
""" | ||
|
||
float_case_sql = """ | ||
{{ log(9876.54321) }} | ||
select NULL | ||
""" | ||
|
||
list_case_sql = """ | ||
{{ log([9, 8, 7, 6, 5, 4, 3, 2, 1]) }} | ||
select NULL | ||
""" | ||
|
||
dict_case_sql = """ | ||
{{ log({9: 8}) }} | ||
select NULL | ||
""" | ||
|
||
|
||
class TestLogArbitraryTypes: | ||
@pytest.fixture(scope="class") | ||
def models(self): | ||
return { | ||
"str_case.sql": str_case_sql, | ||
"int_case.sql": int_case_sql, | ||
"float_case.sql": float_case_sql, | ||
"list_case.sql": list_case_sql, | ||
"dict_case.sql": dict_case_sql, | ||
} | ||
|
||
def test_log(self, project): | ||
# Induce dbt to try scrubbing logs, which fails if not given strings | ||
os.environ["DBT_ENV_SECRET_WHATEVER"] = "1234" | ||
os.environ["DBT_DEBUG"] = "True" | ||
_, log_output = run_dbt_and_capture(["run"]) | ||
|
||
# These aren't theoretically sound ways to verify the output | ||
# but they should be sufficient for this minor test. | ||
assert " str987654321\n" in log_output | ||
assert " 987654321\n" in log_output | ||
assert " 9876.54321\n" in log_output | ||
assert " [9, 8, 7, 6, 5, 4, 3, 2, 1]\n" in log_output | ||
assert " {9: 8}\n" in log_output |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a more specific/appropriate exception than
RuntimeException
?