mirror of
https://github.com/inspec/inspec
synced 2024-11-23 13:13:22 +00:00
add local fetcher
This commit is contained in:
parent
27f7aa7796
commit
a83e29cc01
4 changed files with 110 additions and 0 deletions
31
lib/fetchers/local.rb
Normal file
31
lib/fetchers/local.rb
Normal file
|
@ -0,0 +1,31 @@
|
|||
# encoding: utf-8
|
||||
# author: Dominik Richter
|
||||
# author: Christoph Hartmann
|
||||
|
||||
module Fetchers
|
||||
class Local < Inspec.fetcher(1)
|
||||
name 'local'
|
||||
priority 0
|
||||
|
||||
attr_reader :files
|
||||
|
||||
def self.resolve(target)
|
||||
return nil unless File.exist?(target)
|
||||
new(target)
|
||||
end
|
||||
|
||||
def initialize(target)
|
||||
if File.file?(target)
|
||||
@files = [target]
|
||||
else
|
||||
@files = Dir[File.join(target, '**', '*')]
|
||||
end
|
||||
end
|
||||
|
||||
def read(file)
|
||||
return nil unless files.include?(file)
|
||||
return nil unless File.file?(file)
|
||||
File.read(file)
|
||||
end
|
||||
end
|
||||
end
|
|
@ -35,3 +35,5 @@ module Inspec
|
|||
Inspec::Plugins::Fetcher
|
||||
end
|
||||
end
|
||||
|
||||
require 'fetchers/local'
|
||||
|
|
12
test/unit/fetchers.rb
Normal file
12
test/unit/fetchers.rb
Normal file
|
@ -0,0 +1,12 @@
|
|||
# encoding: utf-8
|
||||
# author: Dominik Richter
|
||||
# author: Christoph Hartmann
|
||||
|
||||
require 'helper'
|
||||
|
||||
describe Inspec::Fetcher do
|
||||
it 'loads the local fetcher for this file' do
|
||||
res = Inspec::Fetcher.resolve(__FILE__)
|
||||
res.must_be_kind_of Fetchers::Local
|
||||
end
|
||||
end
|
65
test/unit/fetchers/local_test.rb
Normal file
65
test/unit/fetchers/local_test.rb
Normal file
|
@ -0,0 +1,65 @@
|
|||
# encoding: utf-8
|
||||
# author: Dominik Richter
|
||||
# author: Christoph Hartmann
|
||||
|
||||
require 'helper'
|
||||
|
||||
describe Fetchers::Local do
|
||||
let(:fetcher) { Fetchers::Local }
|
||||
|
||||
it 'registers with the fetchers registry' do
|
||||
reg = Inspec::Fetcher.registry
|
||||
_(reg['local']).must_equal fetcher
|
||||
end
|
||||
|
||||
describe 'applied to this file' do
|
||||
let(:res) { fetcher.resolve(__FILE__) }
|
||||
|
||||
it 'must be resolved' do
|
||||
_(res).must_be_kind_of fetcher
|
||||
end
|
||||
|
||||
it 'must only contain this file' do
|
||||
_(res.files).must_equal [__FILE__]
|
||||
end
|
||||
|
||||
it 'must not read if the file doesnt exist' do
|
||||
_(res.read(rand.to_s)).must_be_nil
|
||||
end
|
||||
|
||||
it 'must not read files not covered' do
|
||||
_(File.file?('/proc/cpuinfo')).must_equal true
|
||||
_(res.read('/proc/cpuinfo')).must_be_nil
|
||||
end
|
||||
|
||||
it 'must read the contents of the file' do
|
||||
_(res.read(__FILE__)).must_equal File.read(__FILE__)
|
||||
end
|
||||
end
|
||||
|
||||
describe 'applied to this folder' do
|
||||
let(:path) { File.dirname(__FILE__) }
|
||||
let(:res) { fetcher.resolve(path) }
|
||||
|
||||
it 'must be resolved' do
|
||||
_(res).must_be_kind_of fetcher
|
||||
end
|
||||
|
||||
it 'must contain all files' do
|
||||
_(res.files).must_include __FILE__
|
||||
end
|
||||
|
||||
it 'must not read if the file doesnt exist' do
|
||||
_(res.read(rand.to_s)).must_be_nil
|
||||
end
|
||||
|
||||
it 'must not read files not covered' do
|
||||
_(File.file?('/proc/cpuinfo')).must_equal true
|
||||
_(res.read('/proc/cpuinfo')).must_be_nil
|
||||
end
|
||||
|
||||
it 'must read the contents of the file' do
|
||||
_(res.read(__FILE__)).must_equal File.read(__FILE__)
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue