How to use register_rule method of Inspec Package

Best Inspec_ruby code snippet using Inspec.register_rule

runner.rb

Source:runner.rb Github

copy

Full Screen

...94 tests = profile.collect_tests95 all_controls += tests unless tests.nil?96 end97 all_controls.each do |rule|98 register_rule(rule) unless rule.nil?99 end100 end101 def run(with = nil)102 Inspec::Log.debug "Starting run with targets: #{@target_profiles.map(&:to_s)}"103 load104 run_tests(with)105 end106 def render_output(run_data)107 return if @conf['reporter'].nil?108 @conf['reporter'].each do |reporter|109 result = Inspec::Reporters.render(reporter, run_data)110 raise Inspec::ReporterError, "Error generating reporter '#{reporter[0]}'" if result == false111 end112 end113 def report114 Inspec::Reporters.report(@conf['reporter'].first, @run_data)115 end116 def write_lockfile(profile)117 return false if !profile.writable?118 if profile.lockfile_exists?119 Inspec::Log.debug "Using existing lockfile #{profile.lockfile_path}"120 else121 Inspec::Log.debug "Creating lockfile: #{profile.lockfile_path}"122 lockfile = profile.generate_lockfile123 File.write(profile.lockfile_path, lockfile.to_yaml)124 end125 end126 def run_tests(with = nil)127 @run_data = @test_collector.run(with)128 # dont output anything if we want a report129 render_output(@run_data) unless @conf['report']130 @test_collector.exit_code131 end132 #133 # add_target allows the user to add a target whose tests will be134 # run when the user calls the run method.135 #136 # A target is a path or URL that points to a profile. Using this137 # target we generate a Profile and a ProfileContext. The content138 # (libraries, tests, and inputs) from the Profile are loaded139 # into the ProfileContext.140 #141 # If the profile depends on other profiles, those profiles will be142 # loaded on-demand when include_content or required_content are143 # called using similar code in Inspec::DSL.144 #145 # Once the we've loaded all of the tests files in the profile, we146 # query the profile for the full list of rules. Those rules are147 # registered with the @test_collector which is ultimately148 # responsible for actually running the tests.149 #150 # TODO: Deduplicate/clarify the loading code that exists in here,151 # the ProfileContext, the Profile, and Inspec::DSL152 #153 # @params target [String] A path or URL to a profile or raw test.154 # @params _opts [Hash] Unused, but still here to avoid breaking kitchen-inspec155 #156 # @eturns [Inspec::ProfileContext]157 #158 def add_target(target, _opts = [])159 profile = Inspec::Profile.for_target(target,160 vendor_cache: @cache,161 backend: @backend,162 controls: @controls,163 runner_conf: @conf)164 raise "Could not resolve #{target} to valid input." if profile.nil?165 @target_profiles << profile if supports_profile?(profile)166 end167 def supports_profile?(profile)168 if !profile.supports_runtime?169 raise 'This profile requires InSpec version '\170 "#{profile.metadata.inspec_requirement}. You are running "\171 "InSpec v#{Inspec::VERSION}.\n"172 end173 true174 end175 # In some places we read the rules off of the runner, in other176 # places we read it off of the profile context. To keep the API's177 # the same, we provide an #all_rules method here as well.178 def all_rules179 @rules180 end181 def register_rules(ctx)182 new_tests = false183 ctx.rules.each do |rule_id, rule|184 next if block_given? && !(yield rule_id, rule)185 new_tests = true186 register_rule(rule)187 end188 new_tests189 end190 def eval_with_virtual_profile(command)191 require 'fetchers/mock'192 add_target({ 'inspec.yml' => 'name: inspec-shell' })193 our_profile = @target_profiles.first194 ctx = our_profile.runner_context195 # Load local profile dependencies. This is used in inspec shell196 # to provide access to local profiles that add resources.197 @depends.each do |dep|198 # support for windows paths199 dep = dep.tr('\\', '/')200 Inspec::Profile.for_path(dep, { profile_context: ctx }).load_libraries201 end202 ctx.load(command)203 end204 private205 def block_source_info(block)206 return {} if block.nil? || !block.respond_to?(:source_location)207 opts = {}208 file_path, line = block.source_location209 opts['file_path'] = file_path210 opts['line_number'] = line211 opts212 end213 def get_check_example(method_name, arg, block)214 opts = block_source_info(block)215 return nil if arg.empty?216 resource = arg[0]217 # check to see if we are using a filtertable object218 resource = arg[0].resource if arg[0].class.superclass == FilterTable::Table219 if resource.respond_to?(:resource_skipped?) && resource.resource_skipped?220 return rspec_skipped_block(arg, opts, resource.resource_exception_message)221 end222 if resource.respond_to?(:resource_failed?) && resource.resource_failed?223 return rspec_failed_block(arg, opts, resource.resource_exception_message)224 end225 # If neither skipped nor failed then add the resource226 add_resource(method_name, arg, opts, block)227 end228 def register_rule(rule)229 Inspec::Log.debug "Registering rule #{rule}"230 @rules << rule231 checks = ::Inspec::Rule.prepare_checks(rule)232 examples = checks.flat_map do |m, a, b|233 get_check_example(m, a, b)234 end.compact235 examples.each { |e| @test_collector.add_test(e, rule) }236 end237 def rspec_skipped_block(arg, opts, message)238 @test_collector.example_group(*arg, opts) do239 # Send custom `it` block to RSpec240 it message241 end242 end...

Full Screen

Full Screen

profile_context.rb

Source:profile_context.rb Github

copy

Full Screen

...32 else33 @profile_context.instance_eval(content, source || 'unknown', line || 1)34 end35 end36 def unregister_rule(id)37 @rules.delete(full_id(@profile_id, id))38 end39 def register_rule(r)40 # get the full ID41 r.instance_variable_set(:@__file, @current_load[:file])42 r.instance_variable_set(:@__group_title, @current_load[:title])43 # add the rule to the registry44 fid = full_id(Inspec::Rule.profile_id(r), Inspec::Rule.rule_id(r))45 existing = @rules[fid]46 if existing.nil?47 @rules[fid] = r48 else49 Inspec::Rule.merge(existing, r)50 end51 end52 def set_header(field, val)53 @current_load[field] = val54 end55 private56 def full_id(pid, rid)57 return rid.to_s if pid.to_s.empty?58 pid.to_s + '/' + rid.to_s59 end60 # Create the context for controls. This includes all components of the DSL,61 # including matchers and resources.62 #63 # @param [ResourcesDSL] resources_dsl which has all resources to attach64 # @return [RuleContext] the inner context of rules65 def rule_context(resources_dsl)66 require 'rspec/core/dsl'67 Class.new(Inspec::Rule) do68 include RSpec::Core::DSL69 include resources_dsl70 end71 end72 # Creates the heart of the profile context:73 # An instantiated object which has all resources registered to it74 # and exposes them to the a test file. The profile context serves as a75 # container for all profiles which are registered. Within the context76 # profiles get access to all DSL calls for creating tests and controls.77 #78 # @param outer_dsl [OuterDSLClass]79 # @return [ProfileContextClass]80 def create_context(resources_dsl, rule_class) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength81 profile_context_owner = self82 profile_id = @profile_id83 # rubocop:disable Lint/NestedMethodDefinition84 Class.new do85 include Inspec::DSL86 include resources_dsl87 def initialize(backend, conf) # rubocop:disable Lint/NestedMethodDefinition, Lint/DuplicateMethods88 @backend = backend89 @conf = conf90 @skip_profile = false91 end92 define_method :title do |arg|93 profile_context_owner.set_header(:title, arg)94 end95 def to_s96 'Profile Context Run'97 end98 define_method :control do |*args, &block|99 id = args[0]100 opts = args[1] || {}101 register_control(rule_class.new(id, profile_id, opts, &block))102 end103 define_method :describe do |*args, &block|104 loc = block_location(block, caller[0])105 id = "(generated from #{loc} #{SecureRandom.hex})"106 res = nil107 rule = rule_class.new(id, profile_id, {}) do108 res = describe(*args, &block)109 end110 register_control(rule, &block)111 res112 end113 define_method :register_control do |control, &block|114 ::Inspec::Rule.set_skip_rule(control, true) if @skip_profile115 profile_context_owner.register_rule(control, &block) unless control.nil?116 end117 # TODO: mock method for attributes; import attribute handling118 define_method :attributes do |_name, _options|119 nil120 end121 define_method :skip_control do |id|122 profile_context_owner.unregister_rule(id)123 end124 def only_if125 return unless block_given?126 @skip_profile ||= !yield127 end128 alias_method :rule, :control129 alias_method :skip_rule, :skip_control130 private131 def block_location(block, alternate_caller)132 if block.nil?133 alternate_caller[/^(.+:\d+):in .+$/, 1] || 'unknown'134 else135 path, line = block.source_location136 "#{File.basename(path)}:#{line}"...

Full Screen

Full Screen

register_rule

Using AI Code Generation

copy

Full Screen

1 def register_rule(rule)2 def initialize(id, options)3Inspec::Profile.new('my-profile').register_rule(4 Inspec::Rule.new(5 tags: { 'my-tag' => 'my-tag-value' },6 refs: { 'my-ref' => 'my-ref-value' },7Inspec::Profile.new('my-profile').register_rule(8 Inspec::Rule.new(9 tags: { 'my-tag' => 'my-tag-value' },10 refs: { 'my-ref' => 'my-ref-value' },11Inspec::Profile.new('my-profile').register_rule(12 Inspec::Rule.new(13 tags: { 'my-tag' => 'my-tag-value' },14 refs: { 'my-ref' => 'my-ref-value' },15Inspec::Profile.new('my-profile').register_rule(16 Inspec::Rule.new(

Full Screen

Full Screen

register_rule

Using AI Code Generation

copy

Full Screen

1class MyResource < Inspec.resource(1)2 describe my_resource(name: 'foo') do3 its('property') { should eq 'value' }4 def initialize(opts = {})5Inspec.register_rule(MyRule)6Inspec.register_resource(MyResource)7describe my_resource(name: 'foo') do8 its('property') { should eq 'value' }9describe my_resource(name: 'foo') do10 its('property') { should eq 'value' }

Full Screen

Full Screen

register_rule

Using AI Code Generation

copy

Full Screen

1 describe file('/') do2 it { should be_directory }3Profile: InSpec Profile (rule_1)4Profile: InSpec Profile (rule_1)5rule = Inspec::Rule.new('rule_2', 'rule_2 description')6rule.describe file('/') do7 it { should be_directory }8Profile: InSpec Profile (rule_2)9Profile: InSpec Profile (rule_2)

Full Screen

Full Screen

register_rule

Using AI Code Generation

copy

Full Screen

1rule = Inspec::Rule.new(2 tags: {},3 refs: {},4 source_location: {},5ctx.register_rule(rule)6rule = ctx.get_rule('my_rule')7rule = Inspec::Rule.new(8 tags: {},9 refs: {},10 source_location: {},11ctx.register_rule(rule)12rule = ctx.get_rule('my_rule')

Full Screen

Full Screen

register_rule

Using AI Code Generation

copy

Full Screen

1 def register_rule(*args)2 def register_rule(*args)3 def register_rule(*args)4 def register_rule(*args)5 def register_rule(*args)

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Inspec_ruby automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used method in

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful