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

Allow global config for rule testcases #1580

Merged
merged 15 commits into from
Oct 12, 2021
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/sqlfluff/testing/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ def load_test_cases(
y = yaml.safe_load(raw)

rule = y.pop("rule")
global_config = y.pop("configs", None)
if global_config:
for i in y:
if not ("configs" in y[i].keys()):
y[i].update({"configs": global_config})
ids.extend([rule + "_" + t for t in y])
test_cases.extend([RuleTestCase(rule=rule, **v) for k, v in y.items()])

Expand Down
16 changes: 16 additions & 0 deletions test/fixtures/rules/R001_global_config_test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
rule: R001
configs:
core:
dialect: exasol


tc1:
pass_str: |
create table if not exists tab.xxx (col1 varchar(10))
configs:
core:
dialect: ansi

tc2:
pass_str: |
create table tab.xxx (col1 varchar(10))
12 changes: 12 additions & 0 deletions test/rules/yaml_test_cases_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,15 @@ def test__rule_test_case(test_case, caplog):
assert is_fix_compatible(
rule
), f'Rule {test_case.rule} returned fixes but does not specify "@document_fix_compatible".'


def test__rule_test_global_config():
"""Test global config in rule test cases."""
ids, test_cases = load_test_cases(
os.path.join("test/fixtures/rules/R001_global_config_test.yml")
)
assert len(test_cases) == 2
# tc1: overwrites global config
assert test_cases[0].configs["core"]["dialect"] == "ansi"
# tc2: global config is used
assert test_cases[1].configs["core"]["dialect"] == "exasol"