How to use files method of Inspec Package

Best Inspec_ruby code snippet using Inspec.files

inspec.rb

Source:inspec.rb Github

copy

Full Screen

...97 end98 private99 def setup_waivers(opts, config)100 # InSpec expects the singular inflection101 opts[:waiver_file] = config[:waiver_files] || []102 end103 def setup_inputs(opts, config)104 inspec_version = Gem::Version.new(::Inspec::VERSION)105 # Handle input files106 if config[:attrs]107 logger.warn("kitchen-inspec: please use 'input-files' instead of 'attrs'")108 config[:input_files] = config[:attrs]109 end110 if config[:input_files]111 # Note that inspec expects the singular inflection, input_file112 files_key = inspec_version >= Gem::Version.new("3.10") ? :input_file : :attrs113 opts[files_key] = config[:input_files]114 end115 # Handle YAML => Hash inputs116 if config[:attributes]117 logger.warn("kitchen-inspec: please use 'inputs' instead of 'attributes'")118 config[:inputs] = config[:attributes]119 end120 if config[:inputs]121 # Version here is dependent on https://github.com/inspec/inspec/issues/3856122 inputs_key = inspec_version >= Gem::Version.new("4.10") ? :inputs : :attributes123 opts[inputs_key] = Hashie.stringify_keys config[:inputs]124 end125 end126 def load_plugins127 return unless config[:load_plugins]128 v2_loader = ::Inspec::Plugin::V2::Loader.new129 v2_loader.load_all130 v2_loader.exit_on_load_error131 # Suppress input caching or all suites will get identical inputs, not different ones132 if ::Inspec::InputRegistry.instance.respond_to?(:cache_inputs=) && config[:cache_inputs]133 ::Inspec::InputRegistry.instance.cache_inputs = !!config[:cache_inputs]134 end135 end136 def setup_plugin_config(inspec_config)137 return unless config[:load_plugins]138 unless inspec_config.respond_to?(:merge_plugin_config)139 logger.warn("kitchen-inspec: skipping `plugin_config` which requires InSpec version 4.26.2 or higher. Your version: #{::Inspec::VERSION}")140 return141 end142 config[:plugin_config].each do |plugin_name, plugin_config|143 inspec_config.merge_plugin_config(plugin_name, plugin_config)144 end145 end146 # (see Base#load_needed_dependencies!)147 def load_needed_dependencies!148 require "inspec"149 # TODO: this should be easier. I would expect to load a single class here150 # load supermarket plugin, this is part of the inspec gem151 require "bundles/inspec-supermarket/api"152 require "bundles/inspec-supermarket/target"153 # load the compliance plugin154 require "bundles/inspec-compliance/configuration"155 require "bundles/inspec-compliance/support"156 require "bundles/inspec-compliance/http"157 require "bundles/inspec-compliance/api"158 require "bundles/inspec-compliance/target"159 end160 # Returns an Array of test suite filenames for the related suite currently161 # residing on the local workstation. Any special provisioner-specific162 # directories (such as a Chef roles/ directory) are excluded.163 #164 # we support the base directories165 # - test/integration166 # - test/integration/inspec (preferred if used with other test environments)167 #168 # we do not filter for specific directories, this is core of inspec169 #170 # @return [Array<String>] array of suite directories171 # @api private172 def local_suite_files173 base = File.join(config[:test_base_path], config[:suite_name])174 legacy_mode = false175 # check for testing frameworks, we may need to add more176 %w{inspec serverspec bats pester rspec cucumber minitest bash}.each do |fw|177 if Pathname.new(File.join(base, fw)).exist?178 logger.info("Detected alternative framework tests for `#{fw}`")179 legacy_mode = true180 end181 end182 base = File.join(base, "inspec") if legacy_mode183 # only return the directory if it exists184 Pathname.new(base).exist? ? [{ path: base }] : []185 end186 # Takes config[:inspec_tests] and modifies any value with a key of :path by adding the full path187 # @return [Array] array of modified hashes188 # @api private189 def resolve_config_inspec_tests190 config[:inspec_tests].map do |test_item|191 if test_item.is_a?(Hash)192 # replace the "path" key with an absolute path193 test_item[:path] = File.expand_path(test_item[:path]) if test_item.key?(:path)194 # delete any unnecessary keys to ensure deduplication in #collect_tests isn't195 # foiled by extra stuff. However, if the only entry is a "name" key, then196 # leave it alone so it can default to resolving to the Supermarket.197 unless test_item.keys == [:name]198 type_keys = [:path, :url, :git, :compliance, :supermarket]199 git_keys = [:branch, :tag, :ref, :relative_path]200 supermarket_keys = [:supermarket_url]201 test_item.delete_if { |k, v| !(type_keys + git_keys + supermarket_keys).include?(k) }202 end203 elsif File.exist?(test_item)204 # if the entry is a path to something on disk, rewrite as a Hash entry with a path key.205 # This is necessary to ensure that auto-detected local suite files found with206 # #local_suite_files are de-duplicated with relative path entries supplied by the user207 # in the inspec_tests array.208 #209 # If the path doesn't exist, it could be a URL, or it could simply be an error.210 # We will let it fall through and let InSpec raise the appropriate exception.211 test_item = { path: File.expand_path(test_item) }212 end213 test_item unless test_item.nil? || test_item.empty?214 end215 end216 # Returns an array of test profiles217 # @return [Array<String>] array of suite directories or remote urls218 # @api private219 def collect_tests220 # get local tests and get run list of profiles221 (local_suite_files + resolve_config_inspec_tests).compact.uniq222 end223 # Returns a configuration Hash that can be passed to a `Inspec::Runner`.224 #225 # @return [Hash] a configuration hash of string-based keys226 # @api private227 def runner_options(transport, state = {}, platform = nil, suite = nil) # rubocop:disable Metrics/AbcSize228 transport_data = transport.diagnose.merge(state)229 if respond_to?("runner_options_for_#{transport.name.downcase}", true)230 send("runner_options_for_#{transport.name.downcase}", transport_data)231 else232 raise Kitchen::UserError, "Verifier #{name} does not support the #{transport.name} Transport"233 end.tap do |runner_options|234 # default color to true to match InSpec behavior235 runner_options["color"] = (config[:color].nil? ? true : config[:color])236 runner_options["format"] = config[:format] unless config[:format].nil?237 runner_options["output"] = config[:output] % { platform: platform, suite: suite } unless config[:output].nil?238 runner_options["profiles_path"] = config[:profiles_path] unless config[:profiles_path].nil?239 runner_options["reporter"] = config[:reporter].map { |s| s % { platform: platform, suite: suite } } unless config[:reporter].nil?240 runner_options[:controls] = config[:controls]241 # check to make sure we have a valid version for caching242 if config[:backend_cache]243 runner_options[:backend_cache] = config[:backend_cache]244 else245 # default to false until we default to true in inspec246 runner_options[:backend_cache] = false247 end248 end249 end250 # Returns a configuration Hash that can be passed to a `Inspec::Runner`.251 #252 # @return [Hash] a configuration hash of string-based keys253 # @api private254 def runner_options_for_ssh(config_data)255 kitchen = instance.transport.send(:connection_options, config_data).dup256 opts = {257 "backend" => "ssh",258 "logger" => logger,259 # pass-in sudo config from kitchen verifier260 "sudo" => config[:sudo],261 "sudo_command" => config[:sudo_command],262 "sudo_options" => config[:sudo_options],263 "host" => config[:host] || kitchen[:hostname],264 "port" => config[:port] || kitchen[:port],265 "user" => kitchen[:username],266 "keepalive" => kitchen[:keepalive],267 "keepalive_interval" => kitchen[:keepalive_interval],268 "connection_timeout" => kitchen[:timeout],269 "connection_retries" => kitchen[:connection_retries],270 "connection_retry_sleep" => kitchen[:connection_retry_sleep],271 "max_wait_until_ready" => kitchen[:max_wait_until_ready],272 "compression" => kitchen[:compression],273 "compression_level" => kitchen[:compression_level],274 }275 opts["proxy_command"] = config[:proxy_command] if config[:proxy_command]276 opts["bastion_host"] = kitchen[:ssh_gateway] if kitchen[:ssh_gateway]277 opts["bastion_user"] = kitchen[:ssh_gateway_username] if kitchen[:ssh_gateway_username]278 opts["bastion_port"] = kitchen[:ssh_gateway_port] if kitchen[:ssh_gateway_port]279 opts["key_files"] = kitchen[:keys] unless kitchen[:keys].nil?280 opts["password"] = kitchen[:password] unless kitchen[:password].nil?281 opts["forward_agent"] = config[:forward_agent] || kitchen[:forward_agent] if config[:forward_agent] || kitchen[:forward_agent]282 opts283 end284 # Returns a configuration Hash that can be passed to a `Inspec::Runner`.285 #286 # @return [Hash] a configuration hash of string-based keys287 # @api private288 def runner_options_for_winrm(config_data)289 kitchen = instance.transport.send(:connection_options, config_data).dup290 {291 "backend" => "winrm",292 "logger" => logger,293 "ssl" => URI(kitchen[:endpoint]).scheme == "https",...

Full Screen

Full Screen

Rakefile

Source:Rakefile Github

copy

Full Screen

1require "yard" unless defined?(YARD)2desc "noop task that is used by other tasks"3YARD::Rake::YardocTask.new(:yard_for_generate_documentation) do |task|4 util_files = %w{5 architecture6 cloud7 introspection8 os9 platform10 platform_family11 platform_version12 service13 virtualization14 which15 windows16 }17 task.files = ["vendor/ruby/**/bundler/gems/chef*/chef-utils/lib/chef-utils/dsl/{#{util_files.join(',')}}.rb"]18 dsl_files = %w{19 chef_vault20 declare_resource21 }22 task.files.concat(["vendor/ruby/**/bundler/gems/chef*/lib/chef/dsl/{#{dsl_files.join(',')}}.rb"])23 task.options = ["--no-output"]24end25desc "Generate vscode snippets for chef-utils helpers"26def install_gem_utils27 require "bundler"28 Bundler.with_clean_env do29 sh "bundle config --local path 'vendor'"30 sh "bundle install"31 sh "bundle update"32 end33end34task :generate_snippets do35 Encoding.default_external = Encoding::UTF_836 # make sure we have chef-utils files locally for yard inspection37 install_gem_utils38 require "fileutils" unless defined?(FileUtils)39 require "json" unless defined?(JSON)40 Rake::Task[:yard_for_generate_documentation].execute41 doc_data = {}42 YARD::Registry.load!43 YARD::Registry.all(:method).to_a.each do |obj|44 # we don't want to document all the chef-sugar backwards compat aliases45 # We can't use .is_alias? here since it fired on the original methods as well46 next if obj.docstring.match?(/chef-sugar backcompat method/)47 # we don't want private API methods48 next unless obj.visibility == :public49 doc_data[obj.name.to_s] = {}50 doc_data[obj.name.to_s]["prefix"] = obj.name.to_s...

Full Screen

Full Screen

calling_files.rb

Source:calling_files.rb Github

copy

Full Screen

1# example to call file that is loaded in the 'files' folder of Inspec profile using inspec.profile.file2# /Controls/code.rb -> in this example code manifiest is in this file "Calling_files.rb"3# /files/filecontent.yml -> in this example data input is from "names.yml"4# maintainted same file structure for convenience, you can refer the names.yml in the files folder5control 'calling_files' do6 title 'Two scenarios to call the file from local machine and Inspec profile'7 desc 'example to call file from local directory as well as from the files folder of Inspec profile using inspec.profile.file'8# Scenario 1 for calling files from local machine 9 if file('F:\Inpsec\names.yml').exist? # condition to check whether the file exists10 all_names = yaml('F:\Inpsec\names.yml').boys # If condition is true fetch the names from boys Array11 else12 all_names = yaml('F:\Inpsec\names.yml').girls # If condition is false fetch the names from girls Array13 end14puts all_names15#================================================================================================================16# Scenario 2 for calling files from Inspec profile17all_names = inspec.profile.file('names.yml') # syntax to call the file18if all_names.length > 0 # condition to check the varible got values from profile19 all_names = yaml('F:\Inpsec\names.yml').boys # If condition is true fetch the names from boys Array20 else21 all_names = yaml('F:\Inpsec\names.yml').girls # If condition is false fetch the names from girls Array22 end23puts all_names24end...

Full Screen

Full Screen

files

Using AI Code Generation

copy

Full Screen

1files = inspec.files('/tmp/*.txt')2 its('content') { should match(/Hello/) }3files = inspec.files('/tmp/*.txt')4 its('content') { should match(/World/) }5files = inspec.files('/tmp/*.txt')6 its('content') { should match(/Hello/) }7files = inspec.files('/tmp/*.txt')8 its('content') { should match(/World/) }9files = inspec.files('/tmp/*.txt')10 its('content') { should match(/Hello/) }11files = inspec.files('/tmp/*.txt')12 its('content') { should match(/World/) }13files = inspec.files('/tmp/*.txt')14 its('content') { should match(/Hello/) }15files = inspec.files('/tmp/*.txt')16 its('content') { should match(/World/) }17files = inspec.files('/tmp/*.txt')18 its('content') { should match(/Hello/) }

Full Screen

Full Screen

files

Using AI Code Generation

copy

Full Screen

1describe file('C:\Users\Public\Documents\test.txt') do2 its('content') { should match /This is a test file/ }3describe file('C:\Users\Public\Documents\test.txt') do4 its('content') { should match /This is a test file/ }5describe file('C:\Users\Public\Documents\test.txt') do6 its('content') { should match /This is a test file/ }7describe file('C:\Users\Public\Documents\test.txt') do8 its('content') { should match /This is a test file/ }9describe file('C:\Users\Public\Documents\test.txt') do10 its('content') { should match /This is a test file/ }11describe file('C:\Users\Public\Documents\test.txt') do12 its('content') { should match /This is a test file/ }13describe file('C:\Users\Public\Documents\test.txt') do14 its('content') { should match /This is a test file/ }15describe file('C:\Users\Public\Documents\test.txt') do16 its('content') { should match /This is a test file/ }17describe file('C:\Users\Public\Documents\test.txt') do18 its('content') { should match /This is a test file/ }

Full Screen

Full Screen

files

Using AI Code Generation

copy

Full Screen

1content = inspec.file('/tmp/1.txt').content2 it { should cmp content }3content = inspec.file('/tmp/1.txt')4 it { should cmp content }5 it { should cmp inspec.file('/tmp/1.txt') }6 it { should cmp inspec.file('/tmp/1.txt').content }7 it { should cmp inspec.file('/tmp/1.txt').content.gsub('8','') }9 it { should cmp inspec.file('/tmp/1.txt').content.gsub('10','').strip }11 it { should cmp inspec.file('/tmp/1.txt').content.gsub('12','').strip.chomp }13 it { should cmp inspec.file('/tmp/1.txt').content.gsub('14','').strip.chomp.split(' ')[0] }15 it { should cmp inspec.file('/tmp/1.txt').content.gsub('16','').strip.chomp.split(' ')[1] }

Full Screen

Full Screen

files

Using AI Code Generation

copy

Full Screen

1 describe file('file.txt') do2 its('content') { should match(/Hello World/) }3 describe file('file.txt') do4 its('content') { should match(/Hello World/) }5 its('name') { should eq 'file.txt' }6 describe file('file.txt') do7 its('content') { should match(/Hello World/) }8 its('name') { should eq 'file.txt' }9 its('path') { should eq '/tmp/file.txt' }10 describe file('file.txt') do11 its('content') { should match(/Hello World/) }12 its('name') { should eq 'file.txt' }13 its('path') { should eq '/tmp/file.txt' }14 its('size') { should eq 12 }15 describe file('file.txt') do16 its('content') { should match(/Hello World/) }17 its('name') { should eq 'file.txt

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