Skip to content

Commit

Permalink
Merge branch 'master' into fix/issues-542
Browse files Browse the repository at this point in the history
  • Loading branch information
ndrwnaguib authored Oct 16, 2023
2 parents 6545017 + f63e56f commit 809eeca
Show file tree
Hide file tree
Showing 15 changed files with 123 additions and 141 deletions.
5 changes: 2 additions & 3 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v4
- run:
Expand All @@ -38,14 +38,13 @@ jobs:
fail-fast: false
matrix:
python-version:
- '3.7'
- '3.8'
- '3.9'
- '3.10'
- '3.11'
steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
with:
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "yamllint"
description = "A linter for YAML files."
readme = {file = "README.rst", content-type = "text/x-rst"}
requires-python = ">=3.7"
requires-python = ">=3.8"
license = {text = "GPL-3.0-or-later"}
authors = [{name = "Adrien Vergé"}]
keywords = ["yaml", "lint", "linter", "syntax", "checker"]
Expand Down
130 changes: 64 additions & 66 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def utf8_available():
class CommandLineTestCase(unittest.TestCase):
@classmethod
def setUpClass(cls):
super(CommandLineTestCase, cls).setUpClass()
super().setUpClass()

cls.wd = build_temp_workspace({
# .yaml file at root
Expand Down Expand Up @@ -97,7 +97,7 @@ def setUpClass(cls):
'- hétérogénéité\n'
'# 19.99 €\n'
'- お早う御座います。\n'
'# الأَبْجَدِيَّة العَرَبِيَّة\n').encode('utf-8'),
'# الأَبْجَدِيَّة العَرَبِيَّة\n').encode(),
# dos line endings yaml
'dos.yml': '---\r\n'
'dos: true',
Expand All @@ -112,7 +112,7 @@ def setUpClass(cls):

@classmethod
def tearDownClass(cls):
super(CommandLineTestCase, cls).tearDownClass()
super().tearDownClass()

shutil.rmtree(cls.wd)

Expand Down Expand Up @@ -286,8 +286,8 @@ def test_run_with_implicit_extends_config(self):

with RunContext(self) as ctx:
cli.run(('-d', 'default', '-f', 'parsable', path))
expected_out = ('%s:1:1: [warning] missing document start "---" '
'(document-start)\n' % path)
expected_out = (f'{path}:1:1: [warning] missing document start "---" '
f'(document-start)\n')
self.assertEqual(
(ctx.returncode, ctx.stdout, ctx.stderr), (0, expected_out, ''))

Expand Down Expand Up @@ -424,9 +424,9 @@ def test_run_one_problem_file(self):
cli.run(('-f', 'parsable', path))
self.assertEqual(ctx.returncode, 1)
self.assertEqual(ctx.stdout, (
'%s:2:4: [error] trailing spaces (trailing-spaces)\n'
'%s:3:4: [error] no new line character at the end of file '
'(new-line-at-end-of-file)\n' % (path, path)))
f'{path}:2:4: [error] trailing spaces (trailing-spaces)\n'
f'{path}:3:4: [error] no new line character at the end of file '
f'(new-line-at-end-of-file)\n'))
self.assertEqual(ctx.stderr, '')

def test_run_one_warning(self):
Expand Down Expand Up @@ -476,8 +476,8 @@ def test_run_multiple_files(self):
cli.run(['-f', 'parsable'] + items)
self.assertEqual((ctx.returncode, ctx.stderr), (1, ''))
self.assertEqual(ctx.stdout, (
'%s:3:1: [error] duplication of key "key" in mapping '
'(key-duplicates)\n') % path)
f'{path}:3:1: [error] duplication of key "key" in mapping '
f'(key-duplicates)\n'))

def test_run_piped_output_nocolor(self):
path = os.path.join(self.wd, 'a.yaml')
Expand All @@ -486,11 +486,11 @@ def test_run_piped_output_nocolor(self):
cli.run((path, ))
self.assertEqual((ctx.returncode, ctx.stderr), (1, ''))
self.assertEqual(ctx.stdout, (
'%s\n'
' 2:4 error trailing spaces (trailing-spaces)\n'
' 3:4 error no new line character at the end of file '
'(new-line-at-end-of-file)\n'
'\n' % path))
f'{path}\n'
f' 2:4 error trailing spaces (trailing-spaces)\n'
f' 3:4 error no new line character at the end of file '
f'(new-line-at-end-of-file)\n'
f'\n'))

def test_run_default_format_output_in_tty(self):
path = os.path.join(self.wd, 'a.yaml')
Expand All @@ -517,25 +517,25 @@ def test_run_default_format_output_in_tty(self):
output.close()

self.assertEqual(out, (
'\033[4m%s\033[0m\n'
' \033[2m2:4\033[0m \033[31merror\033[0m '
'trailing spaces \033[2m(trailing-spaces)\033[0m\n'
' \033[2m3:4\033[0m \033[31merror\033[0m '
'no new line character at the end of file '
'\033[2m(new-line-at-end-of-file)\033[0m\n'
'\n' % path))
f'\033[4m{path}\033[0m\n'
f' \033[2m2:4\033[0m \033[31merror\033[0m '
f'trailing spaces \033[2m(trailing-spaces)\033[0m\n'
f' \033[2m3:4\033[0m \033[31merror\033[0m '
f'no new line character at the end of file '
f'\033[2m(new-line-at-end-of-file)\033[0m\n'
f'\n'))

def test_run_default_format_output_without_tty(self):
path = os.path.join(self.wd, 'a.yaml')

with RunContext(self) as ctx:
cli.run((path, ))
expected_out = (
'%s\n'
' 2:4 error trailing spaces (trailing-spaces)\n'
' 3:4 error no new line character at the end of file '
'(new-line-at-end-of-file)\n'
'\n' % path)
f'{path}\n'
f' 2:4 error trailing spaces (trailing-spaces)\n'
f' 3:4 error no new line character at the end of file '
f'(new-line-at-end-of-file)\n'
f'\n')
self.assertEqual(
(ctx.returncode, ctx.stdout, ctx.stderr), (1, expected_out, ''))

Expand All @@ -545,11 +545,11 @@ def test_run_auto_output_without_tty_output(self):
with RunContext(self) as ctx:
cli.run((path, '--format', 'auto'))
expected_out = (
'%s\n'
' 2:4 error trailing spaces (trailing-spaces)\n'
' 3:4 error no new line character at the end of file '
'(new-line-at-end-of-file)\n'
'\n' % path)
f'{path}\n'
f' 2:4 error trailing spaces (trailing-spaces)\n'
f' 3:4 error no new line character at the end of file '
f'(new-line-at-end-of-file)\n'
f'\n')
self.assertEqual(
(ctx.returncode, ctx.stdout, ctx.stderr), (1, expected_out, ''))

Expand All @@ -559,13 +559,13 @@ def test_run_format_colored(self):
with RunContext(self) as ctx:
cli.run((path, '--format', 'colored'))
expected_out = (
'\033[4m%s\033[0m\n'
' \033[2m2:4\033[0m \033[31merror\033[0m '
'trailing spaces \033[2m(trailing-spaces)\033[0m\n'
' \033[2m3:4\033[0m \033[31merror\033[0m '
'no new line character at the end of file '
'\033[2m(new-line-at-end-of-file)\033[0m\n'
'\n' % path)
f'\033[4m{path}\033[0m\n'
f' \033[2m2:4\033[0m \033[31merror\033[0m '
f'trailing spaces \033[2m(trailing-spaces)\033[0m\n'
f' \033[2m3:4\033[0m \033[31merror\033[0m '
f'no new line character at the end of file '
f'\033[2m(new-line-at-end-of-file)\033[0m\n'
f'\n')
self.assertEqual(
(ctx.returncode, ctx.stdout, ctx.stderr), (1, expected_out, ''))

Expand All @@ -575,10 +575,10 @@ def test_run_format_colored_warning(self):
with RunContext(self) as ctx:
cli.run((path, '--format', 'colored'))
expected_out = (
'\033[4m%s\033[0m\n'
' \033[2m1:1\033[0m \033[33mwarning\033[0m '
'missing document start "---" \033[2m(document-start)\033[0m\n'
'\n' % path)
f'\033[4m{path}\033[0m\n'
f' \033[2m1:1\033[0m \033[33mwarning\033[0m '
f'missing document start "---" \033[2m(document-start)\033[0m\n'
f'\n')
self.assertEqual(
(ctx.returncode, ctx.stdout, ctx.stderr), (0, expected_out, ''))

Expand All @@ -588,13 +588,12 @@ def test_run_format_github(self):
with RunContext(self) as ctx:
cli.run((path, '--format', 'github'))
expected_out = (
'::group::%s\n'
'::error file=%s,line=2,col=4::2:4 [trailing-spaces] trailing'
' spaces\n'
'::error file=%s,line=3,col=4::3:4 [new-line-at-end-of-file] no'
' new line character at the end of file\n'
'::endgroup::\n\n'
% (path, path, path))
f'::group::{path}\n'
f'::error file={path},line=2,col=4::2:4 [trailing-spaces] trailing'
f' spaces\n'
f'::error file={path},line=3,col=4::3:4 [new-line-at-end-of-file]'
f' no new line character at the end of file\n'
f'::endgroup::\n\n')
self.assertEqual(
(ctx.returncode, ctx.stdout, ctx.stderr), (1, expected_out, ''))

Expand All @@ -608,13 +607,12 @@ def test_github_actions_detection(self):
os.environ['GITHUB_WORKFLOW'] = 'something'
cli.run((path, ))
expected_out = (
'::group::%s\n'
'::error file=%s,line=2,col=4::2:4 [trailing-spaces] trailing'
' spaces\n'
'::error file=%s,line=3,col=4::3:4 [new-line-at-end-of-file] no'
' new line character at the end of file\n'
'::endgroup::\n\n'
% (path, path, path))
f'::group::{path}\n'
f'::error file={path},line=2,col=4::2:4 [trailing-spaces] trailing'
f' spaces\n'
f'::error file={path},line=3,col=4::3:4 [new-line-at-end-of-file]'
f' no new line character at the end of file\n'
f'::endgroup::\n\n')
self.assertEqual(
(ctx.returncode, ctx.stdout, ctx.stderr), (1, expected_out, ''))

Expand All @@ -640,11 +638,11 @@ def test_run_no_warnings(self):
with RunContext(self) as ctx:
cli.run((path, '--no-warnings', '-f', 'auto'))
expected_out = (
'%s\n'
' 2:4 error trailing spaces (trailing-spaces)\n'
' 3:4 error no new line character at the end of file '
'(new-line-at-end-of-file)\n'
'\n' % path)
f'{path}\n'
f' 2:4 error trailing spaces (trailing-spaces)\n'
f' 3:4 error no new line character at the end of file '
f'(new-line-at-end-of-file)\n'
f'\n')
self.assertEqual(
(ctx.returncode, ctx.stdout, ctx.stderr), (1, expected_out, ''))

Expand All @@ -671,10 +669,10 @@ def test_run_non_universal_newline(self):
with RunContext(self) as ctx:
cli.run(('-d', 'rules:\n new-lines:\n type: unix', path))
expected_out = (
'%s\n'
' 1:4 error wrong new line character: expected \\n'
' (new-lines)\n'
'\n' % path)
f'{path}\n'
f' 1:4 error wrong new line character: expected \\n'
f' (new-lines)\n'
f'\n')
self.assertEqual(
(ctx.returncode, ctx.stdout, ctx.stderr), (1, expected_out, ''))

Expand Down
2 changes: 1 addition & 1 deletion tests/test_spec_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
# text = text.replace('\u21d4', '') # byte order mark
# text = text.replace('\u2192', '\t') # right arrow
# text = text.replace('\u00b0', '') # empty scalar
# with open('tests/yaml-1.2-spec-examples/%s' % id, 'w',
# with open(f'tests/yaml-1.2-spec-examples/{id}', 'w',
# encoding='utf-8') as g:
# g.write(text)

Expand Down
43 changes: 16 additions & 27 deletions yamllint/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,52 +49,41 @@ def supports_color():
class Format:
@staticmethod
def parsable(problem, filename):
return ('%(file)s:%(line)s:%(column)s: [%(level)s] %(message)s' %
{'file': filename,
'line': problem.line,
'column': problem.column,
'level': problem.level,
'message': problem.message})
return (f'{filename}:{problem.line}:{problem.column}: '
f'[{problem.level}] {problem.message}')

@staticmethod
def standard(problem, filename):
line = ' %d:%d' % (problem.line, problem.column)
line = f' {problem.line}:{problem.column}'
line += max(12 - len(line), 0) * ' '
line += problem.level
line += max(21 - len(line), 0) * ' '
line += problem.desc
if problem.rule:
line += ' (%s)' % problem.rule
line += f' ({problem.rule})'
return line

@staticmethod
def standard_color(problem, filename):
line = ' \033[2m%d:%d\033[0m' % (problem.line, problem.column)
line = f' \033[2m{problem.line}:{problem.column}\033[0m'
line += max(20 - len(line), 0) * ' '
if problem.level == 'warning':
line += '\033[33m%s\033[0m' % problem.level
line += f'\033[33m{problem.level}\033[0m'
else:
line += '\033[31m%s\033[0m' % problem.level
line += f'\033[31m{problem.level}\033[0m'
line += max(38 - len(line), 0) * ' '
line += problem.desc
if problem.rule:
line += ' \033[2m(%s)\033[0m' % problem.rule
line += f' \033[2m({problem.rule})\033[0m'
return line

@staticmethod
def github(problem, filename):
line = '::'
line += problem.level
line += ' file=' + filename + ','
line += 'line=' + format(problem.line) + ','
line += 'col=' + format(problem.column)
line += '::'
line += format(problem.line)
line += ':'
line += format(problem.column)
line += ' '
line = f'::{problem.level} file={format(filename)},' \
f'line={format(problem.line)},col={format(problem.column)}' \
f'::{format(problem.line)}:{format(problem.column)} '
if problem.rule:
line += '[' + problem.rule + '] '
line += f'[{problem.rule}] '
line += problem.desc
return line

Expand All @@ -118,12 +107,12 @@ def show_problems(problems, file, args_format, no_warn):
print(Format.parsable(problem, file))
elif args_format == 'github':
if first:
print('::group::%s' % file)
print(f'::group::{file}')
first = False
print(Format.github(problem, file))
elif args_format == 'colored':
if first:
print('\033[4m%s\033[0m' % file)
print(f'\033[4m{file}\033[0m')
first = False
print(Format.standard_color(problem, file))
else:
Expand Down Expand Up @@ -184,7 +173,7 @@ def run(argv=None):
action='store_true',
help='output only error level problems')
parser.add_argument('-v', '--version', action='version',
version='{} {}'.format(APP_NAME, APP_VERSION))
version=f'{APP_NAME} {APP_VERSION}')

args = parser.parse_args(argv)

Expand All @@ -202,7 +191,7 @@ def run(argv=None):
try:
if args.config_data is not None:
if args.config_data != '' and ':' not in args.config_data:
args.config_data = 'extends: ' + args.config_data
args.config_data = f'extends: {args.config_data}'
conf = YamlLintConfig(content=args.config_data)
elif args.config_file is not None:
conf = YamlLintConfig(file=args.config_file)
Expand Down
Loading

0 comments on commit 809eeca

Please sign in to comment.