2019-06-11 22:24:35 +00:00
|
|
|
require "helper"
|
|
|
|
require "inspec/library_eval_context"
|
2022-01-28 17:54:01 +00:00
|
|
|
require "inspec/resources/ini"
|
2016-08-31 15:48:51 +00:00
|
|
|
|
|
|
|
describe Inspec::LibraryEvalContext do
|
2019-06-11 22:24:35 +00:00
|
|
|
let(:resource_content) do
|
|
|
|
<<~EOF
|
|
|
|
class MyTestResource < Inspec.resource(1)
|
|
|
|
name 'my_test_resource'
|
|
|
|
|
|
|
|
desc 'A test description'
|
|
|
|
example 'Forgot to write docs, sorry'
|
|
|
|
|
|
|
|
def version
|
|
|
|
'2.0'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
EOF
|
2016-08-31 15:48:51 +00:00
|
|
|
end
|
|
|
|
|
2022-01-28 17:54:01 +00:00
|
|
|
let(:resource_content2) do
|
|
|
|
<<~EOF
|
|
|
|
class AnotherResource < IniConfig
|
|
|
|
name 'another_resource'
|
|
|
|
desc 'Another Resource description'
|
|
|
|
example 'see README'
|
|
|
|
def version
|
|
|
|
'2.0'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
EOF
|
|
|
|
end
|
|
|
|
|
2016-08-31 15:48:51 +00:00
|
|
|
let(:registry) { Inspec::Resource.new_registry }
|
|
|
|
let(:eval_context) { Inspec::LibraryEvalContext.create(registry, nil) }
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "accepts a registry" do
|
2016-08-31 15:48:51 +00:00
|
|
|
Inspec::LibraryEvalContext.create(registry, nil)
|
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "adds the resource to our registry" do
|
2016-08-31 15:48:51 +00:00
|
|
|
eval_context.instance_eval(resource_content)
|
2019-12-24 12:16:48 +00:00
|
|
|
_(registry.keys).must_include "my_test_resource"
|
2016-08-31 15:48:51 +00:00
|
|
|
end
|
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "adds nothing to the default registry" do
|
2019-12-25 10:22:47 +00:00
|
|
|
_(registry.keys.sort).wont_include "my_test_resource"
|
2016-08-31 15:48:51 +00:00
|
|
|
old_default_registry = Inspec::Resource.default_registry.dup
|
2019-12-25 10:22:47 +00:00
|
|
|
_(old_default_registry.keys.sort).wont_include "my_test_resource"
|
|
|
|
|
2016-08-31 15:48:51 +00:00
|
|
|
eval_context.instance_eval(resource_content)
|
2019-12-25 10:22:47 +00:00
|
|
|
_(old_default_registry.keys.sort).must_equal Inspec::Resource.default_registry.keys.sort
|
2019-09-30 22:31:55 +00:00
|
|
|
_(old_default_registry).must_equal Inspec::Resource.default_registry
|
2016-08-31 15:48:51 +00:00
|
|
|
end
|
2018-01-02 20:41:01 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
it "provides an inspec context for requiring local files" do
|
2019-09-30 22:31:55 +00:00
|
|
|
_(eval_context.__inspec_binding).must_be_kind_of Binding
|
2018-01-02 20:41:01 +00:00
|
|
|
end
|
2022-01-28 17:54:01 +00:00
|
|
|
|
|
|
|
it "adds the resource to our registry" do
|
|
|
|
_(registry.keys).wont_include "another_resource"
|
|
|
|
eval_context.instance_eval(resource_content2)
|
|
|
|
old_default_registry = Inspec::Resource.default_registry.dup
|
|
|
|
_(old_default_registry.keys.sort).must_equal Inspec::Resource.default_registry.keys.sort
|
|
|
|
end
|
2016-08-31 15:48:51 +00:00
|
|
|
end
|