]> git.immae.eu Git - github/fretlink/pronto-hlint.git/commitdiff
refactor config handling and specs a little bit
authorMarkus Doits <markus.doits@stellenticket.de>
Sat, 31 Mar 2018 16:34:01 +0000 (18:34 +0200)
committerMarkus Doits <markus.doits@stellenticket.de>
Sat, 31 Mar 2018 16:42:20 +0000 (18:42 +0200)
lib/pronto/eslint_npm.rb
spec/pronto/eslint_spec.rb
spec/spec_helper.rb

index e861ffedb2459d09402b810dd5c22c1a0bfbe0f5..63a215b2c8dda131ca89eef51e971ccc59de21ba 100644 (file)
@@ -22,14 +22,18 @@ module Pronto
       @files_to_lint = regexp.is_a?(Regexp) && regexp || Regexp.new(regexp)
     end
 
-    def read_config
-      config_file = File.join(repo_path, CONFIG_FILE)
-      return unless File.exist?(config_file)
-      config = YAML.load_file(config_file)
+    def config_options
+      @config_options ||=
+        begin
+          config_file = File.join(repo_path, CONFIG_FILE)
+          File.exist?(config_file) && YAML.load_file(config_file) || {}
+        end
+    end
 
-      CONFIG_KEYS.each do |config_key|
-        next unless config[config_key]
-        send("#{config_key}=", config[config_key])
+    def read_config
+      config_options.each do |key, val|
+        next unless CONFIG_KEYS.include?(key.to_s)
+        send("#{key}=", val)
       end
     end
 
@@ -48,7 +52,7 @@ module Pronto
     private
 
     def repo_path
-      @_repo_path ||= @patches.first.repo.path
+      @repo_path ||= @patches.first.repo.path
     end
 
     def inspect(patch)
@@ -75,13 +79,14 @@ module Pronto
 
     def run_eslint(patch)
       Dir.chdir(repo_path) do
-        escaped_file_path = Shellwords.escape(patch.new_file_full_path.to_s)
-        JSON.parse(
-          `#{eslint_executable} #{escaped_file_path} -f json`
-        )
+        JSON.parse `#{eslint_command_line(patch.new_file_full_path.to_s)}`
       end
     end
 
+    def eslint_command_line(path)
+      "#{eslint_executable} #{Shellwords.escape(path)} -f json"
+    end
+
     def clean_up_eslint_output(output)
       # 1. Filter out offences without a warning or error
       # 2. Get the messages for that file
index 52e52f2db6f004b145a21577c0c18ffb3c011589..a00a05b519656393d6bb898916491e6832bc1e5b 100644 (file)
@@ -100,6 +100,45 @@ module Pronto
       it 'is `eslint` by default' do
         expect(eslint_executable).to eql('eslint')
       end
+
+      context(
+        'with different eslint executable config',
+        config: { 'eslint_executable' => 'custom_eslint' }
+      ) do
+        it 'is correct' do
+          eslint.read_config
+          expect(eslint_executable).to eql('custom_eslint')
+        end
+      end
+    end
+
+    describe '#eslint_command_line' do
+      subject(:eslint_command_line) { eslint.send(:eslint_command_line, path) }
+      let(:path) { '/some/path.rb' }
+
+      it 'adds json output flag' do
+        expect(eslint_command_line).to include('-f json')
+      end
+
+      it 'adds path' do
+        expect(eslint_command_line).to include(path)
+      end
+
+      it 'starts with eslint executable' do
+        expect(eslint_command_line).to start_with(eslint.eslint_executable)
+      end
+
+      context 'with path that should be escaped' do
+        let(:path) { '/must be/$escaped' }
+
+        it 'escapes the path correctly' do
+          expect(eslint_command_line).to include('/must\\ be/\\$escaped')
+        end
+
+        it 'does not include unescaped path' do
+          expect(eslint_command_line).not_to include(path)
+        end
+      end
     end
   end
 end
index b3d85969215cd4beaff60c71a8a47755ff961db0..c05f072713d6495f48745d4385c38e336db9b8f2 100644 (file)
@@ -17,10 +17,9 @@ require 'pronto/eslint_npm'
 end
 
 RSpec.shared_context 'with config', config: true do
-  requested_config = metadata[:config].to_yaml
+  requested_config = metadata[:config]
 
-  let(:config_path) { File.join(repo.path.to_s, Pronto::ESLintNpm::CONFIG_FILE) }
-
-  before(:each) { File.write(config_path, requested_config) }
-  after(:each) { FileUtils.rm(config_path) }
+  before(:each) do
+    allow_any_instance_of(Pronto::ESLintNpm).to receive(:config_options).and_return(requested_config)
+  end
 end