From a83e29cc01c2622cb0e4fe22d9c7199e091da252 Mon Sep 17 00:00:00 2001 From: Dominik Richter Date: Sun, 21 Feb 2016 02:02:39 +0100 Subject: [PATCH] add local fetcher --- lib/fetchers/local.rb | 31 +++++++++++++++ lib/inspec/fetcher.rb | 2 + test/unit/fetchers.rb | 12 ++++++ test/unit/fetchers/local_test.rb | 65 ++++++++++++++++++++++++++++++++ 4 files changed, 110 insertions(+) create mode 100644 lib/fetchers/local.rb create mode 100644 test/unit/fetchers.rb create mode 100644 test/unit/fetchers/local_test.rb diff --git a/lib/fetchers/local.rb b/lib/fetchers/local.rb new file mode 100644 index 000000000..9312138ab --- /dev/null +++ b/lib/fetchers/local.rb @@ -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 diff --git a/lib/inspec/fetcher.rb b/lib/inspec/fetcher.rb index c2539b7ab..0a30a1e2a 100644 --- a/lib/inspec/fetcher.rb +++ b/lib/inspec/fetcher.rb @@ -35,3 +35,5 @@ module Inspec Inspec::Plugins::Fetcher end end + +require 'fetchers/local' diff --git a/test/unit/fetchers.rb b/test/unit/fetchers.rb new file mode 100644 index 000000000..d7e90b521 --- /dev/null +++ b/test/unit/fetchers.rb @@ -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 diff --git a/test/unit/fetchers/local_test.rb b/test/unit/fetchers/local_test.rb new file mode 100644 index 000000000..36612ba5e --- /dev/null +++ b/test/unit/fetchers/local_test.rb @@ -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