|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'spec_helper' |
| 4 | + |
| 5 | +describe Overcommit::Hook::PreCommit::Solargraph do |
| 6 | + let(:config) do |
| 7 | + Overcommit::ConfigurationLoader.default_configuration.merge( |
| 8 | + Overcommit::Configuration.new( |
| 9 | + 'PreCommit' => { |
| 10 | + 'Solargraph' => { |
| 11 | + 'problem_on_unmodified_line' => problem_on_unmodified_line |
| 12 | + } |
| 13 | + } |
| 14 | + ) |
| 15 | + ) |
| 16 | + end |
| 17 | + let(:problem_on_unmodified_line) { 'ignore' } |
| 18 | + let(:context) { double('context') } |
| 19 | + let(:messages) { subject.run } |
| 20 | + let(:result) { double('result') } |
| 21 | + subject { described_class.new(config, context) } |
| 22 | + |
| 23 | + before do |
| 24 | + subject.stub(:applicable_files).and_return(%w[file1.rb file2.rb]) |
| 25 | + result.stub(:stderr).and_return(stderr) |
| 26 | + result.stub(:stdout).and_return(stdout) |
| 27 | + end |
| 28 | + |
| 29 | + context 'when Solargraph exits successfully' do |
| 30 | + before do |
| 31 | + result.stub(:success?).and_return(true) |
| 32 | + subject.stub(:execute).and_return(result) |
| 33 | + end |
| 34 | + |
| 35 | + context 'and it printed a message to stderr' do |
| 36 | + let(:stderr) { 'stderr unexpected message that must be fine since command successful' } |
| 37 | + let(:stdout) { '' } |
| 38 | + it { should pass } |
| 39 | + end |
| 40 | + |
| 41 | + context 'and it printed a message to stdout' do |
| 42 | + let(:stderr) { '' } |
| 43 | + let(:stdout) { 'stdout message that must be fine since command successful' } |
| 44 | + it { should pass } |
| 45 | + end |
| 46 | + end |
| 47 | + |
| 48 | + context 'when Solargraph exits unsucessfully' do |
| 49 | + before do |
| 50 | + result.stub(:success?).and_return(false) |
| 51 | + subject.stub(:execute).and_return(result) |
| 52 | + end |
| 53 | + |
| 54 | + context 'and it reports typechecking issues' do |
| 55 | + let(:stdout) do |
| 56 | + normalize_indent(<<-MSG) |
| 57 | + /home/username/src/solargraph-rails/file1.rb:36 - Unresolved constant Solargraph::Parser::Legacy::NodeChainer |
| 58 | + /home/username/src/solargraph-rails/file2.rb:44 - Unresolved call to [] |
| 59 | + /home/username/src/solargraph-rails/file2.rb:99 - Unresolved call to [] |
| 60 | + Typecheck finished in 8.921023999806494 seconds. |
| 61 | + 189 problems found in 14 of 16 files. |
| 62 | + MSG |
| 63 | + end |
| 64 | + |
| 65 | + ['', 'unexpected output'].each do |stderr_string| |
| 66 | + context "with stderr output of #{stderr_string.inspect}" do |
| 67 | + let(:stderr) { stderr_string } |
| 68 | + |
| 69 | + it { should fail_hook } |
| 70 | + it 'reports only three errors and assumes stderr is harmless' do |
| 71 | + expect(messages.size).to eq 3 |
| 72 | + end |
| 73 | + it 'parses filename' do |
| 74 | + expect(messages.first.file).to eq '/home/username/src/solargraph-rails/file1.rb' |
| 75 | + end |
| 76 | + it 'parses line number of messages' do |
| 77 | + expect(messages.first.line).to eq 36 |
| 78 | + end |
| 79 | + it 'parses and returns error message content' do |
| 80 | + msg = '/home/username/src/solargraph-rails/file1.rb:36 - Unresolved constant Solargraph::Parser::Legacy::NodeChainer' |
| 81 | + expect(messages.first.content).to eq msg |
| 82 | + end |
| 83 | + end |
| 84 | + end |
| 85 | + end |
| 86 | + |
| 87 | + context 'but it reports no typechecking issues' do |
| 88 | + let(:stdout) do |
| 89 | + normalize_indent(<<-MSG) |
| 90 | + Typecheck finished in 8.095239999704063 seconds. |
| 91 | + 0 problems found in 0 of 16 files. |
| 92 | + MSG |
| 93 | + end |
| 94 | + |
| 95 | + context 'with no stderr output' do |
| 96 | + let(:stderr) { '' } |
| 97 | + it 'should return no messages' do |
| 98 | + expect(messages).to eq([:fail, 'Solargraph failed to run']) |
| 99 | + end |
| 100 | + end |
| 101 | + |
| 102 | + context 'with stderr output' do |
| 103 | + let(:stderr) { 'something' } |
| 104 | + it 'should raise' do |
| 105 | + expect { messages }.to raise_error(Overcommit::Exceptions::MessageProcessingError) |
| 106 | + end |
| 107 | + end |
| 108 | + end |
| 109 | + end |
| 110 | +end |
0 commit comments