|
1 | 1 | require_relative '../../spec_helper' |
2 | 2 | require_relative 'fixtures/common' |
3 | | -require_relative 'shared/open' |
4 | 3 |
|
5 | 4 | describe "Dir.open" do |
6 | 5 | before :all do |
|
11 | 10 | DirSpecs.delete_mock_dirs |
12 | 11 | end |
13 | 12 |
|
14 | | - it_behaves_like :dir_open, :open |
| 13 | + it "returns a Dir instance representing the specified directory" do |
| 14 | + dir = Dir.open(DirSpecs.mock_dir) |
| 15 | + dir.should.is_a?(Dir) |
| 16 | + dir.close |
| 17 | + end |
| 18 | + |
| 19 | + it "raises a SystemCallError if the directory does not exist" do |
| 20 | + -> do |
| 21 | + Dir.open(DirSpecs.nonexistent) |
| 22 | + end.should.raise(SystemCallError) |
| 23 | + end |
| 24 | + |
| 25 | + it "may take a block which is yielded to with the Dir instance" do |
| 26 | + Dir.open(DirSpecs.mock_dir) {|dir| dir.should.is_a?(Dir)} |
| 27 | + end |
| 28 | + |
| 29 | + it "returns the value of the block if a block is given" do |
| 30 | + Dir.open(DirSpecs.mock_dir) {|dir| :value }.should == :value |
| 31 | + end |
| 32 | + |
| 33 | + it "closes the Dir instance when the block exits if given a block" do |
| 34 | + closed_dir = Dir.open(DirSpecs.mock_dir) { |dir| dir } |
| 35 | + -> { closed_dir.read }.should.raise(IOError) |
| 36 | + end |
| 37 | + |
| 38 | + it "closes the Dir instance when the block exits the block even due to an exception" do |
| 39 | + closed_dir = nil |
| 40 | + |
| 41 | + -> do |
| 42 | + Dir.open(DirSpecs.mock_dir) do |dir| |
| 43 | + closed_dir = dir |
| 44 | + raise "dir specs" |
| 45 | + end |
| 46 | + end.should.raise(RuntimeError, "dir specs") |
| 47 | + |
| 48 | + -> { closed_dir.read }.should.raise(IOError) |
| 49 | + end |
| 50 | + |
| 51 | + it "calls #to_path on non-String arguments" do |
| 52 | + p = mock('path') |
| 53 | + p.should_receive(:to_path).and_return(DirSpecs.mock_dir) |
| 54 | + Dir.open(p) { true } |
| 55 | + end |
| 56 | + |
| 57 | + it "accepts an options Hash" do |
| 58 | + dir = Dir.open(DirSpecs.mock_dir, encoding: "utf-8") {|d| d } |
| 59 | + dir.should.is_a?(Dir) |
| 60 | + end |
| 61 | + |
| 62 | + it "calls #to_hash to convert the options object" do |
| 63 | + options = mock("dir_open") |
| 64 | + options.should_receive(:to_hash).and_return({ encoding: Encoding::UTF_8 }) |
| 65 | + |
| 66 | + dir = Dir.open(DirSpecs.mock_dir, **options) {|d| d } |
| 67 | + dir.should.is_a?(Dir) |
| 68 | + end |
| 69 | + |
| 70 | + it "ignores the :encoding option if it is nil" do |
| 71 | + dir = Dir.open(DirSpecs.mock_dir, encoding: nil) {|d| d } |
| 72 | + dir.should.is_a?(Dir) |
| 73 | + end |
| 74 | + |
| 75 | + platform_is_not :windows do |
| 76 | + it 'sets the close-on-exec flag for the directory file descriptor' do |
| 77 | + Dir.open(DirSpecs.mock_dir) do |dir| |
| 78 | + io = IO.for_fd(dir.fileno) |
| 79 | + io.autoclose = false |
| 80 | + io.should.close_on_exec? |
| 81 | + end |
| 82 | + end |
| 83 | + end |
15 | 84 | end |
0 commit comments