diff --git a/lib/cover_me/results.rb b/lib/cover_me/results.rb index efa0ad1..6d37c8e 100644 --- a/lib/cover_me/results.rb +++ b/lib/cover_me/results.rb @@ -6,7 +6,12 @@ class << self def read_results(path = CoverMe.config.results.store) data = {} if File.exists?(path) - data = eval(File.read(path)) || {} + data = File.read(path, {:encoding => 'ASCII-8BIT', :mode => 'r'}) + begin + data = Marshal.load(data) + rescue + data = {} + end end return data end @@ -31,14 +36,15 @@ def merge_results!(cov_results, path = CoverMe.config.results.store) end end - File.open(path, 'w') do |f| - f.write(data.inspect) + File.open(path, {:encoding => 'ASCII-8BIT', :mode => 'w'}) do |f| + data = Marshal.dump(data) + f.write(data) end - + return data end end end # Results -end # CoverMe \ No newline at end of file +end # CoverMe diff --git a/spec/cover_me/results_spec.rb b/spec/cover_me/results_spec.rb index a83f3f9..d2c2e87 100644 --- a/spec/cover_me/results_spec.rb +++ b/spec/cover_me/results_spec.rb @@ -8,7 +8,7 @@ res = {'file1' => [0, nil, 1], 'file2' => [nil, 1, nil]} File.open(CoverMe.config.results.store, 'w') do |f| - f.write(res.inspect) + f.write(Marshal.dump(res)) end end @@ -31,7 +31,14 @@ res.should be_kind_of(Hash) res.should be_empty end - + + it "should return empty results if reading the file fails with a Syntax error" do + File.stub!(:read).and_return('{garbage}') + res = CoverMe::Results.read_results + res.should be_kind_of(Hash) + res.should be_empty + end + end describe "merge_results!" do @@ -46,16 +53,16 @@ res.should be_kind_of(Hash) res.should == {'file1' => [0, 1, 2], 'file2' => [nil, 1, 0]} - File.read(CoverMe.config.results.store).should == res.inspect + File.read(CoverMe.config.results.store).should == Marshal.dump(res) end it "should merge the results and create a new file if there isn't one" do res = CoverMe::Results.merge_results!(@more_results, @idontexist_path) res.should be_kind_of(Hash) res.should == @more_results - File.read(@idontexist_path).should == res.inspect + File.read(@idontexist_path).should == Marshal.dump(res) end - + end end