Skip to content
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

fix #10422: add test for env_var casing on windows #10436

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,4 @@ venv/

# poetry
poetry.lock
.venv/
7 changes: 7 additions & 0 deletions core/dbt/context/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,13 @@
env = get_invocation_context().env
if var in env:
return_value = env[var]
elif os.name == "nt":

Check warning on line 317 in core/dbt/context/base.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/context/base.py#L317

Added line #L317 was not covered by tests
# Windows env vars are not case-sensitive
# So if there isn't an exact match, try a case-insensitive match
for key in env:
if var.casefold() == key.casefold():
return_value = env[key]
break

Check warning on line 323 in core/dbt/context/base.py

View check run for this annotation

Codecov / codecov/patch

core/dbt/context/base.py#L320-L323

Added lines #L320 - L323 were not covered by tests
elif default is not None:
return_value = default

Expand Down
64 changes: 64 additions & 0 deletions tests/functional/context_methods/test_env_vars_casing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import os

import pytest

from dbt.tests.util import run_dbt

context_sql = """

{{
config(
materialized='table'
)
}}

select
'{{ env_var("local_user", "") }}' as lowercase,
'{{ env_var("LOCAL_USER", "") }}' as uppercase,
'{{ env_var("lOcAl_UsEr", "") }}' as mixedcase
"""


class TestEnvVars:
@pytest.fixture(scope="class")
def models(self):
return {"context.sql": context_sql}

@pytest.fixture(scope="class", autouse=True)
def setup(self):
os.environ["local_user"] = "dan"
yield
del os.environ["local_user"]

def get_ctx_vars(self, project):
fields = [
"lowercase",
"uppercase",
"mixedcase",
]
field_list = ", ".join(['"{}"'.format(f) for f in fields])
query = "select {field_list} from {schema}.context".format(
field_list=field_list, schema=project.test_schema
)
vals = project.run_sql(query, fetch="all")
ctx = dict([(k, v) for (k, v) in zip(fields, vals[0])])
return ctx

def test_env_vars(
self,
project,
):
results = run_dbt(["run"])
assert len(results) == 1
ctx = self.get_ctx_vars(project)

assert ctx["lowercase"] == "dan"

# Windows env-vars are not case-sensitive, but Linux/macOS ones are
# So on Windows, the uppercase and mixedcase vars should also resolve to "dan"
if os.name == "nt":
assert ctx["uppercase"] == "dan"
assert ctx["mixedcase"] == "dan"
else:
assert ctx["uppercase"] == ""
assert ctx["mixedcase"] == ""
Loading