Skip to content

Commit

Permalink
Add encoding CLI argument (#1994)
Browse files Browse the repository at this point in the history
  • Loading branch information
jpy-git committed Nov 29, 2021
1 parent 2c265f5 commit af1100e
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/sqlfluff/cli/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,13 @@ def core_options(f: Callable) -> Callable:
"the standard configuration files. N.B. cfg format is required."
),
)(f)
f = click.option(
"--encoding",
default="autodetect",
help=(
"Specifiy encoding to use when reading and writing files. Defaults to autodetect."
),
)(f)
f = click.option(
"--ignore",
default=None,
Expand Down
32 changes: 32 additions & 0 deletions test/cli/commands_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -840,6 +840,38 @@ def test_encoding(encoding_in, encoding_out):
)


def test_cli_pass_on_correct_encoding_argument():
"""Try loading a utf-8-SIG encoded file using the correct encoding via the cli."""
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"],
],
)
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


@patch(
"sqlfluff.core.linter.linter.progress_bar_configuration", disable_progress_bar=False
)
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/cli/encoding_test.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-- This file is encoded in utf-8-SIG
SELECT
foo
FROM bar; -- utf-8-SIG comment →

0 comments on commit af1100e

Please sign in to comment.