Skip to content

Instantly share code, notes, and snippets.

@vStone
Created August 22, 2016 08:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vStone/4e269150f6fca2727ecbc501877878d6 to your computer and use it in GitHub Desktop.
Save vStone/4e269150f6fca2727ecbc501877878d6 to your computer and use it in GitHub Desktop.
Use external facter for unit testing facts: spec/helpers/external_facter_helper.rb
require 'yaml'
module RSpecHelper
module SystemFacter
def self.resolve_fact (fact, stubs = {}, facter_path = (ENV['FACTER_PATH'] || '/usr/bin/facter'))
env_values = {}
command = [ facter_path, '--yaml']
facts_path = File.expand_path(File.dirname(__FILE__) + '/../../lib/facter')
stubs.each do |key,value|
env_values["FACTER_#{key}"] = value
end
env_values['FACTERLIB'] = facts_path
command += [ fact ]
result_yaml = ''
IO.popen(env_values, command.flatten) do |io|
result_yaml = io.read
end
result_hash = YAML.load(result_yaml)
result_hash[fact]
end
end
end
if RSpecHelper::SystemFacter.resolve_fact('facterversion') !~ /^3/
raise "Wrong facter system version detected: 'RSpecHelper::SystemFacter.resolve_fact('facterversion')"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment