seed_builder 1.3.0 → 1.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,299 +0,0 @@
1
- require "spec_helper"
2
-
3
- # Define Rails module if not already defined
4
- unless defined?(Rails)
5
- module Rails
6
- class << self
7
- attr_accessor :root, :env, :logger
8
- end
9
- end
10
- end
11
-
12
- class SeedBuilderUser < ActiveRecord::Base; end
13
-
14
- class SeedUser < SeedBuilderUser; end
15
-
16
- describe SeedBuilder::Loader do
17
- subject(:loader) { described_class.new }
18
-
19
- let(:logger) { Logger.new($stdout) }
20
- let(:seeds_path) { File.expand_path("../../fixtures/seeds", __FILE__) }
21
- let(:default_seeds_path) { File.expand_path("../../fixtures/seeds.rb", __FILE__) }
22
-
23
- before do
24
- allow(Rails).to receive(:root).and_return(rails_root)
25
- allow(Rails).to receive(:env).and_return(rails_env)
26
- allow(Rails).to receive(:logger).and_return(logger)
27
- allow(Rails).to receive(:logger=)
28
- # Reset SeedBuilder logger to pick up the stubbed Rails.logger
29
- SeedBuilder.instance_variable_set(:@logger, nil) if SeedBuilder.instance_variable_defined?(:@logger)
30
-
31
- SeedBuilder.configure do |config|
32
- config.seeds_full_path = seeds_path
33
- config.default_seeds_full_path = default_seeds_path
34
- config.load_default_seeds = true
35
- config.load_seeds = true
36
- end
37
- end
38
-
39
- describe "#load_seed" do
40
- before do
41
- SeedUser.delete_all
42
- SeedBuilderUser.delete_all
43
- end
44
-
45
- context "when loading default seeds" do
46
- let(:load_default_seeds) { true }
47
- let(:load_seeds) { true }
48
-
49
- before do
50
- SeedBuilder.configure do |config|
51
- config.load_default_seeds = load_default_seeds
52
- config.load_seeds = load_seeds
53
- end
54
- loader.load_seed
55
- end
56
-
57
- it "loads the seeds" do
58
- expect(SeedUser.count).to eq 1
59
- expect(SeedBuilderUser.count).to eq 1
60
- end
61
- end
62
-
63
- context "when seeds directory does not exist" do
64
- let(:log_output) { StringIO.new }
65
- let(:test_logger) { Logger.new(log_output) }
66
-
67
- before do
68
- allow(Rails).to receive(:logger).and_return(test_logger)
69
- # Reset SeedBuilder logger to pick up the stubbed Rails.logger
70
- SeedBuilder.instance_variable_set(:@logger, nil) if SeedBuilder.instance_variable_defined?(:@logger)
71
- SeedBuilder.configure do |config|
72
- config.seeds_full_path = "/non/existent/path"
73
- config.load_default_seeds = false
74
- config.load_seeds = true
75
- end
76
- end
77
-
78
- it "outputs a message" do
79
- loader.load_seed
80
- expect(log_output.string).to match(/Seed directory.*does not exist/)
81
- end
82
- end
83
- end
84
-
85
- describe "#load_seed_file" do
86
- context "when loading by class name" do
87
- before do
88
- SeedBuilderUser.delete_all
89
- loader.load_seed_file("create_users")
90
- end
91
-
92
- it "loads the specific seed file" do
93
- expect(SeedBuilderUser.count).to eq 1
94
- expect(SeedBuilderUser.first.email).to eq "test@example.com"
95
- end
96
- end
97
-
98
- context "when loading by full name" do
99
- before do
100
- SeedBuilderUser.delete_all
101
- loader.load_seed_file("20241206200111_create_users")
102
- end
103
-
104
- it "loads the specific seed file" do
105
- expect(SeedBuilderUser.count).to eq 1
106
- end
107
- end
108
-
109
- context "when loading by timestamp" do
110
- before do
111
- SeedBuilderUser.delete_all
112
- loader.load_seed_file("20241206200111")
113
- end
114
-
115
- it "loads the specific seed file" do
116
- expect(SeedBuilderUser.count).to eq 1
117
- end
118
- end
119
-
120
- context "when seed file does not exist" do
121
- let(:log_output) { StringIO.new }
122
- let(:test_logger) { Logger.new(log_output) }
123
-
124
- before do
125
- allow(Rails).to receive(:logger).and_return(test_logger)
126
- # Reset SeedBuilder logger to pick up the stubbed Rails.logger
127
- SeedBuilder.instance_variable_set(:@logger, nil) if SeedBuilder.instance_variable_defined?(:@logger)
128
- end
129
-
130
- it "outputs an error message" do
131
- loader.load_seed_file("nonexistent_seed")
132
- expect(log_output.string).to match(/Seed file 'nonexistent_seed' not found/)
133
- end
134
-
135
- it "does not raise an error" do
136
- expect { loader.load_seed_file("nonexistent_seed") }.not_to raise_error
137
- end
138
- end
139
-
140
- context "when multiple seed files match the name" do
141
- let(:seed_path_1) { File.expand_path("../../tmp/rspec/db/seeds/20241206200111_create_users.rb", __FILE__) }
142
- let(:seed_path_2) { File.expand_path("../../tmp/rspec/db/seeds/20241206200112_create_users.rb", __FILE__) }
143
- let(:log_output) { StringIO.new }
144
- let(:test_logger) { Logger.new(log_output) }
145
-
146
- before do
147
- allow(Rails).to receive(:logger).and_return(test_logger)
148
- # Reset SeedBuilder logger to pick up the stubbed Rails.logger
149
- SeedBuilder.instance_variable_set(:@logger, nil) if SeedBuilder.instance_variable_defined?(:@logger)
150
- FileUtils.mkdir_p(File.dirname(seed_path_1))
151
- File.write(seed_path_1, "class CreateUsers; def change; end; end")
152
- File.write(seed_path_2, "class CreateUsers; def change; end; end")
153
- SeedBuilder.configure do |config|
154
- config.seeds_full_path = File.dirname(seed_path_1)
155
- end
156
- end
157
-
158
- after do
159
- File.delete(seed_path_1) if File.exist?(seed_path_1)
160
- File.delete(seed_path_2) if File.exist?(seed_path_2)
161
- end
162
-
163
- it "outputs an error message listing all matching files" do
164
- loader.load_seed_file("create_users")
165
- expect(log_output.string).to match(
166
- /Multiple seed files match 'create_users':.*20241206200111_create_users.*20241206200112_create_users.*Please be more specific/m
167
- )
168
- end
169
-
170
- it "does not raise an error" do
171
- expect { loader.load_seed_file("create_users") }.not_to raise_error
172
- end
173
-
174
- it "works when using the full name with timestamp" do
175
- SeedBuilderUser.delete_all
176
- loader.load_seed_file("20241206200111_create_users")
177
- expect(log_output.string).not_to match(/Multiple seed files/)
178
- expect(SeedBuilderUser.count).to eq 0 # The seed doesn't actually create anything, just checks it runs
179
- end
180
- end
181
-
182
- context "when seeds directory does not exist" do
183
- let(:log_output) { StringIO.new }
184
- let(:test_logger) { Logger.new(log_output) }
185
-
186
- before do
187
- allow(Rails).to receive(:logger).and_return(test_logger)
188
- # Reset SeedBuilder logger to pick up the stubbed Rails.logger
189
- SeedBuilder.instance_variable_set(:@logger, nil) if SeedBuilder.instance_variable_defined?(:@logger)
190
- SeedBuilder.configure do |config|
191
- config.seeds_full_path = "/non/existent/path"
192
- end
193
- end
194
-
195
- it "outputs an error message" do
196
- loader.load_seed_file("create_users")
197
- expect(log_output.string).to match(/Seed directory.*does not exist/)
198
- end
199
-
200
- it "does not raise an error" do
201
- expect { loader.load_seed_file("create_users") }.not_to raise_error
202
- end
203
- end
204
-
205
- context "when seed file has invalid format" do
206
- let(:invalid_seed_path) { File.expand_path("../../tmp/rspec/invalid_seed.rb", __FILE__) }
207
- let(:log_output) { StringIO.new }
208
- let(:test_logger) { Logger.new(log_output) }
209
-
210
- before do
211
- allow(Rails).to receive(:logger).and_return(test_logger)
212
- # Reset SeedBuilder logger to pick up the stubbed Rails.logger
213
- SeedBuilder.instance_variable_set(:@logger, nil) if SeedBuilder.instance_variable_defined?(:@logger)
214
- FileUtils.mkdir_p(File.dirname(invalid_seed_path))
215
- File.write(invalid_seed_path, "class InvalidSeed; end")
216
- SeedBuilder.configure do |config|
217
- config.seeds_full_path = File.dirname(invalid_seed_path)
218
- end
219
- end
220
-
221
- after do
222
- File.delete(invalid_seed_path) if File.exist?(invalid_seed_path)
223
- end
224
-
225
- it "outputs an error message" do
226
- loader.load_seed_file("invalid_seed")
227
- expect(log_output.string).to match(/Invalid seed file format/)
228
- end
229
-
230
- it "does not raise an error" do
231
- expect { loader.load_seed_file("invalid_seed") }.not_to raise_error
232
- end
233
- end
234
-
235
- context "when seed class does not respond to change" do
236
- let(:invalid_seed_path) { File.expand_path("../../tmp/rspec/db/seeds/20241206200112_no_change.rb", __FILE__) }
237
-
238
- before do
239
- FileUtils.mkdir_p(File.dirname(invalid_seed_path))
240
- File.write(invalid_seed_path, <<~RUBY)
241
- class NoChange
242
- # Does not have change method
243
- end
244
- RUBY
245
- SeedBuilder.configure do |config|
246
- config.seeds_full_path = File.dirname(invalid_seed_path)
247
- end
248
- end
249
-
250
- after do
251
- File.delete(invalid_seed_path) if File.exist?(invalid_seed_path)
252
- end
253
-
254
- it "raises an error" do
255
- expect { loader.load_seed_file("no_change") }.to raise_error(/does not respond to :change/)
256
- end
257
- end
258
-
259
- context "when seed raises RecordInvalid" do
260
- let(:invalid_seed_path) { File.expand_path("../../tmp/rspec/db/seeds/20241206200113_invalid_record.rb", __FILE__) }
261
- let(:log_output) { StringIO.new }
262
- let(:test_logger) { Logger.new(log_output) }
263
-
264
- before do
265
- allow(Rails).to receive(:logger).and_return(test_logger)
266
- # Reset SeedBuilder logger to pick up the stubbed Rails.logger
267
- SeedBuilder.instance_variable_set(:@logger, nil) if SeedBuilder.instance_variable_defined?(:@logger)
268
- # Add a validation to SeedBuilderUser to make save! fail
269
- SeedBuilderUser.class_eval do
270
- validates :email, presence: true
271
- end
272
-
273
- FileUtils.mkdir_p(File.dirname(invalid_seed_path))
274
- File.write(invalid_seed_path, <<~RUBY)
275
- class InvalidRecord
276
- def change
277
- user = SeedBuilderUser.new # Missing required email
278
- user.save! # This will fail validation
279
- end
280
- end
281
- RUBY
282
- SeedBuilder.configure do |config|
283
- config.seeds_full_path = File.dirname(invalid_seed_path)
284
- end
285
- end
286
-
287
- after do
288
- File.delete(invalid_seed_path) if File.exist?(invalid_seed_path)
289
- # Remove the validation
290
- SeedBuilderUser.reset_column_information
291
- end
292
-
293
- it "outputs error message and re-raises" do
294
- expect { loader.load_seed_file("invalid_record") }.to raise_error(ActiveRecord::RecordInvalid)
295
- expect(log_output.string).to match(/Seeding.*failed/)
296
- end
297
- end
298
- end
299
- end
@@ -1,118 +0,0 @@
1
- require "spec_helper"
2
-
3
- # Define Rails::Railtie stub for testing
4
- unless defined?(Rails::Railtie)
5
- module Rails
6
- class Railtie
7
- def self.config
8
- @config ||= Class.new do
9
- def to_prepare(&block)
10
- @to_prepare_blocks ||= []
11
- if block_given?
12
- @to_prepare_blocks << block
13
- end
14
- self
15
- end
16
-
17
- def call_to_prepare
18
- (@to_prepare_blocks || []).each(&:call)
19
- end
20
- end.new
21
- end
22
- end
23
- end
24
- end
25
-
26
- # Load the Railtie manually since it's conditionally required
27
- require_relative "../../lib/seed_builder/railtie"
28
-
29
- class SeedBuilderUser < ActiveRecord::Base; end
30
- class SeedUser < SeedBuilderUser; end
31
-
32
- describe SeedBuilder::Railtie do
33
- let(:app_class) do
34
- Class.new do
35
- def initialize
36
- @prepared = false
37
- end
38
-
39
- def load_seed
40
- # Default implementation - should be patched by Railtie
41
- raise "Original load_seed called"
42
- end
43
-
44
- def prepare!
45
- @prepared = true
46
- end
47
-
48
- def prepared?
49
- @prepared
50
- end
51
- end
52
- end
53
-
54
- let(:rails_application) { app_class.new }
55
- let(:logger) { Logger.new($stdout) }
56
- let(:seeds_path) { File.expand_path("../../fixtures/seeds", __FILE__) }
57
- let(:default_seeds_path) { File.expand_path("../../fixtures/seeds.rb", __FILE__) }
58
-
59
- before do
60
- # Set up Rails.application mock
61
- # In Rails, Rails.application.class_eval works because Rails.application is an instance
62
- # and class_eval is called on it, which modifies the class.
63
- # We need to stub this properly.
64
- allow(Rails).to receive(:application).and_return(rails_application)
65
- allow(Rails).to receive(:root).and_return(rails_root)
66
- allow(Rails).to receive(:env).and_return(rails_env)
67
- allow(Rails).to receive(:logger).and_return(logger)
68
- allow(Rails).to receive(:logger=)
69
-
70
- SeedBuilder.configure do |config|
71
- config.seeds_full_path = seeds_path
72
- config.default_seeds_full_path = default_seeds_path
73
- config.load_default_seeds = true
74
- config.load_seeds = true
75
- config.use_seed_loader = true
76
- end
77
-
78
- # Clear any existing data
79
- SeedUser.delete_all
80
- SeedBuilderUser.delete_all
81
- end
82
-
83
- describe "Rails.application.load_seed integration" do
84
- context "when use_seed_loader is enabled" do
85
- before do
86
- # Ensure rails_application is created before calling to_prepare
87
- # so that Rails.application returns the correct instance
88
- rails_application
89
- # Simulate Rails initialization by running the Railtie's to_prepare blocks
90
- SeedBuilder::Railtie.config.call_to_prepare
91
- end
92
-
93
- it "patches Rails.application.load_seed to use SeedBuilder loader" do
94
- expect { rails_application.load_seed }.not_to raise_error
95
- end
96
-
97
- it "loads seeds using SeedBuilder loader" do
98
- rails_application.load_seed
99
- expect(SeedUser.count).to eq 1
100
- expect(SeedBuilderUser.count).to eq 1
101
- end
102
- end
103
-
104
- context "when use_seed_loader is disabled" do
105
- before do
106
- SeedBuilder.configure do |config|
107
- config.use_seed_loader = false
108
- end
109
- # Simulate Rails initialization by running the Railtie's to_prepare blocks
110
- SeedBuilder::Railtie.config.call_to_prepare
111
- end
112
-
113
- it "does not patch Rails.application.load_seed" do
114
- expect { rails_application.load_seed }.to raise_error("Original load_seed called")
115
- end
116
- end
117
- end
118
- end
@@ -1,25 +0,0 @@
1
- require "spec_helper"
2
-
3
- describe SeedBuilder do
4
- let(:gem_specification) { Gem::Specification.load(File.expand_path("../../seed_builder.gemspec", __FILE__)) }
5
-
6
- it "has a version number" do
7
- expect(described_class::VERSION).to eq gem_specification.version.to_s
8
- end
9
-
10
- let(:changelog_file) { File.expand_path("../../CHANGELOG.md", __FILE__) }
11
- it "has changelog for the version" do
12
- expect(File.exist?(changelog_file)).to be true
13
- expect(File.read(changelog_file)).to include("# #{gem_specification.version}")
14
- end
15
-
16
- let(:license_file) { File.expand_path("../../LICENSE.md", __FILE__) }
17
- it "has license" do
18
- expect(File.exist?(license_file)).to be true
19
- end
20
-
21
- let(:readme_file) { File.expand_path("../../README.md", __FILE__) }
22
- it "has readme" do
23
- expect(File.exist?(readme_file)).to be true
24
- end
25
- end
data/spec/spec_helper.rb DELETED
@@ -1,26 +0,0 @@
1
- require "simplecov"
2
- SimpleCov.start do
3
- add_filter "/spec/"
4
- add_filter { |source_file| source_file.lines.count < 5 }
5
- end
6
-
7
- require "simplecov-cobertura"
8
- SimpleCov.formatter = SimpleCov::Formatter::CoberturaFormatter
9
-
10
- # Fix for Rails 6.1 compatibility: require Logger before ActiveRecord
11
- # to avoid "uninitialized constant ActiveSupport::LoggerThreadSafeLevel::Logger" error
12
- require "logger"
13
- require "active_record"
14
-
15
- require "seed_builder"
16
-
17
- Dir[File.expand_path("support/**/*.rb", __dir__)].each { |f| require_relative f }
18
-
19
- RSpec.configure do |config|
20
- include RailsHelpers
21
-
22
- config.before(:suite) do
23
- ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
24
- load File.expand_path("../fixtures/schema.rb", __FILE__)
25
- end
26
- end
@@ -1,6 +0,0 @@
1
- if ENV["CI"]
2
- require "rspec_junit_formatter"
3
- RSpec.configure do |config|
4
- config.add_formatter RspecJunitFormatter, "coverage/junit-coverage.xml"
5
- end
6
- end
@@ -1,41 +0,0 @@
1
- module RailsHelpers
2
- def rails_root(destination_root = File.expand_path("../../tmp/rspec", __dir__))
3
- Class.new do
4
- def initialize(destination_root)
5
- @destination_root = destination_root
6
- end
7
-
8
- def to_s
9
- @destination_root
10
- end
11
-
12
- def join(*args)
13
- File.join(@destination_root, *args)
14
- end
15
- end.new(destination_root)
16
- end
17
-
18
- def rails_env(env = "development")
19
- Class.new do
20
- def initialize(env)
21
- @env = env.to_s
22
- end
23
-
24
- def development?
25
- @env == "development"
26
- end
27
-
28
- def test?
29
- @env == "test"
30
- end
31
-
32
- def production?
33
- @env == "production"
34
- end
35
-
36
- def to_s
37
- @env
38
- end
39
- end.new(env)
40
- end
41
- end
data/usr/bin/release.sh DELETED
@@ -1,35 +0,0 @@
1
- #!/bin/bash
2
-
3
- set -e
4
-
5
- e() {
6
- GREEN='\033[0;32m'
7
- NC='\033[0m'
8
- echo -e "${GREEN}$1${NC}"
9
- eval "$1"
10
- }
11
-
12
- e "bundle"
13
- e "bundle exec rspec"
14
-
15
- if [[ $(git diff --shortstat 2>/dev/null | tail -n1) != "" ]]; then
16
- echo -e "\033[1;31mgit working directory not clean, please commit your changes first \033[0m"
17
- exit 1
18
- fi
19
-
20
- GEM_NAME="seed_builder"
21
- VERSION=$(grep -Eo "VERSION\s*=\s*\".+\"" lib/seed_builder.rb | grep -Eo "[0-9.]{5,}")
22
- GEM_FILE="$GEM_NAME-$VERSION.gem"
23
-
24
- e "gem build $GEM_NAME.gemspec"
25
-
26
- echo "Ready to release $GEM_FILE $VERSION"
27
- read -p "Continue? [Y/n] " answer
28
- if [[ "$answer" != "Y" ]]; then
29
- echo "Exiting"
30
- exit 1
31
- fi
32
-
33
- e "gem push $GEM_FILE"
34
- e "git tag $VERSION && git push --tags"
35
- e "gh release create $VERSION --generate-notes"