From c69cb4165b80569ec61677fe5d7d5cb3f1470f26 Mon Sep 17 00:00:00 2001 From: Cole Thompson Date: Sat, 11 Oct 2014 13:11:54 -0700 Subject: [PATCH 1/9] use separate temporary files to store state --- lib/chicanery/persistence.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/chicanery/persistence.rb b/lib/chicanery/persistence.rb index d2bdf03..29bdb9f 100644 --- a/lib/chicanery/persistence.rb +++ b/lib/chicanery/persistence.rb @@ -15,7 +15,7 @@ def restore def persist_state_to path=nil @state = path if path - @state || 'state' + @state || Tempfile.new('state').path end end -end \ No newline at end of file +end From fdc6be0b767b0c0c63bfc31ab75563c6efb5e33d Mon Sep 17 00:00:00 2001 From: Cole Thompson Date: Sat, 11 Oct 2014 13:26:18 -0700 Subject: [PATCH 2/9] forgot to require it --- lib/chicanery/persistence.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/chicanery/persistence.rb b/lib/chicanery/persistence.rb index 29bdb9f..3b58c0b 100644 --- a/lib/chicanery/persistence.rb +++ b/lib/chicanery/persistence.rb @@ -1,3 +1,4 @@ +require 'tempfile' require 'yaml' module Chicanery From f418000602f80c7aa06ecb2a54c7936bd7b3323c Mon Sep 17 00:00:00 2001 From: Cole Thompson Date: Sat, 11 Oct 2014 15:13:33 -0700 Subject: [PATCH 3/9] fix specs --- lib/chicanery/persistence.rb | 2 +- spec/chicanery/collections_spec.rb | 4 +++- spec/chicanery/persistence_spec.rb | 15 ++++++++++----- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/lib/chicanery/persistence.rb b/lib/chicanery/persistence.rb index 3b58c0b..457baa2 100644 --- a/lib/chicanery/persistence.rb +++ b/lib/chicanery/persistence.rb @@ -16,7 +16,7 @@ def restore def persist_state_to path=nil @state = path if path - @state || Tempfile.new('state').path + @state ||= Tempfile.new('state').path end end end diff --git a/spec/chicanery/collections_spec.rb b/spec/chicanery/collections_spec.rb index d72c0bf..4d2e60b 100644 --- a/spec/chicanery/collections_spec.rb +++ b/spec/chicanery/collections_spec.rb @@ -1,3 +1,5 @@ +require 'chicanery' + describe Chicanery::Collections do include Chicanery::Collections @@ -11,4 +13,4 @@ send("#{entity}s").should == [:entity] end end -end \ No newline at end of file +end diff --git a/spec/chicanery/persistence_spec.rb b/spec/chicanery/persistence_spec.rb index 7b1d145..16e56f3 100644 --- a/spec/chicanery/persistence_spec.rb +++ b/spec/chicanery/persistence_spec.rb @@ -3,10 +3,15 @@ let(:file) { double 'file' } let(:state) { double 'state', to_yaml: :yaml } + let(:state_regex) { /.*\/state.*/ } describe '#persist' do it 'should write state to disk as yaml' do - File.should_receive(:open).with('state', 'w').and_yield file + File.should_receive(:open).with(state_regex, 'w').and_yield file + + # fixme? internal Tempfile call? + File.should_receive(:open).with(anything, anything, anything) + file.should_receive(:puts).with :yaml persist state end @@ -21,13 +26,13 @@ describe '#restore' do it 'should return empty hash if state file does not exist' do - File.should_receive(:exist?).with('state').and_return false + File.should_receive(:exist?).with(state_regex).and_return false restore.should == {} end it 'should read yaml from disk' do - File.should_receive(:exist?).with('state').and_return true - YAML.should_receive(:load_file).with('state').and_return state + File.should_receive(:exist?).with(state_regex).and_return true + YAML.should_receive(:load_file).with(state_regex).and_return state restore.should == state end @@ -38,4 +43,4 @@ restore.should == state end end -end \ No newline at end of file +end From 4e2711b8f2f30f7078d104c43978cd3d6edeb384 Mon Sep 17 00:00:00 2001 From: Cole Thompson Date: Sat, 11 Oct 2014 15:25:50 -0700 Subject: [PATCH 4/9] don't use an exotic location for temp files --- lib/chicanery/persistence.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/chicanery/persistence.rb b/lib/chicanery/persistence.rb index 457baa2..e23cd18 100644 --- a/lib/chicanery/persistence.rb +++ b/lib/chicanery/persistence.rb @@ -16,7 +16,7 @@ def restore def persist_state_to path=nil @state = path if path - @state ||= Tempfile.new('state').path + @state ||= Tempfile.new('state', '.').path end end end From a7323b6da268eccef02b4384ea2e274d3d476842 Mon Sep 17 00:00:00 2001 From: Cole Thompson Date: Sat, 11 Oct 2014 15:28:59 -0700 Subject: [PATCH 5/9] use just the basename --- lib/chicanery/persistence.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/chicanery/persistence.rb b/lib/chicanery/persistence.rb index e23cd18..07ed813 100644 --- a/lib/chicanery/persistence.rb +++ b/lib/chicanery/persistence.rb @@ -16,7 +16,7 @@ def restore def persist_state_to path=nil @state = path if path - @state ||= Tempfile.new('state', '.').path + @state ||= File.basename(Tempfile.new 'state', '.') end end end From 8eb67e69b18c39d71db8a571a653d64fb0e06daa Mon Sep 17 00:00:00 2001 From: Cole Thompson Date: Sat, 11 Oct 2014 15:33:10 -0700 Subject: [PATCH 6/9] try a diff approach --- lib/chicanery/persistence.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/chicanery/persistence.rb b/lib/chicanery/persistence.rb index 07ed813..b6a79ae 100644 --- a/lib/chicanery/persistence.rb +++ b/lib/chicanery/persistence.rb @@ -16,7 +16,7 @@ def restore def persist_state_to path=nil @state = path if path - @state ||= File.basename(Tempfile.new 'state', '.') + @state ||= Dir::Tmpname.make_tmpname './state' end end end From eeef93bdd54d2b8343cc8cd228321ba7b4ecf3c1 Mon Sep 17 00:00:00 2001 From: Cole Thompson Date: Sat, 11 Oct 2014 15:33:41 -0700 Subject: [PATCH 7/9] oops --- lib/chicanery/persistence.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/chicanery/persistence.rb b/lib/chicanery/persistence.rb index b6a79ae..51356e5 100644 --- a/lib/chicanery/persistence.rb +++ b/lib/chicanery/persistence.rb @@ -16,7 +16,7 @@ def restore def persist_state_to path=nil @state = path if path - @state ||= Dir::Tmpname.make_tmpname './state' + @state ||= Dir::Tmpname.make_tmpname './state', nil end end end From 9c77368a4ee8c00369038ae80d0941610c5947fa Mon Sep 17 00:00:00 2001 From: Cole Thompson Date: Sat, 11 Oct 2014 15:45:18 -0700 Subject: [PATCH 8/9] put temp files in a subdir --- lib/chicanery/persistence.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/chicanery/persistence.rb b/lib/chicanery/persistence.rb index 51356e5..67261ca 100644 --- a/lib/chicanery/persistence.rb +++ b/lib/chicanery/persistence.rb @@ -3,6 +3,8 @@ module Chicanery module Persistence + TEMP_DIR = './tmp' + def persist state File.open persist_state_to, 'w' do |file| file.puts state.to_yaml @@ -16,7 +18,8 @@ def restore def persist_state_to path=nil @state = path if path - @state ||= Dir::Tmpname.make_tmpname './state', nil + Dir.mkdir TEMP_DIR if not Dir.exist? TEMP_DIR + @state ||= Dir::Tmpname.make_tmpname "#{TEMP_DIR}/state", nil end end end From 09e9d0beb7e0135cd77a954dba9d92397bb4c836 Mon Sep 17 00:00:00 2001 From: Cole Thompson Date: Sat, 11 Oct 2014 15:48:41 -0700 Subject: [PATCH 9/9] fix spec --- spec/chicanery/persistence_spec.rb | 4 ---- 1 file changed, 4 deletions(-) diff --git a/spec/chicanery/persistence_spec.rb b/spec/chicanery/persistence_spec.rb index 16e56f3..2bd0586 100644 --- a/spec/chicanery/persistence_spec.rb +++ b/spec/chicanery/persistence_spec.rb @@ -8,10 +8,6 @@ describe '#persist' do it 'should write state to disk as yaml' do File.should_receive(:open).with(state_regex, 'w').and_yield file - - # fixme? internal Tempfile call? - File.should_receive(:open).with(anything, anything, anything) - file.should_receive(:puts).with :yaml persist state end