Skip to content

Commit

Permalink
Add a test requiring data migrations to have an associated spec file
Browse files Browse the repository at this point in the history
We can determine which files are data migrations since they will define a
class that inherits from ActiveRecord::Base.  If we hook inherited in
AR::Base, we can get the constant and the file in which it is defined
(the data migration).
  • Loading branch information
bdunne committed Aug 8, 2018
1 parent 4f46f8e commit d485d17
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
27 changes: 27 additions & 0 deletions spec/automated_review/data_migrations_need_tests_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
describe "Data migrations" do
KNOWN_MISSING_DATA_MIGRATION_SPEC_FILES = [
"20151021174044_add_tenant_default_group_spec.rb",
"20160317041206_add_maintenance_to_host_spec.rb",
"20160428215808_add_filters_to_entitlements_spec.rb",
"20160713130940_remove_type_template_and_vms_filters_from_miq_search_spec.rb",
"20170419154137_remove_deleted_migration_timestamps_spec.rb",
"20171011180000_move_openstack_refresher_settings_spec.rb",
"20180718132840_remove_transformation_product_setting_spec.rb"
]

def data_migration_files
Spec::Support::ConstantWatcher.classes_by_file.keys.select { |file| file.include?("db/migrate") }
end

it "need tests" do
Dir.glob(ManageIQ::Schema::Engine.root.join("db", "migrate", "*.rb")).each { |i| require i }

data_migration_files.each do |data_migration_file|
spec_file_basename = File.basename(data_migration_file).sub(".rb", "_spec.rb")
next if spec_file_basename.in?(KNOWN_MISSING_DATA_MIGRATION_SPEC_FILES)
spec_file = ManageIQ::Schema::Engine.root.join("spec", "migrations", spec_file_basename)

expect(spec_file.file?).to eq(true), "Missing data migration spec at #{spec_file}"
end
end
end
21 changes: 21 additions & 0 deletions spec/support/constant_watcher.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module Spec
module Support
module ConstantWatcher
mattr_accessor :classes_by_file

def inherited(other)
Spec::Support::ConstantWatcher.add(other)
super
end

def self.add(const)
path = caller_locations[1].path
self.classes_by_file ||= {}
self.classes_by_file[path] ||= []
self.classes_by_file[path] << const.name
end
end
end
end

ActiveRecord::Base.singleton_class.prepend(Spec::Support::ConstantWatcher)

0 comments on commit d485d17

Please sign in to comment.