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

#177: Forbid to use CRLF end-of-line #708

Merged
10 changes: 4 additions & 6 deletions dotenv_linter/violations/values.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,14 @@ class QuotedValueViolation(BaseFSTViolation):
@final
class InvalidEOLViolation(BaseFSTViolation):
"""
Restricts to use `/r/n` (CRLF) end-of-line.
Restricts to use `\r\n` (CRLF) end-of-line.

Reasoning:
???
Mixing different end-of-line chars can lead to different hard-to-debug problems.

Solution:
Use `/n` (LF) end-of-line.

Example::
??? What to add? EOL is not visible
Use `\n` (LF) end-of-line.
Another option is to add line `text eol=lf` to `.gitattributes`.

.. versionadded:: 0.6.0

Expand Down
8 changes: 4 additions & 4 deletions dotenv_linter/visitors/fst/values.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import final
from typing import Final, final

from dotenv_linter.grammar.fst import Value
from dotenv_linter.violations.values import (
Expand All @@ -8,6 +8,8 @@
)
from dotenv_linter.visitors.base import BaseFSTVisitor

CRLF_EOL: Final[str] = '\r'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
CRLF_EOL: Final[str] = '\r'
CRLF_EOL: Final = '\r'



@final
class ValueVisitor(BaseFSTVisitor):
Expand Down Expand Up @@ -41,7 +43,5 @@ def _check_value_quotes(self, node: Value) -> None:
self._add_violation(QuotedValueViolation(node, text=node.raw_text))

def _is_crlf_eol_used(self, node: Value) -> None:
crlf_eol = '\r'

if node.raw_text.endswith(crlf_eol):
if node.raw_text.endswith(CRLF_EOL):
self._add_violation(InvalidEOLViolation(node, text=node.text))
3 changes: 1 addition & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ addopts =
--strict-markers
--strict-config
--doctest-modules
# Disable with option to allow debugging
; --cov=dotenv_linter
--cov=dotenv_linter
--cov-branch
--cov-report=term:skip-covered
--cov-report=html
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/.env.correct
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
# See: https://github.com/wemake-services/dotenv-linter/issues/20
TEST=1
EMPTY=
VARIABLE_WITH_TRAILING_SLASH_R=value/r
VARIABLE_WITH_TRAILING_SLASH_R=value\r
1 change: 0 additions & 1 deletion tests/fixtures/.env.eol

This file was deleted.

13 changes: 11 additions & 2 deletions tests/test_cli/test_lint_command.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import os

import subprocess
from collections.abc import Callable

Expand Down Expand Up @@ -82,15 +84,20 @@ def test_lint_wrong_fixture(fixture_path, all_violations):
assert str(violation_class.code) in stderr


def test_lint_wrong_eol(fixture_path: Callable) -> None:
def test_lint_wrong_eol(fixture_path: Callable[[str], str]) -> None:
"""Checks that `lint` command works for with with CRLF end-of-line."""
temp_file_path = fixture_path('.env.temp')
with open(temp_file_path, mode='w') as temp_file:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, use tempfile module instead here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've used tmp_path pytest fixture instead, tempfile.NamedTemporaryFile didn't work out

_ = temp_file.write("VARIABLE_WITH_CRLF_EOL=123\r\n")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
_ = temp_file.write("VARIABLE_WITH_CRLF_EOL=123\r\n")
temp_file.write("VARIABLE_WITH_CRLF_EOL=123\r\n")


process = subprocess.Popen(
[
'dotenv-linter',
fixture_path('.env.eol'),
fixture_path(temp_file_path),
],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
# It is important to set this to `False`, otherwise eol are normalized
universal_newlines=False,
encoding='utf8',
)
Expand All @@ -99,3 +106,5 @@ def test_lint_wrong_eol(fixture_path: Callable) -> None:
assert process.returncode == 1

assert str(InvalidEOLViolation.code) in stderr

os.remove(temp_file_path)