Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
MarkdownBear: Add validate-links plugin
Add `validate-links` plugin and add argument `filename`
because the`validate-links` plugin emits an error while
obtaining input from `stdin`.

Closes coala#924
  • Loading branch information
yash-nisar committed Jul 18, 2017
1 parent c9be1bf commit 27eef62
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
14 changes: 10 additions & 4 deletions bears/markdown/MarkdownBear.py
Expand Up @@ -8,7 +8,6 @@


@linter(executable='remark',
use_stdin=True,
use_stdout=True,
use_stderr=True)
class MarkdownBear:
Expand All @@ -21,7 +20,8 @@ class MarkdownBear:

LANGUAGES = {'Markdown'}
REQUIREMENTS = {NpmRequirement('remark-cli', '2'),
NpmRequirement('remark-lint', '5')}
NpmRequirement('remark-lint', '5'),
NpmRequirement('remark-validate-links', '5')}
AUTHORS = {'The coala developers'}
AUTHORS_EMAILS = {'coala-devel@googlegroups.com'}
LICENSE = 'AGPL-3.0'
Expand Down Expand Up @@ -65,7 +65,8 @@ def create_arguments(filename, file, config_file,
horizontal_rule: str='*',
horizontal_rule_spaces: bool=False,
horizontal_rule_repeat: int=3,
max_line_length: int=None):
max_line_length: int=None,
check_links: bool=False):
"""
:param bullets:
Character to use for bullets in lists. Can be "-", "*" or "+".
Expand Down Expand Up @@ -108,6 +109,8 @@ def create_arguments(filename, file, config_file,
The number of times the horizontal rule character will be repeated.
:param max_line_length:
The maximum line length allowed.
:param check_links:
Checks if links to headings and files in markdown are valid.
"""
remark_configs_settings = {
'bullet': bullets, # - or *
Expand Down Expand Up @@ -136,13 +139,16 @@ def create_arguments(filename, file, config_file,
# Remove { and } as remark adds them on its own
settings = config_json[1:-1]

args = ['--no-color', '--quiet', '--setting', settings]
args = [filename, '--no-color', '--quiet', '--setting', settings]

if remark_configs_plugins:
config_json = json.dumps(remark_configs_plugins)
plugins = 'lint=' + config_json[1:-1]
args += ['--use', plugins]

if check_links:
args += ['--use', 'validate-links']

return args

def process_output(self, output, filename, file):
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -19,6 +19,7 @@
"ramllint": ">=1.2.2 <1.2.4 || >=1.2.5 <1.3.0",
"remark-cli": "~2",
"remark-lint": "~5",
"remark-validate-links": "~5",
"standard": "~7",
"stylelint": "~7",
"stylint": "~1.5.9",
Expand Down
28 changes: 28 additions & 0 deletions tests/markdown/MarkdownBearTest.py
Expand Up @@ -22,6 +22,16 @@
2. nopqrstuvwxyz
"""

test_file4 = """# Hello
Read more [This link does not exist](#world).
"""

test_file5 = """# world
Read more [This link exists](#world).
"""

MarkdownBearTest = verify_local_bear(MarkdownBear,
valid_files=(test_file2,),
invalid_files=(test_file1,))
Expand Down Expand Up @@ -55,3 +65,21 @@ def test_invalid_message(self):
'Line must be at most 10 characters'
' maximum-line-length remark-lint')
self.assertEqual(results[0].severity, RESULT_SEVERITY.NORMAL)

def test_invalid_link(self):
content = test_file4.splitlines()
self.section.append(Setting('check_links', True))
with prepare_file(content, None) as (file, fname):
with execute_bear(self.uut, fname, file) as results:
self.assertEqual(results[0].message,
'Link to unknown heading: `world`'
' remark-validate-links '
'remark-validate-links')
self.assertEqual(results[0].severity, RESULT_SEVERITY.NORMAL)

def test_valid_link(self):
content = test_file5.splitlines()
self.section.append(Setting('check_links', True))
with prepare_file(content, None) as (file, fname):
with execute_bear(self.uut, fname, file) as results:
self.assertEqual(results, [])

0 comments on commit 27eef62

Please sign in to comment.