Skip to content

Commit

Permalink
Support --stdin-display-name and the mixed style of files and stdin (#…
Browse files Browse the repository at this point in the history
…278)

* Support --stdin-alt-name and the mix style such as `vint a.vim - b.vim`

* Care Python 2

* Add available options in README

* fixup! Care Python 2

* fixup! Care Python 2

* Update README

* Rename --stdin-alt-path to --stdin-display-name

* Add a test for stdin

* fixup! Support --stdin-alt-name and the mix style such as `vint a.vim - b.vim`
  • Loading branch information
Kuniwak authored Jun 29, 2018
1 parent efff450 commit 3a2729e
Show file tree
Hide file tree
Showing 38 changed files with 510 additions and 285 deletions.
34 changes: 34 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,40 @@ You can configure linting severity, max errors, ... as following:

$ vint --color --style ~/.vimrc

And you can see all available options by using `--help`:

::
$ vint --help
usage: vint [-h] [-v] [-V] [-e] [-w] [-s] [-m MAX_VIOLATIONS] [-c]
[--no-color] [-j] [-t] [--enable-neovim] [-f FORMAT]
[--stdin-alt-path STDIN_ALT_PATH]
[files [files ...]]
Lint Vim script
positional arguments:
files file or directory path to lint
optional arguments:
-h, --help show this help message and exit
-v, --version show program's version number and exit
-V, --verbose output verbose message
-e, --error report only errors
-w, --warning report errors and warnings
-s, --style-problem report errors, warnings and style problems
-m MAX_VIOLATIONS, --max-violations MAX_VIOLATIONS
limit max violations count
-c, --color colorize output when possible
--no-color do not colorize output
-j, --json output json style
-t, --stat output statistic info
--enable-neovim enable Neovim syntax
-f FORMAT, --format FORMAT
set output format
--stdin-display-name STDIN_DISPLAY_NAME
specify a file path that is used for reporting when
linting standard inputs

Comment config
~~~~~~~~~~~~~~

Expand Down
2 changes: 1 addition & 1 deletion dev_tool/show_encoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@

file_path = Path(namespace['file'][0])
decoder = Decoder(default_decoding_strategy)
decoder.read(file_path)
decoder.decode(file_path)
pprint(decoder.debug_hint)
12 changes: 12 additions & 0 deletions test/acceptance/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,5 +144,17 @@ def test_exec_vint_with_color_flag(self):
self.assertRegex(got_output, expected_output_pattern)


def test_exec_vint_with_pipe(self):
cmd = 'echo "foo" =~ "bar" | bin/vint --stdin-display-name STDIN_TEST -'

with self.assertRaises(subprocess.CalledProcessError) as context_manager:
subprocess.check_output(cmd, shell=True, universal_newlines=True)

got_output = context_manager.exception.output

expected_output_pattern = '^STDIN_TEST:'
self.assertRegex(got_output, expected_output_pattern)


if __name__ == '__main__':
unittest.main()
8 changes: 7 additions & 1 deletion test/asserting/policy.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from vint.compat.itertools import zip_longest
from vint.linting.policy_set import PolicySet
from vint.linting.linter import Linter
from vint.linting.config.config_default_source import ConfigDefaultSource
from vint.linting.lint_target import LintTargetFile
from vint.linting.level import Level


Expand Down Expand Up @@ -31,7 +33,7 @@ def assertFoundViolationsEqual(self, path, Policy, expected_violations, policy_o
config_dict['policies'][policy_name] = policy_options

linter = Linter(policy_set, config_dict)
violations = linter.lint_file(path)
violations = linter.lint(LintTargetFile(path))

pprint(violations)
self.assertEqual(len(violations), len(expected_violations))
Expand All @@ -43,9 +45,13 @@ def assertFoundViolationsEqual(self, path, Policy, expected_violations, policy_o
def assertViolation(self, actual_violation, expected_violation):
self.assertIsNot(actual_violation, None)
self.assertIsNot(expected_violation, None)

pprint(actual_violation)

self.assertEqual(actual_violation['name'], expected_violation['name'])
self.assertEqual(actual_violation['position'], expected_violation['position'])
self.assertEqual(actual_violation['level'], expected_violation['level'])

self.assertIsInstance(actual_violation['description'], str)


Expand Down
Empty file added test/fixture/lint_target.vim
Empty file.
3 changes: 2 additions & 1 deletion test/integration/vint/ast/plugin/test_scope_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
is_declarative_identifier,
)
from vint.ast.plugin.scope_plugin import ScopePlugin
from vint.linting.lint_target import LintTargetFile


FIXTURE_BASE_PATH = Path('test', 'fixture', 'ast', 'scope_plugin')
Expand Down Expand Up @@ -41,7 +42,7 @@ class Fixtures(enum.Enum):
class TestScopePlugin(unittest.TestCase):
def create_ast(self, file_path):
parser = Parser()
ast = parser.parse_file(file_path.value)
ast = parser.parse(LintTargetFile(file_path.value))
return ast


Expand Down
5 changes: 3 additions & 2 deletions test/integration/vint/linting/test_linter.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from vint.linting.level import Level
from vint.linting.policy.abstract_policy import AbstractPolicy
from vint.linting.linter import Linter
from vint.linting.lint_target import LintTargetFile

INVALID_VIM_SCRIPT = Path('test', 'fixture', 'linter', 'invalid.vim')
BROKEN_VIM_SCRIPT = Path('test', 'fixture', 'linter', 'broken.vim')
Expand Down Expand Up @@ -86,7 +87,7 @@ def test_lint(self):
}

linter = Linter(policy_set, config_dict_global)
got_violations = linter.lint_file(INVALID_VIM_SCRIPT)
got_violations = linter.lint(LintTargetFile(INVALID_VIM_SCRIPT))

expected_violations = [
{
Expand Down Expand Up @@ -147,7 +148,7 @@ def test_lint_with_broken_file(self):
}

linter = Linter(policy_set, config_dict_global)
got_violations = linter.lint_file(BROKEN_VIM_SCRIPT)
got_violations = linter.lint(LintTargetFile(BROKEN_VIM_SCRIPT))

expected_violations = [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
get_lambda_string_expr_content,
FUNCTION_REFERENCE_STRING_EXPR_CONTENT,
)
from vint.linting.lint_target import LintTargetFile


FIXTURE_BASE_PATH = Path('test', 'fixture', 'ast', 'scope_plugin')
Expand All @@ -27,7 +28,7 @@ class Fixtures(enum.Enum):
class TestCallNodeParser(unittest.TestCase):
def create_ast(self, file_path):
parser = Parser()
ast = parser.parse_file(file_path.value)
ast = parser.parse(LintTargetFile(file_path.value))
return ast


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
IDENTIFIER_ATTRIBUTE_LAMBDA_ARGUMENT_FLAG,
IDENTIFIER_ATTRIBUTE_LAMBDA_BODY_CONTEXT,
)
from vint.linting.lint_target import LintTargetFile


FIXTURE_BASE_PATH = Path('test', 'fixture', 'ast', 'scope_plugin')
Expand Down Expand Up @@ -64,7 +65,7 @@
class TestIdentifierClassifier(unittest.TestCase):
def create_ast(self, file_path):
parser = Parser()
ast = parser.parse_file(file_path)
ast = parser.parse(LintTargetFile(file_path))
return ast


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from vint.ast.parsing import Parser
from vint.ast.plugin.scope_plugin.identifier_classifier import IdentifierClassifier
from vint.linting.lint_target import LintTargetFile

FIXTURE_BASE_PATH = Path('test', 'fixture', 'ast', 'scope_plugin')

Expand All @@ -15,7 +16,7 @@
class TestIdentifierCollector(unittest.TestCase):
def create_ast(self, file_path):
parser = Parser()
ast = parser.parse_file(file_path)
ast = parser.parse(LintTargetFile(file_path))

id_classifier = IdentifierClassifier()
attached_ast = id_classifier.attach_identifier_attributes(ast)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
RedirAssignmentParser,
get_redir_content,
)
from vint.linting.lint_target import LintTargetFile


FIXTURE_BASE_PATH = Path('test', 'fixture', 'ast', 'scope_plugin')
Expand All @@ -22,7 +23,7 @@ class Fixtures(enum.Enum):
class TestRedirAssignmentParser(unittest.TestCase):
def create_ast(self, file_path):
parser = Parser()
ast = parser.parse_file(file_path.value)
ast = parser.parse(LintTargetFile(file_path.value))
return ast


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
is_reachable_reference_identifier,
is_referenced_declarative_identifier,
)
from vint.linting.lint_target import LintTargetFile


FIXTURE_BASE_PATH = Path('test', 'fixture', 'ast', 'scope_plugin')
Expand All @@ -31,7 +32,7 @@ class Fixtures(enum.Enum):
class TestReferenceReachabilityTester(unittest.TestCase):
def create_ast(self, file_path):
parser = Parser()
ast = parser.parse_file(file_path.value)
ast = parser.parse(LintTargetFile(file_path.value))
return ast


Expand Down
5 changes: 4 additions & 1 deletion test/unit/vint/ast/plugin/scope_plugin/test_scope_linker.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from pathlib import Path

from vint.ast.parsing import Parser
from vint.ast.plugin.scope_plugin.scope_linker import ScopeLinker, ScopeVisibility
from vint.linting.lint_target import LintTargetFile
from vint.ast.plugin.scope_plugin.scope_linker import ScopeLinker
from vint.ast.plugin.scope_plugin.scope import (
Scope,
Expand Down Expand Up @@ -34,7 +36,8 @@ class Fixtures(enum.Enum):
class TestScopeLinker(unittest.TestCase):
def create_ast(self, file_path):
parser = Parser()
ast = parser.parse_file(file_path.value)
lint_target = LintTargetFile(file_path.value)
ast = parser.parse(lint_target)
return ast


Expand Down
11 changes: 6 additions & 5 deletions test/unit/vint/ast/test_parsing.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import unittest
from vint.ast.parsing import Parser
from vint.ast.node_type import NodeType
from vint.linting.lint_target import LintTargetFile
from test.asserting.ast import get_fixture_path

FIXTURE_FILE = get_fixture_path('fixture_to_parse.vim')
Expand All @@ -10,27 +11,27 @@


class TestParser(unittest.TestCase):
def test_parse_file(self):
def test_parse(self):
parser = Parser()
ast = parser.parse_file(FIXTURE_FILE)
ast = parser.parse(LintTargetFile(FIXTURE_FILE))
self.assertIs(ast['type'], 1)


def test_parse_file_on_ff_dos_and_fenc_cp932(self):
parser = Parser()
ast = parser.parse_file(FIXTURE_FILE_FF_DOS_FENC_CP932)
ast = parser.parse(LintTargetFile(FIXTURE_FILE_FF_DOS_FENC_CP932))
self.assertIs(ast['type'], 1)


def test_parse_file_when_neovim_enabled(self):
parser = Parser(enable_neovim=True)
ast = parser.parse_file(FIXTURE_FILE_NEOVIM)
ast = parser.parse(LintTargetFile(FIXTURE_FILE_NEOVIM))
self.assertIs(ast['type'], 1)


def test_parse_empty_file(self):
parser = Parser()
ast = parser.parse_file(FIXTURE_FILE_EMPTY)
ast = parser.parse(LintTargetFile(FIXTURE_FILE_EMPTY))
self.assertIs(ast['type'], 1)


Expand Down
3 changes: 2 additions & 1 deletion test/unit/vint/ast/test_traversing.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
from vint.ast.parsing import Parser
from vint.ast.node_type import NodeType
from vint.ast.traversing import traverse, SKIP_CHILDREN
from vint.linting.lint_target import LintTargetFile

FIXTURE_FILE = get_fixture_path('fixture_to_traverse.vim')


class TestTraverse(unittest.TestCase):
def setUp(self):
parser = Parser()
self.ast = parser.parse_file(FIXTURE_FILE)
self.ast = parser.parse(LintTargetFile(FIXTURE_FILE))

def test_traverse(self):
expected_order_of_events = [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import unittest
import enum
from vint.linting.linter import Linter
from vint.linting.lint_target import LintTargetFile
from vint.linting.policy_set import PolicySet
from vint.ast.node_type import NodeType
from vint.linting.level import Level
Expand All @@ -20,9 +21,10 @@ def test_simple_example(self):
global_config_dict = {'cmdargs': {'severity': Level.ERROR}}
policy_set = PolicySet([TestConfigNextLineCommentSource.ProhibitStringPolicy])
linter = Linter(policy_set, global_config_dict)
lint_target = LintTargetFile(Fixtures.SIMPLE.value)

reported_string_node_values = [violation['description']
for violation in linter.lint_file(Fixtures.SIMPLE.value)]
for violation in linter.lint(lint_target)]

self.assertEqual(reported_string_node_values, [
"'report me because I have no line config comments'",
Expand All @@ -34,9 +36,10 @@ def test_lambda_string_expr(self):
global_config_dict = {'cmdargs': {'severity': Level.ERROR}}
policy_set = PolicySet([TestConfigNextLineCommentSource.ProhibitStringPolicy])
linter = Linter(policy_set, global_config_dict)
lint_target = LintTargetFile(Fixtures.LAMBDA_STRING_EXPR.value)

reported_string_node_values = [violation['description']
for violation in linter.lint_file(Fixtures.LAMBDA_STRING_EXPR.value)]
for violation in linter.lint(lint_target)]

self.assertEqual(reported_string_node_values, [
"'report me because I have no line config comments'",
Expand Down
7 changes: 3 additions & 4 deletions test/unit/vint/linting/formatter/test_json_formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@

class TestJSONFormatter(FormatterAssertion, unittest.TestCase):
def test_format_violations(self):
cmdargs = {}
formatter = JSONFormatter(cmdargs)
formatter = JSONFormatter()

violations = [
{
Expand All @@ -21,7 +20,7 @@ def test_format_violations(self):
'position': {
'line': 1,
'column': 2,
'path': Path('path', 'to', 'file1')
'path': str(Path('path', 'to', 'file1'))
},
},
{
Expand All @@ -32,7 +31,7 @@ def test_format_violations(self):
'position': {
'line': 11,
'column': 21,
'path': Path('path', 'to', 'file2')
'path': str(Path('path', 'to', 'file2'))
},
},
]
Expand Down
Loading

0 comments on commit 3a2729e

Please sign in to comment.