gemika 0.1.3 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +498 -1
- data/lib/gemika/database.rb +39 -19
- data/lib/gemika/doc/minidusen_test.png +0 -0
- data/lib/gemika/tasks/database.rb +1 -0
- data/lib/gemika/tasks.rb +2 -0
- data/lib/gemika/version.rb +1 -1
- data/lib/gemika.rb +2 -0
- metadata +6 -3
- /data/lib/gemika/{matrix_tasks.rb → tasks/matrix.rb} +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3ffd566679b3249b50ab2132f035b9c315ebe65d
|
4
|
+
data.tar.gz: 4554693a8ccd66887918becad1ac0aa5f2bdcc72
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4577c661e63cc764412c55e84217b423ba845ea95dbef1eba83323da8af9c7caf997b59214519a59649313a94436a28b88bc68866295e57674755cb3eb585c6d
|
7
|
+
data.tar.gz: d399f52bee37862fcdb6233f41adda53b5aa4fa940128bd7dc4712cbb2103f3708b2603ce3ed5e5b2624d31049d3058ab68ca8f496a8ae76823ba1ac62ae51c5
|
data/README.md
CHANGED
@@ -1 +1,498 @@
|
|
1
|
-
#
|
1
|
+
# Gemika
|
2
|
+
|
3
|
+
## Test a Ruby gem against multiple versions of everything
|
4
|
+
|
5
|
+
Gemika helps you test your gem against multiple versions of Ruby, gem dependencies and database types.
|
6
|
+
|
7
|
+
Here's what Gemika can give your test's development setup (all steps are opt-in):
|
8
|
+
|
9
|
+
- Test one codebase against multiple sets of gem dependency sets (e.g. Rails 4.2, Rails 5.0).
|
10
|
+
- Test one codebase against multiple Ruby versions (e.g. Ruby 2.1.8, Ruby 2.3.1).
|
11
|
+
- Test one codebase against multiple database types (currently MySQL or PostgreSQL).
|
12
|
+
- Compute a matrix of all possible dependency permutations (Ruby, gem set, database type). Manually exclude incompatible dependency permutations (e.g. Rails 5.0 does not work with Ruby 2.1).
|
13
|
+
- Let developers enter their local credentials for MySQL and PostgreSQL in a `database.yml` file.
|
14
|
+
- Define default Ruby version, gem dependencies and database for developers who don't care about every possible permutation for everyday work.
|
15
|
+
- Help configure a [Travis CI](https://travis-ci.org/) build that tests every dependency permutation after each `git push`.
|
16
|
+
- Share your Ruby / gem dependeny / database permutation between local development and Travis CI.
|
17
|
+
- Define an [ActiveRecord database migration](http://api.rubyonrails.org/classes/ActiveRecord/Migration.html) that sets up your test database.
|
18
|
+
- Automatically drop and re-create your test database before each run of your test suite.
|
19
|
+
- Configure RSpec to wrap each example in a transaction that is rolled back when the example ends. This way each example starts with a blank database.
|
20
|
+
|
21
|
+
|
22
|
+
## Requirements
|
23
|
+
|
24
|
+
- Gemika currently assumes you're testing with [RSpec](http://rspec.info/).
|
25
|
+
- If you use any database-related features, you need `activaterecord` as a development dependency
|
26
|
+
|
27
|
+
|
28
|
+
## Example directory structure
|
29
|
+
|
30
|
+
Below you can see the directory of a gem with a completed Gemika testing setup. The next section describes how to get there:
|
31
|
+
|
32
|
+
```shell
|
33
|
+
gemfiles/Gemfile.set1 # First dependency set. Should include development dependencies and gemika.
|
34
|
+
gemfiles/Gemfile.set1.lock # Generated by `rake matrix:install`
|
35
|
+
gemfiles/Gemfile.set2 # Second dependency set. Should include development dependencies and gemika.
|
36
|
+
gemfiles/Gemfile.set2.lock # Generated by `rake matrix:install`
|
37
|
+
gemfiles/Gemfile.set3 # Third dependency set. Should include development dependencies and gemika.
|
38
|
+
gemfiles/Gemfile.set3.lock # Generated by `rake matrix:install`
|
39
|
+
Gemfile -> gemfiles/Gemfile.set2 # Symlink to default Gemfile for development
|
40
|
+
Gemfile.lock -> gemfiles/Gemfile.set2.lock # Symlink to default Gemfile.lock for development
|
41
|
+
.ruby-version # Default Ruby version for development
|
42
|
+
.gitignore # Should ignore spec/support/database.yml
|
43
|
+
.travis.yml # Configures all tested Ruby / gemfile combinations, for both local development and Travis CI
|
44
|
+
my_gem.gemspec # Specification for your gem
|
45
|
+
Rakefile # Should require 'gemika/tasks'
|
46
|
+
README.md # README for your gem
|
47
|
+
lib/my_gem.rb # Library files for your gem
|
48
|
+
lib/my_gem/my_class.rb # Class delivered by your gem
|
49
|
+
lib/my_gem/version.rb # Version definition for your gem
|
50
|
+
spec/spec_helper.rb # Requires 'gemika' and all files in support folder
|
51
|
+
spec/support/database.rb # Calls `Gemika.rewrite_schema! { ... }`
|
52
|
+
spec/support/database.yml # Local
|
53
|
+
spec/support/database.travis.yml # Calls `Gemika.rewrite_schema! { ... }`
|
54
|
+
spec/my_gem/my_class_spec.rb # Tests for your gem
|
55
|
+
```
|
56
|
+
|
57
|
+
For a live example of this setup, check the [makandra/minidusen](https://github.com/makandra/minidusen) repo.
|
58
|
+
|
59
|
+
|
60
|
+
## Step-by-step integration
|
61
|
+
|
62
|
+
|
63
|
+
### Have a standard gem setup
|
64
|
+
|
65
|
+
Gemika expects a standard gem directory that looks roughly like this:
|
66
|
+
|
67
|
+
```
|
68
|
+
my_gem.gemspec # Specification for your gem
|
69
|
+
Rakefile # Should require 'gemika/tasks'
|
70
|
+
lib/my_gem.rb # Library files for your gem
|
71
|
+
```
|
72
|
+
|
73
|
+
If you don't have a directory yet, you can [ask Bundler to create it for you](http://bundler.io/rubygems.html):
|
74
|
+
|
75
|
+
```
|
76
|
+
bundle gem my_gem
|
77
|
+
```
|
78
|
+
|
79
|
+
This will create a new directory named `my_gem` with your new gem skeleton.
|
80
|
+
|
81
|
+
|
82
|
+
### Install Gemika
|
83
|
+
|
84
|
+
Switch to your favorite Ruby version and install the Gemika gem:
|
85
|
+
|
86
|
+
```shell
|
87
|
+
gem install gemika
|
88
|
+
```
|
89
|
+
|
90
|
+
Future contributors to your gem can install Gemika using the Gemfiles we will create below.
|
91
|
+
|
92
|
+
|
93
|
+
### Rake tasks
|
94
|
+
|
95
|
+
Add this to your `Rakefile` to gain tasks `matrix:install`, `matrix:spec`, `matrix:update`.
|
96
|
+
|
97
|
+
```ruby
|
98
|
+
begin
|
99
|
+
require 'gemika/tasks'
|
100
|
+
rescue LoadError
|
101
|
+
puts 'Run `gem install gemika` for additional tasks'
|
102
|
+
end
|
103
|
+
```
|
104
|
+
|
105
|
+
Check that the tasks appear with `rake -T`:
|
106
|
+
|
107
|
+
```shell
|
108
|
+
rake matrix:install # Install all Ruby 2.2.4 gemfiles
|
109
|
+
rake matrix:spec # Run specs for all Ruby 2.2.4 gemfiles
|
110
|
+
rake matrix:update[gems] # Update all Ruby 2.2.4 gemfiles
|
111
|
+
```
|
112
|
+
|
113
|
+
We also recommend to make `matrix:spec` the default task in your `Rakefile`:
|
114
|
+
|
115
|
+
```ruby
|
116
|
+
task :default => 'matrix:spec'
|
117
|
+
```
|
118
|
+
|
119
|
+
### Define multiple dependency sets
|
120
|
+
|
121
|
+
We are now creating one `Gemfile` for each set of gems and database type you'd like to test again.
|
122
|
+
|
123
|
+
First, create a directory for the gemfiles:
|
124
|
+
|
125
|
+
```shell
|
126
|
+
mkdir gemfiles
|
127
|
+
```
|
128
|
+
|
129
|
+
For each dependency set, create a `Gemfile` in the `gemfiles` directory that contains:
|
130
|
+
|
131
|
+
1. The runtime dependencies you'd like to test against (e.g. Rails 5)
|
132
|
+
2. The development dependencies for that set (e.g. `rspec`) in a version that is compatible with these runtime gependencies.
|
133
|
+
3. The `gemika` gem
|
134
|
+
4. Your own gem from path `..`
|
135
|
+
|
136
|
+
For instance, if one dependency set is Rails 3.2 with a MySQL database, we would create `gemfiles/Gemfile.4.2.mysql2` with these contents:
|
137
|
+
|
138
|
+
```ruby
|
139
|
+
gem 'rails', '~>3.2.22'
|
140
|
+
gem 'mysql2', '= 0.3.17'
|
141
|
+
gem 'rspec', '~> 3.4'
|
142
|
+
|
143
|
+
gem 'rake'
|
144
|
+
gem 'byebug'
|
145
|
+
gem 'gemika'
|
146
|
+
|
147
|
+
gem 'my_gem', :path => '..'
|
148
|
+
```
|
149
|
+
|
150
|
+
If a second dependency is Rails 5.0 with a PostgreSQL database, we would create `gemfiles/Gemfile.5.0.pg` with these contents:
|
151
|
+
|
152
|
+
```ruby
|
153
|
+
gem 'rails', '~>5.0.0'
|
154
|
+
gem 'pg', '~>0.18.4'
|
155
|
+
gem 'rspec', '~>3.5'
|
156
|
+
|
157
|
+
gem 'rake'
|
158
|
+
gem 'byebug'
|
159
|
+
gem 'gemika'
|
160
|
+
|
161
|
+
gem 'my_gem', :path => '..'
|
162
|
+
```
|
163
|
+
|
164
|
+
In this example, your `gemfiles` directory should now look like this:
|
165
|
+
|
166
|
+
```
|
167
|
+
gemfiles/Gemfile.4.2.mysql2
|
168
|
+
gemfiles/Gemfile.5.0.pg
|
169
|
+
```
|
170
|
+
|
171
|
+
Create lockfiles for each bundle by running:
|
172
|
+
|
173
|
+
```shell
|
174
|
+
rake matrix:install
|
175
|
+
```
|
176
|
+
|
177
|
+
In this example, your `gemfiles` directory should now contain a lockfile for each gemfile:
|
178
|
+
|
179
|
+
```
|
180
|
+
gemfiles/Gemfile.4.2.mysql2
|
181
|
+
gemfiles/Gemfile.4.2.mysql2.lock
|
182
|
+
gemfiles/Gemfile.5.0.pg
|
183
|
+
gemfiles/Gemfile.5.0.pg.lock
|
184
|
+
```
|
185
|
+
|
186
|
+
Gemfiles and lockfiles should be committed to your repo.
|
187
|
+
|
188
|
+
Make sure to re-run `rake matrix:install` after each change to your gemfiles, and commit the generated changes.
|
189
|
+
|
190
|
+
|
191
|
+
### Define combinations of gemfiles and Ruby versions
|
192
|
+
|
193
|
+
We will now define a test matrix that contains all permutations of gemfiles and tested Ruby versions.
|
194
|
+
|
195
|
+
We store the matrix in a `.travis.yml` file, **even if the project is not using Travis CI**. This allows us to configure the matrix once and us it for both local developent and Travis CI builds.
|
196
|
+
|
197
|
+
Create a `.travis.yml` that lists all gemfiles and Ruby versions you'd like to test against:
|
198
|
+
|
199
|
+
```yaml
|
200
|
+
rvm:
|
201
|
+
- 2.1.8
|
202
|
+
- 2.2.4
|
203
|
+
- 2.3.1
|
204
|
+
|
205
|
+
gemfile:
|
206
|
+
- gemfiles/Gemfile.3.2.mysql2
|
207
|
+
- gemfiles/Gemfile.4.2.mysql2
|
208
|
+
- gemfiles/Gemfile.4.2.pg
|
209
|
+
- gemfiles/Gemfile.5.0.mysql2
|
210
|
+
- gemfiles/Gemfile.5.0.pg
|
211
|
+
```
|
212
|
+
|
213
|
+
Don't mind the `rvm` key if you're using a different version manager locally (like rbenv). Things will still work.
|
214
|
+
|
215
|
+
|
216
|
+
#### Excluding incompatible matrix rows
|
217
|
+
|
218
|
+
There might be incompatible combinations of gemfiles and Rubies, e.g. Rails 5.0 does not work with Ruby 2.1 or lower. In this case, add an `matrix`/`exclude` key to your `.travis.yml`:
|
219
|
+
|
220
|
+
```yaml
|
221
|
+
matrix:
|
222
|
+
exclude:
|
223
|
+
- rvm: 2.1.8
|
224
|
+
gemfile: gemfiles/Gemfile.5.0.mysql2
|
225
|
+
- rvm: 2.1.8
|
226
|
+
gemfile: gemfiles/Gemfile.5.0.pg
|
227
|
+
```
|
228
|
+
|
229
|
+
|
230
|
+
### Default Ruby and dependency set
|
231
|
+
|
232
|
+
Your project will be more approachable if you're defining a default Ruby and dependency set. This way a developer can make changes and run code without knowing about the test matrix.
|
233
|
+
|
234
|
+
Create a `.ruby-version` file with the default Ruby version:
|
235
|
+
|
236
|
+
```
|
237
|
+
2.2.4
|
238
|
+
```
|
239
|
+
|
240
|
+
Choose a default dependency set and symlink both gemfile and lockfile to your project root:
|
241
|
+
|
242
|
+
```
|
243
|
+
ln -s gemfiles/Gemfile.4.2.mysql2 Gemfile
|
244
|
+
ln -s gemfiles/Gemfile.4.2.mysql2.lock Gemfile.lock
|
245
|
+
```
|
246
|
+
|
247
|
+
Commit both `.ruby-version` and symlinks to your repo.
|
248
|
+
|
249
|
+
We recommend to setup Travis CI (see below) to check the entire test matrix after each push, even if a developer only tested with the defaults.
|
250
|
+
|
251
|
+
|
252
|
+
### Test databases
|
253
|
+
|
254
|
+
Create a local test database `my_gem_test` in either MySQL, PostgreSQL or both (depending on what you support). If you want to test against multiple database types, you should have created one gemfile for each type above.
|
255
|
+
|
256
|
+
Now create a file `spec/support/database.yml` that contains your local database credentials:
|
257
|
+
|
258
|
+
```yaml
|
259
|
+
mysql:
|
260
|
+
database: my_gem_test
|
261
|
+
host: localhost
|
262
|
+
username: root
|
263
|
+
password: secret
|
264
|
+
|
265
|
+
postgresql:
|
266
|
+
database: minidusen_test
|
267
|
+
user:
|
268
|
+
password:
|
269
|
+
```
|
270
|
+
|
271
|
+
We don't want to commit our local credentials, so add a line to your `.gitignore`:
|
272
|
+
|
273
|
+
```
|
274
|
+
spec/support/database.yml
|
275
|
+
```
|
276
|
+
|
277
|
+
What we *will* commit is a `database.sample.yml` as a template for future contributors:
|
278
|
+
|
279
|
+
```
|
280
|
+
cp spec/support/database.yml spec/support/database.sample.yml
|
281
|
+
```
|
282
|
+
|
283
|
+
Remember to replace any private passwords in `database.sample.yml` with `secret` before committing.
|
284
|
+
|
285
|
+
To have ActiveRecord connect to the database in `database.yml` before your tests, add a file `spec/support/database.rb` with the following content:
|
286
|
+
|
287
|
+
```
|
288
|
+
database = Gemika::Database.new
|
289
|
+
database.connect
|
290
|
+
```
|
291
|
+
|
292
|
+
Now require Gemika and this support file from your `spec_helper.rb`.
|
293
|
+
|
294
|
+
```
|
295
|
+
require 'gemika'
|
296
|
+
require 'spec/support/database'
|
297
|
+
```
|
298
|
+
|
299
|
+
Protip: Instead of requiring support files indidually, configure your `spec_helper.rb` to automatically `require` all files in the `spec/support` folder:
|
300
|
+
|
301
|
+
```ruby
|
302
|
+
Dir["#{File.dirname(__FILE__)}/support/*.rb"].sort.each {|f| require f}
|
303
|
+
```
|
304
|
+
|
305
|
+
Now you have a great place for code snippets that need to run before specs (factories, VCR configuration, etc.).
|
306
|
+
|
307
|
+
|
308
|
+
#### Test database schema
|
309
|
+
|
310
|
+
If your gem is talking to the database, you probably need to create some example tables.
|
311
|
+
|
312
|
+
Magika lets you define an [ActiveRecord database migration](http://api.rubyonrails.org/classes/ActiveRecord/Migration.html) for that. Before your test suite runs, Magika will drop *all* tables in your test database and recreate them using this migration.
|
313
|
+
|
314
|
+
Add your migration to your `spec/support/database.rb` (created and required above):
|
315
|
+
|
316
|
+
```ruby
|
317
|
+
|
318
|
+
database = Gemika::Database.new
|
319
|
+
database.connect
|
320
|
+
database.rewrite_schema! do
|
321
|
+
|
322
|
+
create_table :users do |t|
|
323
|
+
t.string :name
|
324
|
+
t.string :email
|
325
|
+
t.string :city
|
326
|
+
end
|
327
|
+
|
328
|
+
create_table :recipes do |t|
|
329
|
+
t.string :name
|
330
|
+
t.integer :category_id
|
331
|
+
end
|
332
|
+
|
333
|
+
create_table :recipe_ingredients do |t|
|
334
|
+
t.string :name
|
335
|
+
t.integer :recipe_id
|
336
|
+
end
|
337
|
+
|
338
|
+
create_table :recipe_categories do |t|
|
339
|
+
t.string :name
|
340
|
+
end
|
341
|
+
|
342
|
+
end
|
343
|
+
```
|
344
|
+
|
345
|
+
#### Wrap examples in transactions
|
346
|
+
|
347
|
+
A very useful Rails default is to wrap every test in a transaction that is rolled back when the example ends. This way each example starts with a blank database.
|
348
|
+
|
349
|
+
To get the same behavior in your gem tests, append the following to your `spec_helper.rb`:
|
350
|
+
|
351
|
+
```
|
352
|
+
Gemika::RSpec.configure_transactional_examples
|
353
|
+
```
|
354
|
+
|
355
|
+
Note that you also need `require 'gemika'` in your `spec_helper.rb`.
|
356
|
+
|
357
|
+
Now every RSpec example is wrapped in a transaction. To disable this behavior for an individual example, use the `transaction: false` option:
|
358
|
+
|
359
|
+
```
|
360
|
+
it 'should work', transaction: false do
|
361
|
+
# code that doesn't work within a transaction, e.g. threads
|
362
|
+
end
|
363
|
+
```
|
364
|
+
|
365
|
+
### Try it out
|
366
|
+
|
367
|
+
Check if you can install development dependencies for each row in the test matrix:
|
368
|
+
|
369
|
+
```
|
370
|
+
bundle exec rake matrix:install
|
371
|
+
```
|
372
|
+
|
373
|
+
Check if you can run tests for each row in the test matrix:
|
374
|
+
|
375
|
+
```shell
|
376
|
+
bundle exec rake matrix:spec
|
377
|
+
```
|
378
|
+
|
379
|
+
You should see the command output for each row in the test matrix. Gemika will also print a summary at the end:
|
380
|
+
|
381
|
+
![Matrix task output](https://raw.githubusercontent.com/makandra/gemika/master/doc/minidusen_test.png)
|
382
|
+
|
383
|
+
Note that there is no task for running all gemfiles in all Ruby versions. We had something like this in earlier versions of Gemika and it wasn't as practical as we thought. You need to manually switch Ruby versions and re-run `rake matrix:install`. We recommend to setup Travis CI to check the entire test matrix after each push, including all Rubies.
|
384
|
+
|
385
|
+
|
386
|
+
## Activate Travis CI
|
387
|
+
|
388
|
+
We recommend to setup Travis CI to check the entire test matrix after each push. Travis CI will also show the build results on Github's pull request page, helping maintainers decide whether a PR is safe to merge.
|
389
|
+
|
390
|
+
If you plan to use Travis CI, also add a `spec/support/database.travis.yml` with [Travis' default database credentials](https://docs.travis-ci.com/user/database-setup/):
|
391
|
+
|
392
|
+
```yaml
|
393
|
+
mysql:
|
394
|
+
database: my_gem_test
|
395
|
+
username: travis
|
396
|
+
password:
|
397
|
+
|
398
|
+
postgresql:
|
399
|
+
database: my_gem_test
|
400
|
+
user: postgres
|
401
|
+
password:
|
402
|
+
```
|
403
|
+
|
404
|
+
Add options to `.travis.yml` to create databases before running tests:
|
405
|
+
|
406
|
+
```yaml
|
407
|
+
before_script:
|
408
|
+
- psql -c 'create database mygem_test;' -U postgres
|
409
|
+
- mysql -e 'create database IF NOT EXISTS mygem_test;'
|
410
|
+
```
|
411
|
+
|
412
|
+
Also add the other `.travis.yml` settings required for a Ruby project:
|
413
|
+
|
414
|
+
```yaml
|
415
|
+
language: ruby
|
416
|
+
|
417
|
+
sudo: false
|
418
|
+
|
419
|
+
cache: bundler
|
420
|
+
|
421
|
+
notifications:
|
422
|
+
email:
|
423
|
+
- notifications@test.com
|
424
|
+
|
425
|
+
script: bundle exec rspec spec
|
426
|
+
```
|
427
|
+
|
428
|
+
Adjust the `script` option if you're not using RSpec to test your code.
|
429
|
+
|
430
|
+
#### Activate Github integration
|
431
|
+
|
432
|
+
To activate Travis CI for your Github repo:
|
433
|
+
|
434
|
+
- Log into Github
|
435
|
+
- Open your gem's project page
|
436
|
+
- Open *Settings*
|
437
|
+
- Navigate to *Integrations & services*
|
438
|
+
- Open the *Add service* dropdown
|
439
|
+
- Select *Travis CI*
|
440
|
+
- Authenticate via OAuth
|
441
|
+
|
442
|
+
To check if the integration has worked, push a change and check if you can see your build matrix on the [Travis CI dashboard](https://travis-ci.org/).
|
443
|
+
|
444
|
+
#### Build badge
|
445
|
+
|
446
|
+
You might want to a build status badge to your `README.md` like this:
|
447
|
+
|
448
|
+
[![Build Status](https://travis-ci.org/makandra/minidusen.svg?branch=master)](https://travis-ci.org/makandra/minidusen)
|
449
|
+
|
450
|
+
You can add such a badge using this markdown:
|
451
|
+
|
452
|
+
```markdown
|
453
|
+
[![Build Status](https://travis-ci.org/my_org/my_gem.svg?branch=master)](https://travis-ci.org/my_org/my_gem)
|
454
|
+
```
|
455
|
+
|
456
|
+
#### Protect the `master` branch
|
457
|
+
|
458
|
+
If you're super paranoid you can also prevent anyone from pushing to `master` without a green Travis CI build:
|
459
|
+
|
460
|
+
- Open your Github project settings
|
461
|
+
- Navigate to *Branches*
|
462
|
+
- Below *Protected branches*, open the *Choose a branch...* dropdown
|
463
|
+
- Select `master`
|
464
|
+
- Check *Protect this branch*
|
465
|
+
- Check *Require status checks to pass before merging*
|
466
|
+
- Check the status check `continuous-integration/travis-ci`
|
467
|
+
- Press *Save changes*
|
468
|
+
|
469
|
+
|
470
|
+
## Add development instructions to your README
|
471
|
+
|
472
|
+
Your README should contain instructions how to run tests before making a PR. You might also want to educate future contributors about the existence of your test matrix, and how to use it.
|
473
|
+
|
474
|
+
We recommend to add a section like the following to your `README.md`:
|
475
|
+
|
476
|
+
```markdown
|
477
|
+
## Development
|
478
|
+
|
479
|
+
There are tests in `spec`. We only accept PRs with tests. To run tests:
|
480
|
+
|
481
|
+
- Install Ruby x.y.z
|
482
|
+
- Create a local test database `my_gem_test` in both MySQL and PostgreSQL
|
483
|
+
- Copy `spec/support/database.sample.yml` to `spec/support/database.yml` and enter your local credentials for the test databases
|
484
|
+
- Install development dependencies using `bundle install`
|
485
|
+
- Run tests using `bundle exec rspec`
|
486
|
+
|
487
|
+
We recommend to test large changes against multiple versions of Ruby and multiple dependency sets. Supported combinations are configured in `.travis.yml`. We provide some rake tasks to help with this:
|
488
|
+
|
489
|
+
- Install development dependencies using `bundle matrix:install`
|
490
|
+
- Run tests using `bundle matrix:spec`
|
491
|
+
|
492
|
+
Note that we have configured Travis CI to automatically run tests in all supported Ruby versions and dependency sets after each push. We will only merge pull requests after a green Travis build.
|
493
|
+
```
|
494
|
+
|
495
|
+
Credits
|
496
|
+
-------
|
497
|
+
|
498
|
+
Henning Koch from [makandra](http://makandra.com/)
|
data/lib/gemika/database.rb
CHANGED
@@ -4,23 +4,20 @@ module Gemika
|
|
4
4
|
class Database
|
5
5
|
|
6
6
|
def initialize(options = {})
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
File.exists?(
|
11
|
-
|
7
|
+
yaml_config_folder = options.fetch(:config_folder, 'spec/support')
|
8
|
+
yaml_config_filename = travis? ? 'database.travis.yml' : 'database.yml'
|
9
|
+
yaml_config_path = File.join(yaml_config_folder, yaml_config_filename)
|
10
|
+
if File.exists?(yaml_config_path)
|
11
|
+
@yaml_config = YAML.load_file(yaml_config_path)
|
12
|
+
else
|
13
|
+
warn "No database configuration in #{yaml_config_path}, using defaults: #{adapter_config.inspect}"
|
14
|
+
@yaml_config = {}
|
15
|
+
end
|
12
16
|
@connected = false
|
13
17
|
end
|
14
18
|
|
15
19
|
def connect
|
16
20
|
unless @connected
|
17
|
-
if pg?
|
18
|
-
adapter_config = (@config['postgresql'] || @config['postgres'] || @config['pg']).merge(adapter: 'postgresql')
|
19
|
-
elsif mysql2?
|
20
|
-
adapter_config = (@config['mysql'] || @config['mysql2']).merge(adapter: 'mysql2', encoding: 'utf8')
|
21
|
-
else
|
22
|
-
raise "Unknown database type"
|
23
|
-
end
|
24
21
|
ActiveRecord::Base.establish_connection(adapter_config)
|
25
22
|
@connected = true
|
26
23
|
end
|
@@ -46,25 +43,48 @@ module Gemika
|
|
46
43
|
|
47
44
|
private
|
48
45
|
|
46
|
+
def adapter_config
|
47
|
+
default_config = {}
|
48
|
+
default_config['database'] = guess_database_name
|
49
|
+
if pg?
|
50
|
+
default_config['adapter'] = 'postgresql'
|
51
|
+
default_config['username'] = 'postgres' if travis?
|
52
|
+
default_config['password'] = ''
|
53
|
+
user_config = @yaml_config['postgresql'] || @yaml_config['postgres'] || @yaml_config['pg'] || {}
|
54
|
+
elsif mysql2?
|
55
|
+
default_config['adapter'] = 'mysql2'
|
56
|
+
default_config['username'] = 'travis' if travis?
|
57
|
+
default_config['encoding'] = 'utf8'
|
58
|
+
user_config = (@yaml_config['mysql'] || @yaml_config['mysql2']) || {}
|
59
|
+
else
|
60
|
+
raise "Unknown database type"
|
61
|
+
end
|
62
|
+
default_config.merge(user_config)
|
63
|
+
end
|
64
|
+
|
65
|
+
def guess_database_name
|
66
|
+
project_name = File.basename(File.expand_path(__dir__))
|
67
|
+
"#{project_name}_test"
|
68
|
+
end
|
69
|
+
|
49
70
|
def connection
|
50
71
|
ActiveRecord::Base.connection
|
51
72
|
end
|
52
73
|
|
53
74
|
def pg?
|
54
|
-
|
75
|
+
gem_loaded?('pg')
|
55
76
|
end
|
56
77
|
|
57
78
|
def mysql2?
|
58
|
-
|
79
|
+
gem_loaded?('mysql2')
|
59
80
|
end
|
60
81
|
|
61
|
-
def
|
62
|
-
|
82
|
+
def gem_loaded?(name)
|
83
|
+
Gem.loaded_specs.has_key?(name)
|
63
84
|
end
|
64
85
|
|
65
|
-
def
|
66
|
-
|
67
|
-
File.read(gemfile)
|
86
|
+
def travis?
|
87
|
+
!!ENV['TRAVIS']
|
68
88
|
end
|
69
89
|
|
70
90
|
end
|
Binary file
|
@@ -0,0 +1 @@
|
|
1
|
+
# none yet
|
data/lib/gemika/tasks.rb
ADDED
data/lib/gemika/version.rb
CHANGED
data/lib/gemika.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: gemika
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Henning Koch
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-09-
|
11
|
+
date: 2016-09-22 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Helpers for testing Ruby gems
|
14
14
|
email: henning.koch@makandra.de
|
@@ -23,9 +23,12 @@ files:
|
|
23
23
|
- gemika.gemspec
|
24
24
|
- lib/gemika.rb
|
25
25
|
- lib/gemika/database.rb
|
26
|
+
- lib/gemika/doc/minidusen_test.png
|
26
27
|
- lib/gemika/matrix.rb
|
27
|
-
- lib/gemika/matrix_tasks.rb
|
28
28
|
- lib/gemika/rspec.rb
|
29
|
+
- lib/gemika/tasks.rb
|
30
|
+
- lib/gemika/tasks/database.rb
|
31
|
+
- lib/gemika/tasks/matrix.rb
|
29
32
|
- lib/gemika/version.rb
|
30
33
|
homepage: https://github.com/makandra/gemika
|
31
34
|
licenses:
|
File without changes
|