Skip to content
This repository was archived by the owner on Nov 30, 2024. It is now read-only.

Commit c313bc2

Browse files
author
Sam Phippen
committed
Merge pull request #74 from rspec/support-directory-maker
Move the directory maker into RSpec::Support
2 parents 437c41c + 0d9b037 commit c313bc2

File tree

4 files changed

+100
-0
lines changed

4 files changed

+100
-0
lines changed

lib/rspec/support/directory_maker.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
module RSpec::Support
2+
# @api private
3+
#
4+
# Replacement for fileutils#mkdir_p because we don't want to require parts
5+
# of stdlib in RSpec.
6+
class DirectoryMaker
7+
# @api private
8+
#
9+
# Implements nested directory construction
10+
def self.mkdir_p(path)
11+
stack = path.start_with?(File::SEPARATOR) ? File::SEPARATOR : "."
12+
path.split(File::SEPARATOR).each do |part|
13+
stack = File.join(stack, part)
14+
15+
begin
16+
unless directory_exists?(stack)
17+
Dir.mkdir(stack)
18+
end
19+
rescue Errno::ENOTDIR
20+
raise Errno::EEXIST.new($!.message)
21+
end
22+
23+
end
24+
end
25+
26+
private
27+
28+
def self.directory_exists?(dirname)
29+
File.exist?(dirname) && File.directory?(dirname)
30+
end
31+
end
32+
end

lib/rspec/support/spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
RSpec::Support.require_rspec_support "spec/with_isolated_stderr"
44
RSpec::Support.require_rspec_support "spec/stderr_splitter"
55
RSpec::Support.require_rspec_support "spec/formatting_support"
6+
RSpec::Support.require_rspec_support "spec/with_isolated_directory"
67

78
warning_preventer = $stderr = RSpec::Support::StdErrSplitter.new($stderr)
89

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
require 'tmpdir'
2+
3+
RSpec.shared_context "isolated directory", :isolated_directory => true do
4+
around do |ex|
5+
Dir.mktmpdir do |tmp_dir|
6+
Dir.chdir(tmp_dir, &ex)
7+
end
8+
end
9+
end
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
require "spec_helper"
2+
require "fileutils"
3+
4+
RSpec::Support.require_rspec_support("directory_maker")
5+
6+
module RSpec::Support
7+
describe DirectoryMaker do
8+
shared_examples_for "an mkdir_p implementation" do
9+
include_context "isolated directory"
10+
11+
let(:dirname) { File.join(%w[tmp a recursive structure]) }
12+
13+
def directory_exists?(dirname)
14+
File.exist?(dirname) && File.directory?(dirname)
15+
end
16+
17+
it "makes directories recursively" do
18+
mkdir_p.call(dirname)
19+
expect(directory_exists?(dirname)).to be true
20+
end
21+
22+
it "does not raise if the directory already exists" do
23+
Dir.mkdir("tmp")
24+
mkdir_p.call(dirname)
25+
expect(directory_exists?(dirname)).to be true
26+
end
27+
28+
context "when a file already exists" do
29+
before { File.open("tmp", "w") }
30+
31+
it "raises, as it can't make the directory" do
32+
expect {
33+
mkdir_p.call(dirname)
34+
}.to raise_error(Errno::EEXIST)
35+
end
36+
end
37+
38+
context "when the path specified is absolute" do
39+
let(:dirname) { "bees/ponies" }
40+
41+
it "makes directories recursively" do
42+
mkdir_p.call(File.expand_path(dirname))
43+
expect(directory_exists?(dirname)).to be true
44+
end
45+
end
46+
end
47+
48+
describe ".mkdir_p" do
49+
subject(:mkdir_p) { DirectoryMaker.method(:mkdir_p) }
50+
it_behaves_like "an mkdir_p implementation"
51+
end
52+
53+
describe "FileUtils.mkdir_p" do
54+
subject(:mkdir_p) { FileUtils.method(:mkdir_p) }
55+
it_behaves_like "an mkdir_p implementation"
56+
end
57+
end
58+
end

0 commit comments

Comments
 (0)