From cde701c3884bbbd469c497f4518ab1480321912e Mon Sep 17 00:00:00 2001 From: Glenn Sarti Date: Mon, 26 Mar 2018 14:53:33 +0800 Subject: [PATCH] (#78) Add tests for facter version Previously many factset files were in the wrong directory which made maintenance and debugging difficult. This commit adds tests to ensure that the factset files are indeed JSON parsable and that the facter version in the file matches the directory it is located in. This test is maked as skipped until all of the bad factset files can be changed. --- spec/facts_spec.rb | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/spec/facts_spec.rb b/spec/facts_spec.rb index 8968d639..71303136 100644 --- a/spec/facts_spec.rb +++ b/spec/facts_spec.rb @@ -11,6 +11,34 @@ end end +RSpec::Matchers.define :be_valid_json do + match do |actual| + content = File.open(actual, 'rb') { |file| file.read } + valid = false + begin + obj = JSON.parse(content) + valid = true + rescue JSON::ParserError => e + raise "Invalid JSON file #{actual}.\n#{e}" + end + end + + failure_message do |actual| + "expected that fact file #{actual} was a valid JSON file." + end +end + +RSpec::Matchers.define :have_facter_version do |expected_facter_version, filepath| + match do |actual| + # Simple but naive regex check + actual =~ /^#{expected_facter_version}($|\.)/ + end + + failure_message do |actual| + "expected that fact file #{filepath} with facter version #{actual} had a facter version that matched #{expected_facter_version}" + end +end + describe 'Default Facts' do before(:each) do ENV['FACTERDB_SKIP_DEFAULTDB'] = nil @@ -31,5 +59,23 @@ expect(file_hashes[file_hash]).to have_a_unique_hash end end + + it 'should contain fact sets which match are valid JSON' do + FacterDB.default_fact_files.each do |filepath| + expect(filepath).to be_valid_json + end + end + + xit 'should contain fact sets which match the facter version' do + FacterDB.default_fact_files.each do |filepath| + facter_dir_path = File.basename(File.dirname(filepath)) + + content = File.open(filepath, 'rb') { |file| file.read } + obj = JSON.parse(content) + file_facter_version = obj['facterversion'] + + expect(file_facter_version).to have_facter_version(facter_dir_path, filepath) + end + end end end