summaryrefslogtreecommitdiff
path: root/spec/ruby/core/dir/fchdir_spec.rb
blob: 429e569691245b92daff1be13b47fd5f44eefdbe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
require_relative '../../spec_helper'
require_relative 'fixtures/common'

ruby_version_is '3.3' do
  guard -> { Dir.respond_to? :fchdir } do
    describe "Dir.fchdir" do
      before :all do
        DirSpecs.create_mock_dirs
      end

      after :all do
        DirSpecs.delete_mock_dirs
      end

      before :each do
        @dirs = [Dir.new('.')]
        @original = @dirs.first.fileno
      end

      after :each do
        Dir.fchdir(@original)
        @dirs.each(&:close)
      end

      it "changes to the specified directory" do
        dir = Dir.new(DirSpecs.mock_dir)
        @dirs << dir
        Dir.fchdir dir.fileno
        Dir.pwd.should == DirSpecs.mock_dir
      end

      it "returns 0 when successfully changing directory" do
        Dir.fchdir(@original).should == 0
      end

      it "returns the value of the block when a block is given" do
        Dir.fchdir(@original) { :block_value }.should == :block_value
      end

      it "changes to the specified directory for the duration of the block" do
        pwd = Dir.pwd
        dir = Dir.new(DirSpecs.mock_dir)
        @dirs << dir
        Dir.fchdir(dir.fileno) { Dir.pwd }.should == DirSpecs.mock_dir
        Dir.pwd.should == pwd
      end

      it "raises a SystemCallError if the file descriptor given is not valid" do
        -> { Dir.fchdir(-1) }.should raise_error(SystemCallError)
        -> { Dir.fchdir(-1) { } }.should raise_error(SystemCallError)
      end

      it "raises a SystemCallError if the file descriptor given is not for a directory" do
        -> { Dir.fchdir $stdout.fileno }.should raise_error(SystemCallError)
        -> { Dir.fchdir($stdout.fileno) { } }.should raise_error(SystemCallError)
      end
    end
  end

  guard_not -> { Dir.respond_to? :fchdir } do
    describe "Dir.fchdir" do
      it "raises NotImplementedError" do
        -> { Dir.fchdir 1 }.should raise_error(NotImplementedError)
        -> { Dir.fchdir(1) { } }.should raise_error(NotImplementedError)
      end
    end
  end
end