@@ -1212,64 +1212,128 @@ def body=(body); end
12121212 RBI
12131213 end
12141214
1215- it "aborts if there are pending migrations" do
1216- @project . require_real_gem ( "rake" , "13.0.6" )
1217- @project . bundle_install
1215+ describe "pending migrations" do
1216+ before do
1217+ @project . write ( "db/migrate/202001010000_create_articles.rb" , <<~RB )
1218+ class CreateArticles < ActiveRecord::Migration[6.1]
1219+ def change
1220+ create_table(:articles) do |t|
1221+ t.timestamps
1222+ end
1223+ end
1224+ end
1225+ RB
12181226
1219- @project . write ( "lib/post .rb" , <<~RB )
1220- require "smart_properties "
1227+ @project . write ( "lib/database .rb" , <<~RB )
1228+ require "rake "
12211229
1222- class Post
1223- include SmartProperties
1224- property :title, accepts: String
1225- end
1226- RB
1230+ namespace :db do
1231+ task :abort_if_pending_migrations do
1232+ pending_migrations = Dir["\# {Kernel.__dir__}/../db/migrate/*.rb"]
12271233
1228- @project . write ( "db/migrate/202001010000_create_articles.rb" , <<~RB )
1229- class CreateArticles < ActiveRecord::Migration[6.1]
1230- def change
1231- create_table(:articles) do |t|
1232- t.timestamps
1234+ if pending_migrations.any?
1235+ Kernel.puts "You have \# {pending_migrations.size} pending migration:"
1236+
1237+ pending_migrations.each do |pending_migration|
1238+ name = pending_migration.split("/").last
1239+ Kernel.puts name
1240+ end
1241+
1242+ Kernel.abort(%{Run `bin/rails db:migrate` to update your database then try again.})
1243+ end
12331244 end
12341245 end
1235- end
1236- RB
1246+ RB
12371247
1238- @project . write ( "lib/database.rb" , <<~RB )
1239- require "rake"
1248+ @project . require_real_gem ( "rake" , "13.0.6" )
1249+ @project . require_real_gem ( "activerecord" )
1250+ @project . bundle_install
1251+ end
12401252
1241- namespace :db do
1242- task :abort_if_pending_migrations do
1243- pending_migrations = Dir["\# {Kernel.__dir__}/../db/migrate/*.rb"]
1253+ it "aborts if there are pending migrations" do
1254+ @project . write ( "lib/post.rb" , <<~RB )
1255+ class Post < ActiveRecord::Base
1256+ end
1257+ RB
12441258
1245- if pending_migrations.any?
1246- Kernel.puts "You have \# {pending_migrations.size} pending migration:"
1259+ result = @project . tapioca ( "dsl Post" )
12471260
1248- pending_migrations.each do |pending_migration|
1249- name = pending_migration.split("/").last
1250- Kernel.puts name
1251- end
1261+ # FIXME: print the error to the correct stream
1262+ assert_equal ( <<~OUT , result . out )
1263+ Loading Rails application... Done
1264+ Loading DSL compiler classes... Done
1265+ Compiling DSL RBI files...
12521266
1253- Kernel.abort(%{Run `bin/rails db:migrate` to update your database then try again.})
1254- end
1267+ You have 1 pending migration:
1268+ 202001010000_create_articles.rb
1269+ OUT
1270+
1271+ assert_equal ( <<~ERR , result . err )
1272+ Run `bin/rails db:migrate` to update your database then try again.
1273+ ERR
1274+
1275+ refute_success_status ( result )
1276+ end
1277+
1278+ it "aborts if there are pending migrations and no arg was passed" do
1279+ @project . write ( "lib/post.rb" , <<~RB )
1280+ class Post < ActiveRecord::Base
12551281 end
1256- end
1257- RB
1282+ RB
12581283
1259- result = @project . tapioca ( "dsl" )
1284+ result = @project . tapioca ( "dsl" )
12601285
1261- # FIXME: print the error to the correct stream
1262- assert_equal ( <<~OUT , result . out )
1263- Loading Rails application... Done
1264- You have 1 pending migration:
1265- 202001010000_create_articles.rb
1266- OUT
1286+ # FIXME: print the error to the correct stream
1287+ assert_equal ( <<~OUT , result . out )
1288+ Loading Rails application... Done
1289+ Loading DSL compiler classes... Done
1290+ Compiling DSL RBI files...
12671291
1268- assert_equal ( <<~ERR , result . err )
1269- Run `bin/rails db:migrate` to update your database then try again.
1270- ERR
1292+ You have 1 pending migration:
1293+ 202001010000_create_articles.rb
1294+ OUT
12711295
1272- refute_success_status ( result )
1296+ assert_equal ( <<~ERR , result . err )
1297+ Run `bin/rails db:migrate` to update your database then try again.
1298+ ERR
1299+
1300+ refute_success_status ( result )
1301+ end
1302+
1303+ it "does not abort if there are pending migrations but no active record models" do
1304+ @project . write ( "lib/post.rb" , <<~RB )
1305+ require "smart_properties"
1306+
1307+ class Post
1308+ include SmartProperties
1309+ property :title, accepts: String
1310+ end
1311+ RB
1312+
1313+ result = @project . tapioca ( "dsl Post" )
1314+
1315+ assert_equal ( <<~OUT , result . out )
1316+ Loading Rails application... Done
1317+ Loading DSL compiler classes... Done
1318+ Compiling DSL RBI files...
1319+
1320+ create sorbet/rbi/dsl/post.rbi
1321+
1322+ Done
1323+
1324+ Checking generated RBI files... Done
1325+ No errors found
1326+
1327+ All operations performed in working directory.
1328+ Please review changes and commit them.
1329+ OUT
1330+
1331+ assert_empty_stderr ( result )
1332+
1333+ assert_project_file_exist ( "sorbet/rbi/dsl/post.rbi" )
1334+
1335+ assert_success_status ( result )
1336+ end
12731337 end
12741338
12751339 it "overwrites existing RBIs without user input" do
0 commit comments