예제 #1
0
def test__linter__lint_string_vs_file(path):
    """Test the linter finds the same things on strings and files."""
    with open(path, 'r') as f:
        sql_str = f.read()
    lntr = Linter(config=FluffConfig())
    assert (lntr.lint_string(sql_str).check_tuples() == lntr.lint_path(
        path).check_tuples())
예제 #2
0
def test__rules__std_file(rule, path, violations):
    """Test the linter finds the given errors in (and only in) the right places."""
    # Use config to look for only the rule we care about.
    lntr = Linter(config=FluffConfig(overrides=dict(rules=rule)))
    lnt = lntr.lint_path(path)
    # Reformat the test data to match the format we're expecting. We use
    # sets because we really don't care about order and if one is missing,
    # we don't care about the orders of the correct ones.
    assert set(lnt.check_tuples()) == {(rule, v[0], v[1]) for v in violations}
예제 #3
0
def test__linter__lint_file_operators():
    lntr = Linter()
    lnt = lntr.lint_path('test/fixtures/linter/operator_errors.sql')
    # Check the Num violations command while we're here
    assert lnt.num_violations() == 3
    violations = lnt.check_tuples()
    # Check we get comma whitespace errors
    assert ('L006', 3, 9) in violations
    assert ('L006', 4, 8) in violations
    assert ('L007', 5, 8) in violations
예제 #4
0
def test__linter__lint_file_whitespace():
    lntr = Linter()
    lnt = lntr.lint_path('test/fixtures/linter/whitespace_errors.sql')
    violations = lnt.check_tuples()
    # Check we get comma (with leading space) whitespace errors
    # assert ('L005', 2, 8) in violations
    # assert ('L005', 4, 0) in violations
    # Check we get comma (with incorrect trailing space) whitespace errors
    assert ('L008', 3, 12) in violations
    # Check for no false positives on line 4 or 5
    assert not any([v[0] == 'L008' and v[1] == 4 for v in violations])
    assert not any([v[1] == 5 for v in violations])
예제 #5
0
def test__linter__lint_file_indentation():
    lntr = Linter()
    lnt = lntr.lint_path('test/fixtures/linter/indentation_errors.sql')
    violations = lnt.check_tuples()
    # Check we get the trialing whitespace violation
    assert ('L001', 4, 24) in violations
    # Check we get the mixed indentation errors
    assert ('L002', 3, 1) in violations
    assert ('L002', 4, 1) in violations
    # Check we get the space multiple violations
    assert ('L003', 3, 1) in violations
    # Check we get the mixed indentation errors between lines
    assert ('L004', 5, 1) in violations
예제 #6
0
def test__config__nested_config_tests():
    """Test linting with overriden config in nested paths.

    This looks like a linter test but it's actually a config
    test.
    """
    lntr = Linter(config=FluffConfig(overrides=dict(exclude_rules='L002')))
    lnt = lntr.lint_path('test/fixtures/config/inheritance_b')
    violations = lnt.check_tuples(by_path=True)
    for k in violations:
        if k.endswith('nested\\example.sql'):
            assert ('L003', 1, 4) in violations[k]
            assert ('L009', 1, 12) in violations[k]
            assert 'L002' not in [c[0] for c in violations[k]]
        elif k.endswith('inheritance_b\\example.sql'):
            assert ('L003', 1, 4) in violations[k]
            assert 'L002' not in [c[0] for c in violations[k]]
            assert 'L009' not in [c[0] for c in violations[k]]
예제 #7
0
def auto_fix_test(rules, dialect, folder):
    """A test for roundtrip testing, take a file buffer, lint, fix and lint.

    This is explicitly different from the linter version of this, in that
    it uses the command line rather than the direct api.
    """
    filename = 'testing.sql'
    # Lets get the path of a file to use
    tempdir_path = tempfile.mkdtemp()
    filepath = os.path.join(tempdir_path, filename)
    cfgpath = os.path.join(tempdir_path, '.sqlfluff')
    src_filepath = os.path.join(*base_auto_fix_path, dialect, folder,
                                'before.sql')
    cmp_filepath = os.path.join(*base_auto_fix_path, dialect, folder,
                                'after.sql')
    vio_filepath = os.path.join(*base_auto_fix_path, dialect, folder,
                                'violations.json')
    cfg_filepath = os.path.join(*base_auto_fix_path, dialect, folder,
                                '.sqlfluff')
    # Open the example file and write the content to it
    print_buff = ''
    with open(filepath, mode='w') as dest_file:
        with open(src_filepath, mode='r') as source_file:
            for line in source_file:
                dest_file.write(line)
                print_buff += line
    # Copy the config file too
    try:
        with open(cfgpath, mode='w') as dest_file:
            with open(cfg_filepath, mode='r') as source_file:
                for line in source_file:
                    dest_file.write(line)
    except FileNotFoundError:
        # No config file? No biggie
        pass
    print("## Input file:\n{0}".format(print_buff))
    # Do we need to do a violations check?
    try:
        with open(vio_filepath, mode='r') as vio_file:
            violations = json.load(vio_file)
    except FileNotFoundError:
        # No violations file. Let's not worry
        violations = None

    # Run the fix command
    cfg = FluffConfig.from_root(overrides=dict(rules=rules, dialect=dialect))
    lnt = Linter(config=cfg, output_func=lambda m: None)
    res = lnt.lint_path(filepath, fix=True)

    # If we have a violations structure, let's enforce it.
    if violations:
        vs = set(res.check_tuples())
        # Format the violations file
        expected_vs = set()
        for rule_key in violations["violations"]["linting"]:
            for elem in violations["violations"]["linting"][rule_key]:
                expected_vs.add((rule_key, *elem))
        assert expected_vs == vs

    # Actually do the fixes
    res = do_fixes(lnt, res)
    # Read the fixed file
    with open(filepath, mode='r') as fixed_file:
        fixed_buff = fixed_file.read()
    # Clearup once read
    shutil.rmtree(tempdir_path)
    # Read the comparison file
    with open(cmp_filepath, mode='r') as comp_file:
        comp_buff = comp_file.read()

    # Make sure we were successful
    assert res
    # Assert that we fixed as expected
    assert fixed_buff == comp_buff