diff --git a/Gemfile b/Gemfile index 8e54876e..4f8e6681 100644 --- a/Gemfile +++ b/Gemfile @@ -2,7 +2,6 @@ source "https://rubygems.org" gemspec -gem "ammeter", ">= 1.1.3" gem "bundler", ">= 1.5" gem "database_cleaner" gem "pg" diff --git a/spec/generators/fx/function/function_generator_spec.rb b/spec/generators/fx/function/function_generator_spec.rb index a9dfcdc9..92317e79 100644 --- a/spec/generators/fx/function/function_generator_spec.rb +++ b/spec/generators/fx/function/function_generator_spec.rb @@ -6,11 +6,11 @@ migration = file("db/migrate/create_function_test.rb") function_definition = file("db/functions/test_v01.sql") - run_generator ["test"] + run_generator(described_class, ["test"]) expect(function_definition).to exist - expect(migration).to be_a_migration - expect(migration_file(migration)).to contain("CreateFunctionTest") + expect_to_be_a_migration(migration) + expect(migration_content(migration)).to include("CreateFunctionTest") end context "when passed --no-migration" do @@ -18,10 +18,10 @@ migration = file("db/migrate/create_function_test.rb") function_definition = file("db/functions/test_v01.sql") - run_generator ["test", "--no-migration"] + run_generator(described_class, ["test"], {migration: false}) expect(function_definition).to exist - expect(Pathname.new(migration_file(migration))).not_to exist + expect(migration).not_to exist end end @@ -35,12 +35,11 @@ migration = file("db/migrate/update_function_test_to_version_2.rb") function_definition = file("db/functions/test_v02.sql") - run_generator ["test"] + run_generator(described_class, ["test"]) expect(function_definition).to exist - expect(migration).to be_a_migration - expect(migration_file(migration)) - .to contain("UpdateFunctionTestToVersion2") + expect_to_be_a_migration(migration) + expect(migration_content(migration)).to include("UpdateFunctionTestToVersion2") end end end diff --git a/spec/generators/fx/trigger/trigger_generator_spec.rb b/spec/generators/fx/trigger/trigger_generator_spec.rb index b56305dc..e467968b 100644 --- a/spec/generators/fx/trigger/trigger_generator_spec.rb +++ b/spec/generators/fx/trigger/trigger_generator_spec.rb @@ -6,12 +6,15 @@ migration = file("db/migrate/create_trigger_test.rb") trigger_definition = file("db/triggers/test_v01.sql") - run_generator ["test", "table_name" => "some_table"] + run_generator( + described_class, + ["test", {"table_name" => "some_table"}] + ) expect(trigger_definition).to exist - expect(migration).to be_a_migration - expect(migration_file(migration)).to contain("CreateTriggerTest") - expect(migration_file(migration)).to contain("on: :some_table") + expect_to_be_a_migration(migration) + expect(migration_content(migration)).to include("CreateTriggerTest") + expect(migration_content(migration)).to include("on: :some_table") end context "when passed --no-migration" do @@ -19,10 +22,14 @@ migration = file("db/migrate/create_trigger_test.rb") trigger_definition = file("db/triggers/test_v01.sql") - run_generator ["test", {"table_name" => "some_table"}, "--no-migration"] + run_generator( + described_class, + ["test", {"table_name" => "some_table"}], + {migration: false} + ) expect(trigger_definition).to exist - expect(Pathname.new(migration_file(migration))).not_to exist + expect(migration).not_to exist end end @@ -30,17 +37,23 @@ migration = file("db/migrate/create_trigger_test.rb") trigger_definition = file("db/triggers/test_v01.sql") - run_generator ["test", "on" => "some_table"] + run_generator( + described_class, + ["test", {"on" => "some_table"}] + ) expect(trigger_definition).to exist - expect(migration).to be_a_migration - expect(migration_file(migration)).to contain("CreateTriggerTest") - expect(migration_file(migration)).to contain("on: :some_table") + expect_to_be_a_migration(migration) + expect(migration_content(migration)).to include("CreateTriggerTest") + expect(migration_content(migration)).to include("on: :some_table") end it "requires `table_name` or `on` to be specified" do expect do - run_generator ["test", "foo" => "some_table"] + run_generator( + described_class, + ["test", {"foo" => "some_table"}] + ) end.to raise_error(ArgumentError) end @@ -49,11 +62,14 @@ migration = file("db/migrate/update_trigger_test_to_version_2.rb") trigger_definition = file("db/triggers/test_v02.sql") - run_generator ["test", "table_name" => "some_table"] + run_generator( + described_class, + ["test", {"table_name" => "some_table"}] + ) expect(trigger_definition).to exist - expect(migration).to be_a_migration - expect(migration_file(migration)).to contain("UpdateTriggerTestToVersion2") - expect(migration_file(migration)).to contain("on: :some_table") + expect_to_be_a_migration(migration) + expect(migration_content(migration)).to include("UpdateTriggerTestToVersion2") + expect(migration_content(migration)).to include("on: :some_table") end end diff --git a/spec/support/generator_setup.rb b/spec/support/generator_setup.rb index 6ef9b0df..a8d425fa 100644 --- a/spec/support/generator_setup.rb +++ b/spec/support/generator_setup.rb @@ -1,11 +1,52 @@ -require "ammeter/init" +module GeneratorSetup + RAILS_ROOT = Pathname.new(File.expand_path("../../../tmp/dummy", __dir__)).freeze + MIGRATION_TIMESTAMP_PATTERN = /\A\d{14}_/ + + def run_generator(generator_class, args = [], options = {}) + allow(Rails).to receive(:root).and_return(RAILS_ROOT) + generator = generator_class.new(args, options, destination_root: RAILS_ROOT) + + silence_stream($stdout) do + generator.invoke_all + end + end + + def file(relative_path) + RAILS_ROOT.join(relative_path) + end + + def migration_content(file_path) + migration_path = find_migration_files(file_path).first + return if migration_path.nil? + + Pathname.new(migration_path).read + end + + def find_migration_files(file_path) + directory = File.dirname(file_path) + basename = File.basename(file_path, ".rb") + Dir.glob(File.join(directory, "*#{basename}.rb")) + end + + def expect_to_be_a_migration(pathname) + migration_files = find_migration_files(pathname) + + expect(migration_files).to be_present, + "expected #{pathname} to be a migration file" + first_migration = migration_files.first + migration_basename = File.basename(first_migration) + expect(migration_basename).to match(MIGRATION_TIMESTAMP_PATTERN), + "expected #{migration_basename} to have timestamp prefix (format: YYYYMMDDHHMMSS_)" + end +end RSpec.configure do |config| + config.include GeneratorSetup, :generator + config.before(:each, :generator) do - fake_rails_root = File.expand_path("../../../tmp/dummy", __FILE__) - allow(Rails).to receive(:root).and_return(Pathname.new(fake_rails_root)) + FileUtils.rm_rf(GeneratorSetup::RAILS_ROOT) if File.exist?(GeneratorSetup::RAILS_ROOT) + FileUtils.mkdir_p(GeneratorSetup::RAILS_ROOT) - destination fake_rails_root - prepare_destination + allow(Rails).to receive(:root).and_return(Pathname.new(GeneratorSetup::RAILS_ROOT)) end end