inspec/test/unit/dsl/other_keywords_test.rb
Adam Leff 9580732814 Source reader should not hand back files with nil contents (#2003)
If a profile has a data files directory that looks like this:

```
files/platforms/one/data.json
files/platforms/two/data.json
files/platforms/three/data.json
```

... the source reader will return the directories in the list of files but with
nil contents. This causes an issue when Inspec::Profile tries to create a sha256
checksum of the profile contents only to try to cast nil to a string when
building the null-delimited profile contents string.

Files that are empty will have an empty string as its contents, so it's safe to
assume that file entries with nil contents are actually a directory and have no
affect on the profile's checksum. Therefore, this change will eliminate any file
entries in responses from the source readers where the contents are nil.

Signed-off-by: Adam Leff <adam@leff.co>
2017-07-11 21:33:55 +02:00

62 lines
1.7 KiB
Ruby

# encoding: utf-8
# author: Dominik Richter
# author: Christoph Hartmann
require 'helper'
describe 'inspec keyword' do
def load(content)
runner = Inspec::Runner.new({backend: 'mock'})
runner.eval_with_virtual_profile(content)
end
def load_in_profile(cmd)
MockLoader.load_profile('complete-profile').runner_context.load(cmd)
end
it 'is a vailable as a global keyword' do
load('inspec') # wont raise anything
end
it 'is a vailable inside of control blocks' do
load('control 1 do inspec end') # wont raise anything
end
it 'provides version information' do
load('inspec.version').must_equal Inspec::VERSION
end
it 'is associated with resources' do
i = load('os.inspec')
i.wont_be_nil
i.backend.must_be_kind_of Train::Transports::Mock::Connection
end
it 'prints a nice to_s' do
load('inspec').to_s.must_equal 'Inspec::Backend::Class'
end
it 'prints a nice inspect line' do
load('inspec').inspect.must_equal 'Inspec::Backend::Class @transport=Train::Transports::Mock::Connection'
end
describe 'inspec.profile.files' do
it 'lists an empty array when calling #files without any files loaded' do
load('inspec.profile.files').must_equal([])
end
it 'lists all profile files when calling #files' do
load_in_profile('inspec.profile.files').must_equal %w{a_sub_dir/sub_items.conf items.conf}
end
end
describe 'inspec.profile.file' do
it 'raises an error if a file was not found' do
proc { load('inspec.profile.file("test")') }.must_raise RuntimeError
end
it 'provides file contents when calling file(...)' do
load_in_profile('inspec.profile.file("items.conf")').must_equal "one\ntwo\nthree\n"
end
end
end