From 426a3652050394a803b5cfd07b4ae5ea37552b7f Mon Sep 17 00:00:00 2001 From: Mikel Lindsaar Date: Fri, 12 Apr 2013 11:49:13 +1000 Subject: [PATCH 1/2] Adding fix to return empty data if coverage.data has a syntax error --- lib/cover_me/results.rb | 12 ++++++++---- spec/cover_me/results_spec.rb | 11 +++++++++-- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/lib/cover_me/results.rb b/lib/cover_me/results.rb index efa0ad1..82dde74 100644 --- a/lib/cover_me/results.rb +++ b/lib/cover_me/results.rb @@ -6,7 +6,11 @@ class << self def read_results(path = CoverMe.config.results.store) data = {} if File.exists?(path) - data = eval(File.read(path)) || {} + begin + data = eval(File.read(path)) || {} + rescue SyntaxError + data = {} + end end return data end @@ -30,15 +34,15 @@ def merge_results!(cov_results, path = CoverMe.config.results.store) data[file] = results end end - + File.open(path, 'w') do |f| f.write(data.inspect) 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..4dea801 100644 --- a/spec/cover_me/results_spec.rb +++ b/spec/cover_me/results_spec.rb @@ -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 @@ -55,7 +62,7 @@ res.should == @more_results File.read(@idontexist_path).should == res.inspect end - + end end From 76140174648bdda80244e37ff0252ac54a760a7e Mon Sep 17 00:00:00 2001 From: Mikel Lindsaar Date: Fri, 12 Apr 2013 11:59:16 +1000 Subject: [PATCH 2/2] Change from using eval to parse coverage data to Marshal --- lib/cover_me/results.rb | 12 +++++++----- spec/cover_me/results_spec.rb | 6 +++--- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/lib/cover_me/results.rb b/lib/cover_me/results.rb index 82dde74..6d37c8e 100644 --- a/lib/cover_me/results.rb +++ b/lib/cover_me/results.rb @@ -6,9 +6,10 @@ class << self def read_results(path = CoverMe.config.results.store) data = {} if File.exists?(path) + data = File.read(path, {:encoding => 'ASCII-8BIT', :mode => 'r'}) begin - data = eval(File.read(path)) || {} - rescue SyntaxError + data = Marshal.load(data) + rescue data = {} end end @@ -34,9 +35,10 @@ def merge_results!(cov_results, path = CoverMe.config.results.store) data[file] = results 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 diff --git a/spec/cover_me/results_spec.rb b/spec/cover_me/results_spec.rb index 4dea801..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 @@ -53,14 +53,14 @@ 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