diff options
author | Markus Doits <doits@users.noreply.github.com> | 2018-03-31 18:46:32 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-03-31 18:46:32 +0200 |
commit | 016e77289de983d0e7e46ac38aa4a84448388f41 (patch) | |
tree | 199de267f89a7d17752a8540eeee0922c40700ce | |
parent | ed98da4ce890259abc2d82006325be9f37e69908 (diff) | |
parent | c89a62b8e91dc1329aee92392eb32a51a25a8315 (diff) | |
download | pronto-hlint-016e77289de983d0e7e46ac38aa4a84448388f41.tar.gz pronto-hlint-016e77289de983d0e7e46ac38aa4a84448388f41.tar.zst pronto-hlint-016e77289de983d0e7e46ac38aa4a84448388f41.zip |
Merge pull request #10 from doits/refactor_config_handling
refactor config handling and specs a little bit
-rw-r--r-- | lib/pronto/eslint_npm.rb | 29 | ||||
-rw-r--r-- | spec/pronto/eslint_spec.rb | 39 | ||||
-rw-r--r-- | spec/spec_helper.rb | 9 |
3 files changed, 60 insertions, 17 deletions
diff --git a/lib/pronto/eslint_npm.rb b/lib/pronto/eslint_npm.rb index e861ffe..63a215b 100644 --- a/lib/pronto/eslint_npm.rb +++ b/lib/pronto/eslint_npm.rb | |||
@@ -22,14 +22,18 @@ module Pronto | |||
22 | @files_to_lint = regexp.is_a?(Regexp) && regexp || Regexp.new(regexp) | 22 | @files_to_lint = regexp.is_a?(Regexp) && regexp || Regexp.new(regexp) |
23 | end | 23 | end |
24 | 24 | ||
25 | def read_config | 25 | def config_options |
26 | config_file = File.join(repo_path, CONFIG_FILE) | 26 | @config_options ||= |
27 | return unless File.exist?(config_file) | 27 | begin |
28 | config = YAML.load_file(config_file) | 28 | config_file = File.join(repo_path, CONFIG_FILE) |
29 | File.exist?(config_file) && YAML.load_file(config_file) || {} | ||
30 | end | ||
31 | end | ||
29 | 32 | ||
30 | CONFIG_KEYS.each do |config_key| | 33 | def read_config |
31 | next unless config[config_key] | 34 | config_options.each do |key, val| |
32 | send("#{config_key}=", config[config_key]) | 35 | next unless CONFIG_KEYS.include?(key.to_s) |
36 | send("#{key}=", val) | ||
33 | end | 37 | end |
34 | end | 38 | end |
35 | 39 | ||
@@ -48,7 +52,7 @@ module Pronto | |||
48 | private | 52 | private |
49 | 53 | ||
50 | def repo_path | 54 | def repo_path |
51 | @_repo_path ||= @patches.first.repo.path | 55 | @repo_path ||= @patches.first.repo.path |
52 | end | 56 | end |
53 | 57 | ||
54 | def inspect(patch) | 58 | def inspect(patch) |
@@ -75,13 +79,14 @@ module Pronto | |||
75 | 79 | ||
76 | def run_eslint(patch) | 80 | def run_eslint(patch) |
77 | Dir.chdir(repo_path) do | 81 | Dir.chdir(repo_path) do |
78 | escaped_file_path = Shellwords.escape(patch.new_file_full_path.to_s) | 82 | JSON.parse `#{eslint_command_line(patch.new_file_full_path.to_s)}` |
79 | JSON.parse( | ||
80 | `#{eslint_executable} #{escaped_file_path} -f json` | ||
81 | ) | ||
82 | end | 83 | end |
83 | end | 84 | end |
84 | 85 | ||
86 | def eslint_command_line(path) | ||
87 | "#{eslint_executable} #{Shellwords.escape(path)} -f json" | ||
88 | end | ||
89 | |||
85 | def clean_up_eslint_output(output) | 90 | def clean_up_eslint_output(output) |
86 | # 1. Filter out offences without a warning or error | 91 | # 1. Filter out offences without a warning or error |
87 | # 2. Get the messages for that file | 92 | # 2. Get the messages for that file |
diff --git a/spec/pronto/eslint_spec.rb b/spec/pronto/eslint_spec.rb index 52e52f2..a00a05b 100644 --- a/spec/pronto/eslint_spec.rb +++ b/spec/pronto/eslint_spec.rb | |||
@@ -100,6 +100,45 @@ module Pronto | |||
100 | it 'is `eslint` by default' do | 100 | it 'is `eslint` by default' do |
101 | expect(eslint_executable).to eql('eslint') | 101 | expect(eslint_executable).to eql('eslint') |
102 | end | 102 | end |
103 | |||
104 | context( | ||
105 | 'with different eslint executable config', | ||
106 | config: { 'eslint_executable' => 'custom_eslint' } | ||
107 | ) do | ||
108 | it 'is correct' do | ||
109 | eslint.read_config | ||
110 | expect(eslint_executable).to eql('custom_eslint') | ||
111 | end | ||
112 | end | ||
113 | end | ||
114 | |||
115 | describe '#eslint_command_line' do | ||
116 | subject(:eslint_command_line) { eslint.send(:eslint_command_line, path) } | ||
117 | let(:path) { '/some/path.rb' } | ||
118 | |||
119 | it 'adds json output flag' do | ||
120 | expect(eslint_command_line).to include('-f json') | ||
121 | end | ||
122 | |||
123 | it 'adds path' do | ||
124 | expect(eslint_command_line).to include(path) | ||
125 | end | ||
126 | |||
127 | it 'starts with eslint executable' do | ||
128 | expect(eslint_command_line).to start_with(eslint.eslint_executable) | ||
129 | end | ||
130 | |||
131 | context 'with path that should be escaped' do | ||
132 | let(:path) { '/must be/$escaped' } | ||
133 | |||
134 | it 'escapes the path correctly' do | ||
135 | expect(eslint_command_line).to include('/must\\ be/\\$escaped') | ||
136 | end | ||
137 | |||
138 | it 'does not include unescaped path' do | ||
139 | expect(eslint_command_line).not_to include(path) | ||
140 | end | ||
141 | end | ||
103 | end | 142 | end |
104 | end | 143 | end |
105 | end | 144 | end |
diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index b3d8596..c05f072 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb | |||
@@ -17,10 +17,9 @@ require 'pronto/eslint_npm' | |||
17 | end | 17 | end |
18 | 18 | ||
19 | RSpec.shared_context 'with config', config: true do | 19 | RSpec.shared_context 'with config', config: true do |
20 | requested_config = metadata[:config].to_yaml | 20 | requested_config = metadata[:config] |
21 | 21 | ||
22 | let(:config_path) { File.join(repo.path.to_s, Pronto::ESLintNpm::CONFIG_FILE) } | 22 | before(:each) do |
23 | 23 | allow_any_instance_of(Pronto::ESLintNpm).to receive(:config_options).and_return(requested_config) | |
24 | before(:each) { File.write(config_path, requested_config) } | 24 | end |
25 | after(:each) { FileUtils.rm(config_path) } | ||
26 | end | 25 | end |