From 77b136c1c68f70fa3fdb0b1e4844df4ab8ee223c Mon Sep 17 00:00:00 2001 From: martinandert Date: Thu, 24 Aug 2017 12:54:05 +0200 Subject: [PATCH] Close opened file handle in #from_filename We can use the block form of `File.open` to accomplish that. --- lib/pathspec.rb | 2 +- spec/unit/pathspec_spec.rb | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/pathspec.rb b/lib/pathspec.rb index 5293336..70aa73b 100644 --- a/lib/pathspec.rb +++ b/lib/pathspec.rb @@ -75,7 +75,7 @@ def drive_letter_to_path(path) # Generate specs from a filename, such as a .gitignore def self.from_filename(filename, type=:git) - self.from_lines(File.open(filename, 'r'), type) + File.open(filename, 'r') { |io| self.from_lines(io, type) } end def self.from_lines(lines, type=:git) diff --git a/spec/unit/pathspec_spec.rb b/spec/unit/pathspec_spec.rb index 91361a2..9b04a19 100644 --- a/spec/unit/pathspec_spec.rb +++ b/spec/unit/pathspec_spec.rb @@ -310,8 +310,10 @@ context "#from_filename" do it "forwards the type argument" do - expect(File).to receive(:open).and_return(anything) - expect(PathSpec).to receive(:from_lines).with(anything, :regex) + io = double + + expect(File).to receive(:open).and_yield(io) + expect(PathSpec).to receive(:from_lines).with(io, :regex) PathSpec.from_filename "/some/file", :regex end