1
mirror of https://github.com/rapid7/metasploit-framework synced 2024-08-28 23:26:18 +02:00

Extract Metasploit::Framework::Spec::Constants::Suite

MSP-11130

`Metasploit::Framework::Spec::Constants::Suite` extracts out
`LOG_PATHNAME`, `configure!`, and `define_task` as those piece are
specific to handling constant leaks for the entire suite.  This is in
preparation for `Metasploit::Framework::Spec::Constants::Each`.
This commit is contained in:
Luke Imhoff 2014-10-28 15:07:32 -05:00
parent 3ec9cf54c9
commit f9b1f2a424
No known key found for this signature in database
GPG Key ID: 5B1FB01FB33356F8
4 changed files with 75 additions and 57 deletions

View File

@ -9,4 +9,4 @@ require 'metasploit/framework/require'
Metasploit::Framework::Require.optionally_active_record_railtie
Metasploit::Framework::Application.load_tasks
Metasploit::Framework::Spec::Constants.define_task
Metasploit::Framework::Spec::Constants::Suite.define_task

View File

@ -3,10 +3,16 @@ 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
extend ActiveSupport::Autoload
autoload :Suite
#
# CONSTANTS
#
# Regex parsing loaded module constants
LOADED_MODULE_CHILD_CONSTANT_REGEXP = /^Mod(?<unpacked_full_name>[0-9a-f]+)$/
# Path to log holding leaked constants from last spec run.
LOG_PATHNAME = Pathname.new('log/leaked-constants.log')
# The parent namespace child_constant_name that can have children added when loading modules.
PARENT_CONSTANT = Msf::Modules
# Constant names under {PARENT_CONSTANT} that can persist between specs because they are part of the loader library
@ -19,59 +25,6 @@ module Metasploit::Framework::Spec::Constants
VersionCompatibilityError
}.map(&:to_sym)
# Configures after(:suite) callback for RSpec to check for leaked constants.
def self.configure!
unless @configured
RSpec.configure do |config|
config.after(:suite) do
count = 0
LOG_PATHNAME.open('w') do |f|
count = ::Metasploit::Framework::Spec::Constants.each { |child_name|
f.puts child_name
}
end
if count > 0
$stderr.puts "#{count} #{'constant'.pluralize(count)} leaked under #{PARENT_CONSTANT}. " \
"See #{LOG_PATHNAME} for details."
else
LOG_PATHNAME.delete
end
end
end
@configured = true
end
end
# Adds action to `spec` task so that `rake spec` fails if `log/leaked-constants.log` exists after printing out the
# leaked constants.
#
# @return [void]
def self.define_task
Rake::Task.define_task(:spec) do
if LOG_PATHNAME.exist?
$stderr.puts "Leaked constants detected under #{PARENT_CONSTANT}:"
LOG_PATHNAME.open do |f|
f.each_line do |line|
constant_name = line.strip
full_name = self.full_name(constant_name)
if full_name
formatted_full_name = " # #{full_name}"
end
$stderr.puts " #{constant_name}#{formatted_full_name}"
end
end
exit 1
end
end
end
# Yields each child_constant_name under {PARENT_CONSTANT}.
#
# @yield [child_name]

View File

@ -0,0 +1,65 @@
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::Suite
#
# CONSTANTS
#
# Path to log holding leaked constants from last spec run.
LOG_PATHNAME = Pathname.new('log/leaked-constants.log')
# Configures after(:suite) callback for RSpec to check for leaked constants.
def self.configure!
unless @configured
RSpec.configure do |config|
config.after(:suite) do
count = 0
LOG_PATHNAME.open('w') do |f|
count = Metasploit::Framework::Spec::Constants.each { |child_name|
f.puts child_name
}
end
if count > 0
$stderr.puts "#{count} #{'constant'.pluralize(count)} leaked under " \
"#{Metasploit::Framework::Spec::Constants::PARENT_CONSTANT}. See #{LOG_PATHNAME} for details."
else
LOG_PATHNAME.delete
end
end
end
@configured = true
end
end
# Adds action to `spec` task so that `rake spec` fails if `log/leaked-constants.log` exists after printing out the
# leaked constants.
#
# @return [void]
def self.define_task
Rake::Task.define_task(:spec) do
if LOG_PATHNAME.exist?
$stderr.puts "Leaked constants detected under #{Metasploit::Framework::Spec::Constants::PARENT_CONSTANT}:"
LOG_PATHNAME.open do |f|
f.each_line do |line|
constant_name = line.strip
full_name = Metasploit::Framework::Spec::Constants.full_name(constant_name)
if full_name
formatted_full_name = " # #{full_name}"
end
$stderr.puts " #{constant_name}#{formatted_full_name}"
end
end
exit 1
end
end
end
end

View File

@ -54,4 +54,4 @@ RSpec.configure do |config|
config.use_transactional_fixtures = true
end
Metasploit::Framework::Spec::Constants.configure!
Metasploit::Framework::Spec::Constants::Suite.configure!