diff --git a/lib/metasploit/framework.rb b/lib/metasploit/framework.rb index aa488aea2a..a43afdc93c 100644 --- a/lib/metasploit/framework.rb +++ b/lib/metasploit/framework.rb @@ -32,6 +32,10 @@ module Metasploit # works in compatible manner with activerecord's rake tasks and other # railties. module Framework + extend ActiveSupport::Autoload + + autoload :Spec + # Returns the root of the metasploit-framework project. Use in place of # `Rails.root`. # diff --git a/lib/metasploit/framework/spec.rb b/lib/metasploit/framework/spec.rb new file mode 100644 index 0000000000..ef9a15697f --- /dev/null +++ b/lib/metasploit/framework/spec.rb @@ -0,0 +1,5 @@ +module Metasploit::Framework::Spec + extend ActiveSupport::Autoload + + autoload :Constants +end \ No newline at end of file diff --git a/lib/metasploit/framework/spec/constants.rb b/lib/metasploit/framework/spec/constants.rb new file mode 100644 index 0000000000..834b5d0831 --- /dev/null +++ b/lib/metasploit/framework/spec/constants.rb @@ -0,0 +1,43 @@ +require 'msf/core/modules' + +# Monitor constants created by module loading to ensure that the loads in one example don't interfere with the +# assertions in another example. +module Metasploit::Framework::Spec::Constants + # The parent namespace constant that can have children added when loading modules. + PARENT_CONSTANT = Msf::Modules + + # Configures after(:suite) callback for RSpec to check for leaked constants. + def self.configure! + unless @configured + RSpec.configure do |config| + config.after(:suite) do + ::Metasploit::Framework::Spec::Constants.each { |child_name| + $stderr.puts "#{child_name} not removed from #{PARENT_CONSTANT}" + } + end + end + + @configured = true + end + end + + # Yields each constant under {PARENT_CONSTANT}. + # + # @yield [child_name] + # @yieldparam child_name [String] name of constant relative to {PARENT_CONSTANT}. + # @yieldreturn [void] + # @return [Integer] count + def self.each + inherit = false + count = 0 + + child_constant_names = PARENT_CONSTANT.constants(inherit) + + child_constant_names.each do |child_constant_name| + count += 1 + yield child_constant_name + end + + count + end +end \ No newline at end of file diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 26d87fe257..2136654d00 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -53,3 +53,5 @@ RSpec.configure do |config| # instead of true. config.use_transactional_fixtures = true end + +Metasploit::Framework::Spec::Constants.configure!