How to use plain_line method of Inspec Package

Best Inspec_ruby code snippet using Inspec.plain_line

cli_command.rb

Source:cli_command.rb Github

copy

Full Screen

...44 ]45 end46 end47 end48 ui.plain_line(" #{plugin_statuses.count} plugin(s) total")49 puts50 end51 #==================================================================#52 # inspec plugin search53 #==================================================================#54 desc "search [options] PATTERN", "Searches for plugins."55 long_desc <<~EOLD56 Searches rubygems.org or alternate source for #{PRODUCT_NAME} plugins. Exits 0 on a search hit, 1 on user error,57 2 on a search miss. PATTERN is a simple string; a wildcard will be added as58 a suffix, unless -e is used.59 EOLD60 option :all, desc: "List all available versions, not just the latest one.", type: :boolean, aliases: [:a]61 option :exact, desc: "Assume PATTERN is exact; do not add a wildcard to the end", type: :boolean, aliases: [:e]62 option :'include-test-fixture', type: :boolean, desc: "Internal use", hide: true63 option :source, type: :string, desc: "URL of repository, defaults to https://rubygems.org", aliases: [:s]64 # Justification for disabling ABC: currently at 33.51/3365 def search(search_term) # rubocop: disable Metrics/AbcSize66 search_results = installer.search(search_term, exact: options[:exact], source: options[:source])67 # The search results have already been filtered by the reject list. But the68 # RejectList doesn't filter {inspec, train}-test-fixture because we need those69 # for testing. We want to hide those from users, so unless we know we're in70 # test mode, remove them.71 unless options[:'include-test-fixture']72 search_results.delete("inspec-test-fixture")73 search_results.delete("train-test-fixture")74 end75 puts76 ui.bold(format(" %-30s%-50s\n", "Plugin Name", "Versions Available"))77 ui.line78 search_results.keys.sort.each do |plugin_name|79 versions = options[:all] ? search_results[plugin_name] : [search_results[plugin_name].first]80 versions = "(" + versions.join(", ") + ")"81 ui.plain_line(format(" %-30s%-50s", plugin_name, versions))82 end83 ui.line84 ui.plain_line(" #{search_results.count} plugin(s) found")85 puts86 ui.exit Inspec::UI::EXIT_PLUGIN_ERROR if search_results.empty?87 rescue Inspec::Plugin::V2::SearchError => ex88 Inspec::Log.error ex.message89 ui.exit Inspec::UI::EXIT_USAGE_ERROR90 end91 #==================================================================#92 # inspec plugin install93 #==================================================================#94 desc "install [options] PLUGIN", "Installs a plugin from rubygems.org, a gemfile, or a path to local source."95 long_desc <<~EOLD96 PLUGIN may be the name of a gem on rubygems.org (or an alternate source) that begins with `inspec-` or `train-`.97 PLUGIN may also be the path to a local gemfile, which will then be installed like98 any other gem. Finally, if PLUGIN is a path ending in .rb, it is taken to be a99 local file that will act as athe entry point for a plugin (this mode is provided100 for local plugin development). Exit codes are 0 on success, 2 if the plugin is101 already installed, and 1 if any other error occurs.102 EOLD103 option :version, desc: "When installing from rubygems.org, specifies a specific version to install.", aliases: [:v]104 option :source, type: :string, desc: "URL of repository, defaults to https://rubygems.org", aliases: [:s]105 def install(plugin_id_arg)106 if plugin_id_arg =~ /\.gem$/ # Does it end in .gem?107 install_from_gemfile(plugin_id_arg)108 elsif plugin_id_arg =~ %r{[\/\\]} || Dir.exist?(plugin_id_arg) # Does the argument have a slash, or exist as dir in the local directory?109 install_from_path(plugin_id_arg)110 else111 install_from_remote_gem(plugin_id_arg)112 end113 end114 #--------------------------115 # update116 #--------------------------117 desc "update PLUGIN", "Updates a plugin to the latest from from rubygems.org"118 long_desc <<~EOLD119 PLUGIN may be the name of a gem on rubygems.org that begins with inspec- or train-.120 Exit codes are 0 on success, 2 if the plugin is already up to date, and 1 if any121 other error occurs.122 EOLD123 def update(plugin_name)124 pre_update_versions = installer.list_installed_plugin_gems.select { |spec| spec.name == plugin_name }.map { |spec| spec.version.to_s }125 old_version = pre_update_versions.join(", ")126 update_preflight_check(plugin_name, pre_update_versions)127 begin128 installer.update(plugin_name)129 rescue Inspec::Plugin::V2::UpdateError => ex130 ui.plain_line("#{ui.red("Update error:", print: false)} #{ex.message} - update failed")131 ui.exit Inspec::UI::EXIT_USAGE_ERROR132 end133 post_update_versions = installer.list_installed_plugin_gems.select { |spec| spec.name == plugin_name }.map { |spec| spec.version.to_s }134 new_version = (post_update_versions - pre_update_versions).first135 ui.bold(plugin_name + " plugin, version #{old_version} -> " \136 "#{new_version}, updated from rubygems.org\n")137 end138 #--------------------------139 # uninstall140 #--------------------------141 desc "uninstall PLUGIN_NAME", "Uninstalls a gem- or path- based plugin"142 long_desc <<~EOLD143 Removes a plugin from the users configuration.144 In the case of a gem plugin (by far the most common), the plugin gem is removed, along145 with any of its dependencies that are no longer needed by anything else. Finally, the146 plugin configuration file is updated to reflect that the plugin is no longer present.147 In the case of a path-based plugin (often used for plugin development), no changes148 are made to the referenced plugin source code. Rather, the plugin's entry is simply removed149 from the plugin config file.150 EOLD151 def uninstall(plugin_name)152 status = Inspec::Plugin::V2::Registry.instance[plugin_name.to_sym]153 unless status154 ui.plain_line("#{ui.red("No such plugin installed:", print: false)} #{plugin_name} is not " \155 "installed - uninstall failed")156 ui.exit Inspec::UI::EXIT_USAGE_ERROR157 end158 installer = Inspec::Plugin::V2::Installer.instance159 pre_uninstall_versions = installer.list_installed_plugin_gems.select { |spec| spec.name == plugin_name }.map { |spec| spec.version.to_s }160 old_version = pre_uninstall_versions.join(", ")161 installer.uninstall(plugin_name)162 if status.installation_type == :path163 ui.bold(plugin_name + " path-based plugin install has been " \164 "uninstalled\n")165 else166 ui.bold(plugin_name + " plugin, version #{old_version}, has " \167 "been uninstalled\n")168 end169 ui.exit Inspec::UI::EXIT_NORMAL170 end171 private172 #==================================================================#173 # install breakdown174 #==================================================================#175 # These are broken down because rubocop complained.176 def install_from_gemfile(gem_file)177 unless File.exist? gem_file178 ui.red("No such plugin gem file #{gem_file} - installation failed.\n")179 ui.exit Inspec::UI::EXIT_USAGE_ERROR180 end181 plugin_name_parts = File.basename(gem_file, ".gem").split("-")182 version = plugin_name_parts.pop183 plugin_name = plugin_name_parts.join("-")184 check_plugin_name(plugin_name, "installation")185 installer.install(plugin_name, gem_file: gem_file)186 ui.bold("#{plugin_name} plugin, version #{version}, installed from " \187 "local .gem file\n")188 ui.exit Inspec::UI::EXIT_NORMAL189 end190 def install_from_path(path)191 unless File.exist? path192 ui.red("No such source code path #{path} - installation failed.\n")193 ui.exit Inspec::UI::EXIT_USAGE_ERROR194 end195 plugin_name = File.basename(path, ".rb")196 # While installer.install does some rudimentary checking,197 # this file has good UI access, so we promise to validate the198 # input a lot and hand the installer a sure-thing.199 # Name OK?200 check_plugin_name(plugin_name, "installation")201 # Already installed?202 if registry.known_plugin?(plugin_name.to_sym)203 ui.red("Plugin already installed - #{plugin_name} - Use '#{EXEC_NAME} " \204 "plugin list' to see previously installed plugin - " \205 "installation failed.\n")206 ui.exit Inspec::UI::EXIT_PLUGIN_ERROR207 end208 # Can we figure out how to load it?209 entry_point = install_from_path__apply_entry_point_heuristics(path)210 # If you load it, does it act like a plugin?211 install_from_path__probe_load(entry_point, plugin_name)212 # OK, install it!213 installer.install(plugin_name, path: entry_point)214 ui.bold("#{plugin_name} plugin installed via source path reference, " \215 "resolved to entry point #{entry_point}\n")216 ui.exit Inspec::UI::EXIT_NORMAL217 end218 # Rationale for rubocop variances: It's a heuristics method, and will be full of219 # conditionals. The code is well-commented; refactoring into sub-methods would220 # reduce clarity.221 def install_from_path__apply_entry_point_heuristics(path) # rubocop: disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity222 given = Pathname.new(path)223 given = given.expand_path # Resolve any relative paths224 name_regex = /^(inspec|train)-/225 versioned_regex = /^(inspec|train)-[a-z0-9\-\_]+-\d+\.\d+\.\d+$/226 sha_ref_regex = /^(inspec|train)-[a-z0-9\-\_]+-[0-9a-f]{5,40}$/227 # What are the last four things like?228 parts = [229 given.parent.parent.basename,230 given.parent.basename,231 given.basename(".rb"),232 given.extname,233 ].map(&:to_s)234 # Case 1: Simplest case: it was a full entry point, as presented.235 # /home/you/projects/inspec-something/lib/inspec-something.rb236 # parts index: ^0^ ^1^ ^2^ ^3^237 if parts[0] =~ name_regex && parts[1] == "lib" && parts[2] == parts[0] && parts[3] == ".rb"238 return given.to_s239 end240 # Case 2: Also easy: they either referred to the internal library directory,241 # or left the extansion off. Those are the same to us.242 # /home/you/projects/inspec-something/lib/inspec-something243 # parts index: ^0^ ^1^ ^2^ (3 is empty)244 if parts[0] =~ name_regex && parts[1] == "lib" && parts[2] == parts[0] && parts[3].empty?245 return given.to_s + ".rb"246 end247 # Case 3: Maybe they were refering to a path that is inside a gem installation, or an exploded gem?248 # In that case, we'll have a version on the plugin name in part 0249 # /home/you/.gems/2.4.0/gems/inspec-something-3.45.1/lib/inspec-something.rb250 # parts index: ^0^ ^1^ ^2^ ^3^251 if (parts[0] =~ versioned_regex || parts[0] =~ sha_ref_regex) && parts[1] == "lib" && parts[0].start_with?(parts[2]) && parts[3] == ".rb"252 return given.to_s253 end254 # Case 4: Like case 3, but missing the .rb255 # /home/you/.gems/2.4.0/gems/inspec-something-3.45.1/lib/inspec-something256 # parts index: ^0^ ^1^ ^2^ ^3^ (empty)257 if (parts[0] =~ versioned_regex || parts[0] =~ sha_ref_regex) && parts[1] == "lib" && parts[0].start_with?(parts[2]) && parts[3].empty?258 return given.to_s + ".rb"259 end260 # Case 5: Easy to recognize, but harder to handle: they referred to the project root.261 # /home/you/projects/inspec-something262 # parts index: ^0^ ^1^ ^2^ (3 is empty)263 # 0 and 1 are not meaningful to us, but we hope to find a parts[2]/lib/inspec-something.rb.264 entry_point_guess = File.join(given.to_s, "lib", parts[2] + ".rb")265 if parts[2] =~ name_regex && File.exist?(entry_point_guess)266 return entry_point_guess267 end268 # Well, if we got here, parts[2] matches an inspec/train prefix, but we have no idea about anything.269 # Give up.270 ui.red("Unrecognizable plugin structure - #{parts[2]} - When " \271 "installing from a path, please provide the path of the " \272 "entry point file - installation failed.\n")273 ui.exit Inspec::UI::EXIT_USAGE_ERROR274 end275 def install_from_path__probe_load(entry_point, plugin_name)276 # Brazenly attempt to load a file, and see if it registers a plugin.277 begin278 require entry_point279 rescue LoadError => ex280 ui.red("Plugin contains errors - #{plugin_name} - Encountered " \281 "errors while trying to test load the plugin entry point, " \282 "resolved to #{entry_point} - installation failed\n")283 ui.plain_line ex.message284 ui.exit Inspec::UI::EXIT_USAGE_ERROR285 end286 # OK, the wheels didn't fall off. But is it a plugin?287 if plugin_name.to_s.start_with?("train")288 # Train internal names do not include the prix in their registry entries289 # And the registry is keyed on Strings290 registry_key = plugin_name.to_s.sub(/^train-/, "")291 unless Train::Plugins.registry.key?(registry_key)292 ui.red("Does not appear to be a plugin - #{plugin_name} - After " \293 "probe-loading the supposed plugin, it did not register " \294 "itself to Train. Ensure something inherits from " \295 "'Train.plugin(1)' - installation failed.\n")296 ui.exit Inspec::UI::EXIT_USAGE_ERROR297 end298 else299 unless registry.known_plugin?(plugin_name.to_sym)300 ui.red("Does not appear to be a plugin - #{plugin_name} - After " \301 "probe-loading the supposed plugin, it did not register " \302 "itself to InSpec. Ensure something inherits from " \303 "'Inspec.plugin(2)' - installation failed.\n")304 ui.exit Inspec::UI::EXIT_USAGE_ERROR305 end306 end307 end308 def install_from_remote_gem(plugin_name)309 requested_version = options[:version]310 check_plugin_name(plugin_name, "installation")311 # Version pre-flighting312 pre_installed_versions = installer.list_installed_plugin_gems.select { |spec| spec.name == plugin_name }.map { |spec| spec.version.to_s }313 install_from_remote_gem_verson_preflight_check(plugin_name, requested_version, pre_installed_versions)314 install_attempt_install(plugin_name)315 # Success messaging. What did we actually install?316 post_installed_versions = installer.list_installed_plugin_gems.select { |spec| spec.name == plugin_name }.map { |spec| spec.version.to_s }317 new_version = (post_installed_versions - pre_installed_versions).first318 ui.bold("#{plugin_name} plugin, version #{new_version}, installed " \319 "from rubygems.org\n")320 ui.exit Inspec::UI::EXIT_NORMAL321 end322 def install_from_remote_gem_verson_preflight_check(plugin_name, requested_version, pre_installed_versions)323 return if pre_installed_versions.empty?324 # Everything past here in the block is a code 2 error325 # If they didn't ask for a specific version, they implicitly ask for the latest.326 # Do an expensive search to determine the latest version.327 unless requested_version328 latest_version = installer.search(plugin_name, exact: true, scope: :latest)329 latest_version = latest_version[plugin_name]&.last330 if latest_version && !requested_version331 requested_version = latest_version332 end333 end334 # Check for already-installed at desired version conditions335 they_explicitly_asked_for_a_version = !options[:version].nil?336 what_we_would_install_is_already_installed = pre_installed_versions.include?(requested_version)337 if what_we_would_install_is_already_installed && they_explicitly_asked_for_a_version338 ui.red("Plugin already installed at requested version - plugin " \339 "#{plugin_name} #{requested_version} - refusing to install.\n")340 elsif what_we_would_install_is_already_installed && !they_explicitly_asked_for_a_version341 ui.red("Plugin already installed at latest version - plugin " \342 "#{plugin_name} #{requested_version} - refusing to install.\n")343 else344 # There are existing versions installed, but none of them are what was requested345 ui.red("Update required - plugin #{plugin_name}, requested " \346 "#{requested_version}, have " \347 "#{pre_installed_versions.join(", ")}; use `inspec " \348 "plugin update` - refusing to install.\n")349 end350 ui.exit Inspec::UI::EXIT_PLUGIN_ERROR351 end352 # Rationale for RuboCop variance: This is a one-line method with heavy UX-focused error handling.353 def install_attempt_install(plugin_name) # rubocop: disable Metrics/AbcSize354 installer.install(plugin_name, version: options[:version], source: options[:source])355 rescue Inspec::Plugin::V2::PluginExcludedError => ex356 ui.red("Plugin on Exclusion List - #{plugin_name} is listed as an " \357 "incompatible gem - refusing to install.\n")358 ui.plain_line("Rationale: #{ex.details.rationale}")359 ui.plain_line("Exclusion list location: " +360 File.join(Inspec.src_root, "etc", "plugin_filters.json"))361 ui.plain_line("If you disagree with this determination, please accept " \362 "our apologies for the misunderstanding, and open an issue " \363 "at https://github.com/inspec/inspec/issues/new")364 ui.exit Inspec::UI::EXIT_PLUGIN_ERROR365 rescue Inspec::Plugin::V2::InstallError366 raise if Inspec::Log.level == :debug367 results = installer.search(plugin_name, exact: true)368 source_host = URI(options[:source] || "https://rubygems.org/").host369 if results.empty?370 ui.red("No such plugin gem #{plugin_name} could be found on " \371 "#{source_host} - installation failed.\n")372 elsif options[:version] && !results[plugin_name].include?(options[:version])373 ui.red("No such version - #{plugin_name} exists, but no such " \374 "version #{options[:version]} found on #{source_host} - " \375 "installation failed.\n")376 else377 ui.red("Unknown error occured - installation failed.\n")378 end379 ui.exit Inspec::UI::EXIT_USAGE_ERROR380 end381 #==================================================================#382 # update breakdown383 #==================================================================#384 def update_preflight_check(plugin_name, pre_update_versions)385 if pre_update_versions.empty?386 # Check for path install387 status = Inspec::Plugin::V2::Registry.instance[plugin_name.to_sym]388 if !status389 ui.plain_line("#{ui.red("No such plugin installed:", print: false)} #{plugin_name} - update failed")390 ui.exit Inspec::UI::EXIT_USAGE_ERROR391 elsif status.installation_type == :path392 ui.plain_line("#{ui.red("Cannot update path-based install:", print: false)} " \393 "#{plugin_name} is installed via path reference; " \394 "use `inspec plugin uninstall` to remove - refusing to" \395 "update")396 ui.exit Inspec::UI::EXIT_PLUGIN_ERROR397 end398 end399 # Check for latest version (and implicitly, existance)400 latest_version = installer.search(plugin_name, exact: true, scope: :latest)401 latest_version = latest_version[plugin_name]&.last402 if pre_update_versions.include?(latest_version)403 ui.plain_line("#{ui.red("Already installed at latest version:", print: false)} " \404 "#{plugin_name} is at #{latest_version}, which the " \405 "latest - refusing to update")406 ui.exit Inspec::UI::EXIT_PLUGIN_ERROR407 end408 end409 #==================================================================#410 # utilities411 #==================================================================#412 def installer413 Inspec::Plugin::V2::Installer.instance414 end415 def registry416 Inspec::Plugin::V2::Registry.instance417 end...

Full Screen

Full Screen

profile_helper.rb

Source:profile_helper.rb Github

copy

Full Screen

...15 # --------------------------- InSpec Code Generator ---------------------------16 cli.headline("InSpec Tfkit Code Generator")17 full_destination_path = Pathname.new(Dir.pwd).join(name)18 if File.exist?(full_destination_path) && !overwrite_mode19 cli.plain_line "#{cli.emphasis(full_destination_path)} exists already, use --overwrite"20 cli.exit(1)21 end22 # ensure that full_destination_path directory is available23 FileUtils.mkdir_p(full_destination_path)24 # Creating new profile at /Users/mattray/ws/inspec-tfkit/FOO25 cli.plain_line "Creating new profile at #{cli.emphasis(full_destination_path)}"26 # * Creating file README.md27 render_readme_md(cli, name, source_file, platform)28 # * Creating directory controls29 cli.list_item "Creating directory #{cli.emphasis("controls")}"30 FileUtils.mkdir_p("#{name}/controls")31 # * Creating file controls/generated.rb32 render_controls_rb(cli, name, controls)33 # * Creating file inspec.yml34 render_inspec_yml(cli, name, source_file, options, platform)35 cli.plain_line36 end37 def self.render_readme_md(cli, name, source_file, platform)38 cli.list_item "Creating file #{cli.emphasis("README.md")}"39 f = File.new("#{name}/README.md", "w")40 f.puts("# #{name}")41 f.puts42 f.puts("This profile was generated by inspec-tfkit v#{Tfkit::VERSION} from the #{source_file} source file.")43 f.puts(InspecPlugins::Tfkit::Platforms::AwsHelper.readme) if platform.eql?("aws")44 f.puts(InspecPlugins::Tfkit::Platforms::AzureHelper.readme) if platform.eql?("azure")45 f.puts(InspecPlugins::Tfkit::Platforms::GcpHelper.readme) if platform.eql?("gcp")46 f.close47 end48 def self.render_inspec_yml(cli, name, source_file, options, platform)49 cli.list_item "Creating file #{cli.emphasis("inspec.yml")}"...

Full Screen

Full Screen

plain_line

Using AI Code Generation

copy

Full Screen

1describe plain_line('hello') do2 its('content') { should cmp 'hello' }3 its('line') { should eq 1 }4describe plain_line('hello', 2) do5 its('content') { should cmp 'hello' }6 its('line') { should eq 2 }7describe plain_line('hello', 3) do8 its('content') { should cmp 'hello' }9 its('line') { should eq 3 }10describe plain_line('hello', 4) do11 its('content') { should cmp 'hello' }12 its('line') { should eq 4 }13describe plain_line('hello', 5) do14 its('content') { should cmp 'hello' }15 its('line') { should eq 5 }16describe plain_line('hello', 6) do17 its('content') { should cmp 'hello' }18 its('line') { should eq 6 }19describe plain_line('hello', 7) do20 its('content') { should cmp 'hello' }21 its('line') { should eq 7 }22describe plain_line('hello', 8) do23 its('content') { should cmp 'hello' }24 its('line') { should eq 8 }25describe plain_line('hello', 9) do26 its('content') { should cmp 'hello' }27 its('line') { should eq 9 }28describe plain_line('hello',

Full Screen

Full Screen

plain_line

Using AI Code Generation

copy

Full Screen

1puts Inspec::plain_line("Hello World")2puts Inspec::plain_line("Hello World")3puts Inspec::plain_line("Hello World")4puts Inspec::plain_line("Hello World")5puts Inspec::plain_line("Hello World")6puts Inspec::plain_line("Hello World")7puts Inspec::plain_line("Hello World")8puts Inspec::plain_line("Hello World")9puts Inspec::plain_line("Hello World")10puts Inspec::plain_line("Hello World")11puts Inspec::plain_line("Hello World")12puts Inspec::plain_line("Hello World")13puts Inspec::plain_line("Hello World")14puts Inspec::plain_line("Hello World")15puts Inspec::plain_line("Hello World")

Full Screen

Full Screen

plain_line

Using AI Code Generation

copy

Full Screen

1Inspec::RubyHelper.load_ruby_file('lib/inspec/objects/line.rb')2 line = Inspec::Line.new('hello world')3 expect(line.plain_line).to eq 'hello world'

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