diff --git a/lib/compliance_engine/data_loader/file.rb b/lib/compliance_engine/data_loader/file.rb index 06b98af..07a662e 100644 --- a/lib/compliance_engine/data_loader/file.rb +++ b/lib/compliance_engine/data_loader/file.rb @@ -14,12 +14,12 @@ def initialize(file, fileclass: ::File, key: file) end def refresh - size = @fileclass.size(@filename) - mtime = @fileclass.mtime(@filename) - return if size == @size && mtime == @mtime + newsize = @fileclass.size(@filename) + newmtime = @fileclass.mtime(@filename) + return if newsize == @size && newmtime == @mtime - @size = size - @mtime = mtime + @size = newsize + @mtime = newmtime self.data = parse(@fileclass.read(@filename)) end diff --git a/spec/classes/compliance_engine/data_loader/file_spec.rb b/spec/classes/compliance_engine/data_loader/file_spec.rb new file mode 100644 index 0000000..ff63665 --- /dev/null +++ b/spec/classes/compliance_engine/data_loader/file_spec.rb @@ -0,0 +1,23 @@ +# frozen_string_literal: true + +require 'spec_helper' +require 'compliance_engine/data_loader/file' + +RSpec.describe ComplianceEngine::DataLoader::File do + let(:filename) { '/path/to/file' } + + before(:each) do + allow(File).to receive(:size).and_call_original + allow(File).to receive(:size).with(filename).and_return(0) + + allow(File).to receive(:mtime).and_call_original + allow(File).to receive(:mtime).with(filename).and_return(Time.now) + + allow(File).to receive(:read).and_call_original + allow(File).to receive(:read).with(filename).and_return("\n") + end + + it 'fails to initialize' do + expect { described_class.new(filename) }.to raise_error(NoMethodError) + end +end diff --git a/spec/classes/compliance_engine/data_loader/json_spec.rb b/spec/classes/compliance_engine/data_loader/json_spec.rb index 14798f7..c21e98c 100644 --- a/spec/classes/compliance_engine/data_loader/json_spec.rb +++ b/spec/classes/compliance_engine/data_loader/json_spec.rb @@ -5,6 +5,8 @@ RSpec.describe ComplianceEngine::DataLoader::Json do before(:each) do + allow(File).to receive(:size).and_call_original + allow(File).to receive(:mtime).and_call_original allow(File).to receive(:read).and_call_original end @@ -23,6 +25,9 @@ shared_examples 'an observable' do it 'updates the data' do + data_loader.instance_variable_set(:@size, 0) + data_loader.instance_variable_set(:@mtime, Time.now) + data_loader.data = initial_data data_loader.refresh expect(data_loader.data).to eq(updated_data) end @@ -40,6 +45,10 @@ def update(data) attr_accessor :data end + data_loader.instance_variable_set(:@size, 0) + data_loader.instance_variable_set(:@mtime, Time.now) + data_loader.data = initial_data + observer = ExampleObserver.new(data_loader) data_loader.refresh expect(observer.data).to be_instance_of(described_class) @@ -60,6 +69,8 @@ def update(data) end before(:each) do + allow(File).to receive(:size).with(filename).and_return(0) + allow(File).to receive(:mtime).with(filename).and_return(Time.now) allow(File).to receive(:read).with(filename).and_return(initial_data.to_json) end @@ -67,6 +78,8 @@ def update(data) context 'with updated data' do before(:each) do + allow(File).to receive(:size).with(filename).and_return(1) + allow(File).to receive(:mtime).with(filename).and_return(Time.now + 1) allow(File).to receive(:read).with(filename).and_return(updated_data.to_json) end @@ -87,6 +100,8 @@ def update(data) end before(:each) do + allow(File).to receive(:size).with(filename).and_return(0) + allow(File).to receive(:mtime).with(filename).and_return(Time.now) allow(File).to receive(:read).with(filename).and_return(initial_data.to_json) end @@ -94,6 +109,8 @@ def update(data) context 'with updated data' do before(:each) do + allow(File).to receive(:size).with(filename).and_return(1) + allow(File).to receive(:mtime).with(filename).and_return(Time.now + 1) allow(File).to receive(:read).with(filename).and_return(updated_data.to_json) end @@ -114,6 +131,8 @@ def update(data) end before(:each) do + allow(File).to receive(:size).with(filename).and_return(0) + allow(File).to receive(:mtime).with(filename).and_return(Time.now) allow(File).to receive(:read).with(filename).and_return(initial_data.to_json) end @@ -121,6 +140,8 @@ def update(data) context 'with updated data' do before(:each) do + allow(File).to receive(:size).with(filename).and_return(1) + allow(File).to receive(:mtime).with(filename).and_return(Time.now + 1) allow(File).to receive(:read).with(filename).and_return(updated_data.to_json) end @@ -133,6 +154,8 @@ def update(data) let(:data) { 'invalid data' } before(:each) do + allow(File).to receive(:size).with(filename).and_return(0) + allow(File).to receive(:mtime).with(filename).and_return(Time.now) allow(File).to receive(:read).with(filename).and_return(data.to_json) end @@ -147,12 +170,16 @@ def update(data) let(:updated_data) { 'invalid data' } before(:each) do + allow(File).to receive(:size).with(filename).and_return(0) + allow(File).to receive(:mtime).with(filename).and_return(Time.now) allow(File).to receive(:read).with(filename).and_return(initial_data.to_json) end it 'does not update the data' do loader = described_class.new(filename) + allow(File).to receive(:size).with(filename).and_return(1) + allow(File).to receive(:mtime).with(filename).and_return(Time.now + 1) allow(File).to receive(:read).with(filename).and_return(updated_data.to_json) expect { loader.refresh }.to raise_error(ComplianceEngine::Error, 'Data must be a hash') diff --git a/spec/classes/compliance_engine/data_loader/yaml_spec.rb b/spec/classes/compliance_engine/data_loader/yaml_spec.rb index e20e207..3e3f6d9 100644 --- a/spec/classes/compliance_engine/data_loader/yaml_spec.rb +++ b/spec/classes/compliance_engine/data_loader/yaml_spec.rb @@ -5,6 +5,8 @@ RSpec.describe ComplianceEngine::DataLoader::Yaml do before(:each) do + allow(File).to receive(:size).and_call_original + allow(File).to receive(:mtime).and_call_original allow(File).to receive(:read).and_call_original end @@ -23,6 +25,9 @@ shared_examples 'an observable' do it 'updates the data' do + data_loader.instance_variable_set(:@size, 0) + data_loader.instance_variable_set(:@mtime, Time.now) + data_loader.data = initial_data data_loader.refresh expect(data_loader.data).to eq(updated_data) end @@ -40,6 +45,10 @@ def update(data) attr_accessor :data end + data_loader.instance_variable_set(:@size, 0) + data_loader.instance_variable_set(:@mtime, Time.now) + data_loader.data = initial_data + observer = ExampleObserver.new(data_loader) data_loader.refresh expect(observer.data).to be_instance_of(described_class) @@ -60,6 +69,8 @@ def update(data) end before(:each) do + allow(File).to receive(:size).with(filename).and_return(0) + allow(File).to receive(:mtime).with(filename).and_return(Time.now) allow(File).to receive(:read).with(filename).and_return(initial_data.to_yaml) end @@ -67,6 +78,8 @@ def update(data) context 'with updated data' do before(:each) do + allow(File).to receive(:size).with(filename).and_return(1) + allow(File).to receive(:mtime).with(filename).and_return(Time.now + 1) allow(File).to receive(:read).with(filename).and_return(updated_data.to_yaml) end @@ -87,6 +100,8 @@ def update(data) end before(:each) do + allow(File).to receive(:size).with(filename).and_return(0) + allow(File).to receive(:mtime).with(filename).and_return(Time.now) allow(File).to receive(:read).with(filename).and_return(initial_data.to_yaml) end @@ -94,6 +109,8 @@ def update(data) context 'with updated data' do before(:each) do + allow(File).to receive(:size).with(filename).and_return(1) + allow(File).to receive(:mtime).with(filename).and_return(Time.now + 1) allow(File).to receive(:read).with(filename).and_return(updated_data.to_yaml) end @@ -114,6 +131,8 @@ def update(data) end before(:each) do + allow(File).to receive(:size).with(filename).and_return(0) + allow(File).to receive(:mtime).with(filename).and_return(Time.now) allow(File).to receive(:read).with(filename).and_return(initial_data.to_yaml) end @@ -121,6 +140,8 @@ def update(data) context 'with updated data' do before(:each) do + allow(File).to receive(:size).with(filename).and_return(1) + allow(File).to receive(:mtime).with(filename).and_return(Time.now + 1) allow(File).to receive(:read).with(filename).and_return(updated_data.to_yaml) end @@ -133,6 +154,8 @@ def update(data) let(:data) { 'invalid data' } before(:each) do + allow(File).to receive(:size).with(filename).and_return(0) + allow(File).to receive(:mtime).with(filename).and_return(Time.now) allow(File).to receive(:read).with(filename).and_return(data.to_yaml) end @@ -147,12 +170,16 @@ def update(data) let(:updated_data) { 'invalid data' } before(:each) do + allow(File).to receive(:size).with(filename).and_return(0) + allow(File).to receive(:mtime).with(filename).and_return(Time.now) allow(File).to receive(:read).with(filename).and_return(initial_data.to_yaml) end it 'does not update the data' do loader = described_class.new(filename) + allow(File).to receive(:size).with(filename).and_return(1) + allow(File).to receive(:mtime).with(filename).and_return(Time.now + 1) allow(File).to receive(:read).with(filename).and_return(updated_data.to_yaml) expect { loader.refresh }.to raise_error(ComplianceEngine::Error, 'Data must be a hash')