transpec 0.1.3 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (38) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +5 -0
  3. data/README.md +35 -3
  4. data/README.md.erb +35 -3
  5. data/lib/transpec/cli.rb +50 -4
  6. data/lib/transpec/commit_message.rb +25 -0
  7. data/lib/transpec/git.rb +18 -0
  8. data/lib/transpec/record.rb +28 -0
  9. data/lib/transpec/report.rb +109 -0
  10. data/lib/transpec/rewriter.rb +17 -8
  11. data/lib/transpec/syntax.rb +25 -22
  12. data/lib/transpec/syntax/be_close.rb +6 -0
  13. data/lib/transpec/syntax/double.rb +5 -0
  14. data/lib/transpec/syntax/matcher.rb +17 -1
  15. data/lib/transpec/syntax/method_stub.rb +40 -8
  16. data/lib/transpec/syntax/raise_error.rb +17 -0
  17. data/lib/transpec/syntax/send_node_syntax.rb +4 -0
  18. data/lib/transpec/syntax/should.rb +23 -2
  19. data/lib/transpec/syntax/should_receive.rb +54 -2
  20. data/lib/transpec/version.rb +2 -2
  21. data/spec/.rubocop.yml +1 -1
  22. data/spec/support/shared_context.rb +10 -0
  23. data/spec/transpec/cli_spec.rb +76 -29
  24. data/spec/transpec/commit_message_spec.rb +63 -0
  25. data/spec/transpec/configuration_spec.rb +1 -1
  26. data/spec/transpec/git_spec.rb +114 -38
  27. data/spec/transpec/record_spec.rb +18 -0
  28. data/spec/transpec/report_spec.rb +89 -0
  29. data/spec/transpec/rewriter_spec.rb +5 -0
  30. data/spec/transpec/syntax/be_close_spec.rb +10 -1
  31. data/spec/transpec/syntax/double_spec.rb +10 -0
  32. data/spec/transpec/syntax/matcher_spec.rb +31 -0
  33. data/spec/transpec/syntax/method_stub_spec.rb +53 -44
  34. data/spec/transpec/syntax/raise_error_spec.rb +64 -24
  35. data/spec/transpec/syntax/should_receive_spec.rb +67 -7
  36. data/spec/transpec/syntax/should_spec.rb +26 -0
  37. data/tasks/test.rake +27 -12
  38. metadata +11 -2
@@ -4,8 +4,8 @@ module Transpec
4
4
  # http://semver.org/
5
5
  module Version
6
6
  MAJOR = 0
7
- MINOR = 1
8
- PATCH = 3
7
+ MINOR = 2
8
+ PATCH = 0
9
9
 
10
10
  def self.to_s
11
11
  [MAJOR, MINOR, PATCH].join('.')
data/spec/.rubocop.yml CHANGED
@@ -8,7 +8,7 @@ Void:
8
8
 
9
9
  # Lengthen for long descriptions.
10
10
  LineLength:
11
- Max: 110
11
+ Max: 120
12
12
 
13
13
  # raise_error matcher always requires {} form block.
14
14
  Blocks:
@@ -50,3 +50,13 @@ shared_context 'isolated environment' do
50
50
  end
51
51
  end
52
52
  end
53
+
54
+ shared_context 'inside of git repository' do
55
+ around do |example|
56
+ Dir.mkdir('repo')
57
+ Dir.chdir('repo') do
58
+ `git init`
59
+ example.run
60
+ end
61
+ end
62
+ end
@@ -25,20 +25,40 @@ module Transpec
25
25
  end
26
26
  end
27
27
 
28
+ describe '#generates_commit_message?' do
29
+ subject { cli.generates_commit_message? }
30
+
31
+ context 'by default' do
32
+ it { should be_false }
33
+ end
34
+ end
35
+
28
36
  describe '#run' do
37
+ include_context 'isolated environment'
38
+
39
+ subject { cli.run(args) }
40
+
41
+ let(:args) { [file_path] }
42
+ let(:file_path) { 'spec/example_spec.rb' }
43
+ let(:file_content) do
44
+ <<-END
45
+ describe 'something' do
46
+ it 'is 1' do
47
+ 1.should == 1
48
+ end
49
+ end
50
+ END
51
+ end
52
+
29
53
  before do
30
54
  cli.stub(:puts)
31
55
  cli.stub(:warn)
32
- cli.stub(:target_files).and_return(args)
56
+ create_file(file_path, file_content)
33
57
  end
34
58
 
35
- subject { cli.run(args) }
36
- let(:args) { ['some_file.rb'] }
37
- let(:rewriter) { double('rewriter').as_null_object }
38
-
39
59
  shared_examples 'rewrites files' do
40
60
  it 'rewrites files' do
41
- rewriter.should_receive(:rewrite_file!)
61
+ cli.should_receive(:process_file)
42
62
  cli.run(args)
43
63
  end
44
64
 
@@ -47,15 +67,7 @@ module Transpec
47
67
  end
48
68
  end
49
69
 
50
- shared_context 'stubbed rewriter' do
51
- before do
52
- Rewriter.stub(:new).and_return(rewriter)
53
- end
54
- end
55
-
56
70
  context 'when git is available' do
57
- include_context 'stubbed rewriter'
58
-
59
71
  before { Git.stub(:command_available?).and_return(true) }
60
72
 
61
73
  context 'and inside of a repository' do
@@ -68,7 +80,7 @@ module Transpec
68
80
  before { cli.stub(:forced?).and_return(false) }
69
81
 
70
82
  it 'aborts processing' do
71
- rewriter.should_not_receive(:rewrite_file!)
83
+ cli.should_not_receive(:process_file)
72
84
  cli.run(args).should be_false
73
85
  end
74
86
 
@@ -100,14 +112,11 @@ module Transpec
100
112
  end
101
113
 
102
114
  context 'when git is not available' do
103
- include_context 'stubbed rewriter'
104
115
  before { Git.stub(:command_available?).and_return(false) }
105
116
  include_examples 'rewrites files'
106
117
  end
107
118
 
108
119
  context 'when a syntax error is raised while processing files' do
109
- include_context 'isolated environment'
110
-
111
120
  let(:args) { [invalid_syntax_file_path, valid_syntax_file_path] }
112
121
  let(:invalid_syntax_file_path) { 'invalid_example.rb' }
113
122
  let(:valid_syntax_file_path) { 'valid_example.rb' }
@@ -133,30 +142,23 @@ module Transpec
133
142
  end
134
143
 
135
144
  context 'when any other error is raised while running' do
136
- include_context 'stubbed rewriter'
137
-
138
- before do
139
- cli.stub(:parse_options).and_raise(ArgumentError, 'No such file or directory - non-existent-file')
140
- end
145
+ let(:args) { ['non-existent-file'] }
141
146
 
142
147
  it 'return false' do
143
148
  should be_false
144
149
  end
145
150
 
146
151
  it 'prints message of the exception' do
147
- cli.should_receive(:warn).with('No such file or directory - non-existent-file')
148
- cli.run([])
152
+ cli.should_receive(:warn).with(/No such file or directory/)
153
+ cli.run(args)
149
154
  end
150
155
  end
151
156
 
152
157
  context 'when no target paths are specified' do
153
- include_context 'isolated environment'
154
- include_context 'stubbed rewriter'
155
-
156
158
  let(:args) { [] }
157
159
 
158
160
  context 'and there is "spec" directory' do
159
- before { Dir.mkdir('spec') }
161
+ let(:file_path) { 'spec/example_spec.rb' }
160
162
 
161
163
  it 'targets files in the "spec" directoy' do
162
164
  cli.should_receive(:target_files).with(['spec'])
@@ -165,11 +167,35 @@ module Transpec
165
167
  end
166
168
 
167
169
  context 'and there is not "spec" directory' do
170
+ let(:file_path) { 'example_spec.rb' }
171
+
168
172
  it 'aborts' do
169
173
  should be_false
170
174
  end
171
175
  end
172
176
  end
177
+
178
+ context 'when -m/--commit-message option is specified' do
179
+ include_context 'inside of git repository'
180
+
181
+ let(:args) { ['--force', '--commit-message', file_path] }
182
+
183
+ context 'and any conversion is done' do
184
+ it 'writes commit message to .git/COMMIT_EDITMSG' do
185
+ cli.run(args)
186
+ File.read('.git/COMMIT_EDITMSG').should start_with('Convert specs')
187
+ end
188
+ end
189
+
190
+ context 'and no conversion is done' do
191
+ let(:file_content) { '' }
192
+
193
+ it 'does not writes commit message' do
194
+ cli.run(args)
195
+ File.exist?('.git/COMMIT_EDITMSG').should be_false
196
+ end
197
+ end
198
+ end
173
199
  end
174
200
 
175
201
  describe '#process_file' do
@@ -232,6 +258,27 @@ module Transpec
232
258
  end
233
259
  end
234
260
 
261
+ describe '-m/--commit-message option' do
262
+ include_context 'isolated environment'
263
+
264
+ let(:args) { ['--commit-message'] }
265
+
266
+ context 'when inside of git repository' do
267
+ include_context 'inside of git repository'
268
+
269
+ it 'sets #generates_commit_message? true' do
270
+ cli.parse_options(args)
271
+ cli.generates_commit_message?.should be_true
272
+ end
273
+ end
274
+
275
+ context 'when not inside of git repository' do
276
+ it 'raises error' do
277
+ -> { cli.parse_options(args) }.should raise_error(/not in a Git repository/)
278
+ end
279
+ end
280
+ end
281
+
235
282
  describe '-d/--disable option' do
236
283
  [
237
284
  ['expect_to_matcher', :convert_to_expect_to_matcher?],
@@ -0,0 +1,63 @@
1
+ # coding: utf-8
2
+
3
+ require 'spec_helper'
4
+ require 'transpec/commit_message'
5
+ require 'transpec/report'
6
+ require 'transpec/record'
7
+
8
+ module Transpec
9
+ describe CommitMessage do
10
+ subject(:commit_message) { CommitMessage.new(report, cli_args) }
11
+ let(:report) { Report.new }
12
+ let(:cli_args) { %w(--force --commit-message) }
13
+
14
+ before do
15
+ report.records << Record.new('obj.stub(:message)', 'allow(obj).to receive(:message)')
16
+ report.records << Record.new('obj.should', 'expect(obj).to')
17
+ report.records << Record.new('obj.should', 'expect(obj).to')
18
+ end
19
+
20
+ describe '#to_s' do
21
+ it 'wraps lines within 72 characters' do
22
+ commit_message.to_s.each_line do |line|
23
+ line.chomp.size.should <= 72
24
+ end
25
+ end
26
+
27
+ let(:lines) { commit_message.to_s.lines.to_a }
28
+
29
+ it 'has concise summary at first line' do
30
+ lines[0].chomp.should == 'Convert specs to latest RSpec syntax with Transpec'
31
+ end
32
+
33
+ it 'has blank line at second line' do
34
+ lines[1].chomp.should be_empty
35
+ end
36
+
37
+ let(:body_lines) { lines[2..-1] }
38
+
39
+ it 'has Transpec description at the beginning of the body' do
40
+ body_lines[0].chomp
41
+ .should match(/^This conversion is done by Transpec \d+\.\d+\.\d+ with the following command:$/)
42
+ body_lines[1].chomp
43
+ .should == ' transpec --force --commit-message'
44
+ end
45
+
46
+ it 'has blank line after the preface in the body' do
47
+ body_lines[2].chomp.should be_empty
48
+ end
49
+
50
+ it 'has conversion summary in the body' do
51
+ body_lines[3..-1].join('').should == <<-END.gsub(/^\s+\|/, '')
52
+ |* 2 conversions
53
+ | from: obj.should
54
+ | to: expect(obj).to
55
+ |
56
+ |* 1 conversion
57
+ | from: obj.stub(:message)
58
+ | to: allow(obj).to receive(:message)
59
+ END
60
+ end
61
+ end
62
+ end
63
+ end
@@ -43,7 +43,7 @@ module Transpec
43
43
 
44
44
  context 'when a form other than "not_to" or "to_not" is passed' do
45
45
  it 'raises error' do
46
- proc do
46
+ lambda do
47
47
  configuration.negative_form_of_to = 'foo'
48
48
  end.should raise_error(ArgumentError)
49
49
  end
@@ -20,66 +20,142 @@ module Transpec
20
20
  end
21
21
  end
22
22
 
23
- shared_context 'inside of git repository' do
24
- around do |example|
25
- Dir.mkdir('repo')
26
- Dir.chdir('repo') do
27
- `git init`
28
- example.run
29
- end
30
- end
31
- end
32
-
33
23
  describe '.inside_of_repository?' do
34
24
  subject { Git.inside_of_repository? }
35
25
 
36
- context 'when the current directory is inside of git repository' do
37
- include_context 'inside of git repository'
38
- it { should be_true }
26
+ context 'when git command is avaialable' do
27
+ context 'and the current directory is inside of git repository' do
28
+ include_context 'inside of git repository'
29
+ it { should be_true }
30
+ end
31
+
32
+ context 'and the current directory is not inside of git repository' do
33
+ it { should be_false }
34
+ end
39
35
  end
40
36
 
41
- context 'when the current directory is not inside of git repository' do
42
- it { should be_false }
37
+ context 'when git command is not avaialable' do
38
+ before { Git.stub(:command_available?).and_return(false) }
39
+
40
+ it 'raises error' do
41
+ -> { Git.inside_of_repository? }.should raise_error(/command is not available/)
42
+ end
43
43
  end
44
44
  end
45
45
 
46
46
  describe '.clean?' do
47
- include_context 'inside of git repository'
48
-
49
47
  subject { Git.clean? }
50
48
 
51
- before do
52
- File.write('foo', 'This is a sample file')
53
- `git add .`
54
- `git commit -m 'Initial commit'`
55
- end
49
+ context 'when inside of git repository' do
50
+ include_context 'inside of git repository'
56
51
 
57
- context 'when there are no changes' do
58
- it { should be_true }
52
+ before do
53
+ File.write('foo', 'This is a sample file')
54
+ `git add .`
55
+ `git commit -m 'Initial commit'`
56
+ end
57
+
58
+ context 'and there are no changes' do
59
+ it { should be_true }
60
+ end
61
+
62
+ context 'and there is an untracked file' do
63
+ before { File.write('bar', 'This is an untracked file') }
64
+ it { should be_false }
65
+ end
66
+
67
+ context 'and there is a deleted file' do
68
+ before { File.delete('foo') }
69
+ it { should be_false }
70
+ end
71
+
72
+ context 'and there is a not staged change' do
73
+ before { File.write('foo', 'This is modified content') }
74
+ it { should be_false }
75
+ end
76
+
77
+ context 'and there is a staged change' do
78
+ before do
79
+ File.write('foo', 'This is modified content')
80
+ `git add .`
81
+ end
82
+
83
+ it { should be_false }
84
+ end
59
85
  end
60
86
 
61
- context 'when there is an untracked file' do
62
- before { File.write('bar', 'This is an untracked file') }
63
- it { should be_false }
87
+ context 'when not inside of git repository' do
88
+ it 'raises error' do
89
+ -> { Git.clean? }.should raise_error(/is not a Git repository/)
90
+ end
64
91
  end
92
+ end
65
93
 
66
- context 'when there is a deleted file' do
67
- before { File.delete('foo') }
68
- it { should be_false }
94
+ describe '.repository_root' do
95
+ context 'when inside of git repository' do
96
+ include_context 'inside of git repository'
97
+
98
+ context 'and the current working directory is the repository root' do
99
+ it 'returns the repository root path' do
100
+ Git.repository_root.should == File.expand_path('.')
101
+ end
102
+ end
103
+
104
+ context 'and the current working directory is not the repository root' do
105
+ around do |example|
106
+ Dir.mkdir('dir')
107
+ Dir.chdir('dir') do
108
+ example.run
109
+ end
110
+ end
111
+
112
+ it 'returns the repository root path' do
113
+ Git.repository_root.should == File.expand_path('..')
114
+ end
115
+ end
69
116
  end
70
117
 
71
- context 'when there is a not staged change' do
72
- before { File.write('foo', 'This is modified content') }
73
- it { should be_false }
118
+ context 'when not inside of git repository' do
119
+ it 'raises error' do
120
+ -> { Git.repository_root }.should raise_error(/is not a Git repository/)
121
+ end
74
122
  end
123
+ end
75
124
 
76
- context 'when there is a staged change' do
77
- before do
78
- File.write('foo', 'This is modified content')
79
- `git add .`
125
+ describe '.write_commit_message' do
126
+ let(:message) { 'This is the commit message.' }
127
+
128
+ context 'when inside of git repository' do
129
+ include_context 'inside of git repository'
130
+
131
+ context 'and there is .git directory in the current working directory' do
132
+ it 'writes the message to .git/COMMIT_EDITMSG' do
133
+ Git.write_commit_message(message)
134
+ File.read('.git/COMMIT_EDITMSG').should == message
135
+ end
80
136
  end
81
137
 
82
- it { should be_false }
138
+ context 'and there is not .git directory in the current working directory' do
139
+ around do |example|
140
+ Dir.mkdir('dir')
141
+ Dir.chdir('dir') do
142
+ example.run
143
+ end
144
+ end
145
+
146
+ it 'writes the message to .git/COMMIT_EDITMSG in repository root directory' do
147
+ Git.write_commit_message(message)
148
+ File.read('../.git/COMMIT_EDITMSG').should == message
149
+ end
150
+ end
151
+ end
152
+
153
+ context 'when not inside of git repository' do
154
+ it 'raises error' do
155
+ lambda do
156
+ Git.write_commit_message(message)
157
+ end.should raise_error(/is not a Git repository/)
158
+ end
83
159
  end
84
160
  end
85
161
  end