docs: Update example resource (#2904)

* Change `skip_resource` to use raise
* Add `supports` lines to example resource
* Change to rescue `StandardError` vs `Exception`
* Change raise to use `e.message` vs `$!`
* Remove redundant returns
* Change `File.exists?` to `File.exist?`
* Update shasum in tests

Signed-off-by: Jerry Aldrich <jerryaldrichiii@gmail.com>
This commit is contained in:
Jerry Aldrich 2018-04-05 05:50:49 -07:00 committed by Jared Quick
parent cd745f8b9b
commit 4731d95abd
5 changed files with 17 additions and 11 deletions

View file

@ -91,7 +91,7 @@ class GordonConfig < Inspec.resource(1)
f.content
else
# If the file doesn't exist, skip all tests that use gordon_config
skip_resource "Can't read config from #{@path}."
raise Inspec::Exceptions::ResourceSkipped, "Can't read config at #{@path}"
end
end
end

View file

@ -4,6 +4,9 @@ require 'yaml'
class GordonConfig < Inspec.resource(1)
name 'gordon_config'
supports platform: 'unix'
supports platform: 'windows'
desc "
Gordon's resource description ...
"
@ -20,7 +23,10 @@ class GordonConfig < Inspec.resource(1)
@params = {}
@path = '/tmp/gordon/config.yaml'
@file = inspec.file(@path)
return skip_resource "Can't find file \"#{@path}\"" if !@file.file?
unless @file.file?
raise Inspec::Exceptions::ResourceSkipped, "Can't find file `#{@path}`"
end
# Protect from invalid YAML content
begin
@ -29,25 +35,25 @@ class GordonConfig < Inspec.resource(1)
@params['file_size'] = @file.size
@params['file_path'] = @path
@params['ruby'] = 'RUBY IS HERE TO HELP ME!'
rescue Exception
return skip_resource "#{@file}: #{$!}"
rescue StandardError => e
raise Inspec::Exceptions::ResourceSkipped, "#{@file}: #{e.message}"
end
end
# Example method called by 'it { should exist }'
# Returns true or false from the 'File.exists?' method
# Returns true or false from the 'File.exist?' method
def exists?
return File.exists?(@path)
File.exist?(@path)
end
# Example matcher for the number of commas in the file
def comma_count
text = @file.content
return text.count(',')
text.count(',')
end
# Expose all parameters
def method_missing(name)
return @params[name.to_s]
@params[name.to_s]
end
end

View file

@ -58,7 +58,7 @@ describe 'inspec exec with json formatter' do
"license" => "Apache-2.0",
"summary" => "Demonstrates the use of InSpec Compliance Profile",
"version" => "1.0.0",
"sha256" => "8eed5154c9fa0174067ab475c0ad4a053f772590d129eb324101fe43ef30794d",
"sha256" => "57709d3a3d5cd06f4179be7e6fbe254c09e3af25ce274e474d52623e34487cc4",
"supports" => [{"os-family" => "unix"}],
"attributes" => []
})

View file

@ -67,7 +67,7 @@ describe 'inspec exec' do
it 'has a skip_message' do
ex1['skip_message'].must_be :nil?
ex3['skip_message'].must_equal "Can't find file \"/tmp/gordon/config.yaml\""
ex3['skip_message'].must_equal "Can't find file `/tmp/gordon/config.yaml`"
end
end

View file

@ -16,7 +16,7 @@ describe 'inspec exec' do
stdout.must_include "\e[38;5;41m ✔ tmp-1.0: Create /tmp directory\e[0m\n"
stdout.must_include "
\e[38;5;247m gordon-1.0: Verify the version number of Gordon (1 skipped)\e[0m
\e[38;5;247m Can't find file \"/tmp/gordon/config.yaml\"\e[0m
\e[38;5;247m Can't find file `/tmp/gordon/config.yaml`\e[0m
"
stdout.must_include "\nProfile Summary: \e[38;5;41m2 successful controls\e[0m, 0 control failures, \e[38;5;247m1 control skipped\e[0m\n"
stdout.must_include "\nTest Summary: \e[38;5;41m4 successful\e[0m, 0 failures, \e[38;5;247m1 skipped\e[0m\n"