Best Inspec_ruby code snippet using Inspec.bold
cli_command.rb
Source:cli_command.rb
...16 plugin_statuses = Inspec::Plugin::V2::Registry.instance.plugin_statuses17 plugin_statuses.reject! { |s| [:core, :bundle].include?(s.installation_type) } unless options[:all]18 # TODO: ui object support19 puts20 puts(bold { format(' %-30s%-10s%-8s%-6s', 'Plugin Name', 'Version', 'Via', 'ApiVer') })21 puts '-' * 5522 plugin_statuses.sort_by(&:name).each do |status|23 puts(format(' %-30s%-10s%-8s%-6s', status.name, make_pretty_version(status), status.installation_type, status.api_generation.to_s))24 end25 puts '-' * 5526 puts(" #{plugin_statuses.count} plugin(s) total")27 puts28 end29 #==================================================================#30 # inspec plugin search31 #==================================================================#32 desc 'search [options] PATTERN', 'Searches rubygems.org for plugins.'33 long_desc <<~EOLD34 Searches rubygems.org for InSpec plugins. Exits 0 on a search hit, 1 on user error,35 2 on a search miss. PATTERN is a simple string; a wildcard will be added as36 a suffix, unless -e is used.37 EOLD38 option :all, desc: 'List all available versions, not just the latest one.', type: :boolean, aliases: [:a]39 option :exact, desc: 'Assume PATTERN is exact; do not add a wildcard to the end', type: :boolean, aliases: [:e]40 option :'include-test-fixture', type: :boolean, desc: 'Internal use', hide: true41 # Justification for disabling ABC: currently at 33.51/3342 def search(search_term) # rubocop: disable Metrics/AbcSize43 search_results = installer.search(search_term, exact: options[:exact])44 # The search results have already been filtered by the reject list. But the45 # RejectList doesn't filter {inspec, train}-test-fixture because we need those46 # for testing. We want to hide those from users, so unless we know we're in47 # test mode, remove them.48 unless options[:'include-test-fixture']49 search_results.delete('inspec-test-fixture')50 search_results.delete('train-test-fixture')51 end52 # TODO: ui object support53 puts54 puts(bold { format(' %-30s%-50s', 'Plugin Name', 'Versions Available') })55 puts '-' * 5556 search_results.keys.sort.each do |plugin_name|57 versions = options[:all] ? search_results[plugin_name] : [search_results[plugin_name].first]58 versions = '(' + versions.join(', ') + ')'59 puts(format(' %-30s%-50s', plugin_name, versions))60 end61 puts '-' * 5562 puts(" #{search_results.count} plugin(s) found")63 puts64 exit 2 if search_results.empty?65 rescue Inspec::Plugin::V2::SearchError => ex66 Inspec::Log.error ex.message67 exit 168 end69 #==================================================================#70 # inspec plugin install71 #==================================================================#72 desc 'install [-v VERSION] PLUGIN', 'Installs a plugin from rubygems.org, a gemfile, or a path to local source.'73 long_desc <<~EOLD74 PLUGIN may be the name of a gem on rubygems.org that begins with inspec- or train-.75 PLUGIN may also be the path to a local gemfile, which will then be installed like76 any other gem. Finally, if PLUGIN is a path ending in .rb, it is taken to be a77 local file that will act as athe entry point for a plugin (this mode is provided78 for local plugin development). Exit codes are 0 on success, 2 if the plugin is79 already installed, and 1 if any other error occurs.80 EOLD81 option :version, desc: 'When installing from rubygems.org, specifies a specific version to install.', aliases: [:v]82 def install(plugin_id_arg)83 if plugin_id_arg =~ /\.gem$/ # Does it end in .gem?84 install_from_gemfile(plugin_id_arg)85 elsif plugin_id_arg =~ %r{[\/\\]} || Dir.exist?(plugin_id_arg) # Does the argument have a slash, or exist as dir in the local directory?86 install_from_path(plugin_id_arg)87 else88 install_from_remote_gem(plugin_id_arg)89 end90 end91 #--------------------------92 # update93 #--------------------------94 desc 'update PLUGIN', 'Updates a plugin to the latest from from rubygems.org'95 long_desc <<~EOLD96 PLUGIN may be the name of a gem on rubygems.org that begins with inspec- or train-.97 Exit codes are 0 on success, 2 if the plugin is already up to date, and 1 if any98 other error occurs.99 EOLD100 def update(plugin_name)101 pre_update_versions = installer.list_installed_plugin_gems.select { |spec| spec.name == plugin_name }.map { |spec| spec.version.to_s }102 old_version = pre_update_versions.join(', ')103 update_preflight_check(plugin_name, pre_update_versions)104 begin105 installer.update(plugin_name)106 rescue Inspec::Plugin::V2::UpdateError => ex107 puts(red { 'Update error: ' } + ex.message + ' - update failed')108 exit 1109 end110 post_update_versions = installer.list_installed_plugin_gems.select { |spec| spec.name == plugin_name }.map { |spec| spec.version.to_s }111 new_version = (post_update_versions - pre_update_versions).first112 puts(bold { plugin_name } + " plugin, version #{old_version} -> #{new_version}, updated from rubygems.org")113 end114 #--------------------------115 # uninstall116 #--------------------------117 desc 'uninstall PLUGIN_NAME', 'Uninstalls a gem- or path- based plugin'118 long_desc <<~EOLD119 Removes a plugin from the users configuration.120 In the case of a gem plugin (by far the most common), the plugin gem is removed, along121 with any of its dependencies that are no longer needed by anything else. Finally, the122 plugin configuration file is updated to reflect that the plugin is no longer present.123 In the case of a path-based plugin (often used for plugin development), no changes124 are made to the referenced plugin source code. Rather, the plugin's entry is simply removed125 from the plugin config file.126 EOLD127 def uninstall(plugin_name)128 status = Inspec::Plugin::V2::Registry.instance[plugin_name.to_sym]129 unless status130 puts(red { 'No such plugin installed: ' } + "#{plugin_name} is not installed - uninstall failed")131 exit 1132 end133 installer = Inspec::Plugin::V2::Installer.instance134 pre_uninstall_versions = installer.list_installed_plugin_gems.select { |spec| spec.name == plugin_name }.map { |spec| spec.version.to_s }135 old_version = pre_uninstall_versions.join(', ')136 installer.uninstall(plugin_name)137 if status.installation_type == :path138 puts(bold { plugin_name } + ' path-based plugin install has been uninstalled')139 else140 puts(bold { plugin_name } + " plugin, version #{old_version}, has been uninstalled")141 end142 exit 0143 end144 private145 #==================================================================#146 # install breakdown147 #==================================================================#148 # These are broken down because rubocop complained.149 def install_from_gemfile(gem_file)150 unless File.exist? gem_file151 puts(red { 'No such plugin gem file ' } + gem_file + ' - installation failed.')152 exit 1153 end154 plugin_name_parts = File.basename(gem_file, '.gem').split('-')155 version = plugin_name_parts.pop156 plugin_name = plugin_name_parts.join('-')157 check_plugin_name(plugin_name, 'installation')158 installer.install(plugin_name, gem_file: gem_file)159 puts(bold { plugin_name } + " plugin, version #{version}, installed from local .gem file")160 exit 0161 end162 def install_from_path(path)163 unless File.exist? path164 puts(red { 'No such source code path ' } + path + ' - installation failed.')165 exit 1166 end167 plugin_name = File.basename(path, '.rb')168 # While installer.install does some rudimentary checking,169 # this file has good UI access, so we promise to validate the170 # input a lot and hand the installer a sure-thing.171 # Name OK?172 check_plugin_name(plugin_name, 'installation')173 # Already installed?174 if registry.known_plugin?(plugin_name.to_sym)175 puts(red { 'Plugin already installed' } + " - #{plugin_name} - Use 'inspec plugin list' to see previously installed plugin - installation failed.")176 exit 2177 end178 # Can we figure out how to load it?179 entry_point = install_from_path__apply_entry_point_heuristics(path)180 # If you load it, does it act like a plugin?181 install_from_path__probe_load(entry_point, plugin_name)182 # OK, install it!183 installer.install(plugin_name, path: entry_point)184 puts(bold { plugin_name } + ' plugin installed via source path reference, resolved to entry point ' + entry_point)185 exit 0186 end187 # Rationale for rubocop variances: It's a heuristics method, and will be full of188 # conditionals. The code is well-commented; refactoring into sub-methods would189 # reduce clarity.190 def install_from_path__apply_entry_point_heuristics(path) # rubocop: disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity191 given = Pathname.new(path)192 given = given.expand_path # Resolve any relative paths193 name_regex = /^(inspec|train)-/194 versioned_regex = /^(inspec|train)-[a-z0-9\-\_]+-\d+\.\d+\.\d+$/195 # What are the last four things like?196 parts = [197 given.parent.parent.basename,198 given.parent.basename,199 given.basename('.rb'),200 given.extname,201 ].map(&:to_s)202 # Case 1: Simplest case: it was a full entry point, as presented.203 # /home/you/projects/inspec-something/lib/inspec-something.rb204 # parts index: ^0^ ^1^ ^2^ ^3^205 if parts[0] =~ name_regex && parts[1] == 'lib' && parts[2] == parts[0] && parts[3] == '.rb'206 return given.to_s207 end208 # Case 2: Also easy: they either referred to the internal library directory,209 # or left the extansion off. Those are the same to us.210 # /home/you/projects/inspec-something/lib/inspec-something211 # parts index: ^0^ ^1^ ^2^ (3 is empty)212 if parts[0] =~ name_regex && parts[1] == 'lib' && parts[2] == parts[0] && parts[3].empty?213 return given.to_s + '.rb'214 end215 # Case 3: Maybe they were refering to a path that is inside a gem installation, or an exploded gem?216 # In that case, we'll have a version on the plugin name in part 0217 # /home/you/.gems/2.4.0/gems/inspec-something-3.45.1/lib/inspec-something.rb218 # parts index: ^0^ ^1^ ^2^ ^3^219 if parts[0] =~ versioned_regex && parts[1] == 'lib' && parts[0].start_with?(parts[2]) && parts[3] == '.rb'220 return given.to_s221 end222 # Case 4: Like case 3, but missing the .rb223 # /home/you/.gems/2.4.0/gems/inspec-something-3.45.1/lib/inspec-something224 # parts index: ^0^ ^1^ ^2^ ^3^ (empty)225 if parts[0] =~ versioned_regex && parts[1] == 'lib' && parts[0].start_with?(parts[2]) && parts[3].empty?226 return given.to_s + '.rb'227 end228 # Case 5: Easy to recognize, but harder to handle: they referred to the project root.229 # /home/you/projects/inspec-something230 # parts index: ^0^ ^1^ ^2^ (3 is empty)231 # 0 and 1 are not meaningful to us, but we hope to find a parts[2]/lib/inspec-something.rb.232 entry_point_guess = File.join(given.to_s, 'lib', parts[2] + '.rb')233 if parts[2] =~ name_regex && File.exist?(entry_point_guess)234 return entry_point_guess235 end236 # Well, if we got here, parts[2] matches an inspec/train prefix, but we have no idea about anything.237 # Give up.238 puts(red { 'Unrecognizable plugin structure' } + " - #{parts[2]} - When installing from a path, please provide the path of the entry point file - installation failed.")239 exit 1240 end241 def install_from_path__probe_load(entry_point, plugin_name)242 # Brazenly attempt to load a file, and see if it registers a plugin.243 begin244 require entry_point245 rescue LoadError => ex246 puts(red { 'Plugin contains errors' } + " - #{plugin_name} - Encountered errors while trying to test load the plugin entry point, resolved to #{entry_point} - installation failed")247 puts ex.message248 exit 1249 end250 # OK, the wheels didn't fall off. But is it a plugin?251 if plugin_name.to_s.start_with?('train')252 # Train internal names do not include the prix in their registry entries253 # And the registry is keyed on Strings254 registry_key = plugin_name.to_s.sub(/^train-/, '')255 unless Train::Plugins.registry.key?(registry_key)256 puts(red { 'Does not appear to be a plugin' } + " - #{plugin_name} - After probe-loading the supposed plugin, it did not register itself to Train. Ensure something inherits from 'Train.plugin(1)' - installation failed.")257 exit 1258 end259 else260 unless registry.known_plugin?(plugin_name.to_sym)261 puts(red { 'Does not appear to be a plugin' } + " - #{plugin_name} - After probe-loading the supposed plugin, it did not register itself to InSpec. Ensure something inherits from 'Inspec.plugin(2)' - installation failed.")262 exit 1263 end264 end265 end266 def install_from_remote_gem(plugin_name)267 requested_version = options[:version]268 check_plugin_name(plugin_name, 'installation')269 # Version pre-flighting270 pre_installed_versions = installer.list_installed_plugin_gems.select { |spec| spec.name == plugin_name }.map { |spec| spec.version.to_s }271 install_from_remote_gem_verson_preflight_check(plugin_name, requested_version, pre_installed_versions)272 install_attempt_install(plugin_name)273 # Success messaging. What did we actually install?274 post_installed_versions = installer.list_installed_plugin_gems.select { |spec| spec.name == plugin_name }.map { |spec| spec.version.to_s }275 new_version = (post_installed_versions - pre_installed_versions).first276 puts(bold { plugin_name } + " plugin, version #{new_version}, installed from rubygems.org")277 exit 0278 end279 def install_from_remote_gem_verson_preflight_check(plugin_name, requested_version, pre_installed_versions)280 return if pre_installed_versions.empty?281 # Everything past here in the block is a code 2 error282 # If they didn't ask for a specific version, they implicitly ask for the latest.283 # Do an expensive search to determine the latest version.284 unless requested_version285 latest_version = installer.search(plugin_name, exact: true, scope: :latest)286 latest_version = latest_version[plugin_name]&.last287 if latest_version && !requested_version288 requested_version = latest_version289 end290 end...
cluster-test
Source:cluster-test
...108 inspec_inputs.delete109 end110 def display_summary(results)111 if json?112 puts term.bold("Smoke test summary")113 results = results.map{|item| Hash[SUMMARY_HEADER.zip(item)]}114 puts JSON.pretty_generate(results)115 else 116 puts # intentionally left blank117 puts term.bold("Smoke test summary")118 table = TTY::Table.new(SUMMARY_HEADER, results)119 puts table.render(:ascii, padding: [0,1,0,1])120 puts # intentionally left blank121 end122 end123end124AutomateClusterTest.run...
bold
Using AI Code Generation
1describe file('/etc/hosts') do2 it { should be_file }3 its('content') { should match(/
bold
Using AI Code Generation
1 expect(bold("hello")).to eq "\e[1mhello\e[0m"2 expect(underline("hello")).to eq "\e[4mhello\e[0m"3 expect(italic("hello")).to eq "\e[3mhello\e[0m"4 expect(strikethrough("hello")).to eq "\e[9mhello\e[0m"5 expect(black("hello")).to eq "\e[30mhello\e[0m"6 expect(red("hello")).to eq "\e[31mhello\e[0m"7 expect(green("hello")).to eq "\e[32mhello\e[0m"8 expect(yellow("hello")).to eq "\e[33mhello\e[0m"
bold
Using AI Code Generation
1Inspec::Log.info("This is a bold message")2Inspec::Log.info("This is a bold message", :bold)3Inspec::Log.info("This is a bold message", :bold)4Inspec::Log.info("This is a bold message", :bold)5Inspec::Log.info("This is a bold message", :bold)6Inspec::Log.info("This is a bold message", :bold)7Inspec::Log.info("This is a bold message", :bold)8Inspec::Log.info("This is a bold message", :bold)9Inspec::Log.info("This is a bold message", :bold)10Inspec::Log.info("This is a bold message", :bold)11Inspec::Log.info("This is a bold message", :bold)12Inspec::Log.info("This is a bold message", :bold)13Inspec::Log.info("This is a bold message", :bold)14Inspec::Log.info("This is a bold message", :bold)15Inspec::Log.info("This is a bold message", :bold)16Inspec::Log.info("
bold
Using AI Code Generation
1 class Plugin < Inspec.plugin(2)2 def self.bold(str)3 Inspec::UI::Colored.bold(str)4inspec> InspecPlugins::MyPlugin::Plugin.bold("hello")
bold
Using AI Code Generation
1Inspec::UI::Color.bold('This is a bold text')2Inspec::UI::Color.bold('This is a bold text')3Inspec::UI::Color.bold('This is a bold text')4Inspec::UI::Color.bold('This is a bold text')5Inspec::UI::Color.bold('This is a bold text')6Inspec::UI::Color.bold('This is a bold text')7Inspec::UI::Color.bold('This is a bold text')8Inspec::UI::Color.bold('This is a bold text')9Inspec::UI::Color.bold('This is a bold text')10Inspec::UI::Color.bold('This is a bold text')11Inspec::UI::Color.bold('This is a bold text')12Inspec::UI::Color.bold('This is a bold text')
bold
Using AI Code Generation
1 runner.bold('hello world')2 runner.bold('hello world')3 runner.bold('hello world')4 let(:runner) { Inspec::Runner.new }5 runner.bold('hello world')6I tried the second method (let) and it doesn’t work. I get the following error:7Failure/Error: let(:runner) { Inspec::Runner.new }8I tried the second method (let) and it doesn’t work. I get the following error:
bold
Using AI Code Generation
1Inspec::UI::StdoutRouter.instance.bold('This is a bold text')2Inspec::UI::StdoutRouter.instance.bold('This is a bold text', 'This is a normal text')3Inspec::UI::StdoutRouter.instance.bold('This is a bold text', 'This is a normal text', 'This is another bold text')4Inspec::UI::StdoutRouter.instance.bold('This is a bold text', 'This is a normal text', 'This is another bold text', 'This is a normal text')5Inspec::UI::StdoutRouter.instance.bold('This is a bold text', 'This is a normal text', 'This is another bold text', 'This is a normal text', 'This is a bold text')6Inspec::UI::StdoutRouter.instance.bold('This is a bold text', 'This is a normal text', 'This is another bold text', 'This is a normal text', 'This is a bold text', 'This is a normal text')7Inspec::UI::StdoutRouter.instance.bold('This is a bold text', 'This is a normal text', 'This is another bold text', 'This is a normal text', 'This is a bold text', 'This is a normal text', 'This is another bold text')8Inspec::UI::StdoutRouter.instance.bold('This is a bold text', '
bold
Using AI Code Generation
1Inspec::UI::CLI.new.bold("Hello World")2Inspec::UI::CLI.new.bold("Hello World")3Inspec::UI::CLI.new.bold("Hello World")4require File.expand_path('1')5Inspec::UI::CLI.new.bold("Hello World")6require File.join(__dir__, '1')7Inspec::UI::CLI.new.bold("Hello World")8require File.join(File.dirname(__FILE__), '1')9Inspec::UI::CLI.new.bold("Hello World")10require File.expand_path(File.join(File.dirname(__FILE__), '1'))11Inspec::UI::CLI.new.bold("Hello World")12require File.expand_path(File.join(File.dirname(__FILE__), '1'))13Inspec::UI::CLI.new.bold("Hello World")14require File.expand_path(File.join(File.dirname(__FILE__), '1'))
bold
Using AI Code Generation
1Inspec::UI::Color.bold('This is a bold text')2Inspec::UI::Color.bold('This is a bold text')3Inspec::UI::Color.bold('This is a bold text')4Inspec::UI::Color.bold('This is a bold text')5Inspec::UI::Color.bold('This is a bold text')6Inspec::UI::Color.bold('This is a bold text')7Inspec::UI::Color.bold('This is a bold text')8Inspec::UI::Color.bold('This is a bold text')9Inspec::UI::Color.bold('This is a bold text')10Inspec::UI::Color.bold('This is a bold text')11Inspec::UI::Color.bold('This is a bold text')12Inspec::UI::Color.bold('This is a bold text')
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!