From 68acb59bf77d8d818489b1e2a97491cb7f327a0a Mon Sep 17 00:00:00 2001 From: Ashley Penney Date: Tue, 22 Apr 2014 23:14:16 +0200 Subject: [PATCH] Adjust the regular expression for facts. Previously this was incorrectly handling facts that were of the form foo=1+1=2 due to the ='s in the actual fact contents. Fix this and add tests to try and prevent regressions. --- lib/facter/facter_dot_d.rb | 2 +- spec/unit/facter/facter_dot_d_spec.rb | 31 +++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 spec/unit/facter/facter_dot_d_spec.rb diff --git a/lib/facter/facter_dot_d.rb b/lib/facter/facter_dot_d.rb index e414b2077..2c096b049 100644 --- a/lib/facter/facter_dot_d.rb +++ b/lib/facter/facter_dot_d.rb @@ -40,7 +40,7 @@ def fact_type(file) def txt_parser(file) File.readlines(file).each do |line| - if line =~ /^(.+)=(.+)$/ + if line =~ /^([^=]+)=(.+)$/ var = $1; val = $2 Facter.add(var) do diff --git a/spec/unit/facter/facter_dot_d_spec.rb b/spec/unit/facter/facter_dot_d_spec.rb new file mode 100644 index 000000000..1ecffc85a --- /dev/null +++ b/spec/unit/facter/facter_dot_d_spec.rb @@ -0,0 +1,31 @@ +require 'spec_helper' +require 'facter/facter_dot_d' + +describe Facter::Util::DotD do + + context 'returns a simple fact' do + before :each do + Facter.stubs(:version).returns('1.6.1') + subject.stubs(:entries).returns(['/etc/facter/facts.d/fake_fact.txt']) + File.stubs(:readlines).with('/etc/facter/facts.d/fake_fact.txt').returns(['fake_fact=fake fact']) + subject.create + end + + it 'should return successfully' do + Facter.fact(:fake_fact).value.should == 'fake fact' + end + end + + context 'returns a fact with equals signs' do + before :each do + Facter.stubs(:version).returns('1.6.1') + subject.stubs(:entries).returns(['/etc/facter/facts.d/foo.txt']) + File.stubs(:readlines).with('/etc/facter/facts.d/foo.txt').returns(['foo=1+1=2']) + subject.create + end + + it 'should return successfully' do + Facter.fact(:foo).value.should == '1+1=2' + end + end +end