]> git.immae.eu Git - github/fretlink/pronto-hlint.git/blob - spec/pronto/hlint_spec.rb
Welcom pronto-hlint! This is a whole rewrite of pronto-eslint_npm.
[github/fretlink/pronto-hlint.git] / spec / pronto / hlint_spec.rb
1 # frozen_string_literal: true
2
3 require 'spec_helper'
4
5 module Pronto
6 module Hlint
7 describe Runner do
8 let(:hlint) { Hlint::Runner.new(patches) }
9 let(:patches) { [] }
10
11 describe '#run' do
12 subject(:run) { hlint.run }
13
14 context 'patches are nil' do
15 let(:patches) { nil }
16
17 it 'returns an empty array' do
18 expect(run).to eql([])
19 end
20 end
21
22 context 'no patches' do
23 let(:patches) { [] }
24
25 it 'returns an empty array' do
26 expect(run).to eql([])
27 end
28 end
29
30 context 'patches with a one and a four warnings' do
31 include_context 'test repo'
32
33 let(:patches) { repo.diff('master') }
34
35 it 'returns correct number of errors' do
36 expect(run.count).to eql(3)
37 end
38
39 it 'has correct first message' do
40 expect(run.first.msg).to match("Unused LANGUAGE pragma")
41 end
42
43 context(
44 'with files to lint config that never matches',
45 config: { 'files_to_lint' => 'will never match' }
46 ) do
47 it 'returns zero errors' do
48 expect(run.count).to eql(0)
49 end
50 end
51
52 context(
53 'with files to lint config that matches only .lhs',
54 config: { 'files_to_lint' => '\.lhs$' }
55 ) do
56 it 'returns correct amount of errors' do
57 expect(run.count).to eql(0)
58 end
59 end
60
61 context(
62 'with different hlint executable',
63 config: { 'hlint_executable' => './custom_hlint.sh' }
64 ) do
65 it 'calls the custom hlint hlint_executable' do
66 expect { run }.to raise_error(JSON::ParserError, /hlint called!/)
67 end
68 end
69 end
70 end
71
72 describe '#files_to_lint' do
73 subject(:files_to_lint) { hlint.files_to_lint }
74
75 it 'matches .he by default' do
76 expect(files_to_lint).to match('Types.hs')
77 end
78 end
79
80 describe '#hlint_executable' do
81 subject(:hlint_executable) { hlint.hlint_executable }
82
83 it 'is `hlint` by default' do
84 expect(hlint_executable).to eql('hlint')
85 end
86
87 context(
88 'with different hlint executable config',
89 config: { 'hlint_executable' => 'custom_hlint' }
90 ) do
91 it 'is correct' do
92 hlint.read_config
93 expect(hlint_executable).to eql('custom_hlint')
94 end
95 end
96 end
97
98 describe '#hlint_command_line' do
99 subject(:hlint_command_line) { hlint.send(:hlint_command_line, path) }
100 let(:path) { '/some/path.rb' }
101
102 it 'adds json output flag' do
103 expect(hlint_command_line).to include('--json')
104 end
105
106 it 'adds path' do
107 expect(hlint_command_line).to include(path)
108 end
109
110 it 'starts with hlint executable' do
111 expect(hlint_command_line).to start_with(hlint.hlint_executable)
112 end
113
114 context 'with path that should be escaped' do
115 let(:path) { '/must be/$escaped' }
116
117 it 'escapes the path correctly' do
118 expect(hlint_command_line).to include('/must\\ be/\\$escaped')
119 end
120
121 it 'does not include unescaped path' do
122 expect(hlint_command_line).not_to include(path)
123 end
124 end
125
126 context(
127 'with some command line options',
128 config: { 'cmd_line_opts' => '--my command --line opts' }
129 ) do
130 it 'includes the custom command line options' do
131 hlint.read_config
132 expect(hlint_command_line).to include('--my command --line opts')
133 end
134 end
135 end
136 end
137 end
138 end