Skip to content

Commit a4989f6

Browse files
committed
Fix Rakefile to not modify ENV
Instead of modifying ENV and running code inside the rake process, run a ruby process with the environment set correctly. This fixes: rake test_up dev_up which previously would migrate the test database up and not the development database up. Have the specs test that this and similar task combinations work. Since I had to modify it anyway, make the annotate task use the test environment and not the development environment. It's generally problem-free to rebuild a test environment by migrating down and up, while there are cases where that isn't great in a development environment. Update the stack specs to use separate databases for the development, test, and production environments, and test that each of them work as expected. Also test that the annotation is actually applied to the model file.
1 parent e2c546d commit a4989f6

File tree

2 files changed

+36
-31
lines changed

2 files changed

+36
-31
lines changed

Rakefile

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
# Migrate
22

33
migrate = lambda do |env, version|
4-
ENV['RACK_ENV'] = env
5-
require_relative 'db'
6-
require 'logger'
7-
Sequel.extension :migration
8-
DB.loggers << Logger.new($stdout) if DB.loggers.empty?
9-
Sequel::Migrator.apply(DB, 'migrate', version)
4+
sh({'RACK_ENV' => env}, FileUtils::RUBY, "-r", "./db", "-r", "logger", "-e", <<~RUBY)
5+
Sequel.extension :migration
6+
DB.loggers << Logger.new($stdout) if DB.loggers.empty?
7+
Sequel::Migrator.apply(DB, 'migrate', #{version.inspect})
8+
RUBY
109
end
1110

1211
desc "Migrate test database to latest version"
@@ -22,7 +21,7 @@ end
2221
desc "Migrate test database all the way down and then back up"
2322
task :test_bounce do
2423
migrate.call('test', 0)
25-
Sequel::Migrator.apply(DB, 'migrate')
24+
migrate.call('test', nil)
2625
end
2726

2827
desc "Migrate development database to latest version"
@@ -38,7 +37,7 @@ end
3837
desc "Migrate development database all the way down and then back up"
3938
task :dev_bounce do
4039
migrate.call('development', 0)
41-
Sequel::Migrator.apply(DB, 'migrate')
40+
migrate.call('development', nil)
4241
end
4342

4443
desc "Migrate production database to latest version"
@@ -49,15 +48,16 @@ end
4948
# Shell
5049

5150
irb = proc do |env|
52-
ENV['RACK_ENV'] = env
5351
trap('INT', "IGNORE")
5452
dir, base = File.split(FileUtils::RUBY)
5553
cmd = if base.sub!(/\Aruby/, 'irb')
56-
File.join(dir, base)
54+
[File.join(dir, base)]
5755
else
58-
"#{FileUtils::RUBY} -S irb"
56+
[FileUtils::RUBY, "-S", "irb"]
5957
end
60-
sh "#{cmd} -r ./models"
58+
cmd.unshift({"RACK_ENV" => env})
59+
cmd << "-r" << "./models"
60+
sh(*cmd)
6161
end
6262

6363
desc "Open irb shell in test mode"
@@ -85,9 +85,7 @@ spec = proc do |type|
8585

8686
desc "Run #{type} specs with coverage"
8787
task :"#{type}_spec_cov" do
88-
ENV['COVERAGE'] = type
89-
sh "#{FileUtils::RUBY} spec/#{type}.rb"
90-
ENV.delete('COVERAGE')
88+
sh({"COVERAGE" => type, "RODA_RENDER_COMPILED_METHOD_SUPPORT" => "no"}, FileUtils::RUBY, "spec/#{type}.rb")
9189
end
9290
end
9391
spec.call('model')
@@ -98,7 +96,6 @@ task default: [:model_spec, :web_spec]
9896

9997
desc "Run all specs with coverage"
10098
task :spec_cov do
101-
ENV['RODA_RENDER_COMPILED_METHOD_SUPPORT'] = 'no'
10299
FileUtils.rm_r('coverage') if File.directory?('coverage')
103100
Dir.mkdir('coverage')
104101
Rake::Task['_spec_cov'].invoke
@@ -109,11 +106,10 @@ task _spec_cov: [:model_spec_cov, :web_spec_cov]
109106

110107
desc "Annotate Sequel models"
111108
task "annotate" do
112-
ENV['RACK_ENV'] = 'development'
113-
require_relative 'models'
114-
DB.loggers.clear
115-
require 'sequel/annotate'
116-
Sequel::Annotate.annotate(Dir['models/**/*.rb'])
109+
sh({'RACK_ENV' => "test"}, FileUtils::RUBY, "-r", "./models", "-r", "sequel/annotate", "-e", <<~RUBY)
110+
DB.loggers.clear
111+
Sequel::Annotate.annotate(Dir['models/**/*.rb'])
112+
RUBY
117113
end
118114

119115
last_line = __LINE__
@@ -166,6 +162,5 @@ end
166162

167163
desc "Run specs to make sure stack works properly, with debugging enabled"
168164
task :stack_spec_debug do
169-
ENV['DEBUG'] = '1'
170-
sh "#{FileUtils::RUBY} -w stack-spec/stack_spec.rb"
165+
sh({"DEBUG" => '1'}, FileUtils::RUBY, "-w", "stack-spec/stack_spec.rb")
171166
end

stack-spec/stack_spec.rb

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,9 @@ def run_puma(*args)
7070

7171
# Run command capturing stderr/stdout
7272
def run_cmd(*cmds)
73+
env = cmds.shift if cmds.first.is_a?(Hash)
7374
command(cmds)
75+
cmds.unshift(env) if env
7476
read, write = IO.pipe
7577
system(*cmds, out: write, err: write).tap{|x| unless x; write.close; p cmds; puts read.read; end}.must_equal true
7678
write.close
@@ -87,7 +89,10 @@ def rewrite(filename)
8789

8890
Dir.chdir(TEST_STACK_DIR) do
8991
run_cmd(RAKE, 'setup[FooBarApp]')
90-
ENV['FOO_BAR_APP_DATABASE_URL'] = db_url
92+
environments = %w[development test production].freeze
93+
rewrite(".env.rb") do |content|
94+
content.gsub(%r"postgres:///foo_bar_app_(\w+)\?user=foo_bar_app", "#{db_url}_\\1")
95+
end
9196

9297
files = []
9398
directories = []
@@ -118,24 +123,29 @@ def rewrite(filename)
118123
end
119124

120125
# Test migrations
121-
run_cmd(RAKE, 'test_up')
122-
run_cmd(RAKE, 'test_down')
123-
run_cmd(RAKE, 'test_bounce')
124-
run_cmd(RAKE, 'dev_up')
125-
run_cmd(RAKE, 'dev_down')
126-
run_cmd(RAKE, 'dev_bounce')
126+
run_cmd(RAKE, 'test_up', 'dev_up')
127+
run_cmd(RAKE, 'test_down', 'dev_down')
128+
run_cmd(RAKE, 'test_bounce', 'dev_bounce')
127129
run_cmd(RAKE, 'prod_up')
130+
131+
environments.each do |env|
132+
run_cmd({"RACK_ENV"=>env}, RUBY, "-r", "./db", "-e", "raise \"migration rake tasks not successful for #{env} environment, tables: \#{DB.tables.sort.join(', ')}\" unless DB.tables.sort == [:model1s, :schema_info]")
133+
end
128134

129135
Dir.mkdir('views/prefix1')
130136
File.binwrite('views/prefix1/p1.erb', "<p>Model1: <%= Model1.first.name %></p>")
131137
rewrite('routes/prefix1.rb'){|s| s.sub("# /prefix1 branch handling", "r.get{view 'p1'}")}
132-
run_cmd(SEQUEL, db_url, '-c', "DB[:model1s].insert(name: 'M1')")
138+
environments.each do |env|
139+
run_cmd(SEQUEL, "#{db_url}_#{env}", '-c', "DB[:model1s].insert(name: 'M1')")
140+
run_cmd(SEQUEL, "#{db_url}_#{env}", '-c', "raise \"invalid count for models in #{env} environment\" unless DB[:model1s].count == 1")
141+
end
133142

134143
# Test running in development mode
135144
run_puma
136145

137146
# Test annotation
138147
run_cmd(RAKE, 'annotate')
148+
File.read("models/model1.rb").must_include "# Table: model1s"
139149

140150
# Test running with refrigerator
141151
rewrite('config.ru') do |s|

0 commit comments

Comments
 (0)