transpec 2.3.4 → 2.3.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 86662f1a0e4d74de2c72d36a51b77e8a64b28636
4
- data.tar.gz: a6fda615ce95a0808a127d6bf9412665accc3b7d
3
+ metadata.gz: a673a8ed54eae74d68ffe6c597ee95b5a2bbc1d9
4
+ data.tar.gz: 840daba419607526564f7b5375268a56e811993b
5
5
  SHA512:
6
- metadata.gz: 062e9dcef16a3c208fef5fdc439b0e9a1fec8b99eb43dd32a6afc9f2a446c2c9e45f89472728686515e194ebecf3709b505c939edf68454c65dae01f63be81d4
7
- data.tar.gz: e153260bbd9c687c0281a37eb82b8e6b376fa2961601580186bf26074957fa2948e30f6ea3acbf9bf2cad415af33aacdc675f27b3bb678f182a466ce0c3e1789
6
+ metadata.gz: 851f291d709b03c6dc94610aee361813967280d3da164fcbc0e21ab525bde5f941322aa4c6c85d926806bde439f3508b00d2b8b860bdc70233e2b3c090e977d9
7
+ data.tar.gz: d0dd0b1cc61f6f31ee6dfc78bbf4adc01866f500d0cb854c209e5c1cff7177ca256227361247bb18a7480f745026dd54b15dfde676eb779ce7ba8302d866ce9c
data/CHANGELOG.md CHANGED
@@ -2,6 +2,10 @@
2
2
 
3
3
  ## Development
4
4
 
5
+ ## v2.3.5
6
+
7
+ * Avoid crash on processing invalid encoding source files and warn of the encoding errors. ([#81](https://github.com/yujinakayama/transpec/issues/81))
8
+
5
9
  ## v2.3.4
6
10
 
7
11
  * Detect whether RSpec configuration `config.raise_errors_for_deprecations!` which interferes with Transpec's dynamic analysis is enabled in the user's specs and abort processing if so. ([#80](https://github.com/yujinakayama/transpec/issues/80))
@@ -6,7 +6,7 @@ module Transpec
6
6
  class BaseRewriter
7
7
  def rewrite_file!(arg)
8
8
  processed_source = case arg
9
- when String then ProcessedSource.parse_file(arg)
9
+ when String then ProcessedSource.from_file(arg)
10
10
  when ProcessedSource then arg
11
11
  else fail "Invalid argument: #{arg}"
12
12
  end
@@ -19,12 +19,12 @@ module Transpec
19
19
  end
20
20
 
21
21
  def rewrite_source(source, path = nil)
22
- processed_source = ProcessedSource.parse(source, path)
22
+ processed_source = ProcessedSource.new(source, path)
23
23
  rewrite(processed_source)
24
24
  end
25
25
 
26
26
  def rewrite(processed_source)
27
- fail processed_source.syntax_error if processed_source.syntax_error
27
+ fail processed_source.error if processed_source.error
28
28
 
29
29
  source_rewriter = create_source_rewriter(processed_source)
30
30
  incomplete = false
data/lib/transpec/cli.rb CHANGED
@@ -90,9 +90,9 @@ module Transpec
90
90
 
91
91
  warn_annotations(converter.report)
92
92
  report << converter.report
93
- rescue Parser::SyntaxError => error
94
- report.syntax_errors << error
95
- warn_syntax_error(error)
93
+ rescue Parser::SyntaxError, EncodingError => error
94
+ warn_file_error(error, spec.path)
95
+ report.file_errors << error
96
96
  end
97
97
 
98
98
  private
@@ -145,8 +145,18 @@ module Transpec
145
145
  puts "Done! Now run #{'rspec'.bright} and check if everything is green."
146
146
  end
147
147
 
148
- def warn_syntax_error(error)
149
- warn "Syntax error at #{error.diagnostic.location}. Skipping the file.".color(:red)
148
+ def warn_file_error(error, path)
149
+ message = case error
150
+ when Parser::SyntaxError
151
+ "Syntax error at #{error.diagnostic.location}."
152
+ when EncodingError
153
+ "Encoding error in #{path}."
154
+ else
155
+ "#{error.message.capitalize} in #{path}."
156
+ end
157
+
158
+ message << ' Skipping the file.'
159
+ warn message.color(:red)
150
160
  end
151
161
 
152
162
  def warn_annotations(report)
@@ -84,7 +84,7 @@ module Transpec
84
84
  spec_suite = SpecSuite.new(paths)
85
85
 
86
86
  spec_suite.specs.each do |spec|
87
- next if spec.syntax_error
87
+ next if spec.error
88
88
  rewriter.rewrite_file!(spec)
89
89
  end
90
90
  end
@@ -12,37 +12,34 @@ require 'transpec/ast/builder'
12
12
 
13
13
  module Transpec
14
14
  class ProcessedSource
15
- attr_reader :buffer, :ast, :path, :syntax_error
15
+ attr_reader :buffer, :ast, :path, :error
16
16
 
17
- def self.parse_file(path)
17
+ def self.from_file(path)
18
18
  source = File.read(path)
19
- parse(source, path)
19
+ new(source, path)
20
20
  end
21
21
 
22
- def self.parse(source, path = nil)
23
- buffer = Parser::Source::Buffer.new(path || '(string)')
24
- buffer.source = source
25
-
26
- builder = AST::Builder.new
27
- parser = Parser::CurrentRuby.new(builder)
28
-
29
- begin
30
- ast = parser.parse(buffer)
31
- new(buffer, ast, path)
32
- rescue Parser::SyntaxError => error
33
- new(buffer, nil, path, error)
34
- end
35
- end
36
-
37
- def initialize(buffer, ast, path = nil, syntax_error = nil)
38
- @buffer = buffer
39
- @ast = ast
22
+ def initialize(source, path = nil)
40
23
  @path = path
41
- @syntax_error = syntax_error
24
+ parse(source)
42
25
  end
43
26
 
44
27
  def to_s
45
28
  buffer.source
46
29
  end
30
+
31
+ private
32
+
33
+ def parse(source)
34
+ @buffer = Parser::Source::Buffer.new(@path || '(string)')
35
+ @buffer.source = source
36
+
37
+ builder = AST::Builder.new
38
+ parser = Parser::CurrentRuby.new(builder)
39
+
40
+ @ast = parser.parse(@buffer)
41
+ rescue Parser::SyntaxError, EncodingError => error
42
+ @error = error
43
+ end
47
44
  end
48
45
  end
@@ -4,18 +4,18 @@ require 'rainbow'
4
4
 
5
5
  module Transpec
6
6
  class Report
7
- attr_reader :records, :conversion_errors, :syntax_errors
7
+ attr_reader :records, :conversion_errors, :file_errors
8
8
 
9
9
  def initialize
10
10
  @records = []
11
11
  @conversion_errors = []
12
- @syntax_errors = []
12
+ @file_errors = []
13
13
  end
14
14
 
15
15
  def <<(other)
16
16
  records.concat(other.records)
17
17
  conversion_errors.concat(other.conversion_errors)
18
- syntax_errors.concat(other.syntax_errors)
18
+ file_errors.concat(other.file_errors)
19
19
  self
20
20
  end
21
21
 
@@ -113,13 +113,13 @@ module Transpec
113
113
  end
114
114
 
115
115
  def error_stats(base_color)
116
- color = if syntax_errors.empty?
116
+ color = if file_errors.empty?
117
117
  base_color
118
118
  else
119
119
  :red
120
120
  end
121
121
 
122
- colorize(pluralize(syntax_errors.count, 'error'), color)
122
+ colorize(pluralize(file_errors.count, 'error'), color)
123
123
  end
124
124
 
125
125
  def pluralize(number, thing, options = {})
@@ -20,7 +20,7 @@ module Transpec
20
20
 
21
21
  def specs
22
22
  @specs ||= FileFinder.find(@base_paths).map do |path|
23
- ProcessedSource.parse_file(path)
23
+ ProcessedSource.from_file(path)
24
24
  end
25
25
  end
26
26
 
@@ -5,7 +5,7 @@ module Transpec
5
5
  module Version
6
6
  MAJOR = 2
7
7
  MINOR = 3
8
- PATCH = 4
8
+ PATCH = 5
9
9
 
10
10
  def self.to_s
11
11
  [MAJOR, MINOR, PATCH].join('.')
@@ -6,7 +6,7 @@ shared_context 'parsed objects' do
6
6
 
7
7
  let(:processed_source) do
8
8
  require 'transpec/processed_source'
9
- Transpec::ProcessedSource.parse(source, source_path)
9
+ Transpec::ProcessedSource.new(source, source_path)
10
10
  end
11
11
 
12
12
  let(:ast) do
@@ -139,7 +139,7 @@ module Transpec
139
139
 
140
140
  include_examples 'aborts processing'
141
141
 
142
- it 'warns to the user' do
142
+ it 'warns to the version' do
143
143
  cli.should_receive(:warn).with(/rspec.+dependency/i)
144
144
  cli.run(args)
145
145
  end
@@ -176,6 +176,33 @@ module Transpec
176
176
  end
177
177
  end
178
178
 
179
+ context 'when an encoding error is raised while processing files' do
180
+ let(:args) { [invalid_encoding_file_path, valid_encoding_file_path] }
181
+ let(:invalid_encoding_file_path) { 'spec/invalid_example.rb' }
182
+ let(:valid_encoding_file_path) { 'spec/valid_example.rb' }
183
+
184
+ before do
185
+ create_file(invalid_encoding_file_path, <<-END)
186
+ # coding: utf-8
187
+ \xff
188
+ END
189
+
190
+ create_file(valid_encoding_file_path, 'this_is_valid_encoding')
191
+ end
192
+
193
+ it 'warns of the error' do
194
+ cli.should_receive(:warn)
195
+ .with('Encoding error in spec/invalid_example.rb. Skipping the file.')
196
+ cli.run(args)
197
+ end
198
+
199
+ it 'continues processing files' do
200
+ cli.should_receive(:puts).with("Converting #{invalid_encoding_file_path}")
201
+ cli.should_receive(:puts).with("Converting #{valid_encoding_file_path}")
202
+ cli.run(args)
203
+ end
204
+ end
205
+
179
206
  context 'when any other error is raised while running' do
180
207
  let(:args) { ['non-existent-file'] }
181
208
 
@@ -215,7 +242,7 @@ module Transpec
215
242
  let(:processed_source) do
216
243
  path = 'example.rb'
217
244
  create_file(path, source)
218
- ProcessedSource.parse_file(path)
245
+ ProcessedSource.from_file(path)
219
246
  end
220
247
 
221
248
  let(:spec_suite) { SpecSuite.new }
@@ -241,7 +268,7 @@ module Transpec
241
268
  END
242
269
  end
243
270
 
244
- it 'warns to user' do
271
+ it 'warns of the conversion error' do
245
272
  cli.should_receive(:warn) do |message|
246
273
  message.should =~ /cannot/i
247
274
  message.should =~ /context/i
@@ -262,7 +289,7 @@ module Transpec
262
289
  END
263
290
  end
264
291
 
265
- it 'warns to user' do
292
+ it 'warns of less accurate conversion' do
266
293
  cli.should_receive(:warn).with(/converted.+but.+incorrect/i)
267
294
  cli.convert_spec(processed_source, spec_suite)
268
295
  end
@@ -29,7 +29,7 @@ module Transpec
29
29
  end
30
30
 
31
31
  context 'when a processed source is passed' do
32
- let(:processed_source) { ProcessedSource.parse_file(file_path) }
32
+ let(:processed_source) { ProcessedSource.from_file(file_path) }
33
33
 
34
34
  it 'overwrites the file that the processed source was derived from' do
35
35
  converter.convert_file!(processed_source)
@@ -10,7 +10,7 @@ module Transpec
10
10
  include_context 'isolated environment'
11
11
 
12
12
  def find_node_in_file(file_path, &block)
13
- processed_source = ProcessedSource.parse_file(file_path)
13
+ processed_source = ProcessedSource.from_file(file_path)
14
14
  processed_source.ast.each_node.find(&block)
15
15
  end
16
16
 
@@ -16,38 +16,64 @@ module Transpec
16
16
  end
17
17
 
18
18
  it 'parses the file and returns processed source' do
19
- processed_source = ProcessedSource.parse_file(spec_path)
19
+ processed_source = ProcessedSource.from_file(spec_path)
20
20
  processed_source.path.should == spec_path
21
21
  processed_source.ast.should_not be_nil
22
22
  end
23
23
  end
24
24
 
25
- describe '.parse' do
26
- it 'parses the source and returns processed source' do
27
- processed_source = ProcessedSource.parse("puts 'foo'")
28
- processed_source.ast.should_not be_nil
25
+ subject(:processed_source) { ProcessedSource.new(source) }
26
+
27
+ describe '#ast' do
28
+ let(:source) { "puts 'foo'" }
29
+
30
+ it 'returns the root node of AST' do
31
+ processed_source.ast.should be_a(Parser::AST::Node)
29
32
  end
33
+ end
30
34
 
31
- context 'when a file path is passed' do
32
- let(:spec_path) { 'spec/example_spec.rb' }
35
+ describe '#path' do
36
+ let(:source) { "puts 'foo'" }
33
37
 
34
- it 'sets the path to the instance' do
35
- processed_source = ProcessedSource.parse("puts 'foo'", spec_path)
36
- processed_source.path.should == spec_path
38
+ context 'when a file path is passed to .new' do
39
+ subject(:processed_source) { ProcessedSource.new(source, '/path/to/file') }
40
+
41
+ it 'returns the path' do
42
+ processed_source.path.should == '/path/to/file'
37
43
  end
38
44
  end
39
45
 
40
- context 'when no file path is passed' do
41
- it 'does not set the path to the instance' do
42
- processed_source = ProcessedSource.parse("puts 'foo'")
46
+ context 'when no file path is passed to .new' do
47
+ it 'returns nil' do
43
48
  processed_source.path.should be_nil
44
49
  end
45
50
  end
51
+ end
52
+
53
+ describe '#syntax_error' do
54
+ context 'when the source is valid' do
55
+ let(:source) { "puts 'foo'" }
56
+
57
+ it 'returns nil' do
58
+ processed_source.error.should be_nil
59
+ end
60
+ end
61
+
62
+ context 'when the source is invalid' do
63
+ let(:source) { '<' }
64
+
65
+ it 'returns syntax error' do
66
+ processed_source.error.should be_a(Parser::SyntaxError)
67
+ end
68
+ end
46
69
 
47
- context 'when the file has invalid source' do
48
- it 'sets syntax error to the instance' do
49
- processed_source = ProcessedSource.parse('<')
50
- processed_source.syntax_error.should be_a(Parser::SyntaxError)
70
+ context 'when the source includes invalid byte sequence for the encoding' do
71
+ it 'returns encoding error' do
72
+ processed_source = ProcessedSource.new(<<-END)
73
+ # coding: utf-8
74
+ \xff
75
+ END
76
+ processed_source.error.should be_a(EncodingError)
51
77
  end
52
78
  end
53
79
  end
@@ -59,7 +85,7 @@ module Transpec
59
85
  "puts 'bar'"
60
86
  ].join("\n")
61
87
 
62
- processed_source = ProcessedSource.parse(source)
88
+ processed_source = ProcessedSource.new(source)
63
89
  processed_source.to_s.should == source
64
90
  end
65
91
  end
@@ -102,7 +102,7 @@ module Transpec
102
102
  report.records << Record.new('obj.stub(:message)', 'allow(obj).to receive(:message)')
103
103
  report.conversion_errors << ContextError.new('#should', '#expect', double('range'))
104
104
  report.conversion_errors << ContextError.new('#stub', '#allow', double('range'))
105
- report.syntax_errors << double('syntax error')
105
+ report.file_errors << double('file error')
106
106
  report
107
107
  end
108
108
 
@@ -118,8 +118,8 @@ module Transpec
118
118
  concated_report.should have(3).conversion_errors
119
119
  end
120
120
 
121
- it 'concats syntax errors' do
122
- concated_report.should have(1).syntax_errors
121
+ it 'concats file errors' do
122
+ concated_report.should have(1).file_errors
123
123
  end
124
124
  end
125
125
  end
data/transpec.gemspec CHANGED
@@ -22,7 +22,7 @@ Gem::Specification.new do |spec|
22
22
 
23
23
  spec.required_ruby_version = '>= 1.9.3'
24
24
 
25
- spec.add_runtime_dependency 'parser', '>= 2.1.6', '< 3.0'
25
+ spec.add_runtime_dependency 'parser', '>= 2.2.0.pre.3', '< 3.0'
26
26
  spec.add_runtime_dependency 'astrolabe', '>= 0.5.1', '< 0.6'
27
27
  spec.add_runtime_dependency 'bundler', '~> 1.3'
28
28
  spec.add_runtime_dependency 'rainbow', '>= 1.99.1', '< 3.0'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: transpec
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.3.4
4
+ version: 2.3.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yuji Nakayama
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-07-16 00:00:00.000000000 Z
11
+ date: 2014-07-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: parser
@@ -16,7 +16,7 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: 2.1.6
19
+ version: 2.2.0.pre.3
20
20
  - - "<"
21
21
  - !ruby/object:Gem::Version
22
22
  version: '3.0'
@@ -26,7 +26,7 @@ dependencies:
26
26
  requirements:
27
27
  - - ">="
28
28
  - !ruby/object:Gem::Version
29
- version: 2.1.6
29
+ version: 2.2.0.pre.3
30
30
  - - "<"
31
31
  - !ruby/object:Gem::Version
32
32
  version: '3.0'