From 4bec7182b5592c2aeb25172faa1c1f735166dcf5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20Piotaix?= Date: Thu, 2 Nov 2023 18:28:12 +0100 Subject: [PATCH] Use github actions, remove all unsupported versions and refactor tests (#175) --- .github/workflows/master.yml | 28 +++++++ .travis.yml | 29 ------- Gemfile | 7 +- Rakefile | 26 ++----- .../configurator_spec.rb | 76 +++++++++---------- spec/standalone_migrations_spec.rb | 72 ++++++++---------- standalone_migrations.gemspec | 8 +- 7 files changed, 110 insertions(+), 136 deletions(-) create mode 100644 .github/workflows/master.yml delete mode 100644 .travis.yml diff --git a/.github/workflows/master.yml b/.github/workflows/master.yml new file mode 100644 index 0000000..ed8be0b --- /dev/null +++ b/.github/workflows/master.yml @@ -0,0 +1,28 @@ +name: Development + +on: + push: + +jobs: + tests: + name: Tests + runs-on: ubuntu-latest + strategy: + matrix: + ruby_version: + - 2.7.7 + - 3.0.5 + - 3.1.3 + ar: + - "~> 6.0.0" + - "~> 6.1.0" + env: + NOKOGIRI_USE_SYSTEM_LIBRARIES: 1 + AR: ${{ matrix.ar }} + steps: + - uses: actions/checkout@v3 + - uses: ruby/setup-ruby@v1 + with: + ruby-version: ${{ matrix.ruby_version }} + bundler-cache: true + - run: bundle exec rake spec diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 2b98386..0000000 --- a/.travis.yml +++ /dev/null @@ -1,29 +0,0 @@ -language: ruby -rvm: - - 2.5.9 - - 2.6.9 - - 2.7.5 - - 3.0.3 -env: - - AR="~> 4.2.0" SQL="~>1.3.6" NOKOGIRI_USE_SYSTEM_LIBRARIES=1 - - AR="~> 5.0.0" SQL="~>1.3.6" NOKOGIRI_USE_SYSTEM_LIBRARIES=1 - - AR=">= 5.1.0.rc2,< 5.2" SQL="~>1.3.6" NOKOGIRI_USE_SYSTEM_LIBRARIES=1 - - AR=">= 5.2.0.rc2,< 5.3,!= 5.2.3,!=5.2.3.rc1" SQL="~>1.3.6" NOKOGIRI_USE_SYSTEM_LIBRARIES=1 - - AR="~> 6.0.0" SQL="~>1.4" NOKOGIRI_USE_SYSTEM_LIBRARIES=1 - - AR="~> 6.1.0" SQL="~>1.4" NOKOGIRI_USE_SYSTEM_LIBRARIES=1 - - AR="~> 7.0.0" SQL="~>1.4" NOKOGIRI_USE_SYSTEM_LIBRARIES=1 -jobs: - exclude: - - rvm: 2.6.9 - env: AR="~> 4.2.0" SQL="~>1.3.6" NOKOGIRI_USE_SYSTEM_LIBRARIES=1 - - rvm: 2.7.5 - env: AR="~> 4.2.0" SQL="~>1.3.6" NOKOGIRI_USE_SYSTEM_LIBRARIES=1 - - rvm: 3.0.3 - env: AR="~> 4.2.0" SQL="~>1.3.6" NOKOGIRI_USE_SYSTEM_LIBRARIES=1 - - rvm: 2.7.5 - env: AR="~> 5.0.0" SQL="~>1.3.6" NOKOGIRI_USE_SYSTEM_LIBRARIES=1 - - rvm: 2.7.5 - env: AR=">= 5.1.0.rc2,< 5.2" SQL="~>1.3.6" NOKOGIRI_USE_SYSTEM_LIBRARIES=1 - - rvm: 2.7.5 - env: AR=">= 5.2.0.rc2,< 5.3,!= 5.2.3,!=5.2.3.rc1" SQL="~>1.3.6" NOKOGIRI_USE_SYSTEM_LIBRARIES=1 -script: bundle exec rake specs:travis diff --git a/Gemfile b/Gemfile index b8d44d1..569c040 100644 --- a/Gemfile +++ b/Gemfile @@ -1,11 +1,12 @@ source 'https://rubygems.org' gem 'rake', '>= 10.0' -gem 'activerecord', ENV['AR'] ? ENV['AR'].split(",") : [">= 4.2.7", "< 7.1.0", "!= 5.2.3", "!=5.2.3.rc1"] -gem 'railties', ENV['AR'] ? ENV['AR'].split(",") : [">= 4.2.7", "< 7.1.0", "!= 5.2.3", "!=5.2.3.rc1"] +gem 'activerecord', ENV['AR'] ? ENV['AR'].split(",") : [">= 6.0.0", "< 7.1.0"] +gem 'railties', ENV['AR'] ? ENV['AR'].split(",") : [">= 6.0.0", "< 7.1.0"] +gem 'nokogiri', "~> 1.14.pre" group :dev do - gem 'sqlite3', ENV['SQL'] ? ENV['SQL'].split(",") : ['~> 1.4'] + gem 'sqlite3', '~> 1.5' gem 'rspec', '>= 2.99.0' gem 'jeweler' end diff --git a/Rakefile b/Rakefile index 7dae81d..eb92803 100644 --- a/Rakefile +++ b/Rakefile @@ -1,32 +1,16 @@ require 'rubygems' require 'bundler/setup' -task :default do - sh "rspec spec" -end - task :all do sh "AR='~>3.0.0' bundle update activerecord && bundle exec rake" sh "AR='~>3.1.0.rc4' bundle update activerecord && bundle exec rake" end -task :specs => ["specs:nodb"] -namespace :specs do +begin require 'rspec/core/rake_task' - - desc "only specs that don't use database connection" - RSpec::Core::RakeTask.new "nodb" do |t| - t.pattern = "spec/standalone_migrations/**/*_spec.rb" - end - - desc "run all specs for travis" - RSpec::Core::RakeTask.new "travis" do |t| - t.pattern = "spec/**/*_spec.rb" - t.rspec_opts = "--tag ~@travis_error" - end - - desc "run all specs including those which uses database" - task :all => [:default, :nodb] + RSpec::Core::RakeTask.new(:spec) +rescue LoadError + # no rspec available end # rake install -> install gem locally (for tests) @@ -50,3 +34,5 @@ else Jeweler::GemcutterTasks.new end + +task default: "spec" diff --git a/spec/standalone_migrations/configurator_spec.rb b/spec/standalone_migrations/configurator_spec.rb index 5909b1f..b082050 100644 --- a/spec/standalone_migrations/configurator_spec.rb +++ b/spec/standalone_migrations/configurator_spec.rb @@ -4,30 +4,36 @@ module StandaloneMigrations describe Configurator, "which allows define custom dirs and files to work with your migrations" do - describe "environment yaml configuration loading" do + around(:each) do |example| + Dir.mktmpdir do |dir| + Dir.chdir(dir) do + example.run + end + end + end + describe "environment yaml configuration loading" do let(:env_hash_other_db) do { - "development" => { "adapter" => "mysql2", "database" => "database_name" }, - "test" => { "adapter" => "mysql2", "database" => "database_name" }, - "production" => {"adapter" => "mysql2", "database" => "database_name" } + "development" => {"adapter" => "mysql2", "database" => "database_name"}, + "test" => {"adapter" => "mysql2", "database" => "database_name"}, + "production" => {"adapter" => "mysql2", "database" => "database_name"} } end - before(:all) do + around(:each) do |example| @env_hash = { - "development" => { "adapter" => "sqlite3", "database" => "db/development.sql" }, - "test" => { "adapter" => "sqlite3", "database" => "db/test.sql" }, - "production" => {"adapter" => "sqlite3", "database" => ":memory:" } + "development" => {"adapter" => "sqlite3", "database" => "db/development.sql"}, + "test" => {"adapter" => "sqlite3", "database" => "db/test.sql"}, + "production" => {"adapter" => "sqlite3", "database" => ":memory:"} } - @original_dir = Dir.pwd - Dir.chdir( File.expand_path("../../", __FILE__) ) - FileUtils.mkdir_p "tmp/db" - Dir.chdir "tmp" + FileUtils.mkdir_p "db" File.open("db/config.yml", "w") do |f| f.write @env_hash.to_yaml end + + example.run end it "load the specific environment config" do @@ -49,7 +55,7 @@ module StandaloneMigrations let(:configurator) { Configurator.new } before(:all) do - @new_config = { 'sbrobous' => 'test' } + @new_config = {'sbrobous' => 'test'} Configurator.environments_config do |env| env.on "production" do @new_config @@ -80,10 +86,6 @@ module StandaloneMigrations end - after(:all) do - Dir.chdir @original_dir - end - end context "default values when .standalone_configurations is missing" do @@ -107,10 +109,10 @@ module StandaloneMigrations context "passing configurations as a parameter" do let(:args) do { - :config => "custom/config/database.yml", - :migrate_dir => "custom/db/migrate", - :seeds => "custom/db/seeds.rb", - :schema => "custom/db/schema.rb" + :config => "custom/config/database.yml", + :migrate_dir => "custom/db/migrate", + :seeds => "custom/db/seeds.rb", + :schema => "custom/db/schema.rb" } end @@ -138,17 +140,17 @@ module StandaloneMigrations context "using a .standalone_migrations file with configurations" do - before(:all) do - @original_dir = Dir.pwd - Dir.chdir File.expand_path("../", __FILE__) + before(:each) do + file = ".standalone_migrations" + File.open(file, "w") { |file| file.write(yaml_hash.to_yaml) } end let(:yaml_hash) do { "db" => { - "seeds" => "file/db/seeds.rb", - "migrate" => "file/db/migrate", - "schema" => "file/db/schema.rb" + "seeds" => "file/db/seeds.rb", + "migrate" => "file/db/migrate", + "schema" => "file/db/schema.rb" }, "config" => { "database" => "file/config/database.yml" @@ -159,9 +161,9 @@ module StandaloneMigrations let(:yaml_hash_other_db) do { "db" => { - "seeds" => "db2/seeds.rb", - "migrate" => "db2/migrate", - "schema" => "db2/schema.rb" + "seeds" => "db2/seeds.rb", + "migrate" => "db2/migrate", + "schema" => "db2/schema.rb" }, "config" => { "database" => "config/config_other.yml" @@ -170,20 +172,18 @@ module StandaloneMigrations end let(:configurator) do - file = ".standalone_migrations" - File.open(file, "w") { |file| file.write(yaml_hash.to_yaml) } Configurator.new end context "with database environment variable passed" do - before(:all) do + before(:each) do ENV['DATABASE'] = "other_db" + file_other_db = ".other_db.standalone_migrations" + File.open(file_other_db, "w") { |file| file.write(yaml_hash_other_db.to_yaml) } end let(:other_configurator) do - file_other_db = ".other_db.standalone_migrations" - File.open(file_other_db, "w") { |file| file.write(yaml_hash_other_db.to_yaml) } Configurator.new end @@ -196,7 +196,6 @@ module StandaloneMigrations end after(:all) do - File.delete ".other_db.standalone_migrations" ENV['DATABASE'] = nil end @@ -245,11 +244,6 @@ module StandaloneMigrations expect(configurator.schema).to eq(yaml_hash["db"]["schema"]) end - after(:all) do - File.delete ".standalone_migrations" - Dir.chdir @original_dir - end - end end end diff --git a/spec/standalone_migrations_spec.rb b/spec/standalone_migrations_spec.rb index e189e75..c17a7ca 100644 --- a/spec/standalone_migrations_spec.rb +++ b/spec/standalone_migrations_spec.rb @@ -3,36 +3,31 @@ def write(file, content) raise "cannot write nil" unless file - file = tmp_file(file) folder = File.dirname(file) - `mkdir -p #{folder}` unless File.exist?(folder) + FileUtils.mkdir_p(folder) if folder != '' File.open(file, 'w') { |f| f.write content } end def read(file) - File.read(tmp_file(file)) + File.read(file) end def migration(name) - m = `cd spec/tmp/db/migrate && ls`.split("\n").detect { |m| m =~ /#{name}/ } + m = `cd db/migrate && ls`.split("\n").detect { |m| m =~ /#{name}/ } m ? "db/migrate/#{m}" : m end - def tmp_file(file) - "spec/tmp/#{file}" - end - def schema - ENV['SCHEMA'] + ENV['SCHEMA'] || ActiveRecord::Tasks::DatabaseTasks.schema_file(ActiveRecord::Base.schema_format) end def run(cmd) - result = `cd spec/tmp && #{cmd} 2>&1` + result = `#{cmd} 2>&1` raise result unless $?.success? result end - def make_migration(name, options={}) + def make_migration(name, options = {}) task_name = options[:task_name] || 'db:new_migration' migration = run("rake #{task_name} name=#{name}").match(%r{db/migrate/\d+.*.rb})[0] content = read(migration) @@ -42,9 +37,9 @@ def make_migration(name, options={}) migration.match(/\d{14}/)[0] end - def write_rakefile(config=nil) + def write_rakefile(config = nil) write 'Rakefile', <<-TXT -$LOAD_PATH.unshift '#{File.expand_path('lib')}' +$LOAD_PATH.unshift '#{File.expand_path('../../lib')}' begin require "standalone_migrations" StandaloneMigrations::Tasks.load_tasks @@ -56,10 +51,10 @@ def write_rakefile(config=nil) def write_multiple_migrations migration_superclass = if Rails::VERSION::MAJOR >= 5 - "ActiveRecord::Migration[#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}]" - else - "ActiveRecord::Migration" - end + "ActiveRecord::Migration[#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}]" + else + "ActiveRecord::Migration" + end write_rakefile %{t.migrations = "db/migrations", "db/migrations2"} write "db/migrate/20100509095815_create_tests.rb", <<-TXT @@ -86,10 +81,17 @@ def down TXT end + around(:each) do |example| + FileUtils.mkdir_p('tmp') + Dir.mktmpdir("spec-", "tmp") do |dir| + Dir.chdir(dir) do + example.run + end + end + end + before do StandaloneMigrations::Configurator.instance_variable_set(:@env_config, nil) - `rm -rf spec/tmp` if File.exist?('spec/tmp') - `mkdir spec/tmp` write_rakefile write(schema, '') write 'db/config.yml', <<-TXT @@ -105,10 +107,6 @@ def down TXT end - after(:all) do - `rm -rf spec/tmp` if File.exist?('spec/tmp') - end - it "warns of deprecated folder structure" do warning = /DEPRECATED.* db\/migrate/ expect(run("rake db:create")).not_to match(warning) @@ -135,16 +133,14 @@ def down expect(connection_established).to be true end - Dir.chdir(File.join(File.dirname(__FILE__), "tmp")) do - load "Rakefile" - Rake::Task['standalone:connection'].invoke - end + load "Rakefile" + Rake::Task['standalone:connection'].invoke end end describe 'db:new_migration' do it "fails if i do not add a name" do - expect(lambda{ run("rake db:new_migration") }).to raise_error(/name=/) + expect { run("rake db:new_migration") }.to raise_error(/name=/) end it "generates a new migration with this name from ENV and timestamp" do @@ -213,7 +209,7 @@ def down make_migration('yyy') # Rails has a bug where it's sending a bad failure exception # https://github.com/rails/rails/issues/28905 - expect(lambda{ run("rake db:migrate:down") }).to raise_error(/VERSION|version/) + expect { run("rake db:migrate:down") }.to raise_error(/VERSION|version/) end end @@ -232,7 +228,7 @@ def down make_migration('yyy') # Rails has a bug where it's sending a bad failure exception # https://github.com/rails/rails/issues/28905 - expect(lambda{ run("rake db:migrate:up") }).to raise_error(/VERSION|version/) + expect { run("rake db:migrate:up") }.to raise_error(/VERSION|version/) end end @@ -271,7 +267,7 @@ def down describe 'db:schema:load' do it "loads the schema" do run('rake db:schema:dump') - write(schema, read(schema)+"\nputs 'LOADEDDD'") + write(schema, read(schema) + "\nputs 'LOADEDDD'") result = run('rake db:schema:load') expect(result).to match(/LOADEDDD/) end @@ -282,7 +278,7 @@ def down run "rake db:drop" run "rake db:create" run "rake db:schema:load" - expect(run( "rake db:migrate").strip).to eq('') + expect(run("rake db:migrate").strip).to eq('') end end @@ -293,7 +289,7 @@ def down it "fails when migrations are pending" do make_migration('yyy') - expect(lambda{ run("rake db:abort_if_pending_migrations") }).to raise_error(/1 pending migration/) + expect { run("rake db:abort_if_pending_migrations") }.to raise_error(/1 pending migration/) end end @@ -304,9 +300,9 @@ def down end it "fails without schema" do - schema_path = "spec/tmp/#{schema}" + schema_path = schema `rm -rf #{schema_path}` if File.exist?(schema_path) - expect(lambda{ run("rake db:test:load") }).to raise_error(/try again/) + expect { run("rake db:test:load") }.to raise_error(/try again/) end end @@ -335,14 +331,12 @@ def down write(".standalone_migrations", yaml_hash.to_yaml) end - it "loads" do write("db/seeds2.rb", "puts 'LOADEDDD'") expect(run("rake db:seed")).to match(/LOADEDDD/) end end - it "does nothing without seeds" do expect(run("rake db:seed").length).to eq(0) end @@ -352,7 +346,7 @@ def down it "should not error when a seeds file does not exist" do make_migration('yyy') run('rake db:migrate DB=test') - expect(lambda{ run("rake db:reset") }).not_to raise_error + expect { run("rake db:reset") }.not_to raise_error end end @@ -365,7 +359,7 @@ def down end it "should error on an invalid database", :travis_error => true do - expect(lambda{ run("rake db:create RAILS_ENV=nonexistent")}).to raise_error(/rake aborted/) + expect { run("rake db:create RAILS_ENV=nonexistent") }.to raise_error(/rake aborted/) end end end diff --git a/standalone_migrations.gemspec b/standalone_migrations.gemspec index fe9ad97..25db515 100644 --- a/standalone_migrations.gemspec +++ b/standalone_migrations.gemspec @@ -60,12 +60,12 @@ Gem::Specification.new do |s| if s.respond_to? :add_runtime_dependency then s.add_runtime_dependency(%q.freeze, [">= 10.0"]) - s.add_runtime_dependency(%q.freeze, [">= 4.2.7", "< 7.1.0", "!= 5.2.3", "!= 5.2.3.rc1"]) - s.add_runtime_dependency(%q.freeze, [">= 4.2.7", "< 7.1.0", "!= 5.2.3", "!= 5.2.3.rc1"]) + s.add_runtime_dependency(%q.freeze, [">= 6.0.0", "< 7.1.0"]) + s.add_runtime_dependency(%q.freeze, [">= 6.0.0", "< 7.1.0"]) else s.add_dependency(%q.freeze, [">= 10.0"]) - s.add_dependency(%q.freeze, [">= 4.2.7", "< 7.1.0", "!= 5.2.3", "!= 5.2.3.rc1"]) - s.add_dependency(%q.freeze, [">= 4.2.7", "< 7.1.0", "!= 5.2.3", "!= 5.2.3.rc1"]) + s.add_dependency(%q.freeze, [">= 6.0.0", "< 7.1.0"]) + s.add_dependency(%q.freeze, [">= 6.0.0", "< 7.1.0"]) end end