def initialize(self): self._app = Linter(config=FluffConfig.from_root())
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