ruby-changes:46843
From: eregon <ko1@a...>
Date: Mon, 29 May 2017 20:19:46 +0900 (JST)
Subject: [ruby-changes:46843] eregon:r58958 (trunk): Simplify, avoid extra exceptions and add test for concurrent mspec mkdir_p
eregon 2017-05-29 20:19:42 +0900 (Mon, 29 May 2017) New Revision: 58958 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=58958 Log: Simplify, avoid extra exceptions and add test for concurrent mspec mkdir_p Modified files: trunk/spec/mspec/lib/mspec/helpers/fs.rb trunk/spec/mspec/spec/helpers/fs_spec.rb Index: spec/mspec/lib/mspec/helpers/fs.rb =================================================================== --- spec/mspec/lib/mspec/helpers/fs.rb (revision 58957) +++ spec/mspec/lib/mspec/helpers/fs.rb (revision 58958) @@ -17,16 +17,19 @@ class Object https://github.com/ruby/ruby/blob/trunk/spec/mspec/lib/mspec/helpers/fs.rb#L17 parts.each do |part| name = File.join name, part - stat = File.stat name rescue nil - if stat and stat.file? + if File.file? name raise ArgumentError, "path component of #{path} is a file" end - unless stat and stat.directory? + unless File.directory? name begin Dir.mkdir name - rescue Errno::EEXIST - raise unless File.directory? name + rescue Errno::EEXIST => e + if File.directory? name + # OK, another process/thread created the same directory + else + raise e + end end end end Index: spec/mspec/spec/helpers/fs_spec.rb =================================================================== --- spec/mspec/spec/helpers/fs_spec.rb (revision 58957) +++ spec/mspec/spec/helpers/fs_spec.rb (revision 58958) @@ -93,6 +93,19 @@ describe Object, "#mkdir_p" do https://github.com/ruby/ruby/blob/trunk/spec/mspec/spec/helpers/fs_spec.rb#L93 File.open(@dir1, "w") { |f| } lambda { mkdir_p @dir2 }.should raise_error(ArgumentError) end + + it "works if multiple processes try to create the same directory concurrently" do + original = File.method(:directory?) + File.should_receive(:directory?).at_least(:once) { |dir| + ret = original.call(dir) + if !ret and dir == @dir1 + Dir.mkdir(dir) # Simulate race + end + ret + } + mkdir_p @dir1 + original.call(@dir1).should be_true + end end describe Object, "#rm_r" do -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/