rspec_starter 1.3.0 → 1.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +6 -0
- data/README.md +128 -0
- data/lib/rspec_starter/steps/prepare_database_step.rb +11 -14
- data/lib/rspec_starter/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 84e5bb86c48d3b70522183229c840a33c9bc494743fc92525c6ba0c56818c42f
|
4
|
+
data.tar.gz: 6d5fc579a8fce581378bbe3d60bec9acbdce422f829147361013ac66b4835745
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fd401edef689649b8fcefe70012cb71e89aa5f5851012526eadfc4f17eb239e3ccb1d4fb2f39b3b2dde12aa8010d5e95f3b3b17ef55650fd24e690e4bd4a418f
|
7
|
+
data.tar.gz: f46ac44e3b5d4608c4174ff94c70a7c65b25c4e2ed9f4d2fe3c730b237d17e55345d695075a5206d8ee112b144a862f41a8588c3a63568d92db674770a03131e
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,11 @@
|
|
1
1
|
# CHANGELOG
|
2
2
|
|
3
|
+
## 1.4.0 (Oct 12, 2018)
|
4
|
+
|
5
|
+
1. Isolate rebuild command string into a dedicated method.. (Issue #41)
|
6
|
+
1. Add instructions for creating custom steps. (Issue #43)
|
7
|
+
1. Fix database rebuild command hanging when there's too much output. (Issue #45)
|
8
|
+
|
3
9
|
## 1.3.0 (Aug 30, 2018)
|
4
10
|
|
5
11
|
1. Change `cri` version to `~> 2.0`. (Issue #37)
|
data/README.md
CHANGED
@@ -61,6 +61,134 @@ which tells start_rspec, to tell rspec, to only run the feature tests. Run the
|
|
61
61
|
|
62
62
|
$ bin/start_rspec --help
|
63
63
|
|
64
|
+
## Custom Steps
|
65
|
+
|
66
|
+
rspec_starter does not currently have support for creating custom steps. However, there are some techniques that can achieve the same results. rspec_starter currently implements 4 "steps" which can be turned on or off. The steps are implemented by classes and are evaluated in this order:
|
67
|
+
|
68
|
+
1. `VerifyXvfbStep` - Ensures xvfb is installed on systems where it should be used (.i.e. Linux).
|
69
|
+
1. `PrepareDatabaseStep` - Runs `rake db:drop db:create db:migrate RAILS_ENV=test`.
|
70
|
+
1. `RemoveTmpFolderStep` - Deletes the `tmp` folder.
|
71
|
+
1. `InvokeRspecStep` - Runs `bundle exec rspec`.
|
72
|
+
|
73
|
+
All steps implement an `execute` method that actually runs the step. You can inject custom code before or after any one of those steps.
|
74
|
+
|
75
|
+
#### Using prepend to Inject a Custom Module
|
76
|
+
|
77
|
+
One strategy is to open the `bin/start_rspec` file use `prepend` to inject a custom module into the class.
|
78
|
+
|
79
|
+
```ruby
|
80
|
+
require "bundler/setup"
|
81
|
+
require "rspec_starter"
|
82
|
+
|
83
|
+
# The path to the application's root folder.
|
84
|
+
APP_ROOT = Pathname.new File.expand_path('../../', __FILE__)
|
85
|
+
|
86
|
+
module CustomStep
|
87
|
+
def execute
|
88
|
+
# Place code above the super call if you want it run before the targeted step
|
89
|
+
super # super needs to be called if you want the targeted step to execute
|
90
|
+
# Place code after the super call if you want it run after the targeted step
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
RspecStarter::PrepareDatabaseStep.prepend CustomStep
|
95
|
+
|
96
|
+
Dir.chdir APP_ROOT do
|
97
|
+
RspecStarter.start(prepare_db: true, remove_tmp: true, allow_xvfb: true)
|
98
|
+
end
|
99
|
+
```
|
100
|
+
|
101
|
+
In the above example, the `PrepareDatabaseStep` is targeted. By prepending the `CustomStep` module to `PrepareDatabaseStep`, it ensures the `execute` in `CustomStep` is executed before the `execute` method in the targeted step.. At that point, you can execute any code you want, then call `super` to run targeted step, then run any code after the targeted step completes.
|
102
|
+
|
103
|
+
**Note:** If you're trying to run additional rake tasks on the database using this technique, it probably won't work. Your custom rake task will execute in a different process from the rake tasks that rspec_starter runs. This will prevent some changes from getting saved to the database. The techniques described below can help in this situation.
|
104
|
+
|
105
|
+
#### Customizing the Database Prep Step
|
106
|
+
|
107
|
+
Database preparation is performed by `PrepareDatabaseStep`. You can use the above `prepend` technique to add code before/after this step runs, but you can also customize the exact command that is run to prepare your db. By default, `PrepareDatabaseStep` runs `rake db:drop db:create db:migrate RAILS_ENV=test` on your application (as seen [here](https://github.com/roberts1000/rspec_starter/blob/v1.4.0/lib/rspec_starter/steps/prepare_database_step.rb#L46)). You can override that command with:
|
108
|
+
|
109
|
+
```
|
110
|
+
module CustomStep
|
111
|
+
def rebuild_command
|
112
|
+
"rake db:drop db:create db:migreate db:do_something_else RAILS_ENV=test"
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
RspecStarter::PrepareDatabaseStep.prepend CustomStep
|
117
|
+
```
|
118
|
+
|
119
|
+
In this case, calling `super` isn't needed because we don't care about the default implementation.
|
120
|
+
|
121
|
+
**Note:** When using this technique, any output written to the console by rake tasks will not be displayed because the `PrepareDatabaseStep` supresses the output.
|
122
|
+
|
123
|
+
#### Invoking Custom Rake Tasks
|
124
|
+
|
125
|
+
There are several ways to invoke rake tasks in Ruby. Backticks don't seem to work in `rspec_starter`, but calling `system` does work
|
126
|
+
|
127
|
+
```ruby
|
128
|
+
module CustomStep
|
129
|
+
def execute
|
130
|
+
super
|
131
|
+
system("bundle exec rake do_something_else_after_db_is_prepped")
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
135
|
+
RspecStarter::PrepareDatabaseStep.prepend CustomStep
|
136
|
+
```
|
137
|
+
|
138
|
+
You can also invoke the task without calling out to the system. This should also be faster since the Kernel won't have to setup the process.
|
139
|
+
|
140
|
+
```ruby
|
141
|
+
require 'rake'
|
142
|
+
require File.expand_path('config/environment', APP_ROOT)
|
143
|
+
ReplaceWithAppName::Application.load_tasks
|
144
|
+
|
145
|
+
module CustomTask
|
146
|
+
def execute
|
147
|
+
super
|
148
|
+
Rake::Task["do_something_else_after_db_is_prepped"].invoke
|
149
|
+
end
|
150
|
+
end
|
151
|
+
|
152
|
+
RspecStarter::PrepareDatabaseStep.prepend CustomTask
|
153
|
+
```
|
154
|
+
|
155
|
+
In this technique, you must replace the `ReplaceWithAppName` with the name of your Rails application. Open the `config/application.rb` file to find the correct name.
|
156
|
+
|
157
|
+
#### Appending to Existing Rake Tasks
|
158
|
+
|
159
|
+
You can also bypass rspec_starter completely and add additional logic to existing rake tasks. For example, if you **always** want to peform an extra task after `db:migrate` is executed, you can add the following to your `lib/db_migrate.rake` folder:
|
160
|
+
|
161
|
+
```ruby
|
162
|
+
namespace :db do
|
163
|
+
task :migrate do
|
164
|
+
# do something additional
|
165
|
+
end
|
166
|
+
end
|
167
|
+
```
|
168
|
+
|
169
|
+
or
|
170
|
+
|
171
|
+
```ruby
|
172
|
+
namespace :db do
|
173
|
+
task :migrate do
|
174
|
+
Rake::Task["another_task_name"].invoke
|
175
|
+
end
|
176
|
+
end
|
177
|
+
```
|
178
|
+
|
179
|
+
When you redefine an existing rake task, rake actually apends your custom code to the existing rake task instead of overwriting it. If needed, you can also add additional guards to conditionally add the custom logic:
|
180
|
+
|
181
|
+
|
182
|
+
```ruby
|
183
|
+
if Rails.env == "development"
|
184
|
+
namespace :db do
|
185
|
+
task :migrate do
|
186
|
+
Rake::Task["another_task_name"].invoke
|
187
|
+
end
|
188
|
+
end
|
189
|
+
end
|
190
|
+
```
|
191
|
+
|
64
192
|
## Configuration
|
65
193
|
|
66
194
|
The entire idea behind start_rspec is to standardize the process of starting RSpec for an application. You can modify the `bin/start_rspec` file to do whatever you want. If you open that file, you'll see that it does one thing - it calls the following command in the context of the root folder, of your project:
|
@@ -22,36 +22,33 @@ module RspecStarter
|
|
22
22
|
def execute
|
23
23
|
return @success_or_skipped = true if should_skip?
|
24
24
|
|
25
|
-
rebuild_cmd =
|
25
|
+
rebuild_cmd = rebuild_command
|
26
26
|
print "[#{@runner.step_num}] Preparing the test database with '#{rebuild_cmd.colorize(:light_blue)}' ... "
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
@success_or_skipped = successful?(output_array)
|
27
|
+
_stdout, stderr, _status = Open3.capture3(rebuild_cmd)
|
28
|
+
@success_or_skipped = successful?(stderr)
|
31
29
|
|
32
30
|
if @success_or_skipped
|
33
31
|
puts "Success".colorize(:green)
|
34
|
-
puts output_array
|
35
32
|
else
|
36
33
|
puts "Fail".colorize(:red) + "\n\n"
|
37
|
-
puts
|
34
|
+
puts stderr
|
38
35
|
puts "\n\nThere was an error rebuilding the test database. See the output above for details.".colorize(:red)
|
39
36
|
puts "or manually run '#{rebuild_cmd}' for more information.".colorize(:red)
|
40
37
|
end
|
41
38
|
end
|
42
39
|
|
40
|
+
|
43
41
|
private
|
44
42
|
|
43
|
+
def rebuild_command
|
44
|
+
"rake db:drop db:create db:migrate RAILS_ENV=test"
|
45
|
+
end
|
46
|
+
|
45
47
|
# Simply checking the exitstatus isn't good enough. When rake aborts due to a bug, it will still
|
46
48
|
# return a zero exit status. We need to see if 'rake aborted!' has been written to the output.
|
47
|
-
def successful?(
|
49
|
+
def successful?(stderr)
|
48
50
|
return false if $CHILD_STATUS.exitstatus.nonzero?
|
49
|
-
|
50
|
-
end
|
51
|
-
|
52
|
-
def prepare_output_array(array)
|
53
|
-
(0..array.size - 1).each { |i| array[i] = " #{array[i].strip}".colorize(:red) }
|
54
|
-
array
|
51
|
+
!stderr.include?("rake aborted!")
|
55
52
|
end
|
56
53
|
end
|
57
54
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rspec_starter
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Roberts
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-10-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|