Skip to content

Commit 3a2729e

Browse files
authored
Support --stdin-display-name and the mixed style of files and stdin (#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`
1 parent efff450 commit 3a2729e

38 files changed

+510
-285
lines changed

README.rst

+34
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,40 @@ You can configure linting severity, max errors, ... as following:
122122

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

125+
And you can see all available options by using `--help`:
126+
127+
::
128+
$ vint --help
129+
usage: vint [-h] [-v] [-V] [-e] [-w] [-s] [-m MAX_VIOLATIONS] [-c]
130+
[--no-color] [-j] [-t] [--enable-neovim] [-f FORMAT]
131+
[--stdin-alt-path STDIN_ALT_PATH]
132+
[files [files ...]]
133+
134+
Lint Vim script
135+
136+
positional arguments:
137+
files file or directory path to lint
138+
139+
optional arguments:
140+
-h, --help show this help message and exit
141+
-v, --version show program's version number and exit
142+
-V, --verbose output verbose message
143+
-e, --error report only errors
144+
-w, --warning report errors and warnings
145+
-s, --style-problem report errors, warnings and style problems
146+
-m MAX_VIOLATIONS, --max-violations MAX_VIOLATIONS
147+
limit max violations count
148+
-c, --color colorize output when possible
149+
--no-color do not colorize output
150+
-j, --json output json style
151+
-t, --stat output statistic info
152+
--enable-neovim enable Neovim syntax
153+
-f FORMAT, --format FORMAT
154+
set output format
155+
--stdin-display-name STDIN_DISPLAY_NAME
156+
specify a file path that is used for reporting when
157+
linting standard inputs
158+
125159
Comment config
126160
~~~~~~~~~~~~~~
127161

dev_tool/show_encoding.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,5 @@
1919

2020
file_path = Path(namespace['file'][0])
2121
decoder = Decoder(default_decoding_strategy)
22-
decoder.read(file_path)
22+
decoder.decode(file_path)
2323
pprint(decoder.debug_hint)

test/acceptance/test_cli.py

+12
Original file line numberDiff line numberDiff line change
@@ -144,5 +144,17 @@ def test_exec_vint_with_color_flag(self):
144144
self.assertRegex(got_output, expected_output_pattern)
145145

146146

147+
def test_exec_vint_with_pipe(self):
148+
cmd = 'echo "foo" =~ "bar" | bin/vint --stdin-display-name STDIN_TEST -'
149+
150+
with self.assertRaises(subprocess.CalledProcessError) as context_manager:
151+
subprocess.check_output(cmd, shell=True, universal_newlines=True)
152+
153+
got_output = context_manager.exception.output
154+
155+
expected_output_pattern = '^STDIN_TEST:'
156+
self.assertRegex(got_output, expected_output_pattern)
157+
158+
147159
if __name__ == '__main__':
148160
unittest.main()

test/asserting/policy.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
from vint.compat.itertools import zip_longest
55
from vint.linting.policy_set import PolicySet
66
from vint.linting.linter import Linter
7+
from vint.linting.config.config_default_source import ConfigDefaultSource
8+
from vint.linting.lint_target import LintTargetFile
79
from vint.linting.level import Level
810

911

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

3335
linter = Linter(policy_set, config_dict)
34-
violations = linter.lint_file(path)
36+
violations = linter.lint(LintTargetFile(path))
3537

3638
pprint(violations)
3739
self.assertEqual(len(violations), len(expected_violations))
@@ -43,9 +45,13 @@ def assertFoundViolationsEqual(self, path, Policy, expected_violations, policy_o
4345
def assertViolation(self, actual_violation, expected_violation):
4446
self.assertIsNot(actual_violation, None)
4547
self.assertIsNot(expected_violation, None)
48+
49+
pprint(actual_violation)
50+
4651
self.assertEqual(actual_violation['name'], expected_violation['name'])
4752
self.assertEqual(actual_violation['position'], expected_violation['position'])
4853
self.assertEqual(actual_violation['level'], expected_violation['level'])
54+
4955
self.assertIsInstance(actual_violation['description'], str)
5056

5157

test/fixture/lint_target.vim

Whitespace-only changes.

test/integration/vint/ast/plugin/test_scope_plugin.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
is_declarative_identifier,
1111
)
1212
from vint.ast.plugin.scope_plugin import ScopePlugin
13+
from vint.linting.lint_target import LintTargetFile
1314

1415

1516
FIXTURE_BASE_PATH = Path('test', 'fixture', 'ast', 'scope_plugin')
@@ -41,7 +42,7 @@ class Fixtures(enum.Enum):
4142
class TestScopePlugin(unittest.TestCase):
4243
def create_ast(self, file_path):
4344
parser = Parser()
44-
ast = parser.parse_file(file_path.value)
45+
ast = parser.parse(LintTargetFile(file_path.value))
4546
return ast
4647

4748

test/integration/vint/linting/test_linter.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from vint.linting.level import Level
55
from vint.linting.policy.abstract_policy import AbstractPolicy
66
from vint.linting.linter import Linter
7+
from vint.linting.lint_target import LintTargetFile
78

89
INVALID_VIM_SCRIPT = Path('test', 'fixture', 'linter', 'invalid.vim')
910
BROKEN_VIM_SCRIPT = Path('test', 'fixture', 'linter', 'broken.vim')
@@ -86,7 +87,7 @@ def test_lint(self):
8687
}
8788

8889
linter = Linter(policy_set, config_dict_global)
89-
got_violations = linter.lint_file(INVALID_VIM_SCRIPT)
90+
got_violations = linter.lint(LintTargetFile(INVALID_VIM_SCRIPT))
9091

9192
expected_violations = [
9293
{
@@ -147,7 +148,7 @@ def test_lint_with_broken_file(self):
147148
}
148149

149150
linter = Linter(policy_set, config_dict_global)
150-
got_violations = linter.lint_file(BROKEN_VIM_SCRIPT)
151+
got_violations = linter.lint(LintTargetFile(BROKEN_VIM_SCRIPT))
151152

152153
expected_violations = [
153154
{

test/unit/vint/ast/plugin/scope_plugin/test_call_node_parser.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
get_lambda_string_expr_content,
1111
FUNCTION_REFERENCE_STRING_EXPR_CONTENT,
1212
)
13+
from vint.linting.lint_target import LintTargetFile
1314

1415

1516
FIXTURE_BASE_PATH = Path('test', 'fixture', 'ast', 'scope_plugin')
@@ -27,7 +28,7 @@ class Fixtures(enum.Enum):
2728
class TestCallNodeParser(unittest.TestCase):
2829
def create_ast(self, file_path):
2930
parser = Parser()
30-
ast = parser.parse_file(file_path.value)
31+
ast = parser.parse(LintTargetFile(file_path.value))
3132
return ast
3233

3334

test/unit/vint/ast/plugin/scope_plugin/test_identifier_classifier.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
IDENTIFIER_ATTRIBUTE_LAMBDA_ARGUMENT_FLAG,
2222
IDENTIFIER_ATTRIBUTE_LAMBDA_BODY_CONTEXT,
2323
)
24+
from vint.linting.lint_target import LintTargetFile
2425

2526

2627
FIXTURE_BASE_PATH = Path('test', 'fixture', 'ast', 'scope_plugin')
@@ -64,7 +65,7 @@
6465
class TestIdentifierClassifier(unittest.TestCase):
6566
def create_ast(self, file_path):
6667
parser = Parser()
67-
ast = parser.parse_file(file_path)
68+
ast = parser.parse(LintTargetFile(file_path))
6869
return ast
6970

7071

test/unit/vint/ast/plugin/scope_plugin/test_identifier_collector.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
from vint.ast.parsing import Parser
55
from vint.ast.plugin.scope_plugin.identifier_classifier import IdentifierClassifier
6+
from vint.linting.lint_target import LintTargetFile
67

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

@@ -15,7 +16,7 @@
1516
class TestIdentifierCollector(unittest.TestCase):
1617
def create_ast(self, file_path):
1718
parser = Parser()
18-
ast = parser.parse_file(file_path)
19+
ast = parser.parse(LintTargetFile(file_path))
1920

2021
id_classifier = IdentifierClassifier()
2122
attached_ast = id_classifier.attach_identifier_attributes(ast)

test/unit/vint/ast/plugin/scope_plugin/test_redir_assignment_parser.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
RedirAssignmentParser,
1010
get_redir_content,
1111
)
12+
from vint.linting.lint_target import LintTargetFile
1213

1314

1415
FIXTURE_BASE_PATH = Path('test', 'fixture', 'ast', 'scope_plugin')
@@ -22,7 +23,7 @@ class Fixtures(enum.Enum):
2223
class TestRedirAssignmentParser(unittest.TestCase):
2324
def create_ast(self, file_path):
2425
parser = Parser()
25-
ast = parser.parse_file(file_path.value)
26+
ast = parser.parse(LintTargetFile(file_path.value))
2627
return ast
2728

2829

test/unit/vint/ast/plugin/scope_plugin/test_reference_reachability_tester.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
is_reachable_reference_identifier,
99
is_referenced_declarative_identifier,
1010
)
11+
from vint.linting.lint_target import LintTargetFile
1112

1213

1314
FIXTURE_BASE_PATH = Path('test', 'fixture', 'ast', 'scope_plugin')
@@ -31,7 +32,7 @@ class Fixtures(enum.Enum):
3132
class TestReferenceReachabilityTester(unittest.TestCase):
3233
def create_ast(self, file_path):
3334
parser = Parser()
34-
ast = parser.parse_file(file_path.value)
35+
ast = parser.parse(LintTargetFile(file_path.value))
3536
return ast
3637

3738

test/unit/vint/ast/plugin/scope_plugin/test_scope_linker.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
from pathlib import Path
55

66
from vint.ast.parsing import Parser
7+
from vint.ast.plugin.scope_plugin.scope_linker import ScopeLinker, ScopeVisibility
8+
from vint.linting.lint_target import LintTargetFile
79
from vint.ast.plugin.scope_plugin.scope_linker import ScopeLinker
810
from vint.ast.plugin.scope_plugin.scope import (
911
Scope,
@@ -34,7 +36,8 @@ class Fixtures(enum.Enum):
3436
class TestScopeLinker(unittest.TestCase):
3537
def create_ast(self, file_path):
3638
parser = Parser()
37-
ast = parser.parse_file(file_path.value)
39+
lint_target = LintTargetFile(file_path.value)
40+
ast = parser.parse(lint_target)
3841
return ast
3942

4043

test/unit/vint/ast/test_parsing.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import unittest
22
from vint.ast.parsing import Parser
33
from vint.ast.node_type import NodeType
4+
from vint.linting.lint_target import LintTargetFile
45
from test.asserting.ast import get_fixture_path
56

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

1112

1213
class TestParser(unittest.TestCase):
13-
def test_parse_file(self):
14+
def test_parse(self):
1415
parser = Parser()
15-
ast = parser.parse_file(FIXTURE_FILE)
16+
ast = parser.parse(LintTargetFile(FIXTURE_FILE))
1617
self.assertIs(ast['type'], 1)
1718

1819

1920
def test_parse_file_on_ff_dos_and_fenc_cp932(self):
2021
parser = Parser()
21-
ast = parser.parse_file(FIXTURE_FILE_FF_DOS_FENC_CP932)
22+
ast = parser.parse(LintTargetFile(FIXTURE_FILE_FF_DOS_FENC_CP932))
2223
self.assertIs(ast['type'], 1)
2324

2425

2526
def test_parse_file_when_neovim_enabled(self):
2627
parser = Parser(enable_neovim=True)
27-
ast = parser.parse_file(FIXTURE_FILE_NEOVIM)
28+
ast = parser.parse(LintTargetFile(FIXTURE_FILE_NEOVIM))
2829
self.assertIs(ast['type'], 1)
2930

3031

3132
def test_parse_empty_file(self):
3233
parser = Parser()
33-
ast = parser.parse_file(FIXTURE_FILE_EMPTY)
34+
ast = parser.parse(LintTargetFile(FIXTURE_FILE_EMPTY))
3435
self.assertIs(ast['type'], 1)
3536

3637

test/unit/vint/ast/test_traversing.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@
44
from vint.ast.parsing import Parser
55
from vint.ast.node_type import NodeType
66
from vint.ast.traversing import traverse, SKIP_CHILDREN
7+
from vint.linting.lint_target import LintTargetFile
78

89
FIXTURE_FILE = get_fixture_path('fixture_to_traverse.vim')
910

1011

1112
class TestTraverse(unittest.TestCase):
1213
def setUp(self):
1314
parser = Parser()
14-
self.ast = parser.parse_file(FIXTURE_FILE)
15+
self.ast = parser.parse(LintTargetFile(FIXTURE_FILE))
1516

1617
def test_traverse(self):
1718
expected_order_of_events = [

test/unit/vint/linting/config/test_config_next_line_comment_source.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import unittest
22
import enum
33
from vint.linting.linter import Linter
4+
from vint.linting.lint_target import LintTargetFile
45
from vint.linting.policy_set import PolicySet
56
from vint.ast.node_type import NodeType
67
from vint.linting.level import Level
@@ -20,9 +21,10 @@ def test_simple_example(self):
2021
global_config_dict = {'cmdargs': {'severity': Level.ERROR}}
2122
policy_set = PolicySet([TestConfigNextLineCommentSource.ProhibitStringPolicy])
2223
linter = Linter(policy_set, global_config_dict)
24+
lint_target = LintTargetFile(Fixtures.SIMPLE.value)
2325

2426
reported_string_node_values = [violation['description']
25-
for violation in linter.lint_file(Fixtures.SIMPLE.value)]
27+
for violation in linter.lint(lint_target)]
2628

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

3841
reported_string_node_values = [violation['description']
39-
for violation in linter.lint_file(Fixtures.LAMBDA_STRING_EXPR.value)]
42+
for violation in linter.lint(lint_target)]
4043

4144
self.assertEqual(reported_string_node_values, [
4245
"'report me because I have no line config comments'",

test/unit/vint/linting/formatter/test_json_formatter.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99

1010
class TestJSONFormatter(FormatterAssertion, unittest.TestCase):
1111
def test_format_violations(self):
12-
cmdargs = {}
13-
formatter = JSONFormatter(cmdargs)
12+
formatter = JSONFormatter()
1413

1514
violations = [
1615
{
@@ -21,7 +20,7 @@ def test_format_violations(self):
2120
'position': {
2221
'line': 1,
2322
'column': 2,
24-
'path': Path('path', 'to', 'file1')
23+
'path': str(Path('path', 'to', 'file1'))
2524
},
2625
},
2726
{
@@ -32,7 +31,7 @@ def test_format_violations(self):
3231
'position': {
3332
'line': 11,
3433
'column': 21,
35-
'path': Path('path', 'to', 'file2')
34+
'path': str(Path('path', 'to', 'file2'))
3635
},
3736
},
3837
]

0 commit comments

Comments
 (0)