seedbank 0.4.0 → 0.5.0.pre

Sign up to get free protection for your applications and to get access to all the features.
Files changed (51) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +2 -1
  3. data/.hound.yml +3 -0
  4. data/.rubocop.yml +7 -0
  5. data/.rubocop_todo.yml +29 -0
  6. data/.travis.yml +7 -9
  7. data/Gemfile +5 -4
  8. data/README.md +49 -25
  9. data/Rakefile +2 -1
  10. data/TODO.txt +3 -3
  11. data/gemfiles/rake10.gemfile +0 -2
  12. data/gemfiles/{rake0.9.gemfile → rake11.gemfile} +2 -3
  13. data/lib/seedbank.rb +15 -6
  14. data/lib/seedbank/dsl.rb +65 -43
  15. data/lib/seedbank/railtie.rb +2 -7
  16. data/lib/seedbank/runner.rb +19 -32
  17. data/lib/seedbank/version.rb +2 -1
  18. data/lib/tasks/seed.rake +16 -20
  19. data/seedbank.gemspec +32 -53
  20. data/test/dummy/Rakefile +1 -0
  21. data/test/dummy/app/controllers/application_controller.rb +1 -0
  22. data/test/dummy/config.ru +2 -1
  23. data/test/dummy/config/application.rb +2 -22
  24. data/test/dummy/config/boot.rb +2 -1
  25. data/test/dummy/config/environment.rb +1 -0
  26. data/test/dummy/config/environments/development.rb +3 -7
  27. data/test/dummy/config/environments/test.rb +4 -9
  28. data/test/dummy/config/initializers/secret_token.rb +1 -0
  29. data/test/dummy/config/initializers/session_store.rb +2 -1
  30. data/test/dummy/config/initializers/wrap_parameters.rb +2 -1
  31. data/test/dummy/config/routes.rb +1 -0
  32. data/test/dummy/db/seeds.rb +1 -0
  33. data/test/dummy/db/seeds/circular1.seeds.rb +2 -1
  34. data/test/dummy/db/seeds/circular2.seeds.rb +2 -1
  35. data/test/dummy/db/seeds/dependency.seeds.rb +1 -0
  36. data/test/dummy/db/seeds/dependency2.seeds.rb +1 -0
  37. data/test/dummy/db/seeds/dependent.seeds.rb +2 -1
  38. data/test/dummy/db/seeds/dependent_on_nested.seeds.rb +2 -1
  39. data/test/dummy/db/seeds/dependent_on_several.seeds.rb +2 -1
  40. data/test/dummy/db/seeds/development/users.seeds.rb +1 -0
  41. data/test/dummy/db/seeds/no_block.seeds.rb +2 -1
  42. data/test/dummy/db/seeds/reference_memos.seeds.rb +2 -1
  43. data/test/dummy/db/seeds/with_block_memo.seeds.rb +2 -1
  44. data/test/dummy/db/seeds/with_inline_memo.seeds.rb +1 -0
  45. data/test/dummy/script/rails +3 -2
  46. data/test/lib/seedbank/dsl_test.rb +56 -73
  47. data/test/lib/seedbank/runner_test.rb +39 -46
  48. data/test/lib/tasks/seed_rake_test.rb +88 -51
  49. data/test/test_helper.rb +13 -11
  50. metadata +76 -23
  51. data/test/dummy/lib/tasks/dsl.rake +0 -4
@@ -1 +1,2 @@
1
+ # frozen_string_literal: true
1
2
  FakeModel.seed('dependency')
@@ -1 +1,2 @@
1
+ # frozen_string_literal: true
1
2
  FakeModel.seed('dependency2')
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  after :dependency do
2
3
  FakeModel.seed('dependent')
3
- end
4
+ end
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  after :dependent, :dependency2 do
2
3
  FakeModel.seed('dependent on nested')
3
- end
4
+ end
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  after :dependency, :dependency2 do
2
3
  FakeModel.seed('dependent on several')
3
- end
4
+ end
@@ -1 +1,2 @@
1
+ # frozen_string_literal: true
1
2
  FakeModel.seed('development:users')
@@ -1 +1,2 @@
1
- after :dependency
1
+ # frozen_string_literal: true
2
+ after :dependency
@@ -1,5 +1,6 @@
1
+ # frozen_string_literal: true
1
2
  after('with_block_memo') do
2
3
  block_let
3
4
  inline_let
4
5
  inline_method
5
- end
6
+ end
@@ -1,5 +1,6 @@
1
+ # frozen_string_literal: true
1
2
  after('with_inline_memo') do
2
3
  FakeModel.seed 'with_block_memo'
3
4
  let(:block_let) { FakeModel.calling_let 'BLOCK_LET' }
4
5
  let!(:block_let!) { FakeModel.calling_let! 'BLOCK_LET!' }
5
- end
6
+ end
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  def inline_method
2
3
  FakeModel.calling_method 'inline_method'
3
4
  end
@@ -1,6 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
2
3
  # This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application.
3
4
 
4
- APP_PATH = File.expand_path('../../config/application', __FILE__)
5
- require File.expand_path('../../config/boot', __FILE__)
5
+ APP_PATH = File.expand_path('../../config/application', __FILE__)
6
+ require File.expand_path('../../config/boot', __FILE__)
6
7
  require 'rails/commands'
@@ -1,135 +1,119 @@
1
+ # frozen_string_literal: true
1
2
  require 'test_helper'
2
3
 
3
- describe Seedbank::DSL do
4
-
5
- describe "scope_from_seed_file" do
6
-
7
- it "is added to the namesapce" do
8
- ns = Rake.application.in_namespace(:seedy) { self }
9
-
10
- ns.must_respond_to :scope_from_seed_file
11
- end
4
+ using Seedbank::DSL
12
5
 
13
- describe "in an environment directory" do
6
+ describe Seedbank::DSL do
7
+ # TODO: This is private so should really be tested indirectly.
8
+ describe 'scope_from_seed_file' do
9
+ subject { scope_from_seed_file(seed_file) }
14
10
 
11
+ describe 'in an environment directory' do
15
12
  let(:seed_file) { File.expand_path('development/users.seeds.rb', Seedbank.seeds_root) }
16
13
  let(:seed_namespace) { %w(development) }
17
14
 
18
- subject { Seedbank::DSL.scope_from_seed_file seed_file }
19
-
20
- it "returns the enviroment scope" do
15
+ it 'returns the enviroment scope' do
21
16
  subject.must_equal seed_namespace
22
17
  end
23
18
  end
24
19
 
25
- describe "in a nested directory" do
26
-
20
+ describe 'in a nested directory' do
27
21
  let(:seed_file) { File.expand_path('development/shared/accounts.seeds.rb', Seedbank.seeds_root) }
28
22
  let(:seed_namespace) { %w(development shared) }
29
23
 
30
- subject { Seedbank::DSL.scope_from_seed_file seed_file }
31
-
32
- it "returns the nested scope" do
24
+ it 'returns the nested scope' do
33
25
  subject.must_equal seed_namespace
34
26
  end
35
27
  end
36
28
 
37
- describe "in seeds root" do
38
-
29
+ describe 'in seeds root' do
39
30
  let(:seed_file) { File.expand_path('no_block.seeds.rb', Seedbank.seeds_root) }
40
31
 
41
- subject { Seedbank::DSL.scope_from_seed_file seed_file }
42
-
43
- it "returns an array" do
32
+ it 'returns an array' do
44
33
  subject.must_be_instance_of Array
45
34
  end
46
35
 
47
- it "must be empty" do
36
+ it 'must be empty' do
48
37
  subject.must_be_empty
49
38
  end
50
39
  end
51
40
  end
52
41
 
53
- describe "seeds_root" do
54
-
42
+ describe 'seeds_root' do
55
43
  let(:seeds_root) { '/my/seeds/directory' }
56
44
 
57
- subject { Seedbank::DSL.send(:seeds_root) }
45
+ subject { Seedbank::DSL.seeds_root }
58
46
 
59
- it "returns a Pathname" do
47
+ it 'returns a Pathname' do
60
48
  Seedbank.stub(:seeds_root, seeds_root) do
61
-
62
49
  subject.must_equal Pathname.new(seeds_root)
63
-
64
50
  end
65
51
  end
66
-
67
52
  end
68
53
 
69
- describe "glob_seed_files_matching" do
70
-
71
- describe "with no namespace" do
72
-
54
+ describe 'glob_seed_files_matching' do
55
+ describe 'with no namespace' do
73
56
  let(:pattern) { '*.seeds.rb' }
74
57
 
75
- it "returns all the files matching the pattern in seeds_root" do
58
+ it 'returns all the files matching the pattern in seeds_root' do
76
59
  expected_files = Dir.glob(File.join(Seedbank.seeds_root, pattern))
77
60
 
78
61
  Seedbank::DSL.glob_seed_files_matching(pattern).must_equal expected_files
79
62
  end
80
-
81
63
  end
82
64
 
83
- describe "with a namespace" do
84
-
65
+ describe 'with a namespace' do
85
66
  let(:pattern) { '*.seeds.rb' }
86
67
  let(:namespace) { 'development' }
87
68
 
88
- it "returns all the files matching the pattern in seeds_root" do
69
+ it 'returns all the files matching the pattern in seeds_root' do
89
70
  expected_files = Dir.glob(File.join(Seedbank.seeds_root, namespace, pattern))
90
71
 
91
72
  Seedbank::DSL.glob_seed_files_matching(namespace, pattern).must_equal expected_files
92
73
  end
93
-
94
74
  end
95
-
96
75
  end
97
76
 
98
- describe "define_seed_task" do
77
+ describe 'define_seed_task' do
78
+ subject { Seedbank::DSL.define_seed_task(seed_file, task_name => dependencies) }
99
79
 
100
80
  let(:task_name) { 'scoped:my_seed' }
101
- let(:dependencies) { ['environment'] }
81
+ let(:dependencies) { ['scoped:another_seed'] }
102
82
  let(:seed_file) { File.expand_path('development/users.seeds.rb', Seedbank.seeds_root) }
103
83
 
104
- it "returns a fully qualified task name" do
105
- returned_name = Seedbank::DSL.define_seed_task(seed_file, task_name => dependencies)
84
+ def define_prerequisite_task
85
+ Rake::Task.define_task('scoped:another_seed')
86
+ end
106
87
 
107
- returned_name.must_equal task_name
88
+ before do
89
+ define_prerequisite_task
108
90
  end
109
91
 
110
- it "creates a Rake Task" do
111
- Seedbank::DSL.define_seed_task(seed_file, task_name => dependencies)
92
+ it 'returns a fully qualified task name' do
93
+ subject.must_equal task_name
94
+ end
112
95
 
96
+ it 'creates a Rake Task' do
97
+ subject
113
98
  Rake::Task[task_name].wont_be_nil
114
99
  end
115
100
 
116
- it "sets Rake Task description" do
117
- Seedbank::DSL.define_seed_task(seed_file, task_name => dependencies)
101
+ it 'sets Rake Task description' do
102
+ subject
103
+ relative_file = Pathname.new(seed_file).relative_path_from(Rails.root)
118
104
 
119
- Rake::Task[task_name].comment.must_equal "Load the seed data from #{seed_file}"
105
+ Rake::Task[task_name].comment.must_equal "Load the seed data from #{relative_file}"
120
106
  end
121
107
 
122
- it "sets Rake Task action to the seed_file contents" do
123
- Seedbank::DSL.define_seed_task(seed_file, task_name => dependencies)
124
-
108
+ it 'sets Rake Task action to the seed_file contents' do
109
+ subject
125
110
  FakeModel.expect :seed, true, ['development:users']
126
-
127
111
  Rake::Task[task_name].invoke
128
112
  end
129
113
 
130
- describe "when db:abort_if_pending_migrations exists" do
131
- it "sets Rake Task dependencies" do
132
- Seedbank::DSL.define_seed_task(seed_file, task_name => dependencies)
114
+ describe 'when db:abort_if_pending_migrations exists' do
115
+ it 'sets Rake Task dependencies' do
116
+ subject
133
117
  expected_dependencies = dependencies.map { |dependency| Rake::Task[dependency] }
134
118
  expected_dependencies << Rake::Task['db:abort_if_pending_migrations']
135
119
 
@@ -137,39 +121,39 @@ describe Seedbank::DSL do
137
121
  end
138
122
  end
139
123
 
140
- describe "when db:abort_if_pending_migrations does not exist" do
141
- it "sets Rake Task dependencies" do
124
+ describe 'when db:abort_if_pending_migrations does not exist' do
125
+ it 'adds environment dependency' do
142
126
  Rake.application.clear
143
- Rake::Task.define_task 'environment'
127
+ define_prerequisite_task
144
128
 
145
- Seedbank::DSL.define_seed_task(seed_file, task_name => dependencies)
129
+ expected_dependencies = dependencies.map { |dependency| Rake::Task[dependency] }
130
+ expected_dependencies << Rake::Task.define_task('environment')
131
+ subject
146
132
 
147
- Rake::Task[task_name].prerequisite_tasks.must_equal dependencies.map { |dependency| Rake::Task[dependency] }
133
+ Rake::Task[task_name].prerequisite_tasks.must_equal expected_dependencies
148
134
  end
149
135
  end
150
136
  end
151
137
 
152
- describe "override_seed_task" do
153
-
154
- describe "when no task exists to override" do
155
-
138
+ describe 'override_seed_task' do
139
+ describe 'when no task exists to override' do
156
140
  let(:task_name) { 'my_task' }
157
- let(:dependencies) { ['seedy:users'] }
141
+ let(:dependencies) { ['db:abort_if_pending_migrations'] }
158
142
 
159
- it "creates a new task" do
143
+ it 'creates a new task' do
160
144
  Seedbank::DSL.override_seed_task(task_name => dependencies)
161
145
 
162
146
  Rake::Task[task_name].wont_be_nil
163
147
  end
164
148
 
165
- it "applies the dependencies" do
149
+ it 'applies the dependencies' do
166
150
  expected_dependencies = dependencies.map { |dependency| Rake::Task[dependency] }
167
151
  Seedbank::DSL.override_seed_task(task_name => dependencies)
168
152
 
169
153
  Rake::Task[task_name].prerequisite_tasks.must_equal expected_dependencies
170
154
  end
171
155
 
172
- it "applies the description" do
156
+ it 'applies the description' do
173
157
  description = 'Expected Description'
174
158
  Rake.application.last_description = description
175
159
 
@@ -178,6 +162,5 @@ describe Seedbank::DSL do
178
162
  Rake::Task[task_name].full_comment.must_equal description
179
163
  end
180
164
  end
181
-
182
165
  end
183
166
  end
@@ -1,12 +1,11 @@
1
+ # frozen_string_literal: true
1
2
  require 'test_helper'
2
3
 
3
4
  describe Seedbank::Runner do
4
-
5
- describe "seeds with dependency" do
6
-
5
+ describe 'seeds with dependency' do
7
6
  subject { Rake::Task['db:seed:dependent'] }
8
7
 
9
- it "runs the dependencies in order" do
8
+ it 'runs the dependencies in order' do
10
9
  FakeModel.expect :seed, true, ['dependency']
11
10
  FakeModel.expect :seed, true, ['dependent']
12
11
 
@@ -15,7 +14,7 @@ describe Seedbank::Runner do
15
14
  FakeModel.verify
16
15
  end
17
16
 
18
- it "executes the body of the dependencies exactly once per invocation" do
17
+ it 'executes the body of the dependencies exactly once per invocation' do
19
18
  FakeModel.expect :seed, true, ['dependency']
20
19
  FakeModel.expect :seed, true, ['dependent']
21
20
  FakeModel.expect :seed, true, ['dependency']
@@ -24,7 +23,7 @@ describe Seedbank::Runner do
24
23
  subject.invoke
25
24
 
26
25
  # Allow all tasks to be re-executed, including dependencies
27
- Rake.application.tasks.each { |t| t.reenable }
26
+ Rake.application.tasks.each(&:reenable)
28
27
 
29
28
  subject.invoke
30
29
 
@@ -32,11 +31,10 @@ describe Seedbank::Runner do
32
31
  end
33
32
  end
34
33
 
35
- describe "seeds with multiple dependencies" do
36
-
34
+ describe 'seeds with multiple dependencies' do
37
35
  subject { Rake::Task['db:seed:dependent_on_several'] }
38
36
 
39
- it "runs the dependencies in order" do
37
+ it 'runs the dependencies in order' do
40
38
  FakeModel.expect :seed, true, ['dependency']
41
39
  FakeModel.expect :seed, true, ['dependency2']
42
40
  FakeModel.expect :seed, true, ['dependent on several']
@@ -47,11 +45,10 @@ describe Seedbank::Runner do
47
45
  end
48
46
  end
49
47
 
50
- describe "seeds with nested dependencies" do
51
-
48
+ describe 'seeds with nested dependencies' do
52
49
  subject { Rake::Task['db:seed:dependent_on_nested'] }
53
50
 
54
- it "runs all dependencies in order" do
51
+ it 'runs all dependencies in order' do
55
52
  FakeModel.expect :seed, true, ['dependency']
56
53
  FakeModel.expect :seed, true, ['dependent']
57
54
  FakeModel.expect :seed, true, ['dependency2']
@@ -61,14 +58,12 @@ describe Seedbank::Runner do
61
58
 
62
59
  FakeModel.verify
63
60
  end
64
-
65
61
  end
66
62
 
67
- describe "after with no block given" do
68
-
63
+ describe 'after with no block given' do
69
64
  subject { Rake::Task['db:seed:no_block'] }
70
65
 
71
- it "runs the dependencies" do
66
+ it 'runs the dependencies' do
72
67
  FakeModel.expect :seed, true, ['dependency']
73
68
 
74
69
  subject.invoke
@@ -77,20 +72,19 @@ describe Seedbank::Runner do
77
72
  end
78
73
  end
79
74
 
80
- describe "let" do
81
-
82
- describe "evaluates dependencies in order" do
83
-
75
+ describe 'let' do
76
+ describe 'evaluates dependencies in order' do
84
77
  subject { Rake::Task['db:seed:reference_memos'] }
85
78
 
86
- it "runs the dependencies in order" do
79
+ it 'runs the dependencies in order' do
87
80
  FakeModel.expect :seed, true, ['with_inline_memo']
88
81
  FakeModel.expect :seed, true, ['with_block_memo']
89
82
  FakeModel.expect :calling_let, true, ['BLOCK_LET']
90
83
  FakeModel.expect :calling_let, true, ['INLINE_LET']
91
84
 
92
- def FakeModel.calling_let!(*args); end
93
- def FakeModel.calling_method(*args); end
85
+ def FakeModel.calling_let!(*_args); end
86
+
87
+ def FakeModel.calling_method(*_args); end
94
88
 
95
89
  subject.invoke
96
90
 
@@ -98,13 +92,12 @@ describe Seedbank::Runner do
98
92
  end
99
93
  end
100
94
 
101
- describe "a previously defined method" do
102
-
95
+ describe 'a previously defined method' do
103
96
  let(:runner) { Seedbank::Runner.new }
104
97
 
105
98
  before { runner.let(:existing) {} }
106
99
 
107
- %w(__eigenclass existing let let evaluate after).each do |name|
100
+ %w(existing let! let evaluate after extend).each do |name|
108
101
  it 'raises ArgumentError' do
109
102
  assert_raises(ArgumentError, Regexp.new(name)) do
110
103
  runner.let(name)
@@ -112,43 +105,44 @@ describe Seedbank::Runner do
112
105
  end
113
106
  end
114
107
  end
115
-
116
108
  end
117
109
 
118
- describe "let!" do
119
-
120
- describe "evaluates dependencies in order" do
121
-
110
+ # TODO: These don't really test in order. Maybe swap in rspec_mocks
111
+ describe 'let!' do
112
+ describe 'evaluates dependencies in order' do
122
113
  subject { Rake::Task['db:seed:reference_memos'] }
123
114
 
124
- it "runs the dependencies in order" do
115
+ it 'runs the dependencies in order' do
116
+ FakeModel.expect :seed, true, ['with_inline_memo']
125
117
  FakeModel.expect :calling_let!, true, ['INLINE_LET!']
118
+ FakeModel.expect :seed, true, ['with_block_memo']
126
119
  FakeModel.expect :calling_let!, true, ['BLOCK_LET!']
127
-
128
- def FakeModel.seed(*args); end
129
- def FakeModel.calling_let(*args); end
130
- def FakeModel.calling_method(*args); end
120
+ FakeModel.expect :calling_let, true, ['BLOCK_LET']
121
+ FakeModel.expect :calling_let, true, ['INLINE_LET']
122
+ FakeModel.expect :calling_method, true, ['inline_method']
131
123
 
132
124
  subject.invoke
133
125
 
134
126
  FakeModel.verify
135
127
  end
136
128
  end
137
-
138
129
  end
139
130
 
140
- describe "defining an inline method" do
141
-
142
- describe "evaluates dependencies in order" do
143
-
131
+ # TODO: This doesn't do anything more than the above except for
132
+ # isolating the inline method call. Would be better served by it's
133
+ # own seeds in isolation.
134
+ describe 'defining an inline method' do
135
+ describe 'evaluates dependencies in order' do
144
136
  subject { Rake::Task['db:seed:reference_memos'] }
145
137
 
146
- it "runs the dependencies in order" do
138
+ it 'runs the dependencies in order' do
147
139
  FakeModel.expect :calling_method, true, ['inline_method']
148
140
 
149
- def FakeModel.seed(*args); end
150
- def FakeModel.calling_let(*args); end
151
- def FakeModel.calling_let!(*args); end
141
+ def FakeModel.seed(*_args); end
142
+
143
+ def FakeModel.calling_let(*_args); end
144
+
145
+ def FakeModel.calling_let!(*_args); end
152
146
 
153
147
  subject.invoke
154
148
 
@@ -156,5 +150,4 @@ describe Seedbank::Runner do
156
150
  end
157
151
  end
158
152
  end
159
-
160
153
  end