Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ repos:
args: [-d, -i 4, -ci]

- repo: https://github.com/PyCQA/bandit
rev: 1.7.5
rev: 1.8.6
hooks:
- id: bandit

Expand Down
26 changes: 26 additions & 0 deletions ci/test
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,30 @@ run pre_commit_hooks/yamlfmt.py --width 79 "${TMP_FILE}"
run grep -v -q '.\{82\}' "${TMP_FILE}"
run rm -f "${TMP_FILE}"

# Test line endings
# File command will say nothing about line terminators for LF, but will for CRLF and CR endings
TMP_FILE="$(mktemp pre-commit-hook-yamlfmt-test.XXXXXXXXXXXXXXXXXXXX)"
run cp -f ci/test-eolchars.txt "${TMP_FILE}"
run pre_commit_hooks/yamlfmt.py --mapping 4 --sequence 6 --offset 4 --preserve-quotes "${TMP_FILE}"
run grep -q -v "CRLF line terminators" <(file -b "${TMP_FILE}")
run grep -q -v "CR line terminators" <(file -b "${TMP_FILE}")
# No line-endings argument passed - should be LF only when run on *nix plaftorms
run cp -f ci/test-eolchars.txt "${TMP_FILE}"
run pre_commit_hooks/yamlfmt.py --mapping 4 --sequence 6 --offset 4 --preserve-quotes --line-endings lf "${TMP_FILE}"
run grep -q -v 'CRLF line terminators' <(file -b "${TMP_FILE}")
run grep -q -v 'CR line terminators' <(file -b "${TMP_FILE}")
# LF line-endings argument passed - Should be not CR and not CRLF
run cp -f ci/test-eolchars.txt "${TMP_FILE}"
run pre_commit_hooks/yamlfmt.py --mapping 4 --sequence 6 --offset 4 --preserve-quotes --line-endings crlf "${TMP_FILE}"
run grep -q 'CRLF line terminators' <(file -b "${TMP_FILE}")
run grep -q -v 'CR line terminators' <(file -b "${TMP_FILE}")
# CRLF argument passed - Should be CRLF line terminators
run cp -f ci/test-eolchars.txt "${TMP_FILE}"
run pre_commit_hooks/yamlfmt.py --mapping 4 --sequence 6 --offset 4 --preserve-quotes --line-endings cr "${TMP_FILE}"
run grep -q -v 'CRLF line terminators' <(file -b "${TMP_FILE}")
run grep -q 'CR line terminators' <(file -b "${TMP_FILE}")
# CR argument passed - Should be CR line terminators
# If the tests fail, the temp file is still there to inspect.
run rm -f "${TMP_FILE}"

run pre-commit run --all-files --hook-stage manual
5 changes: 5 additions & 0 deletions ci/test-eolchars.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
bar:
baz:
- blah
- phfft
21 changes: 19 additions & 2 deletions pre_commit_hooks/yamlfmt.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,13 @@ def __init__(self):
action="store_true",
help="whether to keep null values"
)
parser.add_argument(
"--line-endings",
choices=['lf', 'crlf', 'cr'],
default="lf",
help="write output files with specific EOL (default EOL depends"
" on OS)"
)
parser.add_argument(
"file_names",
metavar="FILE_NAME",
Expand Down Expand Up @@ -139,6 +146,14 @@ def represent_none(self, _data):
return self.represent_scalar('tag:yaml.org,2002:null', 'null')
yaml.representer.add_representer(type(None), represent_none)

line_endings_map = {
"lf": "\n",
"crlf": "\r\n",
"cr": "\r"
}
self.line_endings = line_endings_map.get(
kwargs.get("line_endings", None), None)

self.yaml = yaml
self.path = kwargs.get("path", None)
self.content = list({})
Expand Down Expand Up @@ -167,7 +182,8 @@ def write_file(self, path=None):
if not path:
path = self.path
try:
with open(path, "w", encoding='utf-8') as stream:
with open(path, "w", encoding='utf-8',
newline=self.line_endings) as stream:
self.yaml.dump_all(self.content, stream)
except IOError:
self.fail(f"Unable to write {path}")
Expand All @@ -191,7 +207,8 @@ def main():
preserve_quotes=args.preserve_quotes,
preserve_null=args.preserve_null,
explicit_start=args.explicit_start,
explicit_end=args.explicit_end
explicit_end=args.explicit_end,
line_endings=args.line_endings
)
for file_name in args.file_names:
formatter.format(file_name)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
name='yamlfmt',
description='A pre-commit hook to format YAML files',
url='https://github.com/jumanjihouse/pre-commit-hook-yamlfmt',
version='0.3.0',
version='0.4.0',

install_requires=[
'ruamel.yaml >=0.16.10, <=0.17.21',
Expand Down