inspec/test/unit/plugin/v2/api_dsl_test.rb

45 lines
1.4 KiB
Ruby
Raw Normal View History

Plugin Type: DSLs (#3557) This PR adds 5 closely related plugin types, which allow a plugin to implement new DSL methods / keywords. The mechanism to activate the plugins are all very similar - basically, in a particular location in the code, `method_missing` is implemented, and is used to activate the particular type of DSL being requested. 4 of the DSL plugin types relate to code that could appear in a profile control file. * outer_profile_dsl plugins allow you to extend the code in profile Ruby files that appear outside `control` or `describe` blocks. * control_dsl plugins allow you to extend the code within `control` blocks. * describe_dsl plugins allow you to extend the code within `describe` blocks. * test_dsl plugins allow you to extend the code within `it`/`its` blocks. Finally, the `resource_dsl` plugin allows you to extend the code used within custom resources. Basic unit tests are provided to prove that the plugin types are properly defined. A simple plugin fixture defining DSL hooks (based on favorite foods) is included, and is exercised through a set of functional tests. The plugin developer docs are updated to describe the 5 DSLs. *Note*: Implementing a plugin using any of the DSL plugin types is experimental. The contexts that are exposed to the DSL methods are private and poorly documented. The InSpec project does not claim the APIs used by these plugin types are covered by SemVer. Plugin authors are encouraged to pin tightly to the `inspec` gem in their gemspecs. Motivation for this plugin comes from the desire to allow passionate community members to implement things like "2 out of 3" tests, example groups, improved serverspec compatibility, "they/their" and other "fluency" changes, as well as make it possible for future work by the InSpec team to be implemented as a core plugin, rather than a direct change to the main codebase.
2018-11-29 19:14:06 +00:00
# Tests for the *DSL plugin types
require 'minitest/autorun'
require 'minitest/test'
require 'byebug'
require_relative '../../../../lib/inspec/plugin/v2'
module DslUnitTests
[
:outer_profile_dsl,
:control_dsl,
:describe_dsl,
:test_dsl,
:resource_dsl,
].each do |plugin_type_under_test|
Class.new(MiniTest::Test) do
# Assign name to anonymous class, so test output is meaningful
Object.const_set(plugin_type_under_test.to_s.upcase + '_UnitTests', self)
# One day I will understand Ruby scoping and closures.
# Until then, re-expose this as class variable.
@@plugin_type = plugin_type_under_test
def test_calling_Inspec_dot_plugin_with_plugin_type_returns_the_base_class
klass = Inspec.plugin(2, @@plugin_type)
assert_kind_of Class, klass
assert_equal 'Inspec::Plugin::V2::PluginType::Dsl', klass.name
end
def test_plugin_type_base_classes_can_be_accessed_by_name
klass = Inspec::Plugin::V2::PluginBase.base_class_for_type(@@plugin_type)
assert_kind_of Class, klass
assert_equal 'Inspec::Plugin::V2::PluginType::Dsl', klass.name
end
def test_plugin_type_registers_an_activation_dsl_method
klass = Inspec::Plugin::V2::PluginBase
assert_respond_to klass, @@plugin_type, "Activation method for #{@@plugin_type}"
end
end
end
end