Skip to content

Commit

Permalink
Fix bug where "encoding" setting in .sqlfluff file was not being resp…
Browse files Browse the repository at this point in the history
…ected (#3170)

Co-authored-by: Barry Hart <barry.hart@mailchimp.com>
Co-authored-by: Barry Pollard <barry_pollard@hotmail.com>
  • Loading branch information
3 people committed Apr 25, 2022
1 parent 6c026c7 commit 3793f35
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 30 deletions.
2 changes: 1 addition & 1 deletion src/sqlfluff/cli/commands.py
Expand Up @@ -247,7 +247,7 @@ def core_options(f: Callable) -> Callable:
)(f)
f = click.option(
"--encoding",
default="autodetect",
default=None,
help=(
"Specify encoding to use when reading and writing files. Defaults to "
"autodetect."
Expand Down
54 changes: 25 additions & 29 deletions test/cli/commands_test.py
Expand Up @@ -1256,44 +1256,40 @@ def test_encoding(encoding_in, encoding_out):
)


def test_cli_pass_on_correct_encoding_argument():
@pytest.mark.parametrize(
"encoding,method,expect_success",
[
("utf-8", "command-line", False),
("utf-8-SIG", "command-line", True),
("utf-8", "config-file", False),
("utf-8-SIG", "config-file", True),
],
)
def test_cli_encoding(encoding, method, expect_success, tmpdir):
"""Try loading a utf-8-SIG encoded file using the correct encoding via the cli."""
sql_path = "test/fixtures/cli/encoding_test.sql"
if method == "command-line":
options = [sql_path, "--encoding", encoding]
else:
assert method == "config-file"
with open(str(tmpdir / ".sqlfluff"), "w") as f:
print(f"[sqlfluff]\ndialect=ansi\nencoding = {encoding}", file=f)
shutil.copy(sql_path, tmpdir)
options = [str(tmpdir / "encoding_test.sql")]
result = invoke_assert_code(
ret_code=65,
args=[
lint,
[
"test/fixtures/cli/encoding_test.sql",
"--encoding",
"utf-8-SIG",
],
],
)
raw_output = repr(result.output)

# Incorrect encoding raises paring and lexer errors.
assert r"L: 1 | P: 1 | LXR |" not in raw_output
assert r"L: 1 | P: 1 | PRS |" not in raw_output


def test_cli_fail_on_wrong_encoding_argument():
"""Try loading a utf-8-SIG encoded file using the wrong encoding via the cli."""
result = invoke_assert_code(
ret_code=65,
args=[
lint,
[
"test/fixtures/cli/encoding_test.sql",
"--encoding",
"utf-8",
],
options,
],
)
raw_output = repr(result.output)

# Incorrect encoding raises paring and lexer errors.
assert r"L: 1 | P: 1 | LXR |" in raw_output
assert r"L: 1 | P: 1 | PRS |" in raw_output
# Incorrect encoding raises parsing and lexer errors.
success1 = r"L: 1 | P: 1 | LXR |" not in raw_output
success2 = r"L: 1 | P: 1 | PRS |" not in raw_output
assert success1 == expect_success
assert success2 == expect_success


def test_cli_no_disable_noqa_flag():
Expand Down

0 comments on commit 3793f35

Please sign in to comment.