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 9, 2018
1 parent ef12004 commit 622e6fa
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
29 changes: 29 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,29 @@
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"
].freeze

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")
spec_file = ManageIQ::Schema::Engine.root.join("spec", "migrations", spec_file_basename)

if spec_file_basename.in?(KNOWN_MISSING_DATA_MIGRATION_SPEC_FILES)
expect(spec_file.file?).to eq(false), "Thanks for adding a test!! Please remove #{spec_file_basename} from KNOWN_MISSING_DATA_MIGRATION_SPEC_FILES in #{__FILE__}"
else
expect(spec_file.file?).to eq(true), "Missing data migration spec at #{spec_file}"
end
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(2..2).first.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 622e6fa

Please sign in to comment.