mirror of
https://github.com/inspec/inspec
synced 2024-11-10 23:24:18 +00:00
add csv support
This commit is contained in:
parent
b9d4fc6d8c
commit
cf3dddf1a3
5 changed files with 50 additions and 3 deletions
26
lib/resources/csv.rb
Normal file
26
lib/resources/csv.rb
Normal file
|
@ -0,0 +1,26 @@
|
|||
# encoding: utf-8
|
||||
|
||||
# Parses a csv document
|
||||
# Usage:
|
||||
# describe csv('example.csv') do
|
||||
# its('name') { should eq(['John', 'Alice']) }
|
||||
# end
|
||||
#
|
||||
# This implementation was inspired by a blog post
|
||||
# @see http://technicalpickles.com/posts/parsing-csv-with-ruby
|
||||
class CsvConfig < JsonConfig
|
||||
name 'csv'
|
||||
|
||||
# override file load and parse hash from csv
|
||||
def parse(content)
|
||||
require 'csv'
|
||||
# convert empty field to nil
|
||||
CSV::Converters[:blank_to_nil] = lambda do |field|
|
||||
field && field.empty? ? nil : field
|
||||
end
|
||||
# implicit conversion of values
|
||||
csv = CSV.new(content, headers: true, converters: [:all, :blank_to_nil])
|
||||
# convert to hash
|
||||
csv.to_a.map(&:to_hash)
|
||||
end
|
||||
end
|
|
@ -1,7 +1,5 @@
|
|||
# encoding: utf-8
|
||||
|
||||
require 'json'
|
||||
|
||||
# Parses a json document
|
||||
# Usage:
|
||||
# describe yaml('policyfile.lock.json') do
|
||||
|
@ -20,6 +18,7 @@ class JsonConfig < Vulcano.resource(1)
|
|||
end
|
||||
|
||||
def parse(content)
|
||||
require 'json'
|
||||
JSON.parse(content, object_class: OpenStruct)
|
||||
end
|
||||
|
||||
|
|
|
@ -56,5 +56,9 @@ require 'resources/security_policy'
|
|||
require 'resources/service'
|
||||
require 'resources/ssh_conf'
|
||||
require 'resources/windows_feature'
|
||||
require 'resources/yaml'
|
||||
require 'resources/yum'
|
||||
|
||||
# file formats, depend on json implementation
|
||||
require 'resources/json'
|
||||
require 'resources/yaml'
|
||||
require 'resources/csv'
|
||||
|
|
|
@ -45,6 +45,7 @@ def loadResource (resource, *args)
|
|||
'/etc/mysql/my.cnf' => mockfile.('mysql.conf'),
|
||||
'/etc/mysql/mysql2.conf' => mockfile.('mysql2.conf'),
|
||||
'kitchen.yml' => mockfile.('kitchen.yml'),
|
||||
'example.csv' => mockfile.('example.csv'),
|
||||
'policyfile.lock.json' => mockfile.('policyfile.lock.json'),
|
||||
}
|
||||
|
||||
|
|
17
test/unit/resource_csv_test.rb
Normal file
17
test/unit/resource_csv_test.rb
Normal file
|
@ -0,0 +1,17 @@
|
|||
# encoding: utf-8
|
||||
|
||||
require 'helper'
|
||||
require 'vulcano/resource'
|
||||
|
||||
describe 'Vulcano:Resources::CSV' do
|
||||
describe 'cars' do
|
||||
|
||||
let(:resource) { loadResource('csv', 'example.csv') }
|
||||
|
||||
it 'verify csv parsing' do
|
||||
_(resource.params).wont_equal nil
|
||||
_(resource.send('1.addressable')).must_equal 'astrolabe'
|
||||
_(resource.send('addressable')).must_equal %w{ast astrolabe berkshelf}
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue