How to use shellout method of Fetchers Package

Best Inspec_ruby code snippet using Fetchers.shellout

git.rb

Source:git.rb Github

copy

Full Screen

1# encoding: utf-82require 'tmpdir'3require 'fileutils'4require 'mixlib/shellout'5require 'inspec/log'6module Fetchers7 #8 # The git fetcher uses the git binary to fetch remote git sources.9 # Git-based sources should be specified with the `git:` key in the10 # source hash. Additionally, we accept `:branch`, `:ref`, and `:tag`11 # keys to allow users to pin to a particular revision.12 #13 # Parts of this class are derived from:14 #15 # https://github.com/chef/omnibus/blob/master/lib/omnibus/fetchers/git_fetcher.rb16 #17 # which is Copyright 2012-2014 Chef Software, Inc. and offered under18 # the same Apache 2 software license as inspec.19 #20 # Many thanks to the omnibus authors!21 #22 # Note that we haven't replicated all of omnibus' features here. If23 # you got to this file during debugging, you may want to look at the24 # omnibus source for hints.25 #26 class Git < Inspec.fetcher(1) # rubocop:disable ClassLength27 name 'git'28 priority 20029 def self.resolve(target, opts = {})30 if target.respond_to?(:has_key?) &&target.key?(:git)31 new(target[:git], opts.merge(target))32 end33 end34 def initialize(remote_url, opts = {})35 @branch = opts[:branch]36 @tag = opts[:tag]37 @ref = opts[:ref]38 @remote_url = remote_url39 @repo_directory = nil40 end41 def fetch(dir)42 @repo_directory = dir43 FileUtils.mkdir_p(dir) unless Dir.exist?(dir)44 if cloned?45 checkout46 else47 Dir.mktmpdir do |tmpdir|48 checkout(tmpdir)49 Inspec::Log.debug("Checkout of #{resolved_ref} successful. Moving checkout to #{dir}")50 FileUtils.cp_r(tmpdir, @repo_directory)51 end52 end53 @repo_directory54 end55 def cache_key56 resolved_ref57 end58 def archive_path59 @repo_directory60 end61 def resolved_source62 { git: @remote_url, ref: resolved_ref }63 end64 private65 def resolved_ref66 @resolved_ref ||= if @ref67 @ref68 elsif @branch69 resolve_ref(@branch)70 elsif @tag71 resolve_ref(@tag)72 else73 resolve_ref('master')74 end75 end76 def resolve_ref(ref_name)77 cmd = shellout("git ls-remote \"#{@remote_url}\" \"#{ref_name}*\"")78 ref = parse_ls_remote(cmd.stdout, ref_name)79 if !ref80 raise "Unable to resolve #{ref_name} to a specific git commit for #{@remote_url}"81 end82 ref83 end84 #85 # The following comment is a minor modification of the comment in86 # the omnibus source for a similar function:87 #88 # Dereference annotated tags.89 #90 # The +remote_list+ parameter is assumed to look like this:91 #92 # a2ed66c01f42514bcab77fd628149eccb4ecee28 refs/tags/rel-0.11.093 # f915286abdbc1907878376cce9222ac0b08b12b8 refs/tags/rel-0.11.0^{}94 #95 # The SHA with ^{} is the commit pointed to by an annotated96 # tag. If ref isn't an annotated tag, there will not be a line97 # with trailing ^{}.98 #99 # @param [String] output100 # output from `git ls-remote origin` command101 # @param [String] ref_name102 # the target git ref_name103 #104 # @return [String]105 #106 def parse_ls_remote(output, ref_name)107 pairs = output.lines.map { |l| l.chomp.split("\t") }108 tagged_commit = pairs.find { |m| m[1].end_with?("#{ref_name}^{}") }109 if tagged_commit110 tagged_commit.first111 else112 found = pairs.find { |m| m[1].end_with?(ref_name.to_s) }113 if found114 found.first115 end116 end117 end118 def cloned?119 File.directory?(File.join(@repo_directory, '.git'))120 end121 def clone(dir = @repo_directory)122 git_cmd("clone #{@remote_url} ./", dir) unless cloned?123 @repo_directory124 end125 def checkout(dir = @repo_directory)126 clone(dir)127 git_cmd("checkout #{resolved_ref}", dir)128 @repo_directory129 end130 def git_cmd(cmd, dir = @repo_directory)131 cmd = shellout("git #{cmd}", cwd: dir)132 cmd.error!133 cmd.status134 rescue Errno::ENOENT135 raise 'To use git sources, you must have git installed.'136 end137 def shellout(cmd, opts = {})138 Inspec::Log.debug("Running external command: #{cmd} (#{opts})")139 cmd = Mixlib::ShellOut.new(cmd, opts)140 cmd.run_command141 Inspec::Log.debug("External command: completed with exit status: #{cmd.exitstatus}")142 Inspec::Log.debug('External command: STDOUT BEGIN')143 Inspec::Log.debug(cmd.stdout)144 Inspec::Log.debug('External command: STDOUT END')145 Inspec::Log.debug('External command: STDERR BEGIN')146 Inspec::Log.debug(cmd.stderr)147 Inspec::Log.debug('External command: STDERR END')148 cmd149 end150 end151end...

Full Screen

Full Screen

git_test.rb

Source:git_test.rb Github

copy

Full Screen

1# encoding: utf-82# author: Dominik Richter3# author: Christoph Hartmann4require 'helper'5describe Fetchers::Git do6 let(:fetcher) { Fetchers::Git }7 it 'registers with the fetchers registry' do8 reg = Inspec::Fetcher.registry9 _(reg['git']).must_equal fetcher10 end11 it "handles sources specified by a :git key" do12 f = fetcher.resolve({git: "https://example.com/foo.gi"})13 f.wont_be_nil14 f.must_be_kind_of Fetchers::Git15 end16 describe "when given a valid repository" do17 let(:git_dep_dir) { "test-directory" }18 let(:git_master_ref) { "bf4d5774f02d24155bfc34b5897d22785a304cfa" }19 let(:git_branch_ref) { "b979579e5fc8edb72511fe5d2a1230dede71eff7" }20 let(:git_tag_ref) { "efc85d89ee9d5798ca93ee95db0c711b99061590" }21 let(:git_output) {22 out = mock()23 out.stubs(:stdout).returns("")24 out.stubs(:exitstatus).returns(0)25 out.stubs(:stderr).returns("")26 out.stubs(:status).returns(true)27 out.stubs(:error!).returns(false)28 out.stubs(:run_command).returns(true)29 out30 }31 let(:git_ls_remote_output) {32 out = mock()33 out.stubs(:stdout).returns("9abea97db10a428709353fd582b969d0e17cb923\tHEAD34bf4d5774f02d24155bfc34b5897d22785a304cfa\trefs/heads/master35b979579e5fc8edb72511fe5d2a1230dede71eff7\trefs/heads/somebranch36d9d5a6fe85c3df709bb1c878c2de8f2cb5893ced\trefs/tags/boringtag37ad280246a1a2b3d1b1179e1a8d9e1a044e7ee94f\trefs/tags/antag38efc85d89ee9d5798ca93ee95db0c711b99061590\trefs/tags/antag^{}39be002c56b0806ea40aabf7a2b742c41182336198\trefs/tags/anothertag40a7729ce65636d6d8b80159dd5dd7a40fdb6f2501\trefs/tags/anothertag^{}\n")41 out.stubs(:exitstatus).returns(0)42 out.stubs(:stderr).returns("")43 out.stubs(:error!).returns(false)44 out.stubs(:run_command).returns(true)45 out46 }47 before do48 # git fetcher likes to make directories, let's stub that for every test49 Dir.stubs(:mktmpdir).yields("test-tmp-dir")50 File.stubs(:directory?).with("fetchpath/.git").returns(false)51 FileUtils.stubs(:mkdir_p)52 end53 def expect_ls_remote(ref)54 Mixlib::ShellOut.expects(:new).with("git ls-remote \"#{git_dep_dir}\" \"#{ref}*\"", {}).returns(git_ls_remote_output)55 end56 def expect_checkout(ref, at='test-tmp-dir')57 Mixlib::ShellOut.expects(:new).with("git checkout #{ref}", {cwd: at}).returns(git_output)58 end59 def expect_clone60 Mixlib::ShellOut.expects(:new).with("git clone #{git_dep_dir} ./", {cwd: 'test-tmp-dir'}).returns(git_output)61 end62 def expect_mv_into_place63 FileUtils.expects(:cp_r).with('test-tmp-dir', 'fetchpath')64 end65 it "resolves to the revision of master by default" do66 expect_ls_remote('master')67 result = fetcher.resolve({git: git_dep_dir})68 result.resolved_source.must_equal({git: git_dep_dir, ref: git_master_ref })69 end70 it "can resolve a tag" do71 expect_ls_remote('antag')72 result = fetcher.resolve({git: git_dep_dir, tag: 'antag'})73 result.resolved_source.must_equal({git: git_dep_dir, ref: git_tag_ref })74 end75 it "can resolve a branch" do76 expect_ls_remote('somebranch')77 result = fetcher.resolve({git: git_dep_dir, branch: 'somebranch'})78 result.resolved_source.must_equal({git: git_dep_dir, ref: git_branch_ref })79 end80 it "assumes the ref you gave it is the thing you want" do81 result = fetcher.resolve({git: git_dep_dir, ref: 'a_test_ref'})82 result.resolved_source.must_equal({git: git_dep_dir, ref: 'a_test_ref' })83 end84 it "fetches to the given location" do85 expect_ls_remote('master')86 expect_clone()87 expect_checkout(git_master_ref)88 expect_mv_into_place()89 result = fetcher.resolve({git: git_dep_dir})90 result.fetch("fetchpath")91 end92 it "doesn't refetch an already cloned repo" do93 File.expects(:directory?).with("fetchpath/.git").at_least_once.returns(true)94 expect_ls_remote('master')95 expect_checkout(git_master_ref, 'fetchpath')96 result = fetcher.resolve({git: git_dep_dir})97 result.fetch("fetchpath")98 end99 it "returns the repo_path that we fetched to as the archive_path" do100 File.expects(:directory?).with("fetchpath/.git").at_least_once.returns(true)101 expect_ls_remote('master')102 expect_checkout(git_master_ref, 'fetchpath')103 result = fetcher.resolve({git: git_dep_dir})104 result.fetch("fetchpath")105 result.archive_path.must_equal 'fetchpath'106 end107 end108end...

Full Screen

Full Screen

shellout

Using AI Code Generation

copy

Full Screen

1fetcher.fetch_content("ls -l /tmp")2fetcher.fetch_content("http://www.google.com")3fetcher.fetch_content("http://www.google.com")4fetcher.fetch_content("http://www.google.com")5fetcher.fetch_file("http://www.google.com", "/tmp/google.html")6fetcher.fetch_file("http://www.google.com", "/tmp/google.html")7fetcher.fetch_file("http://www.google.com", "/tmp/google.html")8fetcher.fetch_file("http://www.google.com", "/tmp/google.html")9fetcher.fetch_file("http://www.google.com", "/tmp/google.html")10fetcher.fetch_file("http://www.google.com", "/tmp/google.html")11fetcher.fetch_file("http://www.google.com", "/tmp/google.html")

Full Screen

Full Screen

shellout

Using AI Code Generation

copy

Full Screen

1 def get_output(cmd)2 shell_out!(cmd).stdout3output = fetcher.get_output('cat /etc/hosts')4 def get_output(cmd)5 shell_out!(cmd).stdout6output = fetcher.get_output('cat /etc/hosts')7 def get_output(cmd)8 shell_out!(cmd).stdout9output = fetcher.get_output('cat /etc/hosts')10 def get_output(cmd)11 shell_out!(cmd).stdout12output = fetcher.get_output('cat /etc/hosts')13 def get_output(cmd)14 shell_out!(cmd).stdout15output = fetcher.get_output('cat /etc/hosts')

Full Screen

Full Screen

shellout

Using AI Code Generation

copy

Full Screen

1fetcher.fetch_path('http://www.rubyinside.com/test.txt', 'test.txt')2fetcher.fetch_path('http://www.rubyinside.com/test.txt', 'test.txt')3fetcher.fetch_path('http://www.rubyinside.com/test.txt', 'test.txt')4fetcher.fetch_path('http://www.rubyinside.com/test.txt', 'test.txt')5fetcher.fetch_path('http://www.rubyinside.com/test.txt', 'test.txt')6fetcher.fetch_path('http://www.rubyinside.com/test.txt', 'test.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.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful