CFINSPEC-13 Added exception handling for Gem BadRequirement error.

Signed-off-by: Vasu1105 <vasundhara.jagdale@chef.io>
This commit is contained in:
Vasu1105 2022-03-04 17:13:01 +05:30
parent c32f48ca88
commit c73dec70a7
6 changed files with 56 additions and 11 deletions

View file

@ -29,21 +29,25 @@ module Inspec
def install_from_remote_gems(requested_gem_name, opts)
version = opts[:version].split(",")
gem_dependency = Gem::Dependency.new(requested_gem_name, version || "> 0")
# BestSet is rubygems.org API + indexing, APISet is for custom sources
sources = if opts[:source]
Gem::Resolver::APISet.new(URI.join(opts[:source] + "/api/v1/dependencies"))
else
Gem::Resolver::BestSet.new
end
begin
gem_dependency = Gem::Dependency.new(requested_gem_name, version || "> 0")
# BestSet is rubygems.org API + indexing, APISet is for custom sources
sources = if opts[:source]
Gem::Resolver::APISet.new(URI.join(opts[:source] + "/api/v1/dependencies"))
else
Gem::Resolver::BestSet.new
end
install_gem_to_gems_dir(gem_dependency, [sources], opts[:update_mode])
rescue Gem::RemoteFetcher::FetchError => gem_ex
ex = Inspec::GemDependencyInstallError.new(gem_ex.message)
ex.gem_name = requested_gem_name
raise ex
rescue Gem::Requirement::BadRequirementError => gem_ex
ex = Inspec::GemDependencyInstallError.new(gem_ex.message)
ex.gem_name = requested_gem_name
raise "Unparseable gem dependency '#{version}' for '#{ex.gem_name}'"
end
end

View file

@ -126,6 +126,8 @@ module Inspec
orig.split(",").map { |c| Gem::Requirement.parse(c) }
rescue Gem::Requirement::BadRequirementError
errors.push "Unparseable gem dependency '#{orig}' for #{entry[:name]}"
rescue Inspec::GemDependencyInstallError => e
errors.push e.message
end
end
extra = (entry.keys - %i{name version})

View file

@ -428,7 +428,7 @@ module Inspec
dependency_loader = DependencyLoader.new(nil, [gem_data])
dependency_loader.load
rescue Inspec::GemDependencyLoadError => e
raise e
raise e.message
end
# Requires gem_data as argument.
@ -437,7 +437,7 @@ module Inspec
gem_dependency = DependencyInstaller.new(nil, [gem_data])
gem_dependency.install
rescue Inspec::GemDependencyInstallError => e
raise e
raise e.message
end
def to_s

View file

@ -0,0 +1,18 @@
# copyright: 2018, The Authors
title "sample section"
# you can also use plain tests
describe file("/tmp") do
it { should be_directory }
end
# you add controls here
control "tmp-1.0" do # A unique ID for this control
impact 0.7 # The criticality, if this control fails.
title "Create /tmp directory" # A human-readable title
desc "An optional description..."
describe file("/tmp") do # The actual test
it { should be_directory }
end
end

View file

@ -0,0 +1,13 @@
name: profile-with-illformed-gem-depedency
title: InSpec Profile
maintainer: The Authors
copyright: The Authors
copyright_email: you@example.com
license: Apache-2.0
summary: An InSpec Compliance Profile
version: 0.1.0
supports:
platform: os
gem_dependencies:
name: "mongo"
version: "+ 2.3.12"

View file

@ -6,6 +6,8 @@ describe "profile with gem dependencies" do
let(:config_dir_path) { File.expand_path "test/fixtures/config_dirs" }
let(:depdent_profile_gem_dependency) { File.join(profile_path, "profile-with-dependent-gem-dependency") }
let(:ruby_abi_version) { RbConfig::CONFIG["ruby_version"] }
let(:illformatted_gem_dependncy) { File.join(profile_path, "profile-with-illformed-gem-depedency") }
def reset_globals
ENV["HOME"] = Dir.home
end
@ -40,4 +42,10 @@ describe "profile with gem dependencies" do
_(File.directory?(File.join(config_dir_path, "profile_gems", ".inspec/gems/#{ruby_abi_version}/gems"))).must_equal true
assert_exit_code 0, out
end
it "raises error for illformated gem dependencies found in the meta data file" do
out = inspec_with_env("exec #{illformatted_gem_dependncy} --no-create-lockfile --auto-install-gems")
_(out.stderr).must_include "Unparseable gem dependency '[\"+ 2.3.12\"]' for 'mongo'"
assert_exit_code 1, out
end
end