From ad5b3c835f6de90ee3d31734c6a74d2c40a595bc Mon Sep 17 00:00:00 2001 From: Chad Shaffer Date: Fri, 4 Nov 2016 12:34:56 -0700 Subject: [PATCH] Allow for empty last_run files for parallel tests that end up running no tests --- CHANGELOG.md | 2 ++ lib/simplecov/last_run.rb | 3 ++- spec/last_run_spec.rb | 45 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 spec/last_run_spec.rb diff --git a/CHANGELOG.md b/CHANGELOG.md index 4c6dd574..37318f5c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,8 @@ Unreleased ([changes](https://github.com/colszowka/simplecov/compare/v0.12.0...m ## Bugfixes +* Fix parallel_tests where one thread ends up running no tests. See [#533](https://github.com/colszowka/simplecov/pull/533) (thanks @cshaffer) + 0.12.0 2016-07-02 ([changes](https://github.com/colszowka/simplecov/compare/v0.11.2...v0.12.0)) ================= diff --git a/lib/simplecov/last_run.rb b/lib/simplecov/last_run.rb index 64b2f948..90c41102 100644 --- a/lib/simplecov/last_run.rb +++ b/lib/simplecov/last_run.rb @@ -9,7 +9,8 @@ def last_run_path def read return nil unless File.exist?(last_run_path) - JSON.parse(File.read(last_run_path)) + json = File.read(last_run_path) + JSON.parse(json) unless json.strip.empty? end def write(json) diff --git a/spec/last_run_spec.rb b/spec/last_run_spec.rb new file mode 100644 index 00000000..30793865 --- /dev/null +++ b/spec/last_run_spec.rb @@ -0,0 +1,45 @@ +require "helper" + +describe SimpleCov::LastRun do + subject { SimpleCov::LastRun } + + it "defines a last_run_path" do + expect(subject.last_run_path).to eq(File.join(SimpleCov.coverage_path, ".last_run.json")) + end + + it "writes json to its last_run_path" do + json = [] + subject.write(json) + expect(File.read(subject.last_run_path).strip).to eq(JSON.pretty_generate(json)) + end + + context "reading" do + context "but the last_run file does not exist" do + before { File.delete(subject.last_run_path) if File.exist?(subject.last_run_path) } + + it "returns nil" do + expect(subject.read).to be_nil + end + end + + context "a non empty result" do + before { subject.write([]) } + + it "reads json from its last_run_path" do + expect(subject.read).to eq(JSON.parse("[]")) + end + end + + context "an empty result" do + before do + File.open(subject.last_run_path, "w+") do |f| + f.puts "" + end + end + + it "returns nil" do + expect(subject.read).to be_nil + end + end + end +end if SimpleCov.usable?