Permalink
Browse files

fallback to other filenames if 'log' is a directory

  • Loading branch information...
1 parent c2d0022 commit bdfa74236e7b7422d111c2465b4710df42add92d @davejacobs committed Oct 2, 2012
Showing with 15 additions and 1 deletion.
  1. +4 −1 lib/letters/core_ext.rb
  2. +11 −0 spec/letters/core_ext_spec.rb
View
5 lib/letters/core_ext.rb
@@ -74,7 +74,10 @@ def e(opts={})
def f(opts={})
opts = { format: "yaml", name: "log" }.merge opts
tap do |o|
- File.open(opts[:name], "w+") do |file|
+ suffixes = [""] + (1..50).to_a
+ deduper = suffixes.detect {|x| !File.directory? "#{opts[:name]}#{x}" }
+
+ File.open("#{opts[:name]}#{deduper}", "w+") do |file|
# Override :stream
opts.merge! :stream => file
Helpers.message opts
View
11 spec/letters/core_ext_spec.rb
@@ -120,6 +120,17 @@ module Letters
File.read("log").chomp.should == hash.awesome_inspect
end
end
+
+ describe "when 'log' is a directory" do
+ before { FileUtils.mkdir "log" }
+ after { FileUtils.rm_rf "log" }
+
+ it "appends the first integer that de-dupes the name" do
+ File.exist?("log1").should be_false
+ lambda { hash.f }.should_not raise_error
+ File.exist?("log1").should be_true
+ end
+ end
end
describe "#j (jump)" do

0 comments on commit bdfa742

Please sign in to comment.