Extract module instantiation context to shared examples

MSP-11145
This commit is contained in:
Luke Imhoff 2014-10-20 13:21:22 -05:00
parent 7e9398e1af
commit 2c997d99b5
No known key found for this signature in database
GPG Key ID: 5B1FB01FB33356F8
1 changed files with 54 additions and 36 deletions

View File

@ -1,56 +1,74 @@
require 'spec_helper'
describe 'modules' do
include_context 'Msf::Simple::Framework'
modules_pathname = Pathname.new(__FILE__).parent.parent.join('modules')
modules_path = modules_pathname.to_path
#
# lets
# shared examples
#
let(:loader) {
loader = framework.modules.send(:loaders).find { |loader|
loader.loadable?(modules_path)
shared_examples_for 'all modules with module type can be instantiated' do |options={}|
options.assert_valid_keys(:module_type, :modules_pathname, :type_directory)
module_type = options.fetch(:module_type)
modules_pathname = options.fetch(:modules_pathname)
modules_path = modules_pathname.to_path
type_directory = options.fetch(:type_directory)
include_context 'Msf::Simple::Framework'
#
# lets
#
let(:loader) {
loader = framework.modules.send(:loaders).find { |loader|
loader.loadable?(modules_path)
}
# Override load_error so that rspec will print it instead of going to framework log
def loader.load_error(module_path, error)
raise error
end
loader
}
# Override load_error so that rspec will print it instead of going to framework log
def loader.load_error(module_path, error)
raise error
end
context module_type do
let(:module_set) {
framework.send(module_type)
}
loader
}
type_pathname = modules_pathname.join(type_directory)
module_extension = ".rb"
module_extension_regexp = /#{Regexp.escape(module_extension)}$/
type = 'auxiliary'
context type do
let(:module_set) {
framework.send(type)
}
Dir.glob(type_pathname.join('**', "*#{module_extension}")) do |module_path|
module_pathname = Pathname.new(module_path)
module_reference_pathname = module_pathname.relative_path_from(type_pathname)
module_reference_name = module_reference_pathname.to_path.gsub(module_extension_regexp, '')
type_pathname = modules_pathname.join('auxiliary')
context module_reference_name do
it 'can be instantiated' do
loaded = loader.load_module(modules_path, module_type, module_reference_name)
Dir.glob(type_pathname.join('**', '*.rb')) do |module_path|
module_pathname = Pathname.new(module_path)
module_reference_pathname = module_pathname.relative_path_from(type_pathname)
module_reference_name = module_reference_pathname.to_path.gsub(/\.rb$/, '')
expect(loaded).to eq(true), "#{module_reference_name} failed to load from #{modules_path}"
context module_reference_name do
it 'can be instantiated' do
loaded = loader.load_module(modules_path, type, module_reference_name)
module_instance = nil
expect(loaded).to eq(true), "#{module_reference_name} failed to load from #{modules_path}"
expect {
module_instance = module_set.create(module_reference_name)
}.not_to raise_error
module_instance = nil
expect {
module_instance = module_set.create(module_reference_name)
}.not_to raise_error
expect(module_instance).not_to be_nil, "Could not instantiate #{type}/#{module_reference_name}"
expect(module_instance).not_to be_nil, "Could not instantiate #{module_type}/#{module_reference_name}"
end
end
end
end
end
modules_pathname = Pathname.new(__FILE__).parent.parent.join('modules')
it_should_behave_like 'all modules with module type can be instantiated',
module_type: 'auxiliary',
modules_pathname: modules_pathname,
type_directory: 'auxiliary'
end