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

Read filenames using case insensitive extension #2263

Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions core/dbt/clients/system.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import json
import os
import os.path
import re
import shutil
import subprocess
import sys
Expand Down Expand Up @@ -40,6 +41,8 @@ def find_matching(
"""
matching = []
root_path = os.path.normpath(root_path)
regex = fnmatch.translate(file_pattern)
reobj = re.compile(regex, re.IGNORECASE)

for relative_path_to_search in relative_paths_to_search:
absolute_path_to_search = os.path.join(
Expand All @@ -52,8 +55,7 @@ def find_matching(
relative_path = os.path.relpath(
absolute_path, absolute_path_to_search
)

if fnmatch.fnmatch(local_file, file_pattern):
if reobj.match(local_file):
matching.append({
'searched_path': relative_path_to_search,
'absolute_path': absolute_path,
Expand Down
17 changes: 16 additions & 1 deletion core/dbt/parser/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
)
from dbt.exceptions import (
validator_error_message, JSONValidationException,
raise_invalid_schema_yml_version, ValidationException, CompilationException
raise_invalid_schema_yml_version, ValidationException,
CompilationException, warn_or_error
)
from dbt.node_types import NodeType
from dbt.parser.base import SimpleParser
Expand Down Expand Up @@ -130,6 +131,20 @@ def resource_type(self) -> NodeType:
return NodeType.Test

def get_paths(self):
# TODO: In order to support this, make FilesystemSearcher accept a list
# of file patterns. eg: ['.yml', '.yaml']
Comment on lines +134 to +135
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should be able to just return itertools.chain(yaml_files, yml_files). It won't be the most efficient thing in the world, but that's probably fine.

yaml_files = list(FilesystemSearcher(
self.project, self.project.all_source_paths, '.yaml'
))
if yaml_files:
warn_or_error(
'A future version of dbt will parse files with both'
' .yml and .yaml file extensions. dbt found'
f' {len(yaml_files)} files with .yaml extensions in'
' your dbt project. To avoid errors when upgrading'
' to a future release, either remove these files from'
' your dbt project, or change their extensions.'
)
return FilesystemSearcher(
self.project, self.project.all_source_paths, '.yml'
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
select 1 as id
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
version: 2

models:
- name: lowercase
columns:
- name: id
quote: true
tests:
- unique
- name: uppercase
columns:
- name: id
quote: true
tests:
- unique
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
select 1 as id
40 changes: 40 additions & 0 deletions test/integration/008_schema_tests_test/test_schema_v2_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,3 +361,43 @@ def test_postgres_argument_rendering(self):
results = self.run_dbt(['test', '--vars', '{myvar: foo}'])
self.assertEqual(len(results), 1)
self.run_dbt(['test'], expect_pass=False)


class TestSchemaCaseInsensitive(DBTIntegrationTest):
@property
def schema(self):
return "schema_tests_008"

@property
def models(self):
return "case-sensitive-models"

@use_profile('postgres')
def test_postgres_schema_lowercase_sql(self):
results = self.run_dbt(strict=False)
self.assertEqual(len(results), 2)
results = self.run_dbt(['test', '-m', 'lowercase'], strict=False)
self.assertEqual(len(results), 1)

@use_profile('postgres')
def test_postgres_schema_uppercase_sql(self):
results = self.run_dbt(strict=False)
self.assertEqual(len(results), 2)
results = self.run_dbt(['test', '-m', 'uppercase'], strict=False)
self.assertEqual(len(results), 1)


class TestSchemaYAMLExtension(DBTIntegrationTest):
@property
def schema(self):
return "schema_tests_008"

@property
def models(self):
return "case-sensitive-models"

@use_profile('postgres')
def test_postgres_yaml_extension(self):
with self.assertRaises(Exception) as exc:
self.run_dbt(["run"])
self.assertIn('yaml', str(exc.exception))
52 changes: 51 additions & 1 deletion test/unit/test_system_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import shutil
import stat
import unittest
from tempfile import mkdtemp
from tempfile import mkdtemp, NamedTemporaryFile

from dbt.exceptions import ExecutableError, WorkingDirectoryError
import dbt.clients.system
Expand Down Expand Up @@ -133,3 +133,53 @@ def test__ok(self):
out, err = dbt.clients.system.run_cmd(self.run_dir, self.exists_cmd)
self.assertEqual(out.strip(), b'hello')
self.assertEqual(err.strip(), b'')


class TestFindMatching(unittest.TestCase):

def setUp(self):
self.base_dir = mkdtemp()
self.tempdir = mkdtemp(dir=self.base_dir)

def test_find_matching_lowercase_file_pattern(self):
with NamedTemporaryFile(
prefix='sql-files', suffix='.sql', dir=self.tempdir
) as named_file:
file_path = os.path.dirname(named_file.name)
relative_path = os.path.basename(file_path)
out = dbt.clients.system.find_matching(
self.base_dir, [relative_path], '*.sql'
)
expected_output = [{
'searched_path': relative_path,
'absolute_path': named_file.name,
'relative_path': os.path.basename(named_file.name)
}]
self.assertEqual(out, expected_output)

def test_find_matching_uppercase_file_pattern(self):
with NamedTemporaryFile(prefix='sql-files', suffix='.SQL', dir=self.tempdir) as named_file:
file_path = os.path.dirname(named_file.name)
relative_path = os.path.basename(file_path)
out = dbt.clients.system.find_matching(
self.base_dir, [relative_path], '*.sql'
)
expected_output = [{
'searched_path': relative_path,
'absolute_path': named_file.name,
'relative_path': os.path.basename(named_file.name)
}]
self.assertEqual(out, expected_output)

def test_find_matching_file_pattern_not_found(self):
with NamedTemporaryFile(
prefix='sql-files', suffix='.SQLT', dir=self.tempdir
):
out = dbt.clients.system.find_matching(self.tempdir, [''], '*.sql')
self.assertEqual(out, [])

def tearDown(self):
try:
shutil.rmtree(self.base_dir)
except:
pass